Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/groupon_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
require "groupon_api/request"

module GrouponApi
API_KEY_FORMAT = /US_AFF_0_\d+_212556_0/
API_BASE = 'partner-api.groupon.com'
API_KEY_FORMAT = /UK_AFF_0_\d+_\d+_0/
API_BASE = 'partner-int-api.groupon.com'
end
10 changes: 7 additions & 3 deletions lib/groupon_api/deals.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
module GrouponApi
def self.deals(params)
raise ::ArgumentError, 'param :ts_token cannot be nil' if GrouponApi.config.ts_token.nil?
raise ::ArgumentError, 'param :ts_token must match /US_AFF_0_\d+_212556_0/' unless GrouponApi.config.ts_token.match(API_KEY_FORMAT)
raise ::ArgumentError, 'param :ts_token must match /UK_AFF_0_\d+_\d+_0/' unless GrouponApi.config.ts_token.match(API_KEY_FORMAT)

params.merge!(tsToken: GrouponApi.config.ts_token)
params.merge!(GrouponApi.config.deals) if GrouponApi.config.deals.kind_of?(Hash)

puts "#{__FILE__}:#{__LINE__} params: #{params}" if GrouponApi.config.debug

GrouponApi::Request.call('deals', params)['deals'].collect{|deal| HashWithIndifferentAccess.new(deal)}
results = GrouponApi::Request.call('deals', params)

return [] if results.nil? || results.length == 0 || results['deals'].nil?

results['deals'].collect{|deal| HashWithIndifferentAccess.new(deal)}
end
end
end
34 changes: 34 additions & 0 deletions spec/groupon_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
radius: 10
}
}
let(:empty_lat_long_params){
{
lat: nil,
lng: nil,
radius: 10
}
}
context 'without API key' do
include_context :without_api_key
it "throws an exception" do
Expand All @@ -48,6 +55,33 @@
expect(result.size).to eq(0)
end
end
context 'crazy lat and long' do
it 'returns an empty Array' do
allow(GrouponApi::Request).to receive('call').with('deals', empty_lat_long_params).and_return([])
result = described_class.deals(empty_lat_long_params)

expect(result).to be_a(Array)
expect(result.size).to eq(0)
end
end
context 'problem in GrouponApi' do
it 'returns an empty Array' do
allow(GrouponApi::Request).to receive('call').with('deals', valid_params).and_return(nil)
result = described_class.deals(valid_params)

expect(result).to be_a(Array)
expect(result.size).to eq(0)
end
end
context 'no deals returned in GrouponApi' do
it 'returns an empty Array' do
allow(GrouponApi::Request).to receive('call').with('deals', valid_params).and_return({deals: []})
result = described_class.deals(valid_params)

expect(result).to be_a(Array)
expect(result.size).to eq(0)
end
end
context 'with results' do
it "returns an Array of Hashes" do
result = described_class.deals(valid_params)
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
require 'groupon_api' # and any other gems you need

def your_ts_token
''
'UK_AFF_0_205557_212556_0'
end