Class: Appfigures::Sale

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/appfigures/sale.rb

Constant Summary collapse

TYPE =
{:DATES => "dates", :COUNTRIES => "countries", :PRODUCTS => "products", :REGION => "regions",
  :DATES_PRODUCTS => "dates+products", :PRODUCTS_DATES => "products+dates", 
  :COUNTRIES_PRODUCTS => "countries+products", :PRODUCTS_COUNTRIES => "products+countries",
  :COUNTRIES_DATES => "countries+dates", :DATES_COUNTRIES => "dates+countries"
}
DATASOURCE =
{:DAILY => "daily", :WEEKLY => "weekly", :MONTHLY => "monthly"}

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Sale

Returns a new instance of Sale.



17
18
19
# File 'lib/appfigures/sale.rb', line 17

def initialize(username, password)
  @auth = {:username => username, :password => password}
end

Instance Method Details

#alltime_sales(type, product_ids = [], options = {}) ⇒ Object

Generating all time totals report type must be one of products, products+countries, countries, countries+products

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
# File 'lib/appfigures/sale.rb', line 35

def alltime_sales(type, product_ids=[], options={})
  accept_types = %w{products products+countries countries countries+products}
  raise ArgumentError, "Type must be one of TYPE: #{accept_types}" unless accept_types.include?(type)
  product_ids = [product_ids] unless product_ids.is_a?(Array)

  options.merge!({:basic_auth => @auth, :products => product_ids.join(";")})
  url = "/reports/sales/#{type}"
  self.class.get(url, options)
end

#region_sales(start_date, end_date, options = {}) ⇒ Object

Generating a By Region Sales Report



46
47
48
49
50
# File 'lib/appfigures/sale.rb', line 46

def region_sales(start_date, end_date, options={})
  options.merge!({:basic_auth => @auth})
  url = "/sales/regions/#{start_date}/#{end_date}"    
  self.class.get(url, options)
end

#sales(type, start_date, end_date, product_ids, options = {}) ⇒ Object

Generating a By App, By Country, or By Date Sales Report options can be data_source, apps or country

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
# File 'lib/appfigures/sale.rb', line 23

def sales(type, start_date, end_date, product_ids, options={})
  raise ArgumentError, "Type must be one of TYPE: #{TYPE.values.join(", ")}" unless TYPE.values.index(type)
  raise ArgumentError, "Type must be one of DATASOURCE: #{DATASOURCE.values.join(", ")}" if (options[:data_source] && !DATASOURCE.values.index(options[:data_source]))

  product_ids = [product_ids] unless product_ids.is_a?(Array)
  options.merge!({:basic_auth => @auth, :product_ids => product_ids.join(";")})
  url = "/sales/#{type}/#{start_date}/#{end_date}/"    
  self.class.get(url, options)
end