2

I am attempting to download a page from Wikipedia. For such a task, I am using gems. When using net/http, all I get is an empty string. So I tried with open-uri and it works fine.

Nevertheless, I prefer the first option because it gives me a much more explicit control; but why is it returning an empty string?

class Downloader
    attr_accessor :entry, :url, :page

    def initialize
        # require 'net/http'
        require 'open-uri'
    end

    def getEntry
        print "Article name? "
        @entry = gets.chomp
    end

    def getURL(entry)

        if entry.include?(" ")
            @url = "http://en.wikipedia.org/wiki/" + entry.gsub!(/\s/, "_")
        else
            @url = "http://en.wikipedia.org/wiki/" + entry
        end
        @url.downcase!
    end

    def getPage(url)

=begin THIS FAULTY SOLUTION RETURNS AN EMPTY STRING ???
        connector = URI.parse(url)
        connection = Net::HTTP.start(connector.host, connector.port) do |http|
            http.get(connector.path)
        end
        puts "Body:"
        @page = connection.body
=end

        @page = open(url).read
    end

end

test = Downloader.new
test.getEntry
test.getURL(test.entry)
test.getPage(test.url)
puts test.page

P.S.: I am an autodidact programmer so the code might not fit good practices. My apologies.

gcarbonell
  • 63
  • 3
  • 8
  • No idea, I am getting the body properly. (As an aside, you don't have to check whether `entry` contains spaces; `gsub` will happily change zero spaces into zero underscores. You shouldn't `gsub!` on a parameter unless it is the purpose of the method - unexpected consequences and all that. Don't require in `initialize`, top of the file is usually clearer unless you have particular reason to. ) – Amadan May 13 '15 at 08:34

1 Answers1

1

Because your request return 301 Redirect (check connection.code value), you should follow redirect manually if you are using net/http. Here is more details.

Community
  • 1
  • 1