Class: Omnivore::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/omnivore/http_client.rb

Overview

A simple HTTP client with a redirect feature.

Class Method Summary collapse

Class Method Details

.get(url, redirects = 3) ⇒ String

Sends a ‘GET` request to the specified url, following the provided number of maximum redirects.

Parameters:

  • url (String)

    the url to be requested

  • redirects (Integer) (defaults to: 3)

    the number of redirects to follow

Returns:

  • (String)

    the response body of the request.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
# File 'lib/omnivore/http_client.rb', line 15

def self.get(url, redirects=3)
  raise ArgumentError, 'HTTP redirect too deep' if redirects == 0
  response = Net::HTTP.get_response(URI.parse(url))
  case response
  when Net::HTTPSuccess then response.body
  when Net::HTTPRedirection then HttpClient.get(response['location'], redirects - 1)
  else
    response.error!
  end
end