Class: Dealmap::Client
- Inherits:
-
Object
- Object
- Dealmap::Client
- Defined in:
- lib/dealmap.rb
Overview
Defines methods for the Dealmap API
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
Instance Method Summary collapse
-
#deal_details(deal_id, options = {}) ⇒ Hashie::Mash
Get deal details.
-
#initialize(api_key) ⇒ Client
constructor
Initialize the Dealmap client.
-
#search_businesses(options = {}) ⇒ Array, Fixnum
Search for businesses.
-
#search_deals(options = {}) ⇒ Array, Fixnum
Search for deals.
Constructor Details
#initialize(api_key) ⇒ Client
Initialize the Dealmap client
14 15 16 17 18 19 20 21 |
# File 'lib/dealmap.rb', line 14 def initialize(api_key) raise Dealmap::ApiKeyMissingError, "You must supply an API key" if api_key.nil? @api_key = api_key @conn = Faraday.new(:url => 'http://api.thedealmap.com') do |builder| builder.adapter :typhoeus builder.adapter :logger end end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
11 12 13 |
# File 'lib/dealmap.rb', line 11 def api_key @api_key end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
11 12 13 |
# File 'lib/dealmap.rb', line 11 def conn @conn end |
Instance Method Details
#deal_details(deal_id, options = {}) ⇒ Hashie::Mash
Get deal details.
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/dealmap.rb', line 150 def deal_details(deal_id, = {}) = .merge(:key => api_key) response = conn.get("/deals/#{deal_id}") { |req| req.params = } doc = Nokogiri::XML(response.body) deal = doc.search("Deal").first attrs = Hashie::Mash.new deal.children.each do |attr| attrs[attr.name.underscore] = attr.text.strip.rstrip end return attrs end |
#search_businesses(options = {}) ⇒ Array, Fixnum
Search for businesses
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/dealmap.rb', line 83 def search_businesses( = {}) = .merge(:key => api_key) response = conn.get("/search/businesses/") { |req| req.params = } doc = Nokogiri::XML(response.body) results = doc.search("Business") total = doc.search("TotalResults").first.text.to_i businesses = [] results.each do |business| attrs = Hashie::Mash.new business.children.map do |child| attrs[child.name.underscore] = child.text.strip.rstrip end businesses << attrs end return businesses, total end |
#search_deals(options = {}) ⇒ Array, Fixnum
Search for deals
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dealmap.rb', line 45 def search_deals( = {}) = .merge(:key => api_key) response = conn.get("/search/deals/") { |req| req.params = } doc = Nokogiri::XML(response.body) results = doc.search("Deal") total = doc.search("TotalResults").first.text.to_i deals = [] results.each do |deal| attrs = Hashie::Mash.new deal.children.map do |child| attrs[child.name.underscore] = child.text.strip.rstrip end deals << attrs end return deals, total end |