Class: CommissionJunction
- Inherits:
-
Object
- Object
- CommissionJunction
- 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 =
'1.8.0'.freeze
Instance Attribute Summary collapse
-
#cj_objects ⇒ Object
readonly
debug_output $stderr.
-
#page_number ⇒ Object
readonly
debug_output $stderr.
-
#records_returned ⇒ Object
readonly
debug_output $stderr.
-
#total_matched ⇒ Object
readonly
debug_output $stderr.
Instance Method Summary collapse
- #advertiser_lookup(params = {}) ⇒ Object
- #categories(params = {}) ⇒ Object
- #commissions(params = {}) ⇒ Object
- #extract_contents(response, first_level, second_level = nil) ⇒ Object
-
#initialize(developer_key, website_id, timeout = 10) ⇒ CommissionJunction
constructor
A new instance of CommissionJunction.
- #item_detail(original_action_ids) ⇒ Object
- #link_search(params) ⇒ Object
- #product_search(params) ⇒ Object
Constructor Details
#initialize(developer_key, website_id, timeout = 10) ⇒ CommissionJunction
Returns a new instance of CommissionJunction.
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 Fixnum; got #{timeout.class} instead" unless timeout.is_a?(Fixnum) 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_objects ⇒ Object (readonly)
debug_output $stderr
13 14 15 |
# File 'lib/commission_junction.rb', line 13 def cj_objects @cj_objects end |
#page_number ⇒ Object (readonly)
debug_output $stderr
13 14 15 |
# File 'lib/commission_junction.rb', line 13 def page_number @page_number end |
#records_returned ⇒ Object (readonly)
debug_output $stderr
13 14 15 |
# File 'lib/commission_junction.rb', line 13 def records_returned @records_returned end |
#total_matched ⇒ Object (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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# 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 = [] begin 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 rescue Timeout::Error @total_matched = @records_returned = @page_number = 0 end @cj_objects end |
#categories(params = {}) ⇒ Object
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
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/commission_junction.rb', line 136 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 = [] begin 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 rescue Timeout::Error @total_matched = @records_returned = @page_number = 0 end @cj_objects end |
#extract_contents(response, first_level, second_level = nil) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/commission_junction.rb', line 182 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? = cj_api['error_message'] raise ArgumentError, if return cj_api[first_level] if second_level.nil? cj_api[first_level][second_level] end |
#item_detail(original_action_ids) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/commission_junction.rb', line 161 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 = [] begin 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. rescue Timeout::Error @total_matched = @records_returned = @page_number = 0 end @cj_objects end |
#link_search(params) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/commission_junction.rb', line 107 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 = [] begin 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 rescue Timeout::Error @total_matched = @records_returned = @page_number = 0 end @cj_objects end |
#product_search(params) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/commission_junction.rb', line 78 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 = [] begin 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 rescue Timeout::Error @total_matched = @records_returned = @page_number = 0 end @cj_objects end |