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.
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' }
- VERSION =
'1.7.2'
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.
- #link_search(params) ⇒ Object
- #product_search(params) ⇒ Object
Constructor Details
#initialize(developer_key, website_id, timeout = 10) ⇒ CommissionJunction
Returns a new instance of CommissionJunction.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/commission_junction.rb', line 26 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" unless developer_key.length > 0 website_id = website_id.to_s raise ArgumentError, "You must supply your website ID.\nSee cj.com > Account > Web site Settings > PID" unless website_id.length > 0 @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
12 13 14 |
# File 'lib/commission_junction.rb', line 12 def cj_objects @cj_objects end |
#page_number ⇒ Object (readonly)
debug_output $stderr
12 13 14 |
# File 'lib/commission_junction.rb', line 12 def page_number @page_number end |
#records_returned ⇒ Object (readonly)
debug_output $stderr
12 13 14 |
# File 'lib/commission_junction.rb', line 12 def records_returned @records_returned end |
#total_matched ⇒ Object (readonly)
debug_output $stderr
12 13 14 |
# File 'lib/commission_junction.rb', line 12 def total_matched @total_matched end |
Instance Method Details
#advertiser_lookup(params = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/commission_junction.rb', line 51 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
42 43 44 45 46 47 48 49 |
# File 'lib/commission_junction.rb', line 42 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
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/commission_junction.rb', line 134 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
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/commission_junction.rb', line 159 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 |
#link_search(params) ⇒ Object
105 106 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 |
# File 'lib/commission_junction.rb', line 105 def link_search(params) raise ArgumentError, "params must be a Hash; got #{params.class} instead" unless params.is_a?(Hash) unless params.size > 0 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" 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
76 77 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 |
# File 'lib/commission_junction.rb', line 76 def product_search(params) raise ArgumentError, "params must be a Hash; got #{params.class} instead" unless params.is_a?(Hash) unless params.size > 0 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" 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 |