Class: HitfoxCouponApi::Coupon

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

Constant Summary

Constants inherited from Client

HitfoxCouponApi::Client::HitfoxApiException

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Client

#apiheaders, #configuration, #generate_url, #handle_coupon_results

Constructor Details

#initialize(application, code, url = nil) ⇒ Coupon

Returns a new instance of Coupon.



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

def initialize(application, code, url = nil)
  @application, @code, @url = application, code, url
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



7
8
9
# File 'lib/hitfox_coupon_api/coupon.rb', line 7

def code
  @code
end

#stateObject

Returns the value of attribute state.



7
8
9
# File 'lib/hitfox_coupon_api/coupon.rb', line 7

def state
  @state
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/hitfox_coupon_api/coupon.rb', line 7

def url
  @url
end

Instance Method Details

#add(cpns_code) ⇒ Object

Add new coupon codes to an application. Take an array of coupon details and create deal coupons. The array is assumed to be structured as: [type, code, type, code, link, type, code, type, code, … etc] where type is one of: :actlink (2 args), :ingame (1 arg), :url (1 arg), e.g.

[:actlink, code, link, :ingame, code, :url, link, :ingame, code, ....]

would a valid array. The :actlink type takes two further parameters: link and code, while the ingame and url types only take one further argument.



21
22
23
24
25
26
27
# File 'lib/hitfox_coupon_api/coupon.rb', line 21

def add(cpns_code)
  headers, params = build_request_data(cpns_code.count) do |config, headers, hshstr|
    [config.api_version.to_s, Digest::SHA1.hexdigest(hshstr)]
  end
  urlstr = generate_url('/%s/coupons/create.json?hash=%s', params)
  JSON.parse(RestClient.post(urlstr, { :coupons => cpns_code }, headers))
end

#add!(cpns_code) ⇒ Object



29
30
31
32
# File 'lib/hitfox_coupon_api/coupon.rb', line 29

def add!(cpns_code)
  res = add(cpns_code)
  res["status"] == 0 ? true : raise(HitfoxApiException, "#{res['status']}: #{res['msg']}")
end

#app_idObject



79
80
81
# File 'lib/hitfox_coupon_api/coupon.rb', line 79

def app_id
  @application.identifier
end

#available?(count = 1) ⇒ Boolean

Are there count-number of coupons avaiable for this product. This is not strictly related to a particular coupon, rather this is delegated down from the application to the coupon, Application#available? should be used instead of this method directly.

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'lib/hitfox_coupon_api/coupon.rb', line 71

def available?(count = 1)
  headers, params = build_request_data(count) do |config, headers, hshstr|
    [config.api_version.to_s, Digest::SHA1.hexdigest(hshstr), count.to_s]
  end
  urlstr = generate_url('/%s/coupons/available.json?hash=%s&count=%s', params)
  JSON.parse(RestClient.get(urlstr, headers))["status"] == 0
end

#infoObject

Provide details on this coupon. The code, download url and the status are provided of the coupon.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hitfox_coupon_api/coupon.rb', line 53

def info
  headers, params = build_request_data(@code) do |config, headers, hshstr|
    [config.api_version.to_s, @code, Digest::SHA1.hexdigest(hshstr)]
  end

  urlstr = generate_url('/%s/coupon/%s/info.json?hash=%s', params)

  handle_coupon_results(JSON.parse(RestClient.get(urlstr, headers))) do |cpn|
    Coupon.new(Application.new(cpn["app_id"]), cpn["code"], cpn["download_url"]).tap do |cc|
      cc.state = cpn["state"]
    end
  end
end

#usedObject

Make a coupon as having been used by the end-user. This should be called once the user has entered and reclaimed their coupon. It provides HitFox with the information that the coupon has been used.



38
39
40
41
42
43
44
# File 'lib/hitfox_coupon_api/coupon.rb', line 38

def used
  headers, params = build_request_data(@code) do |config, headers, hshstr|
    [config.api_version.to_s, @code, Digest::SHA1.hexdigest(hshstr)]
  end
  urlstr = generate_url('/%s/coupon/%s/used.json?hash=%s', params)
  JSON.parse(RestClient.get(urlstr, headers))
end

#used!Object



46
47
48
49
# File 'lib/hitfox_coupon_api/coupon.rb', line 46

def used!
  res = used
  res["status"] == 0 ? true : raise(HitfoxApiException, "#{res['status']}: #{res['msg']}")
end