diff --git a/lib/appfigures.rb b/lib/appfigures.rb index 3a69b5b..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' @@ -17,6 +18,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, @@ -28,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({ @@ -39,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 diff --git a/spec/product_sales_spec.rb b/spec/product_sales_spec.rb index 1527bec..471c792 100644 --- a/spec/product_sales_spec.rb +++ b/spec/product_sales_spec.rb @@ -89,5 +89,14 @@ 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 end