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
19 changes: 11 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
appfigures (0.0.1)
appfigures (0.0.2)
faraday
faraday_middleware
hashie
Expand All @@ -11,13 +11,13 @@ GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.1.3)
faraday (0.8.1)
multipart-post (~> 1.1)
faraday_middleware (0.8.8)
faraday (>= 0.7.4, < 0.9)
hashie (1.2.0)
multi_json (1.3.6)
multipart-post (1.1.5)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
faraday_middleware (0.9.1)
faraday (>= 0.7.4, < 0.10)
hashie (3.3.2)
multi_json (1.10.1)
multipart-post (2.0.0)
rake (0.9.2.2)
rspec (2.11.0)
rspec-core (~> 2.11.0)
Expand All @@ -35,3 +35,6 @@ DEPENDENCIES
appfigures!
rake
rspec (>= 2.11)

BUNDLED WITH
1.16.1
189 changes: 155 additions & 34 deletions lib/appfigures.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,176 @@
require 'appfigures/version'
require 'appfigures/connection'
require 'utils/hash_extensions'

require 'date'
require 'json'

class Appfigures
attr_reader :connection
def initialize(options = {})
@connection = Appfigures::Connection.new options[:username], options[:password]
@connection = Appfigures::Connection.new options[:username], options[:password], options[:client_key]
end

#https://api.appfigures.com/v2/reports/sales/?group_by=product&client_key=c0725e4c875b412fbca6b12b5db44a4e
def product_sales
self.connection.get('sales/products').body.map do |id, hash|
Hashie::Mash.new({
'product_id' => hash['product']['id'],
'store_id' => hash['product']['store_id'],
'store_name' => hash['product']['store_name'],
'name' => hash['product']['name'],
'sku' => hash['product']['sku'],
'downloads' => hash['downloads'].to_i,
'returns' => hash['returns'].to_i,
'updates' => hash['updates'].to_i,
'net_downloads' => hash['net_downloads'].to_i,
'promos' => hash['promos'].to_i,
'gift_redemptions'=> hash['gift_redemptions'].to_i,
'revenue' => hash['revenue'].to_f
})
url = 'reports/sales'
options = {group_by: 'product'}

response = self.connection.get(url, options)
response.body.map do |id, hash|
if response.status == 200
Hashie::Mash.new({
'product_id' => hash['product']['id'],
'store_id' => hash['product']['store_id'],
'store_name' => hash['product']['store'],
'name' => hash['product']['name'],
'sku' => hash['product']['sku'],
'ref_no' => hash['product']['ref_no'],
'added_timestamp' => Date.parse(hash['product']['source']['added_timestamp']),
'icon' => hash['product']['icon'],
'downloads' => hash['downloads'].to_i,
'returns' => hash['returns'].to_i,
'updates' => hash['updates'].to_i,
'net_downloads' => hash['net_downloads'].to_i,
'promos' => hash['promos'].to_i,
'gift_redemptions'=> hash['gift_redemptions'].to_i,
'revenue' => hash['revenue'].to_f,
'active' => hash['product']['source']['active']
})
else
puts 'Appfigures service error:'
puts hash.to_json
Hashie::Mash.new
end
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')}"
self.connection.get(url).body.map do |date, product|
product.map do |product_id, hash|

# GET /reports/sales/dates+products?start=2017-03-01&end=2017-03-31&products=6403600
# See http://docs.appfigures.com/api/reference/v2/sales
def date_sales(start_date, end_date, options = {})
url = "reports/sales/dates+products"

options = {start: start_date.strftime('%Y-%m-%d'),
end: end_date.strftime('%Y-%m-%d')}.merge(options)

response = self.connection.get(url, options)
if response.status == 200
response.body.map do |date, product|
product.map do |product_id, hash|
Hashie::Mash.new({
'date' => Date.parse(date),
'product_id' => hash['product_id'].to_i,
'downloads' => hash['downloads'].to_i,
'returns' => hash['returns'].to_i,
'updates' => hash['updates'].to_i,
'net_downloads' => hash['net_downloads'].to_i,
'promos' => hash['promos'].to_i,
'gift_redemptions'=> hash['gift_redemptions'].to_i,
'revenue' => hash['revenue'].to_f
})
end.first
end
else
response.body.map do |id, hash|
puts 'Appfigures service error:'
puts hash.to_json
Hashie::Mash.new
end
end
end

# GET /reports/sales?group_by=country&start=start_date&end=end_date
# See http://docs.appfigures.com/api/reference/v2/sales
def country_sales(start_date, end_date, options = {})
url = "reports/sales"
options = {group_by: 'country',
start: start_date.strftime('%Y-%m-%d'),
end: end_date.strftime('%Y-%m-%d')}.merge(options)
#"/?group_by=country&start=#{start_date.strftime('%Y-%m-%d')}/#{end_date.strftime('%Y-%m-%d')}#{options.to_query_string(true)}"

response = self.connection.get(url, options)
response.body.map do |country, hash|
if response.status == 200
Hashie::Mash.new({
'date' => Date.parse(date),
'product_id' => hash['product']['id'],
'store_id' => hash['product']['store_id'],
'store_name' => hash['product']['store_name'],
'name' => hash['product']['name'],
'sku' => hash['product']['sku'],
'downloads' => hash['downloads'].to_i,
'returns' => hash['returns'].to_i,
'updates' => hash['updates'].to_i,
'net_downloads' => hash['net_downloads'].to_i,
'promos' => hash['promos'].to_i,
'gift_redemptions'=> hash['gift_redemptions'].to_i,
'revenue' => hash['revenue'].to_f
'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'],
'gift_redemptions' => hash['gift_redemptions'],
})
end.first
else
puts 'Appfigures service error:'
puts hash.to_json
Hashie::Mash.new
end
end
end

# GET /reviews?products={productId}&lang=en
# See http://docs.appfigures.com/api/reference/v2/reviews
def product_reviews(product_id, options = {})
url = "reviews"
options = {products: product_id,
lang: 'en'
}.merge(options)

response = self.connection.get(url, options)
if response.status == 200
body = response.body
reviews = Hashie::Mash.new({
'total' => body['total'].to_i,
'pages' => body['pages'].to_i,
'this_page' => body['this_page'].to_i,
'reviews' => body['reviews'].map do |review|
Hashie::Mash.new({
'author' => review['author'],
'title' => review['title'],
'review' => review['review'],
'stars' => review['stars'],
'iso' => review['iso'],
'version' => review['version'],
'date' => review['date'],
'product' => review['product'],
'id' => review['id']
})
end
})
else
puts 'Appfigures service error:'
puts response.body.to_json
return Hashie::Mash.new
end
end

# GET /ratings?group_by=product&start_date={today}&end_date={today}products={productId}
def product_ratings(product_id, options = {})
url = "ratings"
now = Time.now
today = Date.new(now.year, now.month, now.day).strftime('%Y-%m-%d')

options = {group_by: 'product',
products: product_id,
start_date: today,
end_date: today}.merge(options)

response = self.connection.get(url, options)
response.body.map do |product, hash|
if response.status == 200
Hashie::Mash.new({
'stars' => product['stars'],
'product' => product['product']
})
else
puts 'Appfigures service error:'
puts hash.to_json
return Hashie::Mash.new
end
end.first
end

end
7 changes: 4 additions & 3 deletions lib/appfigures/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@

class Appfigures
class Connection < Faraday::Connection
def initialize(username, password)
super('https://api.appfigures.com/v1.1/') do |builder|
def initialize(username, password, client_key)
super('https://api.appfigures.com/v2/') do |builder|
builder.use FaradayMiddleware::EncodeJson
builder.adapter Faraday.default_adapter
builder.response :json, :content_type => /\bjson$/
builder.adapter Faraday.default_adapter
end

self.basic_auth username, password
self.headers["Accept"] = 'application/json'
self.headers["X-Client-Key"] = client_key
end
end
end
2 changes: 1 addition & 1 deletion lib/appfigures/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Appfigures
VERSION = "0.0.1"
VERSION = "0.0.2"
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', client_key: 'test'
@stubs = Faraday::Adapter::Test::Stubs.new do |stub|
stub.get('/v2/reports/sales?end=2013-03-31&group_by=country&start=2013-03-01') { [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
Loading