Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Country sales #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
33 changes: 31 additions & 2 deletions lib/appfigures.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'appfigures/version'
require 'appfigures/connection'
require 'utils/hash_extensions'

require 'date'

Expand All @@ -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,
Expand All @@ -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({
Expand All @@ -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,
Expand All @@ -51,5 +59,26 @@ def date_sales(start_date, end_date)
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
13 changes: 13 additions & 0 deletions lib/utils/hash_extensions.rb
Original file line number Diff line number Diff line change
@@ -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
64 changes: 64 additions & 0 deletions spec/country_sales_spec.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 17 additions & 1 deletion spec/date_sales_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/product_sales_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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