Class: Foxy::Client

Inherits:
Object show all
Includes:
RateLimit
Defined in:
lib/foxy/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/foxy/client.rb', line 15

def config
  @config
end

#connObject (readonly)

Returns the value of attribute conn.



15
16
17
# File 'lib/foxy/client.rb', line 15

def conn
  @conn
end

#default_optionsObject (readonly)

Returns the value of attribute default_options.



15
16
17
# File 'lib/foxy/client.rb', line 15

def default_options
  @default_options
end

Instance Method Details

#cacheObject



65
66
67
# File 'lib/foxy/client.rb', line 65

def cache
  @cache ||= FileCache.new(self.class.name.split("::").last.downcase)
end

#eraw(options) ⇒ Object



54
55
56
57
58
59
# 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") ⇒ Object



69
70
71
# File 'lib/foxy/client.rb', line 69

def fixed(id, legth = 2, fill = "0")
  id.to_s.rjust(legth, fill)
end

#json(options) ⇒ Object



46
47
48
# File 'lib/foxy/client.rb', line 46

def json(options)
  JSON[raw(options)]
end

#raw(options) ⇒ Object



50
51
52
# File 'lib/foxy/client.rb', line 50

def raw(options)
  request(options).body
end

#request(options) ⇒ Object



39
40
41
42
43
44
# 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

#urlObject



61
62
63
# File 'lib/foxy/client.rb', line 61

def url
  "http://www.example.com"
end

#user_agentObject



35
36
37
# 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