Forum: Ruby on Rails NoMethodError "Read" for URI

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
cdubd (Guest)
on 2009-01-24 02:42
(Received via mailing list)
Hey Everyone,

I have a pretty interesting problem that I thought I would try and get
some feedback on. The following method below accepts a URL string as a
parameter ex "http://www.google.com," and it uses regular expressions
to try and find the images in the html. The odd thing is.... that I
have tested it over and over again.... and it has always grabbed the
images and worked exactly as I coded it too, but lately it has been
telling me that the URI class doesn't have a read method.... when it
must because I have been using it!

I am just so confused why rails would suddenly have this issue, and I
am wondering if anyone can make heads or tails out of it. The code is
posted below.

Thanks!





<================== CODE ===========================================>

def get_images(url)
    @image_links = []

    if validate_link(url) == false
      return @image_links
    end

    if ((url =~ /\.jpg/)||(url =~ /\.png/)||(url =~ /\.gif/)) == nil
        begin
          uri = URI.parse(url)
          str = uri.read
          host = uri.host
        rescue SystemCallError
          return @image_links

        end


        @body = str.scan(/<body.+<\/body>/)
        @images = str.scan(/(src=\"http.+\.jpg?\")/)
        str = @images.to_s
        @images = str.scan(/\".+?\"/)
        @image_links = filter_images(@images)
        @image_links.push("no_image.jpg")
    else
          @image_links.push(url)
          @image_links.push("no_image.jpg")
    end

    return @image_links
  end
cdubd (Guest)
on 2009-01-24 12:40
(Received via mailing list)
oh and I thought I should provide the error message itself:

NoMethodError (undefined method `read' for #<URI::HTTP:0x256f764>):
Frederick C. (Guest)
on 2009-01-24 15:12
(Received via mailing list)
On 24 Jan 2009, at 10:39, cdubd wrote:

>
> oh and I thought I should provide the error message itself:
>
> NoMethodError (undefined method `read' for #<URI::HTTP:0x256f764>):
>
>
URI does not have an read method. OpenURI adds one, so the logical
conclusion for me is that something in your environment has changed
that results in openuri not having been required when previously it was.

Fred
cdubd (Guest)
on 2009-01-24 16:17
(Received via mailing list)
Thanks Fred,

I checked it out, and you were right. Open-URI is required now, and it
works like a charm.


Casen

On Jan 24, 2:11 pm, Frederick C. <removed_email_address@domain.invalid>
This topic is locked and can not be replied to.