From d27acdbc681deaad50a73e8fa44f59aa11e27e64 Mon Sep 17 00:00:00 2001 From: Hisham Al-Shurafa Date: Tue, 9 Apr 2013 20:58:00 -0600 Subject: [PATCH 1/4] added ref_no, added_timestamp, and icon variables to products query --- lib/appfigures.rb | 3 +++ spec/product_sales_spec.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/appfigures.rb b/lib/appfigures.rb index 3a69b5b..adba43c 100644 --- a/lib/appfigures.rb +++ b/lib/appfigures.rb @@ -17,6 +17,9 @@ def product_sales 'store_name' => hash['product']['store_name'], 'name' => hash['product']['name'], 'sku' => hash['product']['sku'], + 'ref_no' => hash['product']['ref_no'], + 'added_timestamp' => Date.parse(hash['product']['added_timestamp']), + 'icon' => hash['product']['icon'], 'downloads' => hash['downloads'].to_i, 'returns' => hash['returns'].to_i, 'updates' => hash['updates'].to_i, diff --git a/spec/product_sales_spec.rb b/spec/product_sales_spec.rb index 1527bec..4b37275 100644 --- a/spec/product_sales_spec.rb +++ b/spec/product_sales_spec.rb @@ -90,4 +90,16 @@ expect(@api.product_sales.first.revenue).to eq(100.99) end + it 'returns a ref_no' do + expect(@api.product_sales.first.ref_no).to eq('536354432') + end + + it 'returns an added timestamp' do + expect(@api.product_sales.first.added_timestamp).to eq(Date.parse('2012-07-23T00:00:00')) + end + + it 'returns an icon' do + expect(@api.product_sales.first.icon).to eq('http://a5.mzstatic.com/us/r1000/091/Purple/v4/20/69/65/20696562-4e19-17fe-5ffe-cb77a78e1651/mzl.jtpselsb.png') + end + end From e5c8cf3b799a7d107b24ba75c0d16c002efa4c50 Mon Sep 17 00:00:00 2001 From: Hisham Al-Shurafa Date: Tue, 9 Apr 2013 20:58:40 -0600 Subject: [PATCH 2/4] remove unnecessary new lines --- spec/product_sales_spec.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/spec/product_sales_spec.rb b/spec/product_sales_spec.rb index 4b37275..471c792 100644 --- a/spec/product_sales_spec.rb +++ b/spec/product_sales_spec.rb @@ -89,15 +89,12 @@ it 'returns a revenue number' do expect(@api.product_sales.first.revenue).to eq(100.99) end - it 'returns a ref_no' do expect(@api.product_sales.first.ref_no).to eq('536354432') end - it 'returns an added timestamp' do expect(@api.product_sales.first.added_timestamp).to eq(Date.parse('2012-07-23T00:00:00')) end - it 'returns an icon' do expect(@api.product_sales.first.icon).to eq('http://a5.mzstatic.com/us/r1000/091/Purple/v4/20/69/65/20696562-4e19-17fe-5ffe-cb77a78e1651/mzl.jtpselsb.png') end From 208163f9107e1bb906261eeefc70b5e3b44b5e9d Mon Sep 17 00:00:00 2001 From: Hisham Al-Shurafa Date: Wed, 10 Apr 2013 14:32:51 -0600 Subject: [PATCH 3/4] add ability to pass parameters to date_sales query --- lib/appfigures.rb | 9 +++++++-- lib/utils/hash_extensions.rb | 13 +++++++++++++ spec/date_sales_spec.rb | 18 +++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 lib/utils/hash_extensions.rb diff --git a/lib/appfigures.rb b/lib/appfigures.rb index adba43c..94e48e0 100644 --- a/lib/appfigures.rb +++ b/lib/appfigures.rb @@ -1,5 +1,6 @@ require 'appfigures/version' require 'appfigures/connection' +require 'utils/hash_extensions' require 'date' @@ -31,8 +32,11 @@ def product_sales end end - def date_sales(start_date, end_date) - url = "sales/dates+products/#{start_date.strftime('%Y-%m-%d')}/#{end_date.strftime('%Y-%m-%d')}" + + # GET /sales/dates+products/2013-03-01/2013-03-31 + # See http://docs.appfigures.com/api/reference/v1-1/sales + def date_sales(start_date, end_date, options = {}) + url = "sales/dates+products/#{start_date.strftime('%Y-%m-%d')}/#{end_date.strftime('%Y-%m-%d')}#{options.to_query_string(true)}" self.connection.get(url).body.map do |date, product| product.map do |product_id, hash| Hashie::Mash.new({ @@ -42,6 +46,7 @@ def date_sales(start_date, end_date) 'store_name' => hash['product']['store_name'], 'name' => hash['product']['name'], 'sku' => hash['product']['sku'], + 'ref_no' => hash['product']['ref_no'], 'downloads' => hash['downloads'].to_i, 'returns' => hash['returns'].to_i, 'updates' => hash['updates'].to_i, diff --git a/lib/utils/hash_extensions.rb b/lib/utils/hash_extensions.rb new file mode 100644 index 0000000..6a4722a --- /dev/null +++ b/lib/utils/hash_extensions.rb @@ -0,0 +1,13 @@ + +class Hash + def to_query_string(include_question_mark = true) + query_string = '' + unless empty? + query_string << '?' if include_question_mark + query_string << inject([]) do |params, (key, value)| + params << "#{key}=#{value}" + end.join('&') + end + query_string + end +end diff --git a/spec/date_sales_spec.rb b/spec/date_sales_spec.rb index 2791ad0..69384a3 100644 --- a/spec/date_sales_spec.rb +++ b/spec/date_sales_spec.rb @@ -47,10 +47,23 @@ } } EOF + body223123 = <<-EOF + { + "2012-09-01": { + "223123": { + "downloads": 30, + "product": { + "id": 223123 + } + } + } + } + EOF @api = Appfigures.new username: 'test', password: 'test' @stubs = Faraday::Adapter::Test::Stubs.new do |stub| + stub.get('/v1.1/sales/dates+products/2012-09-01/2012-09-01?products=223123') { [status_code, headers, body223123] } stub.get('/v1.1/sales/dates+products/2012-09-01/2012-09-01') { [status_code, headers, body] } - end + end @api.connection.adapter :test, @stubs end @@ -94,5 +107,8 @@ it 'returns a revenue number' do expect(@api.date_sales(start_date, end_date).first.revenue).to eq(100.99) end + it 'returns a specific product ID' do + expect(@api.date_sales(start_date, end_date, { :products => 223123 }).first.product_id).to eq(223123) + end end From 00fac373fe652e54b74f3f6573a3a551ef47b629 Mon Sep 17 00:00:00 2001 From: Hisham Al-Shurafa Date: Fri, 12 Apr 2013 11:44:23 -0600 Subject: [PATCH 4/4] added country_sales query --- lib/appfigures.rb | 21 +++++++++++++ spec/country_sales_spec.rb | 64 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 spec/country_sales_spec.rb diff --git a/lib/appfigures.rb b/lib/appfigures.rb index 94e48e0..bac8041 100644 --- a/lib/appfigures.rb +++ b/lib/appfigures.rb @@ -59,5 +59,26 @@ def date_sales(start_date, end_date, options = {}) end end + # GET /sales/country/{start_date}/{end_date}?data_source={data_source}&products={product_ids}&country={country}&format={format} + # See http://docs.appfigures.com/api/reference/v1-1/sales + def country_sales(start_date, end_date, options = {}) + url = "sales/countries/#{start_date.strftime('%Y-%m-%d')}/#{end_date.strftime('%Y-%m-%d')}#{options.to_query_string(true)}" + self.connection.get(url).body.map do |country, hash| + Hashie::Mash.new({ + 'iso' => hash['iso'], + 'country' => hash['country'], + 'downloads' => hash['downloads'], + 'updates' => hash['updates'], + 'returns' => hash['returns'], + 'net_downloads' => hash['net_downloads'], + 'promos' => hash['promos'], + 'revenue' => hash['revenue'], + 'git_redemptions' => hash['gift_redemptions'], + }) + end + end + + + end diff --git a/spec/country_sales_spec.rb b/spec/country_sales_spec.rb new file mode 100644 index 0000000..267c32a --- /dev/null +++ b/spec/country_sales_spec.rb @@ -0,0 +1,64 @@ +require 'spec_helper' + +describe 'Appfigures country sales' do + before do + status_code = 200 + headers = { + 'Cache-Control' => 'private', + 'Content-Type' => 'application/json', + 'Server' => 'Microsoft-IIS/7.5', + 'X-AspNetMvc-Version' => '2.0', + 'X-Request-Limit' => '1000', + 'X-Request-Usage' => '4', + 'X-AspNet-Version' => '4.0.30319', + 'X-Server-ID' => '10', + 'Date' => 'Tue, 24 Jul 2012 19:56:51 GMT', + 'Connection' => 'close', + 'Transfer-Encoding' => 'Identity' + } + body = <<-EOF + { + "US": { + "downloads": 213, + "updates": 715, + "returns": 0, + "net_downloads": 213, + "promos": 0, + "revenue": "0.00", + "gift_redemptions": 0, + "country": "United States", + "iso": "US" + }, + "VE": { + "downloads": 1, + "updates": 2, + "returns": 0, + "net_downloads": 1, + "promos": 0, + "revenue": "0.00", + "gift_redemptions": 0, + "country": "Venezuela", + "iso": "VE" + } + } + EOF + @api = Appfigures.new username: 'test', password: 'test' + @stubs = Faraday::Adapter::Test::Stubs.new do |stub| + stub.get('/v1.1/sales/countries/2013-03-01/2013-03-31') { [status_code, headers, body] } + end + @api.connection.adapter :test, @stubs + end + + let(:start_date) { Date.parse('2013-03-01') } + let(:end_date) { Date.parse('2013-03-31') } + + it 'returns an iso' do + expect(@api.country_sales(start_date, end_date).first.iso).to eq("US") + end + + it 'returns downloads' do + expect(@api.country_sales(start_date, end_date).first.downloads).to eq(213) + end + + +end