class Birdwatcher::HttpClient

Constants

DEFAULT_RETRIES

Default request retries

DEFAULT_TIMEOUT

Default request timeout

RETRIABLE_EXCEPTIONS

List of retriable exceptions

Response
USER_AGENTS

List of User-Agent strings to use for client spoofing

Public Class Methods

new(options = {}) click to toggle source

Class initializer

@params options [Hash] client options @option options :timeout [Integer] request timeout @option options :retries [Integer] request retries @option options :user_agent [String] User-Agent to use (random if not set) @option options :retriable_exceptions [Array] exception classes to retry on

@note The options list is incomplete; the class supports all options for the HTTParty gem @see www.rubydoc.info/github/jnunemaker/httparty/HTTParty/ClassMethods

# File lib/birdwatcher/http_client.rb, line 60
def initialize(options = {})
  @options = {
    :timeout              => DEFAULT_TIMEOUT,
    :retries              => DEFAULT_RETRIES,
    :user_agent           => nil,
    :retriable_exceptions => RETRIABLE_EXCEPTIONS
  }.merge(options)
end

Public Instance Methods

do_delete(path, params=nil, options={}) click to toggle source

Perform a DELETE request

@param path [String] Path to request @param params [Hash] Request params @param options [Hash] Request options @return [Birdwatcher::HttpClient::Response]

# File lib/birdwatcher/http_client.rb, line 115
def do_delete(path, params=nil, options={})
  do_request(:delete, path, {:query => params}.merge(options))
end
do_get(path, params=nil, options={}) click to toggle source

Perform a GET request

@param path [String] Path to request @param params [Hash] Request params @param options [Hash] Request options @return [Birdwatcher::HttpClient::Response]

# File lib/birdwatcher/http_client.rb, line 75
def do_get(path, params=nil, options={})
  do_request(:get, path, {:query => params}.merge(options))
end
do_head(path, params=nil, options={}) click to toggle source

Perform a HEAD request

@param path [String] Path to request @param params [Hash] Request params @param options [Hash] Request options @return [Birdwatcher::HttpClient::Response]

# File lib/birdwatcher/http_client.rb, line 85
def do_head(path, params=nil, options={})
  do_request(:head, path, {:query => params}.merge(options))
end
do_post(path, params=nil, options={}) click to toggle source

Perform a POST request

@param path [String] Path to request @param params [Hash] Request params @param options [Hash] Request options @return [Birdwatcher::HttpClient::Response]

# File lib/birdwatcher/http_client.rb, line 95
def do_post(path, params=nil, options={})
  do_request(:post, path, {:query => params}.merge(options))
end
do_put(path, params=nil, options={}) click to toggle source

Perform a PUT request

@param path [String] Path to request @param params [Hash] Request params @param options [Hash] Request options @return [Birdwatcher::HttpClient::Response]

# File lib/birdwatcher/http_client.rb, line 105
def do_put(path, params=nil, options={})
  do_request(:put, path, {:query => params}.merge(options))
end

Private Instance Methods

do_request(method, path, options) click to toggle source

Perform a request @private

@param method [Symbol] Class method to call @param path [String] Request path @param options [Hash] Request options

@return Birdwatcher::HttpClient::Response

# File lib/birdwatcher/http_client.rb, line 129
def do_request(method, path, options)
  opts = @options.merge({
      :headers => {
        "Accept"          => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding" => "gzip",
        "Accept-Language" => "en-US,en;q=0.8",
        "Cache-Control"   => "max-age=0",
        "Connection"      => "close",
        "User-Agent"      => @options[:user_agent] || USER_AGENTS.sample
      },
      :verify => false
    }).merge(options)
  with_retries do
    response = self.class.send(method, path, opts)
    Response.new(response.request.last_uri.to_s, response.code, response.headers, response.body)
  end
end
with_retries() { || ... } click to toggle source

Execute code block and retry if a retriable exception is raised @private

@param &block Code block to run

# File lib/birdwatcher/http_client.rb, line 151
def with_retries(&block)
  tries = @options[:retries].to_i
  yield
rescue *@options[:retriable_exceptions] => ex
  tries -= 1
  if tries > 0
    sleep 0.2
    retry
  else
    raise ex
  end
end