Class: Adility::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/adility.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Client

Initialize the client. TODO: Document.



13
14
15
16
17
18
19
20
21
22
# File 'lib/adility.rb', line 13

def initialize(*args)
  options = args.extract_options!
  @api_key = args[0]
  @conn = Faraday.new(:url => "https://testapi.offersdb.com") do |builder|
    builder.adapter Faraday.default_adapter
    builder.adapter  :logger if options[:debug] == true
    builder.use Faraday::Response::ParseJson
    builder.use Faraday::Response::Mashify
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/adility.rb', line 23

def method_missing(sym, *args, &block)
  options = args.extract_options!
  if args[0]
    url = "/distribution/beta/#{sym.to_s}/#{args[0]}.json"
  else
    url = "/distribution/beta/#{sym.to_s}.json"
  end
  response = conn.get(url) do |req| 
    req.headers['X-OFFERSDB-API-KEY'] = api_key
    req.params = options
  end
  total_pages = response.body.send :total_pages
  total_items = response.body.send :total_items
  page = response.body.send :page
  if args[0].nil?
    ret = response.body.send sym
    return ret, page, total_pages
  else
    ret = response.body.send sym.to_s.chop
    return ret
  end
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/adility.rb', line 9

def api_key
  @api_key
end

#connObject (readonly)

Returns the value of attribute conn.



9
10
11
# File 'lib/adility.rb', line 9

def conn
  @conn
end