Created
December 2, 2014 19:17
-
-
Save jmiramant/81e54faf849e6e294522 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def self.request(prev_day) | |
@date = (Date.today - (prev_day)).strftime("%Y-%m-%d") | |
parameters = [ | |
{ | |
account_id: 156021, | |
'app_id' => "892559034", | |
start_date: @date, | |
end_date: @date | |
} | |
# { | |
# account_id: 186243, | |
# 'app_id' => "co.good.android", MY QUESTION IS WHAT IS THE APP_ID FOR ANDROID? | |
# start_date: @date, | |
# end_date: @date | |
# } | |
] | |
parameters.each do |p| | |
data = AppAnnie::App.new(AppAnnie.accounts({}, @date).first, p).sales.first['units']['app'] | |
store data | |
end | |
end | |
<------------------------------------------------------> | |
module AppAnnie | |
class << self | |
attr_writer :api_key | |
def api_key | |
@api_key || ENV['APPANNIE_API_KEY'] | |
end | |
end | |
def self.accounts(options = {}, date) | |
response = connection.get do |req| | |
req.headers['Authorization'] = "Bearer #{api_key}" | |
req.headers['Accept'] = 'application/json' | |
req.url '/v1/accounts', options | |
end | |
if response.status == 200 | |
JSON.parse(response.body)['account_list'].map { |hash| Account.new(hash, date) } | |
else | |
ErrorResponse.raise_for(response) | |
end | |
end | |
def self.connection | |
@connection ||= Faraday.new url: 'https://api.appannie.com' do |faraday| | |
faraday.adapter Faraday.default_adapter | |
end | |
end | |
class App | |
attr_reader :raw, | |
:account, | |
:id, | |
:account_id, | |
:status, | |
:name, | |
:start_date, | |
:end_date, | |
:icon | |
def initialize(account, attributes) | |
@raw = attributes | |
@account = account | |
@id = attributes['app_id'] | |
@start_date = attributes[:start_date] | |
@end_date = attributes[:end_date] | |
end | |
def sales(options = {}) | |
response = AppAnnie.connection.get do |req| | |
req.headers['Authorization'] = "Bearer #{AppAnnie.api_key}" | |
req.headers['Accept'] = 'application/json' | |
req.url "/v1/accounts/#{@account.id}/apps/#{@id}/sales?start_date=" + @start_date + "&end_date=" + @end_date | |
end | |
if response.status == 200 | |
JSON.parse(response.body)['sales_list'] | |
else | |
ErrorResponse.raise_for(response) | |
end | |
end | |
end | |
class Account | |
attr_reader :raw, | |
:id, | |
:name, | |
:status, | |
:platform, | |
:start_date, | |
:end_date, | |
:publisher_name | |
def initialize(attributes, date) | |
@raw = attributes | |
@id = attributes['account_id'] | |
@name = attributes['account_name'] | |
@status = attributes['account_status'] | |
@platform = attributes['platform'] | |
@publisher_name = attributes['publisher_name'] | |
@start_date = date | |
@end_date = date | |
end | |
def apps(options = {}) | |
response = AppAnnie.connection.get do |req| | |
req.headers['Authorization'] = "Bearer #{AppAnnie.api_key}" | |
req.headers['Accept'] = 'application/json' | |
req.url "/v1/accounts/#{id}/apps", options | |
end | |
if response.status == 200 | |
JSON.parse(response.body)['app_list'].map { |hash| App.new(self, hash) } | |
else | |
ErrorResponse.raise_for(response) | |
end | |
end | |
def sales(options = {}) | |
response = AppAnnie.connection.get do |req| | |
req.headers['Authorization'] = "Bearer #{AppAnnie.api_key}" | |
req.headers['Accept'] = 'application/json' | |
req.url "/v1/accounts/#{id}/sales", options | |
end | |
if response.status == 200 | |
JSON.parse(response.body)['sales_list'] | |
else | |
ErrorResponse.raise_for(response) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment