From ee90d59064983e98b218715899121949872cb1b8 Mon Sep 17 00:00:00 2001 From: John Polling Date: Wed, 8 Jul 2015 10:46:57 +0100 Subject: [PATCH 1/6] Changed the country code --- lib/groupon_api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/groupon_api.rb b/lib/groupon_api.rb index ad6a511..be30fb0 100644 --- a/lib/groupon_api.rb +++ b/lib/groupon_api.rb @@ -4,6 +4,6 @@ require "groupon_api/request" module GrouponApi - API_KEY_FORMAT = /US_AFF_0_\d+_212556_0/ + API_KEY_FORMAT = /UK_AFF_0_\d+_212556_0/ API_BASE = 'partner-api.groupon.com' end From fbfcd2f611dce6b0c3ca5992c1530f6226dd75fb Mon Sep 17 00:00:00 2001 From: John Polling Date: Wed, 8 Jul 2015 12:31:38 +0100 Subject: [PATCH 2/6] Changed to International endpoint --- lib/groupon_api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/groupon_api.rb b/lib/groupon_api.rb index be30fb0..9eb4bbb 100644 --- a/lib/groupon_api.rb +++ b/lib/groupon_api.rb @@ -5,5 +5,5 @@ module GrouponApi API_KEY_FORMAT = /UK_AFF_0_\d+_212556_0/ - API_BASE = 'partner-api.groupon.com' + API_BASE = 'partner-int-api.groupon.com' end From 7fa7edf84c38487ce2a0947a8f628a1b29077f93 Mon Sep 17 00:00:00 2001 From: John Polling Date: Sun, 19 Jul 2015 07:51:59 +0100 Subject: [PATCH 3/6] Make sure the Deals API call fails ok If the API return no data, or no deals data errors were being raised --- lib/groupon_api/deals.rb | 10 +++++++--- spec/groupon_api_spec.rb | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/groupon_api/deals.rb b/lib/groupon_api/deals.rb index 4eb81a3..24db1e6 100644 --- a/lib/groupon_api/deals.rb +++ b/lib/groupon_api/deals.rb @@ -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+_212556_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['deals'].nil? + + results['deals'].collect{|deal| HashWithIndifferentAccess.new(deal)} end -end \ No newline at end of file +end diff --git a/spec/groupon_api_spec.rb b/spec/groupon_api_spec.rb index 1591b41..fe0a2c9 100644 --- a/spec/groupon_api_spec.rb +++ b/spec/groupon_api_spec.rb @@ -42,12 +42,31 @@ include_context :valid_api_key context 'without results' do it 'returns an empty Array' do + puts described_class params = valid_params.merge(limit: 0, offset: 0) result = described_class.deals(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) From 4427e28ff403a665056123197fa79018ab6ea8a6 Mon Sep 17 00:00:00 2001 From: John Polling Date: Sun, 19 Jul 2015 10:17:28 +0100 Subject: [PATCH 4/6] Fix if an empty array is returned --- lib/groupon_api/deals.rb | 2 +- spec/groupon_api_spec.rb | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/groupon_api/deals.rb b/lib/groupon_api/deals.rb index 24db1e6..8f003eb 100644 --- a/lib/groupon_api/deals.rb +++ b/lib/groupon_api/deals.rb @@ -10,7 +10,7 @@ def self.deals(params) results = GrouponApi::Request.call('deals', params) - return [] if results.nil? || results['deals'].nil? + return [] if results.nil? || results.length == 0 || results['deals'].nil? results['deals'].collect{|deal| HashWithIndifferentAccess.new(deal)} end diff --git a/spec/groupon_api_spec.rb b/spec/groupon_api_spec.rb index fe0a2c9..4ffb261 100644 --- a/spec/groupon_api_spec.rb +++ b/spec/groupon_api_spec.rb @@ -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 @@ -42,13 +49,21 @@ include_context :valid_api_key context 'without results' do it 'returns an empty Array' do - puts described_class params = valid_params.merge(limit: 0, offset: 0) result = described_class.deals(params) expect(result).to be_a(Array) 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) From 44d8c15a7f65207474c43df246ca15729e09432a Mon Sep 17 00:00:00 2001 From: John Polling Date: Thu, 6 Aug 2015 11:26:33 +0100 Subject: [PATCH 5/6] Allow any media id --- lib/groupon_api.rb | 2 +- lib/groupon_api/deals.rb | 2 +- spec/spec_helper.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/groupon_api.rb b/lib/groupon_api.rb index 9eb4bbb..84176ee 100644 --- a/lib/groupon_api.rb +++ b/lib/groupon_api.rb @@ -4,6 +4,6 @@ require "groupon_api/request" module GrouponApi - API_KEY_FORMAT = /UK_AFF_0_\d+_212556_0/ + API_KEY_FORMAT = /UK_AFF_0_\d+_\d_0/ API_BASE = 'partner-int-api.groupon.com' end diff --git a/lib/groupon_api/deals.rb b/lib/groupon_api/deals.rb index 8f003eb..d1120bc 100644 --- a/lib/groupon_api/deals.rb +++ b/lib/groupon_api/deals.rb @@ -1,7 +1,7 @@ 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 /UK_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) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 019f230..b722865 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,5 +4,5 @@ require 'groupon_api' # and any other gems you need def your_ts_token - '' + 'UK_AFF_0_205557_212556_0' end From a2a8a6782f46c3d822663053bc8adb413c0af748 Mon Sep 17 00:00:00 2001 From: John Polling Date: Thu, 6 Aug 2015 11:31:39 +0100 Subject: [PATCH 6/6] Wrong regex --- lib/groupon_api.rb | 2 +- lib/groupon_api/deals.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/groupon_api.rb b/lib/groupon_api.rb index 84176ee..d202f25 100644 --- a/lib/groupon_api.rb +++ b/lib/groupon_api.rb @@ -4,6 +4,6 @@ require "groupon_api/request" module GrouponApi - API_KEY_FORMAT = /UK_AFF_0_\d+_\d_0/ + API_KEY_FORMAT = /UK_AFF_0_\d+_\d+_0/ API_BASE = 'partner-int-api.groupon.com' end diff --git a/lib/groupon_api/deals.rb b/lib/groupon_api/deals.rb index d1120bc..c066d48 100644 --- a/lib/groupon_api/deals.rb +++ b/lib/groupon_api/deals.rb @@ -1,7 +1,7 @@ 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 /UK_AFF_0_\d+_\d_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)