Class: Voucherify

Inherits:
Object
  • Object
show all
Defined in:
lib/voucherify.rb,
lib/voucherify/version.rb

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Voucherify

Returns a new instance of Voucherify.



8
9
10
11
12
13
14
15
16
17
# File 'lib/voucherify.rb', line 8

def initialize(options)
  @backend_url = "https://api.voucherify.io/v1"
  @options = options
  @headers = {
    "X-App-Id" => @options["applicationId"],
    "X-App-Token" => @options["clientSecretKey"],
    "X-Voucherify-Channel" => "Ruby-SDK",
    :accept => :json
  }
end

Instance Method Details

#create(code, options = {}) ⇒ Object

‘code` is optional - will be generated if absent. Sample `options` object: {

category: "New Customers",
discount: {
  percent_off: 10.0,
  type: "PERCENT"
},
start_date: "2016-01-01T00:00:00Z",
expiration_date: "2016-12-31T23:59:59Z",
redemption: {
  quantity: 100 
}

}



95
96
97
98
99
100
# File 'lib/voucherify.rb', line 95

def create(code, options = {})
  url = @backend_url + "/vouchers/"
  url += URI.encode(code) if code
  response = RestClient.post(url, options.to_json, @headers.merge({ :content_type => :json }))
  JSON.parse(response.body)
end

#disable(code) ⇒ Object



108
109
110
111
112
# File 'lib/voucherify.rb', line 108

def disable(code)
  url = @backend_url + "/vouchers/" + URI.encode(code) + "/disable"
  response = RestClient.post(url, nil, @headers.merge({ :content_type => :json }))
  nil
end

#enable(code) ⇒ Object



102
103
104
105
106
# File 'lib/voucherify.rb', line 102

def enable(code)
  url = @backend_url + "/vouchers/" + URI.encode(code) + "/enable"
  response = RestClient.post(url, nil, @headers.merge({ :content_type => :json }))
  nil
end

#get(code) ⇒ Object



19
20
21
22
23
# File 'lib/voucherify.rb', line 19

def get(code)
  url = @backend_url + "/vouchers/" + URI.encode(code)
  response = RestClient.get(url, @headers)
  JSON.parse(response.body)
end

#list(query) ⇒ Object

List vouchers. Query parameters:

  • code_query

  • limit (default 10)

  • skip (default 0)

  • category

  • campaign

  • customer



32
33
34
35
36
# File 'lib/voucherify.rb', line 32

def list(query)
  url = @backend_url + "/vouchers/"
  response = RestClient.get(url, @headers.merge({ :params => query }))
  JSON.parse(response.body)
end

#publish(campaign_name) ⇒ Object



75
76
77
78
79
# File 'lib/voucherify.rb', line 75

def publish(campaign_name)
  url = @backend_url + "/vouchers/publish?campaign=" + URI.encode(campaign_name)
  response = RestClient.post(url, nil, @headers.merge({ :content_type => :json }))
  JSON.parse(response.body)
end

#redeem(code, tracking_id = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/voucherify.rb', line 58

def redeem(code, tracking_id = nil)
  payload = {}

  if code.is_a? Hash
    payload = code
    code = payload["voucher"] || payload[:voucher]
    payload.delete "voucher"
    payload.delete :voucher
  end

  url = @backend_url + "/vouchers/" + URI.encode(code) + "/redemption"
  url += ("?tracking_id=" + URI.encode(tracking_id)) if tracking_id

  response = RestClient.post(url, payload.to_json, @headers.merge({ :content_type => :json }))
  JSON.parse(response.body)
end

#redemption(code) ⇒ Object



38
39
40
41
42
# File 'lib/voucherify.rb', line 38

def redemption(code)
  url = @backend_url + "/vouchers/" + URI.encode(code) + "/redemption"
  response = RestClient.get(url, @headers)
  JSON.parse(response.body)
end

#redemptions(query) ⇒ Object

List redemptions. Sample query (1000 successful redemptions from April 2016):

limit: 1000,
page: 0,
start_date: "2016-04-01T00:00:00",
end_date: "2016-04-30T23:59:59",
result: "Success"



52
53
54
55
56
# File 'lib/voucherify.rb', line 52

def redemptions(query)
  url = @backend_url + "/redemptions/"
  response = RestClient.get(url, @headers.merge({ :params => query }))
  JSON.parse(response.body)
end

#rollback(redemption_id, tracking_id = nil, reason = nil) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/voucherify.rb', line 114

def rollback(redemption_id, tracking_id=nil, reason=nil)
  url = @backend_url + "/redemptions/" + URI.encode(redemption_id) + "/rollback"
  if tracking_id || reason
      url += "?" + URI.encode_www_form(:tracking_id => tracking_id, :reason => reason)
  end
  response = RestClient.post(url, nil, @headers.merge({ :content_type => :json }))
  JSON.parse(response.body)
end