Class: Voucherify::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/voucherify/client.rb', line 10

def initialize (options)
  @options = options
  @backend_url = 'https://api.voucherify.io/v1'
  @headers = {
      'X-App-Id' => @options[:applicationId] || @options['applicationId'],
      'X-App-Token' => @options[:clientSecretKey] || @options['clientSecretKey'],
      'X-Voucherify-Channel' => 'Ruby-SDK',
      'X-Voucherify-API-Version' => @options[:apiVersion] || @options['apiVersion'] || 'v2018-08-01',
      :accept => :json,
  }.reject{ |k,v| v.nil? }
  @timeout = @options[:timeout] || @options['timeout']
end

Instance Attribute Details

#backend_urlObject (readonly)

Returns the value of attribute backend_url.



8
9
10
# File 'lib/voucherify/client.rb', line 8

def backend_url
  @backend_url
end

Instance Method Details

#campaignsObject



47
48
49
# File 'lib/voucherify/client.rb', line 47

def campaigns
  Voucherify::Service::Campaigns.new(self)
end

#customersObject



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

def customers
  Voucherify::Service::Customers.new(self)
end

#delete(path, params = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/voucherify/client.rb', line 101

def delete(path, params = {})
  begin
    url = @backend_url + path
    RestClient::Request::execute(method: :delete, url: url, headers: @headers.merge({:params => params}), read_timeout: @timeout, open_timeout: @timeout)
    nil
  rescue RestClient::Exception => e
    raise VoucherifyError.new(e)
  end
end

#distributionsObject



27
28
29
# File 'lib/voucherify/client.rb', line 27

def distributions
  Voucherify::Service::Distributions.new(self)
end

#eventsObject



59
60
61
# File 'lib/voucherify/client.rb', line 59

def events
  Voucherify::Service::Events.new(self)
end

#get(path, params = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/voucherify/client.rb', line 67

def get(path, params = {})
  begin
    url = @backend_url + path
    response = RestClient::Request::execute(method: :get, url: url, headers: @headers.merge({:params => params}), read_timeout: @timeout, open_timeout: @timeout)
    JSON.parse(response.body)
  rescue RestClient::Exception => e
    raise VoucherifyError.new(e)
  end
end

#post(path, body, params = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/voucherify/client.rb', line 87

def post(path, body, params = {})
  begin
    url = @backend_url + path
    response = RestClient::Request::execute(method: :post, url: url, payload: body, headers: @headers.merge({:params => params, :content_type => :json}), read_timeout: @timeout, open_timeout: @timeout)
    if !response.body.empty?
      JSON.parse(response.body)
    else
      nil
    end
  rescue RestClient::Exception => e
    raise VoucherifyError.new(e)
  end
end

#productsObject



43
44
45
# File 'lib/voucherify/client.rb', line 43

def products
  Voucherify::Service::Products.new(self)
end

#promotionsObject



63
64
65
# File 'lib/voucherify/client.rb', line 63

def promotions
  Voucherify::Service::Promotions.new(self)
end

#put(path, body, params = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/voucherify/client.rb', line 77

def put(path, body, params = {})
  begin
    url = @backend_url + path
    response = RestClient::Request::execute(method: :put, url: url, payload: body, headers: @headers.merge({:params => params, :content_type => :json}), read_timeout: @timeout, open_timeout: @timeout)
    JSON.parse(response.body)
  rescue RestClient::Exception => e
    raise VoucherifyError.new(e)
  end
end

#redemptionsObject



35
36
37
# File 'lib/voucherify/client.rb', line 35

def redemptions
  Voucherify::Service::Redemptions.new(self)
end

#segmentsObject



55
56
57
# File 'lib/voucherify/client.rb', line 55

def segments
  Voucherify::Service::Segments.new(self)
end

#validation_rulesObject



51
52
53
# File 'lib/voucherify/client.rb', line 51

def validation_rules
  Voucherify::Service::ValidationRules.new(self)
end

#validationsObject



31
32
33
# File 'lib/voucherify/client.rb', line 31

def validations
  Voucherify::Service::Validations.new(self)
end

#vouchersObject



23
24
25
# File 'lib/voucherify/client.rb', line 23

def vouchers
  Voucherify::Service::Vouchers.new(self)
end