class Easywins::HttpClient

Constants

DEFAULT_RETRIES
DEFAULT_TIMEOUT
HANDLED_EXCEPTIONS
USER_AGENTS

Public Class Methods

new(config = {}) click to toggle source
# File lib/easywins/http_client.rb, line 108
def initialize(config = {})
  @config = {
    :timeout          => DEFAULT_TIMEOUT,
    :retries          => DEFAULT_RETRIES
  }.merge(config)
  default_timeout = @config[:timeout]
end

Public Instance Methods

do_delete(path, params=nil, opt={}) click to toggle source
# File lib/easywins/http_client.rb, line 132
def do_delete(path, params=nil, opt={})
  do_request(:delete, path, {:query => params}.merge(opt))
end
do_get(path, params=nil, opt={}) click to toggle source
# File lib/easywins/http_client.rb, line 120
def do_get(path, params=nil, opt={})
  do_request(:get, path, {:query => params}.merge(opt))
end
do_head(path, params=nil, opt={}) click to toggle source
# File lib/easywins/http_client.rb, line 116
def do_head(path, params=nil, opt={})
  do_request(:head, path, {:query => params}.merge(opt))
end
do_post(path, params=nil, opt={}) click to toggle source
# File lib/easywins/http_client.rb, line 124
def do_post(path, params=nil, opt={})
  do_request(:post, path, {:body => params}.merge(opt))
end
do_put(path, params=nil, opt={}) click to toggle source
# File lib/easywins/http_client.rb, line 128
def do_put(path, params=nil, opt={})
  do_request(:put, path, {:body => params}.merge(opt))
end

Private Instance Methods

do_request(method, path, options) click to toggle source
# File lib/easywins/http_client.rb, line 138
def do_request(method, path, options)
  o = {
    :headers => {
      'User-Agent' => random_user_agent
    }
  }.merge(options)
  o[:headers]['X-Forwarded-for'] = random_ip_address if @config[:spoof]
  with_retries do
    self.class.send(method, path, o)
  end
end
random_ip_address() click to toggle source
# File lib/easywins/http_client.rb, line 165
def random_ip_address
  ipv4 = IPAddr.new(rand(2**32), Socket::AF_INET).to_s
end
random_user_agent() click to toggle source
# File lib/easywins/http_client.rb, line 161
def random_user_agent
  USER_AGENTS.sample
end
with_retries() { || ... } click to toggle source
# File lib/easywins/http_client.rb, line 150
def with_retries(&block)
  tries ||= @config[:retries]
  yield
rescue *HANDLED_EXCEPTIONS => ex
  if (tries -= 1) > 0
    sleep 0.2
    retry
  end
  raise ex
end