Class: CommissionJunction

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

Overview

Interact with CJ web services.

Defined Under Namespace

Classes: Advertiser, CjObject, Product

Constant Summary collapse

WEB_SERVICE_URIS =
{
  :product_search => 'https://product-search.api.cj.com/v2/product-search',
  :advertiser_lookup => 'https://advertiser-lookup.api.cj.com/v3/advertiser-lookup'
}

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)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/commission_junction.rb', line 20

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

  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)
  # This causes the Invalid Key error. We don't need this anymore
  #self_class.default_params('website-id' => website_id)
end

Instance Attribute Details

#cj_objectsObject (readonly)

debug_output $stderr



9
10
11
# File 'lib/commission_junction.rb', line 9

def cj_objects
  @cj_objects
end

#page_numberObject (readonly)

debug_output $stderr



9
10
11
# File 'lib/commission_junction.rb', line 9

def page_number
  @page_number
end

#records_returnedObject (readonly)

debug_output $stderr



9
10
11
# File 'lib/commission_junction.rb', line 9

def records_returned
  @records_returned
end

#total_matchedObject (readonly)

debug_output $stderr



9
10
11
# File 'lib/commission_junction.rb', line 9

def total_matched
  @total_matched
end

Instance Method Details

#advertiser_lookup(params = { 'advertiser-ids' => 'joined', 'records-per-page' => '5' }) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/commission_junction.rb', line 37

def advertiser_lookup(params = { 'advertiser-ids' => 'joined', 'records-per-page' => '5' })
  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/advertiser_lookup_service_rest.htm"
  end

  @cj_objects = []
  begin
    #response = self.class.get("https://advertiser-lookup.api.cj.com/v3/advertiser-lookup?advertiser-ids=#{advertiser_ids}")
    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

#product_search(params) ⇒ Object

Raises:

  • (ArgumentError)


70
71
72
73
74
75
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
104
105
# File 'lib/commission_junction.rb', line 70

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

  @cj_objects = []

  begin
    if caller_method_name == 'test_product_search_with_keywords_non_live'
      response = Crack::XML.parse(File.read('test/test_response.xml'))
    else
      response = self.class.get(WEB_SERVICE_URIS[:product_search], :query => params, :timeout => @timeout)
    end

    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