Class: Adknowledge::Performance

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/adknowledge/performance.rb

Constant Summary collapse

URL =
'http://api.publisher.adknowledge.com/performance'
VALID_MEASURES =
[
  :revenue, :schedules, :clicks, :paid_clicks, :valid_clicks,
  :invalid_clicks, :test_clicks, :domestic_paid_clicks,
  :domestic_unpaid_clicks, :foreign_paid_clicks, :foreign_unpaid_clicks,
  :foreign_clicks, :badip_clicks, :badagent_clicks, :badreferrer_clicks,
  :ecpm, :epc, :source_expense, :source_profit, :affiliate_percent,
  :gross_revenue, :ppc, :adjustments, :promotions, :referrals,
  :expense_accruals, :adjustment_accruals, :promotion_accruals,
  :referral_accruals, :accruals, :total_payment, :sent_amount,
  :domain_group, :source_account_name, :records
]
VALID_DIMENSIONS =
[
  :product_guid, :report_date, :report_hour, :report_30min, :report_15min,
  :is_accrued, :revenue_type, :source_product_guid, :list_id, :product_id,
  :source_account_name, :domain_group_id, :domain_group, :report_time,
  :subid, :country_cd, :accrual_date, :suppress_date, :suppress_md5,
  :suppress_type
]
VALID_FILTERS =
[:start_date, :end_date] + VALID_DIMENSIONS
VALID_PIVOT_KEYS =
[:pivot, :sum, :count]
DEFAULT_FILTER =
{product_id: '2', product_guid: '*'}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePerformance

Returns a new instance of Performance.



43
44
45
46
47
48
49
# File 'lib/adknowledge/performance.rb', line 43

def initialize
  @measures   = {}
  @dimensions = {}
  @filter     = DEFAULT_FILTER.dup
  @options    = {}
  @pivot_options = {}
end

Instance Attribute Details

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



10
11
12
# File 'lib/adknowledge/performance.rb', line 10

def dimensions
  @dimensions
end

#filterObject (readonly)

Returns the value of attribute filter.



10
11
12
# File 'lib/adknowledge/performance.rb', line 10

def filter
  @filter
end

#measuresObject (readonly)

Returns the value of attribute measures.



10
11
12
# File 'lib/adknowledge/performance.rb', line 10

def measures
  @measures
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/adknowledge/performance.rb', line 10

def options
  @options
end

#pivot_optionsObject (readonly)

Returns the value of attribute pivot_options.



10
11
12
# File 'lib/adknowledge/performance.rb', line 10

def pivot_options
  @pivot_options
end

#recordsArray (readonly)

Return the query result records

Returns:

  • (Array)

    query result records



187
188
189
# File 'lib/adknowledge/performance.rb', line 187

def records
  @records
end

#sort_optionObject (readonly)

Returns the value of attribute sort_option.



10
11
12
# File 'lib/adknowledge/performance.rb', line 10

def sort_option
  @sort_option
end

Instance Method Details

#display_all(display_all) ⇒ Adknowledge::Performance

Force query to show filtered dimensions to be shown

Parameters:

  • display_all (Boolean)

Returns:



141
142
143
144
# File 'lib/adknowledge/performance.rb', line 141

def display_all display_all
  @options[:all] = booleanize 'DisplayAll', display_all
  self
end

#eachObject

Iterate the query results. Runs the query if it hasn’t been already.

Parameters:

  • block (Block)


54
55
56
57
58
59
60
61
62
# File 'lib/adknowledge/performance.rb', line 54

def each
  if block_given?
    records.each do |doc|
      yield doc
    end
  else
    to_enum
  end
end

#full(full) ⇒ Adknowledge::Performance

Specify whether to display the full set even if entries are 0

Parameters:

  • full (Boolean)

Returns:



123
124
125
126
# File 'lib/adknowledge/performance.rb', line 123

def full full
  @options[:full] = booleanize 'Full', full
  self
end

#group_by(*groupings) ⇒ Adknowledge::Performance

Specify the dimension(s) to group measures by

Parameters:

  • grouping(s) (Array)

Returns:



77
78
79
80
# File 'lib/adknowledge/performance.rb', line 77

def group_by *groupings
  @dimensions.merge! paramerize(groupings, VALID_DIMENSIONS, 'Invalid dimension group')
  self
end

#limit(limit) ⇒ Adknowledge::Performance

Specify a number of results to retrun

Parameters:

  • limit (Integer)

Returns:



99
100
101
102
103
104
105
# File 'lib/adknowledge/performance.rb', line 99

def limit limit
  unless limit.is_a? Fixnum
    raise ArgumentError, 'Limit must be an integer'
  end
  @options[:limit] = limit.to_s
  self
end

#nocache(nocache) ⇒ Adknowledge::Performance

Disable caching of queries. By default queries are cached for 60 seconds

Parameters:

  • nocache (Boolean)

Returns:



132
133
134
135
# File 'lib/adknowledge/performance.rb', line 132

def nocache nocache
  @options[:nocache] = booleanize 'NoCache', nocache
  self
end

#pivot(pivot_opt) ⇒ Adknowledge::Performance

Specify pivot options

Parameters:

  • pivot (Object)

    Existing grouped field (as symbol) or Hash of options

Returns:



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/adknowledge/performance.rb', line 150

def pivot pivot_opt
  case pivot_opt
  when Symbol
    unless valid_pivot_values.include? pivot_opt
      raise ArgumentError, 'Pivotted field must be a grouped dimension'
    end
    @pivot_options[:pivot] = pivot_opt.to_s
  when Hash
    unless (pivot_opt.values - VALID_MEASURES).empty?
      raise ArgumentError, 'Pivotted value must be a measurement'
    end
    unless (pivot_opt.keys - [:sum, :count]).empty?
      raise ArgumentError, 'Pivot must be sum or count'
    end
    @pivot_options = pivot_opt
  else
    raise ArgumentError, 'Pivot options must be a symbol or hash'
  end
  self
end

#query_paramsHash

Displays the query parameters passed to Adknowledge performance API

Returns:

  • (Hash)

    query parameters



174
175
176
177
178
179
180
181
182
# File 'lib/adknowledge/performance.rb', line 174

def query_params
  p = base_params.merge(filter_params).
    merge(options_params).
    merge(pivot_params)
  p.merge!(measures: measures_param) unless measures.empty?
  p.merge!(dimensions: dimensions_param) unless dimensions.empty?
  p.merge!(sort: sort_option) if sort_option
  p
end

#select(*selection) ⇒ Adknowledge::Performance

Specify the measure(s) to select in the query

Parameters:

  • selection(s) (Array)

Returns:



68
69
70
71
# File 'lib/adknowledge/performance.rb', line 68

def select *selection
  @measures.merge! paramerize(selection, VALID_MEASURES, 'Invalid measurement selection')
  self
end

#sort(sort_option) ⇒ Adknowledge::Performance

Specify the column index to sort by

Parameters:

  • sort_option (Integer)

Returns:



111
112
113
114
115
116
117
# File 'lib/adknowledge/performance.rb', line 111

def sort sort_option
  unless sort_option.is_a? Fixnum
    raise ArgumentError, 'Sort option must be an integer'
  end
  @sort_option = sort_option.to_s
  self
end

#where(criteria) ⇒ Adknowledge::Performance

Specify the filter criteria to limit query by

Parameters:

  • criteria (Hash)

Returns:



86
87
88
89
90
91
92
93
# File 'lib/adknowledge/performance.rb', line 86

def where criteria
  unless(criteria.keys - VALID_FILTERS).empty?
    raise ArgumentError, 'Invalid filter criteria'
  end
  criteria.each{|k,v| criteria[k] = v.to_s}
  @filter.merge! criteria
  self
end