commission_junction 1.7.3 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 75905dfef02eddb83dc1441839a42ab720533b6b
4
- data.tar.gz: b98a7d4ee12ea22cee9be46d9a3d027793978a90
3
+ metadata.gz: bae9d34f121c997545515282c9ff05fb25a24a4a
4
+ data.tar.gz: 3ee56677421920706ceb322f6b038da241cb38d4
5
5
  SHA512:
6
- metadata.gz: c300121aac7c08c9f09209100f59bee34a6228cb7d1933779c5812033efe9c65d2f9ff259ad9285fce7b5cb9af12a17d6f7b2c7e2deceda239527e0c559c896b
7
- data.tar.gz: 283bd1a79bb664846802e503c694453e698b821d6a35e73dff0680d2ec4f0f475614f91b2c2e7b6c8e900fdcdc2735a467a5bfdc38b6fbb49434d5d68ee09c91
6
+ metadata.gz: dbb7d127f62de372d6aa32bbd6f0616ae0bb333e9e51c191da5c737f62532c33b8022897ea27a48f5f6089caa72a6aa51287acd4d2447860013ea8dbc32bd814
7
+ data.tar.gz: 8ac81f01181cc6050141d66203134657bf8e47affede11bd4b617d5ed070238e6e45092d36767e1d74f5abb5cbef467f4f0646e1c76a55cdd14061a4bfc97d9a
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ Metrics/LineLength:
2
+ Enabled: false
3
+ Metrics/MethodLength:
4
+ Enabled: false
5
+ Metrics/AbcSize:
6
+ Enabled: false
7
+ Metrics/ClassLength:
8
+ Enabled: false
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Ruby wrapper for the Commission Junction web services APIs (REST)
4
4
 
5
+ See https://cjcommunity.force.com/s/article/4777058.
6
+
5
7
  [![Gem Version](https://badge.fury.io/rb/commission_junction.png)](http://badge.fury.io/rb/commission_junction)
6
8
 
7
9
  ## Installation
@@ -21,18 +23,17 @@ Or install it yourself as:
21
23
  ## Example
22
24
 
23
25
  ```ruby
24
- require 'rubygems'
25
26
  require 'commission_junction'
26
27
 
27
- # See https://api.cj.com/sign_up.cj
28
- DEVELOPER_KEY = '????????'
28
+ # See https://api.cj.com/sign_up.cj.
29
+ DEVELOPER_KEY = '????????'.freeze
29
30
 
30
- # See cj.com > Account > Web site Settings > PID
31
- WEBSITE_ID = '????????'
31
+ # See cj.com > Account > Websites.
32
+ WEBSITE_ID = '????????'.freeze
32
33
 
33
34
  cj = CommissionJunction.new(DEVELOPER_KEY, WEBSITE_ID)
34
35
 
35
- # See http://help.cj.com/en/web_services/product_catalog_search_service_rest.htm
36
+ # See https://cjcommunity.force.com/s/article/4777185
36
37
  # for the list of request and response parameters.
37
38
  cj.product_search('keywords' => '+blue +jeans',
38
39
  'advertiser-ids' => 'joined',
@@ -45,7 +46,7 @@ cj.product_search('keywords' => '+blue +jeans',
45
46
  puts ''
46
47
  end
47
48
 
48
- # See http://help.cj.com/en/web_services/advertiser_lookup_service_rest.htm
49
+ # See https://cjcommunity.force.com/s/article/4777195
49
50
  # for the list of request and response parameters.
50
51
  cj.advertiser_lookup('keywords' => '+used +books',
51
52
  'advertiser-ids' => 'joined',
@@ -56,7 +57,7 @@ cj.advertiser_lookup('keywords' => '+used +books',
56
57
  puts ''
57
58
  end
58
59
 
59
- # See http://help.cj.com/en/web_services/link_search_service_rest.htm
60
+ # See https://cjcommunity.force.com/s/article/4777180
60
61
  # for the list of request and response parameters.
61
62
  cj.link_search('keywords' => '+used +books',
62
63
  'advertiser-ids' => 'joined',
@@ -68,13 +69,16 @@ cj.link_search('keywords' => '+used +books',
68
69
  puts ''
69
70
  end
70
71
 
71
- # See http://help.cj.com/en/web_services/support_services_rest.htm
72
+ # See https://cjcommunity.force.com/s/article/4777190
72
73
  # for the list of request and response parameters.
73
74
  puts cj.categories
74
75
 
75
- # See http://help.cj.com/en/web_services/Commission_Detail_Service.htm
76
+ # See https://cjcommunity.force.com/s/article/4777175
76
77
  # for the list of request and response parameters.
78
+ ids = []
79
+
77
80
  cj.commissions.each do |commission|
81
+ ids << commission.original_action_id
78
82
  puts commission.action_type
79
83
  puts commission.aid
80
84
  puts commission.commission_id
@@ -84,6 +88,26 @@ cj.commissions.each do |commission|
84
88
  puts commission.sid
85
89
  puts ''
86
90
  end
91
+
92
+ # Each commission comes from the sale of one or more items.
93
+ # Commissions and their items are linked by original_action_id.
94
+ cj.item_detail(ids[0, 50]).each do |item_detail|
95
+ puts item_detail['original_action_id']
96
+
97
+ items = item_detail['item']
98
+ # If there is exactly one item, put it in an array.
99
+ items = [items] if items.is_a?(Hash)
100
+
101
+ items.each do |item|
102
+ puts item['sku']
103
+ puts item['quantity']
104
+ puts item['posting_date']
105
+ puts item['commission_id']
106
+ puts item['sale_amount']
107
+ puts item['discount']
108
+ puts item['publisher_commission']
109
+ end if items
110
+ end
87
111
  ```
88
112
 
89
113
  ## Dependencies
data/Rakefile CHANGED
@@ -7,4 +7,4 @@ Rake::TestTask.new(:test) do |test|
7
7
  test.verbose = true
8
8
  end
9
9
 
10
- task :default => :test
10
+ task default: :test
@@ -8,16 +8,16 @@ Gem::Specification.new do |gem|
8
8
  gem.version = CommissionJunction::VERSION
9
9
  gem.author = 'Albert Vernon'
10
10
  gem.email = 'aev@vernon.nu'
11
- gem.description = %q{Ruby wrapper for the Commission Junction web services APIs (REST)}
12
- gem.summary = %q{Commission Junction web services APIs (REST)}
13
- gem.homepage = %q{https://github.com/aevernon/commission_junction}
11
+ gem.description = 'Ruby wrapper for the Commission Junction web services APIs (REST)'
12
+ gem.summary = 'Commission Junction web services APIs (REST)'
13
+ gem.homepage = 'https://github.com/aevernon/commission_junction'
14
14
 
15
- gem.files = `git ls-files`.split($/)
16
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ['lib']
19
19
  gem.add_dependency 'httparty', '~> 0.13'
20
20
  gem.add_development_dependency 'minitest', '~> 0'
21
21
  gem.add_dependency 'ox', '~> 2.1'
22
- gem.license = 'BSD-3-Clause'
22
+ gem.license = 'BSD-3-Clause'
23
23
  end
@@ -1,3 +1,3 @@
1
1
  class CommissionJunction
2
- VERSION = '1.7.3'
2
+ VERSION = '1.8.0'.freeze
3
3
  end
@@ -3,11 +3,12 @@ require 'ox'
3
3
  require 'httparty'
4
4
 
5
5
  # Interact with CJ web services.
6
+ # See https://cjcommunity.force.com/s/article/4777058.
6
7
  class CommissionJunction
7
8
  include HTTParty
8
9
  default_options.update(verify: false) # Skip SSL certificate verification.
9
10
  format(:xml)
10
- #debug_output $stderr
11
+ # debug_output $stderr
11
12
 
12
13
  attr_reader :total_matched,
13
14
  :records_returned,
@@ -15,20 +16,21 @@ class CommissionJunction
15
16
  :cj_objects
16
17
 
17
18
  WEB_SERVICE_URIS =
18
- {
19
- :product_search => 'https://product-search.api.cj.com/v2/product-search',
20
- :link_search => 'https://link-search.api.cj.com/v2/link-search',
21
- :advertiser_lookup => 'https://advertiser-lookup.api.cj.com/v3/advertiser-lookup',
22
- :categories => 'https://support-services.api.cj.com/v2/categories',
23
- :commissions => 'https://commission-detail.api.cj.com/v3/commissions'
24
- }
19
+ {
20
+ product_search: 'https://product-search.api.cj.com/v2/product-search',
21
+ link_search: 'https://link-search.api.cj.com/v2/link-search',
22
+ advertiser_lookup: 'https://advertiser-lookup.api.cj.com/v3/advertiser-lookup',
23
+ categories: 'https://support-services.api.cj.com/v2/categories',
24
+ commissions: 'https://commission-detail.api.cj.com/v3/commissions',
25
+ item_detail: 'https://commission-detail.api.cj.com/v3/item-detail/'
26
+ }.freeze
25
27
 
26
28
  def initialize(developer_key, website_id, timeout = 10)
27
29
  raise ArgumentError, "developer_key must be a String; got #{developer_key.class} instead" unless developer_key.is_a?(String)
28
- raise ArgumentError, "You must supply your developer key.\nSee https://api.cj.com/sign_up.cj" unless developer_key.length > 0
30
+ raise ArgumentError, "You must supply your developer key.\nSee https://api.cj.com/sign_up.cj" if developer_key.empty?
29
31
 
30
32
  website_id = website_id.to_s
31
- raise ArgumentError, "You must supply your website ID.\nSee cj.com > Account > Web site Settings > PID" unless website_id.length > 0
33
+ raise ArgumentError, "You must supply your website ID.\nSee cj.com > Account > Web site Settings > PID" if website_id.empty?
32
34
  @website_id = website_id
33
35
 
34
36
  raise ArgumentError, "timeout must be a Fixnum; got #{timeout.class} instead" unless timeout.is_a?(Fixnum)
@@ -42,21 +44,21 @@ class CommissionJunction
42
44
  def categories(params = {})
43
45
  raise ArgumentError, "params must be a Hash; got #{params.class} instead" unless params.is_a?(Hash)
44
46
 
45
- params = {'locale' => 'en'}.merge(params)
47
+ params = { 'locale' => 'en' }.merge(params)
46
48
 
47
- response = self.class.get(WEB_SERVICE_URIS[:categories], :query => params, :timeout => @timeout)
49
+ response = self.class.get(WEB_SERVICE_URIS[:categories], query: params, timeout: @timeout)
48
50
  @categories = extract_contents(response, 'categories', 'category')
49
51
  end
50
52
 
51
53
  def advertiser_lookup(params = {})
52
54
  raise ArgumentError, "params must be a Hash; got #{params.class} instead" unless params.is_a?(Hash)
53
55
 
54
- params = {'advertiser-ids' => 'joined'}.merge(params)
56
+ params = { 'advertiser-ids' => 'joined' }.merge(params)
55
57
 
56
58
  @cj_objects = []
57
59
 
58
60
  begin
59
- response = self.class.get(WEB_SERVICE_URIS[:advertiser_lookup], :query => params)
61
+ response = self.class.get(WEB_SERVICE_URIS[:advertiser_lookup], query: params)
60
62
  advertisers = extract_contents(response, 'advertisers')
61
63
 
62
64
  @total_matched = advertisers['total_matched'].to_i
@@ -76,8 +78,8 @@ class CommissionJunction
76
78
  def product_search(params)
77
79
  raise ArgumentError, "params must be a Hash; got #{params.class} instead" unless params.is_a?(Hash)
78
80
 
79
- unless params.size > 0
80
- raise ArgumentError, "You must provide at least one request parameter, for example, \"keywords\".\nSee http://help.cj.com/en/web_services/product_catalog_search_service_rest.htm"
81
+ if params.empty?
82
+ raise ArgumentError, "You must provide at least one request parameter, for example, \"keywords\".\nSee https://cjcommunity.force.com/s/article/4777185."
81
83
  end
82
84
 
83
85
  params['website-id'] = @website_id
@@ -85,7 +87,7 @@ class CommissionJunction
85
87
  @cj_objects = []
86
88
 
87
89
  begin
88
- response = self.class.get(WEB_SERVICE_URIS[:product_search], :query => params, :timeout => @timeout)
90
+ response = self.class.get(WEB_SERVICE_URIS[:product_search], query: params, timeout: @timeout)
89
91
  products = extract_contents(response, 'products')
90
92
 
91
93
  @total_matched = products['total_matched'].to_i
@@ -105,8 +107,8 @@ class CommissionJunction
105
107
  def link_search(params)
106
108
  raise ArgumentError, "params must be a Hash; got #{params.class} instead" unless params.is_a?(Hash)
107
109
 
108
- unless params.size > 0
109
- raise ArgumentError, "You must provide at least one request parameter, for example, \"keywords\".\nSee http://help.cj.com/en/web_services/product_catalog_search_service_rest.htm"
110
+ if params.empty?
111
+ raise ArgumentError, "You must provide at least one request parameter, for example, \"keywords\".\nSee https://cjcommunity.force.com/s/article/4777180."
110
112
  end
111
113
 
112
114
  params['website-id'] = @website_id
@@ -114,7 +116,7 @@ class CommissionJunction
114
116
  @cj_objects = []
115
117
 
116
118
  begin
117
- response = self.class.get(WEB_SERVICE_URIS[:link_search], :query => params, :timeout => @timeout)
119
+ response = self.class.get(WEB_SERVICE_URIS[:link_search], query: params, timeout: @timeout)
118
120
  links = extract_contents(response, 'links')
119
121
 
120
122
  @total_matched = links['total_matched'].to_i
@@ -134,12 +136,12 @@ class CommissionJunction
134
136
  def commissions(params = {})
135
137
  raise ArgumentError, "params must be a Hash; got #{params.class} instead" unless params.is_a?(Hash)
136
138
 
137
- params = {'date-type' => 'event'}.merge(params)
139
+ params = { 'date-type' => 'event' }.merge(params)
138
140
 
139
141
  @cj_objects = []
140
142
 
141
143
  begin
142
- response = self.class.get(WEB_SERVICE_URIS[:commissions], :query => params)
144
+ response = self.class.get(WEB_SERVICE_URIS[:commissions], query: params)
143
145
  commissions = extract_contents(response, 'commissions')
144
146
 
145
147
  @total_matched = commissions['total_matched'].to_i
@@ -156,10 +158,31 @@ class CommissionJunction
156
158
  @cj_objects
157
159
  end
158
160
 
159
- def extract_contents(response, first_level, second_level=nil)
161
+ def item_detail(original_action_ids)
162
+ raise ArgumentError, "original_action_ids must be an Array; got #{original_action_ids.class} instead" unless original_action_ids.is_a?(Array)
163
+
164
+ unless (1..50).cover?(original_action_ids.size)
165
+ raise ArgumentError, "You must provide between 1 and 50 original action IDs.\nSee https://cjcommunity.force.com/s/article/4777175."
166
+ end
167
+
168
+ @cj_objects = []
169
+
170
+ begin
171
+ ids = original_action_ids.join(',')
172
+ response = self.class.get(WEB_SERVICE_URIS[:item_detail] + ids, query: "original-action-id=#{ids}")
173
+ @cj_objects = extract_contents(response, 'item_details')
174
+ @cj_objects = [@cj_objects] if @cj_objects.is_a?(Hash) # If we got exactly one result, put it in an array.
175
+ rescue Timeout::Error
176
+ @total_matched = @records_returned = @page_number = 0
177
+ end
178
+
179
+ @cj_objects
180
+ end
181
+
182
+ def extract_contents(response, first_level, second_level = nil)
160
183
  cj_api = response['cj_api']
161
184
 
162
- raise ArgumentError, "cj api missing from response" if cj_api.nil?
185
+ raise ArgumentError, 'cj api missing from response' if cj_api.nil?
163
186
 
164
187
  error_message = cj_api['error_message']
165
188
 
@@ -169,10 +192,11 @@ class CommissionJunction
169
192
  cj_api[first_level][second_level]
170
193
  end
171
194
 
195
+ # Turn a hash into an object where each key becomes an instance method.
172
196
  class CjObject
173
197
  def initialize(params)
174
198
  raise ArgumentError, "params must be a Hash; got #{params.class} instead" unless params.is_a?(Hash)
175
- raise ArgumentError, 'Expecting at least one parameter' unless params.size > 0
199
+ raise ArgumentError, 'Expecting at least one parameter' if params.empty?
176
200
 
177
201
  # Create instance variables and attribute readers on the fly.
178
202
  # Credit: http://listlibrary.net/ruby-talk/2004/03/00sGI1cD
@@ -180,7 +204,7 @@ class CommissionJunction
180
204
  raise ArgumentError, "key must be a String; got #{key.class} instead" unless key.is_a?(String)
181
205
  clean_key = clean_key_name(key)
182
206
  instance_variable_set("@#{clean_key}".intern, val)
183
- instance_eval %Q{ class << self ; attr_reader #{clean_key.intern.inspect} ; end }
207
+ instance_eval %( class << self ; attr_reader #{clean_key.intern.inspect} ; end )
184
208
  end
185
209
  end
186
210
 
@@ -43,11 +43,11 @@ class CommissionJunctionTest < Minitest::Test
43
43
 
44
44
  def test_new_cj_with_non_string_param
45
45
  assert_raises ArgumentError do
46
- CommissionJunction.new(123456, 'website_id')
46
+ CommissionJunction.new(123_456, 'website_id')
47
47
  end
48
48
 
49
49
  assert_nothing_raised ArgumentError do
50
- CommissionJunction.new('developer_key', 123456)
50
+ CommissionJunction.new('developer_key', 123_456)
51
51
  end
52
52
  end
53
53
 
@@ -103,7 +103,7 @@ class CommissionJunctionTest < Minitest::Test
103
103
 
104
104
  def test_new_product_with_hash_params_and_non_string_keys
105
105
  assert_raises ArgumentError do
106
- CommissionJunction::Product.new(:name => 'blue jeans', :price => '49.95')
106
+ CommissionJunction::Product.new(name: 'blue jeans', price: '49.95')
107
107
  end
108
108
  end
109
109
 
@@ -151,12 +151,11 @@ class CommissionJunctionTest < Minitest::Test
151
151
  end
152
152
 
153
153
  def test_product_search_with_keywords_live
154
- key_file = File.join(ENV['HOME'], '.commission_junction.yaml')
155
-
156
- skip "#{key_file} does not exist. Put your CJ developer key and website ID in there to enable live testing." unless File.exist?(key_file)
154
+ unless ENV['CJ_DEVELOPER_KEY'] && ENV['CJ_WEBSITE_ID']
155
+ skip 'Skipping live testing because environment variables CJ_DEVELOPER_KEY and CJ_WEBSITE_ID are not set.'
156
+ end
157
157
 
158
- credentials = YAML.load(File.read(key_file))
159
- cj = CommissionJunction.new(credentials['developer_key'], credentials['website_id'])
158
+ cj = CommissionJunction.new(ENV['CJ_DEVELOPER_KEY'], ENV['CJ_WEBSITE_ID'])
160
159
 
161
160
  # Zero results
162
161
  assert_nothing_raised do
@@ -180,7 +179,7 @@ class CommissionJunctionTest < Minitest::Test
180
179
  check_search_results(cj)
181
180
 
182
181
  # Short timeout
183
- cj = CommissionJunction.new(credentials['developer_key'], credentials['website_id'], 1)
182
+ cj = CommissionJunction.new(ENV['CJ_DEVELOPER_KEY'], ENV['CJ_WEBSITE_ID'], 1)
184
183
 
185
184
  assert_nothing_raised do
186
185
  cj.product_search('keywords' => 'One Great Blue Jean~No Limits', 'records-per-page' => '1')
@@ -219,12 +218,11 @@ class CommissionJunctionTest < Minitest::Test
219
218
  end
220
219
 
221
220
  def test_advertiser_lookup_live
222
- key_file = File.join(ENV['HOME'], '.commission_junction.yaml')
223
-
224
- skip "#{key_file} does not exist. Put your CJ developer key and website ID in there to enable live testing." unless File.exist?(key_file)
221
+ unless ENV['CJ_DEVELOPER_KEY'] && ENV['CJ_WEBSITE_ID']
222
+ skip 'Skipping live testing because environment variables CJ_DEVELOPER_KEY and CJ_WEBSITE_ID are not set.'
223
+ end
225
224
 
226
- credentials = YAML.load(File.read(key_file))
227
- cj = CommissionJunction.new(credentials['developer_key'], credentials['website_id'])
225
+ cj = CommissionJunction.new(ENV['CJ_DEVELOPER_KEY'], ENV['CJ_WEBSITE_ID'])
228
226
 
229
227
  # Use default lookup parameters.
230
228
  assert_nothing_raised do
@@ -248,7 +246,7 @@ class CommissionJunctionTest < Minitest::Test
248
246
  check_advertiser_lookup_results(cj)
249
247
 
250
248
  # Short timeout
251
- cj = CommissionJunction.new(credentials['developer_key'], credentials['website_id'], 1)
249
+ cj = CommissionJunction.new(ENV['CJ_DEVELOPER_KEY'], ENV['CJ_WEBSITE_ID'], 1)
252
250
 
253
251
  assert_nothing_raised do
254
252
  cj.advertiser_lookup('keywords' => 'One Great Blue Jean~No Limits', 'records-per-page' => '1')
@@ -280,23 +278,21 @@ class CommissionJunctionTest < Minitest::Test
280
278
  end
281
279
 
282
280
  def test_categories_live
283
- key_file = File.join(ENV['HOME'], '.commission_junction.yaml')
284
-
285
- skip "#{key_file} does not exist. Put your CJ developer key and website ID in there to enable live testing." unless File.exist?(key_file)
281
+ unless ENV['CJ_DEVELOPER_KEY'] && ENV['CJ_WEBSITE_ID']
282
+ skip 'Skipping live testing because environment variables CJ_DEVELOPER_KEY and CJ_WEBSITE_ID are not set.'
283
+ end
286
284
 
287
- credentials = YAML.load(File.read(key_file))
288
- cj = CommissionJunction.new(credentials['developer_key'], credentials['website_id'])
285
+ cj = CommissionJunction.new(ENV['CJ_DEVELOPER_KEY'], ENV['CJ_WEBSITE_ID'])
289
286
 
290
- assert cj.categories.size > 0
287
+ assert !cj.categories.empty?
291
288
  end
292
289
 
293
290
  def test_commissions_live
294
- key_file = File.join(ENV['HOME'], '.commission_junction.yaml')
295
-
296
- skip "#{key_file} does not exist. Put your CJ developer key and website ID in there to enable live testing." unless File.exist?(key_file)
291
+ unless ENV['CJ_DEVELOPER_KEY'] && ENV['CJ_WEBSITE_ID']
292
+ skip 'Skipping live testing because environment variables CJ_DEVELOPER_KEY and CJ_WEBSITE_ID are not set.'
293
+ end
297
294
 
298
- credentials = YAML.load(File.read(key_file))
299
- cj = CommissionJunction.new(credentials['developer_key'], credentials['website_id'])
295
+ cj = CommissionJunction.new(ENV['CJ_DEVELOPER_KEY'], ENV['CJ_WEBSITE_ID'])
300
296
 
301
297
  assert_nothing_raised do
302
298
  cj.commissions
@@ -342,13 +338,86 @@ class CommissionJunctionTest < Minitest::Test
342
338
  end
343
339
  end
344
340
 
345
- def test_link_search_live
346
- key_file = File.join(ENV['HOME'], '.commission_junction.yaml')
341
+ def test_item_detail_with_no_params
342
+ assert_raises ArgumentError do
343
+ CommissionJunction.new('developer_key', 'website_id').item_detail
344
+ end
345
+ end
346
+
347
+ def test_item_detail_with_nil_params
348
+ assert_raises ArgumentError do
349
+ CommissionJunction.new('developer_key', 'website_id').item_detail(nil)
350
+ end
351
+ end
352
+
353
+ def test_item_detail_with_too_few_ids
354
+ assert_raises ArgumentError do
355
+ CommissionJunction.new('developer_key', 'website_id').item_detail([])
356
+ end
357
+ end
358
+
359
+ def test_item_detail_with_too_many_ids
360
+ assert_raises ArgumentError do
361
+ CommissionJunction.new('developer_key', 'website_id').item_detail(Array.new(51))
362
+ end
363
+ end
347
364
 
348
- skip "#{key_file} does not exist. Put your CJ developer key and website ID in there to enable live testing." unless File.exist?(key_file)
365
+ def test_item_detail_with_non_array_params
366
+ assert_raises ArgumentError do
367
+ CommissionJunction.new('developer_key', 'website_id').item_detail('string')
368
+ end
369
+ end
370
+
371
+ def test_item_detail_live
372
+ unless ENV['CJ_DEVELOPER_KEY'] && ENV['CJ_WEBSITE_ID']
373
+ skip 'Skipping live testing because environment variables CJ_DEVELOPER_KEY and CJ_WEBSITE_ID are not set.'
374
+ end
375
+
376
+ cj = CommissionJunction.new(ENV['CJ_DEVELOPER_KEY'], ENV['CJ_WEBSITE_ID'])
377
+
378
+ ids = []
379
+
380
+ cj.commissions.each do |commission|
381
+ ids << commission.original_action_id
382
+ end
383
+
384
+ skip 'Skipping live testing of item_detail because there are no original action IDs in your account.' if ids.empty?
385
+
386
+ assert_nothing_raised do
387
+ cj.item_detail(ids[0, 1])
388
+ check_item_detail_results(cj)
389
+ cj.item_detail(ids[0, 2])
390
+ check_item_detail_results(cj)
391
+ end
392
+ end
393
+
394
+ def check_item_detail_results(results)
395
+ assert_instance_of(Array, results.cj_objects)
396
+
397
+ results.cj_objects.each do |item_detail|
398
+ assert_instance_of(Hash, item_detail)
399
+ assert item_detail.key?('original_action_id')
400
+ assert item_detail.key?('item')
401
+
402
+ item = item_detail['item']
403
+ item = item.first if item.is_a?(Array)
404
+
405
+ assert item.key?('sku')
406
+ assert item.key?('quantity')
407
+ assert item.key?('posting_date')
408
+ assert item.key?('commission_id')
409
+ assert item.key?('sale_amount')
410
+ assert item.key?('discount')
411
+ assert item.key?('publisher_commission')
412
+ end
413
+ end
414
+
415
+ def test_link_search_live
416
+ unless ENV['CJ_DEVELOPER_KEY'] && ENV['CJ_WEBSITE_ID']
417
+ skip 'Skipping live testing because environment variables CJ_DEVELOPER_KEY and CJ_WEBSITE_ID are not set.'
418
+ end
349
419
 
350
- credentials = YAML.load(File.read(key_file))
351
- cj = CommissionJunction.new(credentials['developer_key'], credentials['website_id'])
420
+ cj = CommissionJunction.new(ENV['CJ_DEVELOPER_KEY'], ENV['CJ_WEBSITE_ID'])
352
421
 
353
422
  assert_nothing_raised do
354
423
  cj.link_search('keywords' => '+blue +jeans', 'advertiser-ids' => 'joined')
@@ -390,35 +459,35 @@ class CommissionJunctionTest < Minitest::Test
390
459
  end
391
460
 
392
461
  def set_up_service
393
- CommissionJunction.new('developer_key', 123456)
462
+ CommissionJunction.new('developer_key', 123_456)
394
463
  end
395
464
 
396
465
  def test_contents_extractor_with_first_level
397
- contents = "abc"
398
- response = {'cj_api' => {'first' => contents}}
466
+ contents = 'abc'
467
+ response = { 'cj_api' => { 'first' => contents } }
399
468
 
400
469
  cj = set_up_service
401
470
 
402
- assert_equal(contents, cj.extract_contents(response, "first"))
471
+ assert_equal(contents, cj.extract_contents(response, 'first'))
403
472
  end
404
473
 
405
474
  def test_contents_extractor_with_second_level
406
- contents = "abc"
407
- response = {'cj_api' => {'first' => {'second' => contents}}}
475
+ contents = 'abc'
476
+ response = { 'cj_api' => { 'first' => { 'second' => contents } } }
408
477
 
409
478
  cj = set_up_service
410
479
 
411
- assert_equal(contents, cj.extract_contents(response, "first", "second"))
480
+ assert_equal(contents, cj.extract_contents(response, 'first', 'second'))
412
481
  end
413
482
 
414
483
  def test_contents_extractor_with_error_message
415
- contents = "abc"
416
- response = {'cj_api' => {'error_message' => contents}}
484
+ contents = 'abc'
485
+ response = { 'cj_api' => { 'error_message' => contents } }
417
486
 
418
487
  cj = set_up_service
419
488
 
420
489
  assert_raises ArgumentError do
421
- cj.extract_contents(response, "first")
490
+ cj.extract_contents(response, 'first')
422
491
  end
423
492
  end
424
493
 
@@ -428,28 +497,28 @@ class CommissionJunctionTest < Minitest::Test
428
497
  cj = set_up_service
429
498
 
430
499
  assert_raises ArgumentError do
431
- cj.extract_contents(response, "first")
500
+ cj.extract_contents(response, 'first')
432
501
  end
433
502
  end
434
503
 
435
504
  def set_up_cj_object
436
- CommissionJunction::CjObject.new({"a" => "a"})
505
+ CommissionJunction::CjObject.new('a' => 'a')
437
506
  end
438
507
 
439
508
  def test_key_conversion_with_spaces
440
509
  cjo = set_up_cj_object
441
510
 
442
- assert_equal("abc_def", cjo.clean_key_name("abc def"))
511
+ assert_equal('abc_def', cjo.clean_key_name('abc def'))
443
512
  end
444
513
 
445
514
  def test_key_conversion_with_trailing_spaces
446
515
  cjo = set_up_cj_object
447
516
 
448
- assert_equal("abcdef", cjo.clean_key_name("abcdef "))
517
+ assert_equal('abcdef', cjo.clean_key_name('abcdef '))
449
518
  end
450
519
 
451
520
  def test_initializing_product_using_key_with_spaces
452
- product = CommissionJunction::Product.new("abc def" => "123")
453
- assert_equal(product.abc_def, "123")
521
+ product = CommissionJunction::Product.new('abc def' => '123')
522
+ assert_equal(product.abc_def, '123')
454
523
  end
455
524
  end
data/test/test_helper.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'rubygems'
2
1
  require 'minitest/autorun'
3
2
 
4
3
  $LOAD_PATH.unshift(File.dirname(__FILE__))
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commission_junction
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.3
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Albert Vernon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-12 00:00:00.000000000 Z
11
+ date: 2016-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -59,6 +59,7 @@ extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
61
  - ".gitignore"
62
+ - ".rubocop.yml"
62
63
  - Gemfile
63
64
  - LICENSE
64
65
  - README.md
@@ -88,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
89
  version: '0'
89
90
  requirements: []
90
91
  rubyforge_project:
91
- rubygems_version: 2.2.2
92
+ rubygems_version: 2.4.8
92
93
  signing_key:
93
94
  specification_version: 4
94
95
  summary: Commission Junction web services APIs (REST)