Reputation: 7844
I read some code like:
source = URI.join(uri).read
This basically goes to the URI, and stores the source of the webpage in the variable.
I tried this in IRB:
URI.join(uri).class
and it returned URI::HTTP
, but, when I tried URI.join(uri).read.class => String
.
I checked String
class but there is no read
method there.
I want to stub this method but am not able to because I don't know where it comes from.
Upvotes: 1
Views: 1482
Reputation: 51093
Rather than stub URI.read()
(provided by OpenURI as noted in the Tin Man's answer), consider using a library like WebMock that can intercept HTTP requests regardless of the exact mechanism used to make them.
Upvotes: 0
Reputation: 160551
read
is from Ruby's StringIO module, which acts like the normal IO class.
OpenURI provides read
via the StringIO class, which is how it fools us, and our code, into thinking the URL is a device we can read from.
URI.join(uri).read.class
returns String
because the page is read from the site pointed to by the URL, and the content is a string. OpenURI overrides the URI class and adds read
to it. If you try:
require 'uri'
URI.join('http://example.com').read
without having required OpenURI, you'll get an error because, by itself, URI doesn't know how to read
.
URI.join('http://example.com')
=> #<URI::HTTP:0x0000010292de68 URL:http://example.com>
URI.join('http://example.com').read
NoMethodError: undefined method `read' for #<URI::HTTP:0x0000010218b3b8 URL:http://example.com>
Upvotes: 2