Class: Groupon::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/groupon/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Initialize the Groupon client

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :api_key (String) — default: Groupon.api_key

    Your Groupon API token



12
13
14
# File 'lib/groupon/client.rb', line 12

def initialize(options={})
  @api_key = options[:api_key] || Groupon.api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



3
4
5
# File 'lib/groupon/client.rb', line 3

def api_key
  @api_key
end

Class Method Details

.get(*args) ⇒ Object



48
# File 'lib/groupon/client.rb', line 48

def self.get(*args); handle_response super end

.handle_response(response) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/groupon/client.rb', line 51

def self.handle_response(response)
  case response.code
  when 500...600; raise GrouponError.new(Hashie::Mash.new(response).status)
  else; response
  end

  Hashie::Mash.new(response)

end

.post(*args) ⇒ Object



49
# File 'lib/groupon/client.rb', line 49

def self.post(*args); handle_response super end

Instance Method Details

#deals(query = {}) ⇒ Array<Hashie::Mash>

Returns a list of deals.

The API returns an ordered list of deals currently running for a given Division.

Priority is based on position within the response (top deals are higher in priority).

Parameters:

  • options (Hash)

    a customizable set of options

Returns:

  • (Array<Hashie::Mash>)

    an array of deals

See Also:



40
41
42
43
44
45
46
# File 'lib/groupon/client.rb', line 40

def deals(query={})
  division = query.delete(:division)
  query.merge! :client_id => @api_key
  path = division ? "/#{division}" : ""
  path += "/deals.json"
  self.class.get(path, :query => query).deals
end

#divisionsArray<Hashie::Mash>

Returns a list of divisions - cities where Groupon is live.

Returns:

  • (Array<Hashie::Mash>)

    an array of divisions

See Also:



19
20
21
# File 'lib/groupon/client.rb', line 19

def divisions
  self.class.get("/divisions.json",:query => { :client_id => @api_key }).divisions
end