Class: Voucherify
- Inherits:
-
Object
- Object
- Voucherify
- Defined in:
- lib/voucherify.rb,
lib/voucherify/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
-
#create(code, options = {}) ⇒ Object
‘code` is optional - will be generated if absent.
- #disable(code) ⇒ Object
- #enable(code) ⇒ Object
- #get(code) ⇒ Object
-
#initialize(options) ⇒ Voucherify
constructor
A new instance of Voucherify.
-
#list(query) ⇒ Object
List vouchers.
- #publish(campaign_name) ⇒ Object
- #redeem(code, tracking_id = nil) ⇒ Object
- #redemption(code) ⇒ Object
-
#redemptions(query) ⇒ Object
List redemptions.
Constructor Details
#initialize(options) ⇒ Voucherify
Returns a new instance of Voucherify.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/voucherify.rb', line 11 def initialize() @backend_url = "https://api.voucherify.io/v1" @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
}
}
91 92 93 94 95 96 |
# File 'lib/voucherify.rb', line 91 def create(code, = {}) url = @backend_url + "/vouchers/" url += URI.encode(code) if code response = RestClient.post(url, .to_json, @headers.merge({ :content_type => :json })) JSON.parse(response.body) end |
#disable(code) ⇒ Object
104 105 106 107 108 |
# File 'lib/voucherify.rb', line 104 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
98 99 100 101 102 |
# File 'lib/voucherify.rb', line 98 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
22 23 24 25 26 |
# File 'lib/voucherify.rb', line 22 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. Sample query: { limit: 100, skip: 200, category: “Loyalty” }
29 30 31 32 33 |
# File 'lib/voucherify.rb', line 29 def list(query) url = @backend_url + "/vouchers/" response = RestClient.get(url, @headers.merge({ :params => query })) JSON.parse(response.body) end |
#publish(campaign_name) ⇒ Object
71 72 73 74 75 |
# File 'lib/voucherify.rb', line 71 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/voucherify.rb', line 55 def redeem(code, tracking_id = nil) payload = {} if code.is_a? Hash payload = code code = payload["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
35 36 37 38 39 |
# File 'lib/voucherify.rb', line 35 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"
49 50 51 52 53 |
# File 'lib/voucherify.rb', line 49 def redemptions(query) url = @backend_url + "/redemptions/" response = RestClient.get(url, @headers.merge({ :params => query })) JSON.parse(response.body) end |