Class: HitfoxCouponApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hitfox_coupon_api/client.rb

Direct Known Subclasses

Coupon, Order, User

Constant Summary collapse

HitfoxApiException =
Class.new(RuntimeError)

Instance Method Summary collapse

Instance Method Details

#apiheadersObject



9
10
11
12
13
14
15
16
# File 'lib/hitfox_coupon_api/client.rb', line 9

def apiheaders
  config = configuration
  {
    "X-API-TOKEN"     => config.api_token,
    "X-API-APP-ID"    => @application.identifier,
    "X-API-TIMESTAMP" => config.generate_timestamp,
  }
end

#configurationObject



5
6
7
# File 'lib/hitfox_coupon_api/client.rb', line 5

def configuration
  @application.configuration || HitfoxCouponApi.configuration
end

#generate_url(path, params) ⇒ Object



18
19
20
# File 'lib/hitfox_coupon_api/client.rb', line 18

def generate_url(path, params)
  '%s%s' % [configuration.api_endpoint, path % params.map { |a| CGI.escape(a) }]
end

#handle_coupon_results(res) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/hitfox_coupon_api/client.rb', line 22

def handle_coupon_results(res)
  if res["status"] == 0
    res["coupons"].map { |c| c[c.keys.first] }.map do |cc|
      block_given? ? yield(cc) : Coupon.new(@application, cc["code"], cc["download_url"])
    end
  else
    raise HitfoxApiException, "#{res['status']}: #{res['msg']}"
  end
end