Class: Chuckle::Client

Inherits:
Object
  • Object
show all
Includes:
Options
Defined in:
lib/chuckle/client.rb

Constant Summary

Constants included from Options

Options::DEFAULT_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Options

#cache_dir, #cache_errors?, #cookies?, #expires_in, #nretries, #rate_limit, #timeout, #user_agent, #verbose?

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
# File 'lib/chuckle/client.rb', line 9

def initialize(options = {})
  self.options = DEFAULT_OPTIONS.dup
  options.each { |k, v| self.options[k] = v if v }
  self.cache = Cache.new(self)
  sanity!
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



7
8
9
# File 'lib/chuckle/client.rb', line 7

def cache
  @cache
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/chuckle/client.rb', line 7

def options
  @options
end

Instance Method Details

#create_request(uri, body = nil) ⇒ Object

main entry points



20
21
22
23
# File 'lib/chuckle/client.rb', line 20

def create_request(uri, body = nil)
  uri = URI.parse(uri.to_s) if !uri.is_a?(URI)
  Request.new(self, uri, body)
end

#get(uri) ⇒ Object



25
26
27
# File 'lib/chuckle/client.rb', line 25

def get(uri)
  run(create_request(uri))
end

#inspectObject

:nodoc:



45
46
47
# File 'lib/chuckle/client.rb', line 45

def inspect #:nodoc:
  self.class.name
end

#post(uri, body) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/chuckle/client.rb', line 29

def post(uri, body)
  body = case body
  when Hash
    Util.hash_to_query(body)
  else
    body.to_s
  end
  run(create_request(uri, body))
end

#run(request) ⇒ Object



39
40
41
42
43
# File 'lib/chuckle/client.rb', line 39

def run(request)
  response = cache.get(request) || curl(request)
  raise_errors(response)
  response
end