class Foxy::Client

Attributes

config[R]
conn[R]
default_options[R]

Public Class Methods

new(config = {}) click to toggle source
# File lib/foxy/client.rb, line 17
def initialize(config = {})
  @config = config
  @default_options = config.fetch(:default_options, {}).recursive_hash

  @conn = Faraday.new(url: url) do |connection|
    connection.options[:timeout] = config.fetch(:timeout, 120)
    connection.options[:open_timeout] = config.fetch(:open_timeout, 20)
    connection.headers[:user_agent] = user_agent

    connection.use(Faraday::Response::Middleware)
    # connection.response :logger
    # connection.response :json
    # connection.use FaradayMiddleware::Gzip
    # connection.adapter(Faraday.default_adapter)
    connection.adapter :patron
  end
end

Public Instance Methods

cache() click to toggle source
# File lib/foxy/client.rb, line 65
def cache
  @cache ||= FileCache.new(self.class.name.split("::").last.downcase)
end
eraw(options) click to toggle source
# File lib/foxy/client.rb, line 54
def eraw(options)
  cacheopts = options.delete(:cache)
  klass = options.delete(:class) || Foxy::HtmlResponse
  response_options = options.merge(options.delete(:response_params) || {})
  klass.new(raw_with_cache(options, cacheopts), response_options)
end
fixed(id, legth = 2, fill = "0") click to toggle source
# File lib/foxy/client.rb, line 69
def fixed(id, legth = 2, fill = "0")
  id.to_s.rjust(legth, fill)
end
json(options) click to toggle source
# File lib/foxy/client.rb, line 46
def json(options)
  JSON[raw(options)]
end
raw(options) click to toggle source
# File lib/foxy/client.rb, line 50
def raw(options)
  request(options).body
end
request(options) click to toggle source
# File lib/foxy/client.rb, line 39
def request(options)
  wait!
  opts = default_options.deep_merge(options)

  conn.get(opts.fetch(:path), opts.fetch(:params, {}))
end
url() click to toggle source
# File lib/foxy/client.rb, line 61
def url
  "http://www.example.com"
end
user_agent() click to toggle source
# File lib/foxy/client.rb, line 35
def user_agent
  "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
end

Private Instance Methods

raw_with_cache(options, cacheopts) click to toggle source
# File lib/foxy/client.rb, line 75
def raw_with_cache(options, cacheopts)
  raw(options)
end