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.

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.1'

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)


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_objectsObject (readonly)

debug_output $stderr



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

def cj_objects
  @cj_objects
end

#page_numberObject (readonly)

debug_output $stderr



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

def page_number
  @page_number
end

#records_returnedObject (readonly)

debug_output $stderr



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

def records_returned
  @records_returned
end

#total_matchedObject (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

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/commission_junction.rb', line 57

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)
    cj_api = response['cj_api']
    error_message = cj_api['error_message']

    raise ArgumentError, error_message if error_message

    advertisers = cj_api['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

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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)

  cj_api = response['cj_api']
  error_message = cj_api['error_message']

  raise ArgumentError, error_message if error_message

  @categories = cj_api['categories']['category']
end

#commissions(params = {}) ⇒ Object

Raises:

  • (ArgumentError)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/commission_junction.rb', line 157

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)
    cj_api = response['cj_api']
    error_message = cj_api['error_message']

    raise ArgumentError, error_message if error_message

    commissions = cj_api['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

Raises:

  • (ArgumentError)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/commission_junction.rb', line 122

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)

    cj_api = response['cj_api']
    error_message = cj_api['error_message']

    raise ArgumentError, error_message if error_message

    links = cj_api['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

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/commission_junction.rb', line 87

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)

    cj_api = response['cj_api']
    error_message = cj_api['error_message']

    raise ArgumentError, error_message if error_message

    products = cj_api['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