Class: Birdwatcher::HttpClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/birdwatcher/http_client.rb

Direct Known Subclasses

KloutClient

Defined Under Namespace

Classes: Response

Constant Summary collapse

DEFAULT_TIMEOUT =

Default request timeout

15.freeze
DEFAULT_RETRIES =

Default request retries

2.freeze
RETRIABLE_EXCEPTIONS =

List of retriable exceptions

[
  Errno::ETIMEDOUT,
  Errno::ECONNRESET,
  Errno::ECONNREFUSED,
  Errno::ENETUNREACH,
  Errno::EHOSTUNREACH,
  Errno::EINVAL,
  SocketError,
  Net::OpenTimeout,
  EOFError
].freeze
USER_AGENTS =

List of User-Agent strings to use for client spoofing

[
  "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
  "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
  "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36",
  "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36",
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36",
  "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0",
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36",
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.8 (KHTML, like Gecko) Version/9.1.3 Safari/601.7.8",
  "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0",
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
  "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:48.0) Gecko/20100101 Firefox/48.0",
  "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0",
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Safari/602.1.50",
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.7",
  "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
  "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36",
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
  "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HttpClient

Note:

The options list is incomplete; the class supports all options for the HTTParty gem

Class initializer

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :timeout (Integer)

    request timeout

  • :retries (Integer)

    request retries

  • :user_agent (String)

    User-Agent to use (random if not set)

  • :retriable_exceptions (Array)

    exception classes to retry on

See Also:



60
61
62
63
64
65
66
67
# 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

Instance Method Details

#do_delete(path, params = nil, options = {}) ⇒ Birdwatcher::HttpClient::Response

Perform a DELETE request

Parameters:

  • path (String)

    Path to request

  • params (Hash) (defaults to: nil)

    Request params

  • options (Hash) (defaults to: {})

    Request options

Returns:



115
116
117
# 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 = {}) ⇒ Birdwatcher::HttpClient::Response

Perform a GET request

Parameters:

  • path (String)

    Path to request

  • params (Hash) (defaults to: nil)

    Request params

  • options (Hash) (defaults to: {})

    Request options

Returns:



75
76
77
# 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 = {}) ⇒ Birdwatcher::HttpClient::Response

Perform a HEAD request

Parameters:

  • path (String)

    Path to request

  • params (Hash) (defaults to: nil)

    Request params

  • options (Hash) (defaults to: {})

    Request options

Returns:



85
86
87
# 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 = {}) ⇒ Birdwatcher::HttpClient::Response

Perform a POST request

Parameters:

  • path (String)

    Path to request

  • params (Hash) (defaults to: nil)

    Request params

  • options (Hash) (defaults to: {})

    Request options

Returns:



95
96
97
# 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 = {}) ⇒ Birdwatcher::HttpClient::Response

Perform a PUT request

Parameters:

  • path (String)

    Path to request

  • params (Hash) (defaults to: nil)

    Request params

  • options (Hash) (defaults to: {})

    Request options

Returns:



105
106
107
# File 'lib/birdwatcher/http_client.rb', line 105

def do_put(path, params=nil, options={})
  do_request(:put, path, {:query => params}.merge(options))
end