Module: WebHelpers

Instance Method Summary collapse

Instance Method Details

#get(url, limit = 10) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/web_helpers.rb', line 5

def get(url, limit=10)
  raise ArgumentError, "get() redirects too deep" if limit < 1

  raise EditUrlError, "shouldn't be trying to edit... #{url}" if url =~ /[?&]action=edit\b/

  response = Net::HTTP.get_response(URI(url))
  case response
  when Net::HTTPSuccess     then response.body
  when Net::HTTPRedirection then get(response["location"], limit - 1)
  else                           response.error!
  end
end