Class: LinkshareAPI::CouponWebService

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

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCouponWebService

Returns a new instance of CouponWebService.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/linkshare_api/coupon_web_service.rb', line 11

def initialize
  @token        = LinkshareAPI.token
  @api_base_url = LinkshareAPI::WEB_SERVICE_URIS[:coupon_web_service]
  @api_timeout  = LinkshareAPI.api_timeout

  if @token.nil?
    raise AuthenticationError.new(
      "No token. Set your token by using 'LinkshareAPI.token = <TOKEN>'. " +
      "You can retrieve your token from LinkhShare's Web Services page under the Links tab. " +
      "See https://rakutenlinkshare.zendesk.com/hc/en-us/articles/200992487-What-is-a-Web-Services-Token-Feed-Token- for details."
    )
  end

  self.class.default_timeout @api_timeout
end

Instance Attribute Details

#api_base_urlObject (readonly)

Returns the value of attribute api_base_url.



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

def api_base_url
  @api_base_url
end

#api_timeoutObject (readonly)

Returns the value of attribute api_timeout.



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

def api_timeout
  @api_timeout
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#query(params) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/linkshare_api/coupon_web_service.rb', line 27

def query(params)
  raise ArgumentError, "Hash expected, got #{params.class} instead" unless params.is_a?(Hash)

  params.merge!(token: token)
  begin
    response = self.class.get(
      api_base_url,
      query: params
    )
  rescue Timeout::Error, Net::OpenTimeout
    raise ConnectionError.new("Timeout error (#{api_timeout}s)")
  end

  if response.code != 200
    raise Error.new(response.message, response.code)
  end
  error = response["fault"]
  raise InvalidRequestError.new(error["errorstring"], error["errorcode"].to_i) if error

  Response.new(response, :coupon_web_service)
end