lib/voucherify/client.rb in voucherify-1.6.0 vs lib/voucherify/client.rb in voucherify-1.6.1
- old
+ new
@@ -15,10 +15,11 @@
'X-App-Token' => @options[:clientSecretKey] || @options['clientSecretKey'],
'X-Voucherify-Channel' => 'Ruby-SDK',
'X-Voucherify-API-Version' => @options[:apiVersion] || @options['apiVersion'],
:accept => :json,
}.reject{ |k,v| v.nil? }
+ @timeout = @options[:timeout] || @options['timeout']
end
def vouchers
Voucherify::Service::Vouchers.new(self)
end
@@ -64,31 +65,31 @@
end
def get(path, params = {})
begin
url = @backend_url + path
- response = RestClient.get(url, @headers.merge({:params => params}))
+ 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
def put(path, body, params = {})
begin
url = @backend_url + path
- response = RestClient.put(url, body, @headers.merge({:params => params, :content_type => :json}))
+ 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
def post(path, body, params = {})
begin
url = @backend_url + path
- response = RestClient.post(url, body, @headers.merge({:params => params, :content_type => :json}))
+ 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
@@ -98,11 +99,11 @@
end
def delete(path, params = {})
begin
url = @backend_url + path
- RestClient.delete(url, @headers.merge({:params => params}))
+ 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
@@ -113,15 +114,21 @@
attr_reader :code
attr_reader :details
attr_reader :key
def initialize (restClientError)
+ if restClientError.is_a? RestClient::Exceptions::Timeout
+ @response = restClientError
+ @details = restClientError
+ super(restClientError)
+ else
@response = restClientError.response
parsedResponse = JSON.parse(@response)
@code = parsedResponse['code']
@details = parsedResponse['details']
@key = parsedResponse['key']
super(parsedResponse['message'])
end
+ end
end
end
\ No newline at end of file