lib/razorpay/request.rb in razorpay-1.2.1 vs lib/razorpay/request.rb in razorpay-2.0.0
- old
+ new
@@ -1,6 +1,6 @@
-require 'razorpay'
+require 'razorpay/constants'
require 'httparty'
module Razorpay
# Request objects are used to create fetch
# objects, which make requests to the server
@@ -11,16 +11,20 @@
ssl_ca_file File.dirname(__FILE__) + '/../ca-bundle.crt'
def initialize(entity_name)
self.class.base_uri(Razorpay::BASE_URI)
@entity_name = entity_name
+ custom_headers = Razorpay.custom_headers || {}
+ predefined_headers = {
+ 'User-Agent' => "Razorpay-Ruby/#{Razorpay::VERSION}"
+ }
+ # Order is important to give precedence to predefined headers
+ headers = custom_headers.merge(predefined_headers)
@options = {
basic_auth: Razorpay.auth,
timeout: 30,
- headers: {
- 'User-Agent' => "Razorpay-Ruby/#{Razorpay::VERSION}"
- }
+ headers: headers
}
end
def fetch(id)
request :get, "/#{@entity_name}/#{id}"
@@ -32,20 +36,30 @@
def post(url, data = {})
request :post, "/#{@entity_name}/#{url}", data
end
+ def get(url)
+ request :get, "/#{@entity_name}/#{url}"
+ end
+
+ def put(id, data = {})
+ request :put, "/#{@entity_name}/#{id}", data
+ end
+
def create(data)
request :post, "/#{@entity_name}", data
end
def request(method, url, data = {})
case method
when :get
@options[:query] = data
when :post
@options[:body] = data
+ when :put
+ @options[:body] = data
end
create_instance self.class.send(method, url, @options)
end
# Since we need to change the base route
@@ -73,18 +87,18 @@
klass.new(response)
end
def raise_error(error, status)
# Get the error class name, require it and instantiate an error
- require "razorpay/errors/#{error['code'].downcase}"
class_name = error['code'].split('_').map(&:capitalize).join('')
args = [error['code'], status]
args.push error['field'] if error.key?('field')
klass =
begin
+ require "razorpay/errors/#{error['code'].downcase}"
Razorpay.const_get(class_name)
# We got an unknown error, cast it to Error for now
- rescue NameError
+ rescue NameError, LoadError
Razorpay::Error
end
raise klass.new(*args), error['description']
end
end