Class: CommissionJunction

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/commission_junction.rb,
lib/commission_junction/version.rb

Overview

Interact with CJ web services. See cjcommunity.force.com/s/article/4777058.

Defined Under Namespace

Classes: Advertiser, CjObject, Commission, Link, Product

Constant Summary collapse

WEB_SERVICE_URIS =
{
  product_search: 'https://product-search.api.cj.com/v2/product-search',
  link_search: 'https://link-search.api.cj.com/v2/link-search',
  advertiser_lookup: 'https://advertiser-lookup.api.cj.com/v3/advertiser-lookup',
  categories: 'https://support-services.api.cj.com/v2/categories',
  commissions: 'https://commission-detail.api.cj.com/v3/commissions',
  item_detail: 'https://commission-detail.api.cj.com/v3/item-detail/'
}.freeze
VERSION =
'2.0.0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(developer_key, website_id, timeout = 10) ⇒ CommissionJunction

Returns a new instance of CommissionJunction.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/commission_junction.rb', line 28

def initialize(developer_key, website_id, timeout = 10)
  raise ArgumentError, "developer_key must be a String; got #{developer_key.class} instead" unless developer_key.is_a?(String)
  raise ArgumentError, "You must supply your developer key.\nSee https://api.cj.com/sign_up.cj" if developer_key.empty?

  website_id = website_id.to_s
  raise ArgumentError, "You must supply your website ID.\nSee cj.com > Account > Web site Settings > PID" if website_id.empty?
  @website_id = website_id

  raise ArgumentError, "timeout must be a Integer; got #{timeout.class} instead" unless timeout.is_a?(Integer)
  raise ArgumentError, "timeout must be > 0; got #{timeout} instead" unless timeout > 0
  @timeout = timeout

  self_class = self.class
  self_class.headers('authorization' => developer_key)
end

Instance Attribute Details

#cj_objectsObject (readonly)

debug_output $stderr



13
14
15
# File 'lib/commission_junction.rb', line 13

def cj_objects
  @cj_objects
end

#page_numberObject (readonly)

debug_output $stderr



13
14
15
# File 'lib/commission_junction.rb', line 13

def page_number
  @page_number
end

#records_returnedObject (readonly)

debug_output $stderr



13
14
15
# File 'lib/commission_junction.rb', line 13

def records_returned
  @records_returned
end

#total_matchedObject (readonly)

debug_output $stderr



13
14
15
# File 'lib/commission_junction.rb', line 13

def total_matched
  @total_matched
end

Instance Method Details

#advertiser_lookup(params = {}) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/commission_junction.rb', line 53

def advertiser_lookup(params = {})
  raise ArgumentError, "params must be a Hash; got #{params.class} instead" unless params.is_a?(Hash)

  params = { 'advertiser-ids' => 'joined' }.merge(params)

  @cj_objects = []

  response = self.class.get(WEB_SERVICE_URIS[:advertiser_lookup], query: params)
  advertisers = extract_contents(response, 'advertisers')

  @total_matched = advertisers['total_matched'].to_i
  @records_returned = advertisers['records_returned'].to_i
  @page_number = advertisers['page_number'].to_i

  advertiser = advertisers['advertiser']
  advertiser = [advertiser] if advertiser.is_a?(Hash) # If we got exactly one result, put it in an array.
  advertiser.each { |item| @cj_objects << Advertiser.new(item) } if advertiser

  @cj_objects
end

#categories(params = {}) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
# File 'lib/commission_junction.rb', line 44

def categories(params = {})
  raise ArgumentError, "params must be a Hash; got #{params.class} instead" unless params.is_a?(Hash)

  params = { 'locale' => 'en' }.merge(params)

  response = self.class.get(WEB_SERVICE_URIS[:categories], query: params, timeout: @timeout)
  @categories = extract_contents(response, 'categories', 'category')
end

#commissions(params = {}) ⇒ Object

Raises:

  • (ArgumentError)


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/commission_junction.rb', line 124

def commissions(params = {})
  raise ArgumentError, "params must be a Hash; got #{params.class} instead" unless params.is_a?(Hash)

  params = { 'date-type' => 'event' }.merge(params)

  @cj_objects = []

  response = self.class.get(WEB_SERVICE_URIS[:commissions], query: params)
  commissions = extract_contents(response, 'commissions')

  @total_matched = commissions['total_matched'].to_i
  @records_returned = commissions['records_returned'].to_i
  @page_number = commissions['page_number'].to_i

  commission = commissions['commission']
  commission = [commission] if commission.is_a?(Hash) # If we got exactly one result, put it in an array.
  commission.each { |item| @cj_objects << Commission.new(item) } if commission

  @cj_objects
end

#extract_contents(response, first_level, second_level = nil) ⇒ Object

Raises:

  • (ArgumentError)


162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/commission_junction.rb', line 162

def extract_contents(response, first_level, second_level = nil)
  cj_api = response['cj_api']

  raise ArgumentError, 'cj api missing from response' if cj_api.nil?

  error_message = cj_api['error_message']

  raise ArgumentError, error_message if error_message

  return cj_api[first_level] if second_level.nil?
  cj_api[first_level][second_level]
end

#item_detail(original_action_ids) ⇒ Object

Raises:

  • (ArgumentError)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/commission_junction.rb', line 145

def item_detail(original_action_ids)
  raise ArgumentError, "original_action_ids must be an Array; got #{original_action_ids.class} instead" unless original_action_ids.is_a?(Array)

  unless (1..50).cover?(original_action_ids.size)
    raise ArgumentError, "You must provide between 1 and 50 original action IDs.\nSee https://cjcommunity.force.com/s/article/4777175."
  end

  @cj_objects = []

  ids = original_action_ids.join(',')
  response = self.class.get(WEB_SERVICE_URIS[:item_detail] + ids, query: "original-action-id=#{ids}")
  @cj_objects = extract_contents(response, 'item_details')
  @cj_objects = [@cj_objects] if @cj_objects.is_a?(Hash) # If we got exactly one result, put it in an array.

  @cj_objects
end

Raises:

  • (ArgumentError)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/commission_junction.rb', line 99

def link_search(params)
  raise ArgumentError, "params must be a Hash; got #{params.class} instead" unless params.is_a?(Hash)

  if params.empty?
    raise ArgumentError, "You must provide at least one request parameter, for example, \"keywords\".\nSee https://cjcommunity.force.com/s/article/4777180."
  end

  params['website-id'] = @website_id

  @cj_objects = []

  response = self.class.get(WEB_SERVICE_URIS[:link_search], query: params, timeout: @timeout)
  links = extract_contents(response, 'links')

  @total_matched = links['total_matched'].to_i
  @records_returned = links['records_returned'].to_i
  @page_number = links['page_number'].to_i

  link = links['link']
  link = [link] if link.is_a?(Hash) # If we got exactly one result, put it in an array.
  link.each { |item| @cj_objects << Link.new(item) } if link

  @cj_objects
end

#product_search(params) ⇒ Object

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/commission_junction.rb', line 74

def product_search(params)
  raise ArgumentError, "params must be a Hash; got #{params.class} instead" unless params.is_a?(Hash)

  if params.empty?
    raise ArgumentError, "You must provide at least one request parameter, for example, \"keywords\".\nSee https://cjcommunity.force.com/s/article/4777185."
  end

  params['website-id'] = @website_id

  @cj_objects = []

  response = self.class.get(WEB_SERVICE_URIS[:product_search], query: params, timeout: @timeout)
  products = extract_contents(response, 'products')

  @total_matched = products['total_matched'].to_i
  @records_returned = products['records_returned'].to_i
  @page_number = products['page_number'].to_i

  product = products['product']
  product = [product] if product.is_a?(Hash) # If we got exactly one result, put it in an array.
  product.each { |item| @cj_objects << Product.new(item) } if product

  @cj_objects
end