Class: Groupon::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Client

Initialize the client. TODO: Document.



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

def initialize(*args)
  options = args.extract_options!
  @api_key = args[0]
  @conn = Faraday.new(:url => "http://api.groupon.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



79
80
81
82
83
# File 'lib/groupon.rb', line 79

def method_missing(sym, *args, &block)
  options = args.extract_options!.merge(:client_id => api_key)
  response = conn.get("/v2/#{sym.to_s}/#{args[0]}") { |req| req.params = options  }
  args[0].nil? ? response.body.send(sym) : response.body.send(sym.to_s.chop)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/groupon.rb', line 8

def api_key
  @api_key
end

#connObject (readonly)

Returns the value of attribute conn.



8
9
10
# File 'lib/groupon.rb', line 8

def conn
  @conn
end

Instance Method Details

#deals(options = {}) ⇒ Array #deals(deal_id) ⇒ Hashie::Mash

Overloads:

  • #deals(options = {}) ⇒ Array

    Search for Deals

    Examples:

    Search deals by latitude and longitude

    client.deals(:lat => "-37.74", :lon => "-76.00")

    Search deals within a 2 miles radius of latitude and longitude

    client.deals(:lat => "-37.74", :lon => "-76.00", :radius => 2)

    Search deals from Groupon

    client.deals(:source => "Groupon")

    Search deals based on phone number

    client.deals(:phone => "7185551212")

    Search deals based on tags

    client.deals(:tag => "restaurants,spa")

    Search deals based on paid status. (Defaults to false)

    client.deals(:paid => true)

    Parameters:

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

      A customizable set of options

    Options Hash (options):

    • :lat (String)

      Latitude of point to to sort deals by proximity to. Use with lon. Uses radius.

    • :lon (String)

      Longitude of point to to sort deals by proximity to. Use with lat. Uses radius.

    • :radius (Fixnum)

      Maximum distance of radius in miles to deal location from center point. Defaults to 10. Requires lat and lon

    • :divisions (String)

      One or more division slugs (comma separated). See Divisions API for more details.

    • :source (String)

      One or more source slugs (comma separated). See Sources API for more details.

    • :phone (String)

      Deals available at a business matching one of the phone numbers. See Businesses API for more details.

    • :tag (String)

      One or more tag slugs (comma separated). See Tags API for more details. Note: Specifying multiple tags returns deals matching any one of the tags, NOT all of them

    • :paid (Boolean)

      Shows deals filtered on paid status. Defaults to false. Set to true if you would like to see all deals.

    Returns:

    • (Array)

      An array of Hashie::Mash objects representing Groupon deals

  • #deals(deal_id) ⇒ Hashie::Mash

    Get deal details

    Parameters:

    • deal_id (Fixnum)

      A Deal Id

    Returns:

    • (Hashie::Mash)

      A Hashie::Mash object representing a Groupon deal



51
52
53
# File 'lib/groupon.rb', line 51

def deals(*args)
  super
end

#divisions(*args) ⇒ Object



75
76
77
# File 'lib/groupon.rb', line 75

def divisions(*args)
  super
end

#sources(options = {}) ⇒ Object #sources(source_id) ⇒ Hashie::Mash

Overloads:

  • #sources(options = {}) ⇒ Object

    Search for sources

    Parameters:

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

      A customizable set of options

    Options Hash (options):

    • :division (String)

      One or more division slugs. See Divisions API for more details. Note: Specifying multiple divisions returns sources that exist in either of the divisions, NOT all of them.

    • :paid (String)

      When paid is true, only paid sources are returned. Defaults to false.

  • #sources(source_id) ⇒ Hashie::Mash

    Get source details

    Parameters:

    • slug (String)

      A source slug

    Returns:

    • (Hashie::Mash)

      A Hashie::Mash object representing a Groupon Deal Source



64
65
66
# File 'lib/groupon.rb', line 64

def sources(*args)
  super
end

#tags(options = {}) ⇒ Array

This method returns a list of all tags. There are no parameters specific to tags.

Parameters:

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

    A customizable set of options

Returns:

  • (Array)

    An array of Hashie::Mash objects representing Groupon tags.



72
73
74
# File 'lib/groupon.rb', line 72

def tags(*args)
  super
end