Class: Shipay::Request
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#path ⇒ Object
Returns the value of attribute path.
-
#query ⇒ Object
Returns the value of attribute query.
Class Method Summary collapse
- .auth(url, options = {}) ⇒ Object
- .delete(url, options = {}) ⇒ Object
- .get(url, options = {}) ⇒ Object
- .patch(url, options = {}) ⇒ Object
- .post(url, options = {}) ⇒ Object
- .put(url, options = {}) ⇒ Object
Instance Method Summary collapse
- #call(ressource_name) ⇒ Object
- #full_api_url ⇒ Object
-
#initialize(path, method, options = {}) ⇒ Request
constructor
A new instance of Request.
- #request_params ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(path, method, options = {}) ⇒ Request
16 17 18 19 20 21 22 23 24 |
# File 'lib/shipay/request.rb', line 16 def initialize(path, method, ={}) @path = path @method = method @parameters = [:params] || nil @query = [:query] || Hash.new @headers = [:headers] || Hash.new @auth = [:auth] || false @client_key = [:client_key] || @parameters && ( @parameters[:client_key] || @parameters["client_key"] ) || Shipay.default_client_key end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
14 15 16 |
# File 'lib/shipay/request.rb', line 14 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
14 15 16 |
# File 'lib/shipay/request.rb', line 14 def method @method end |
#parameters ⇒ Object
Returns the value of attribute parameters.
14 15 16 |
# File 'lib/shipay/request.rb', line 14 def parameters @parameters end |
#path ⇒ Object
Returns the value of attribute path.
14 15 16 |
# File 'lib/shipay/request.rb', line 14 def path @path end |
#query ⇒ Object
Returns the value of attribute query.
14 15 16 |
# File 'lib/shipay/request.rb', line 14 def query @query end |
Class Method Details
.auth(url, options = {}) ⇒ Object
66 67 68 69 |
# File 'lib/shipay/request.rb', line 66 def self.auth(url, ={}) [:auth] = true self.new url, 'POST', end |
.delete(url, options = {}) ⇒ Object
83 84 85 |
# File 'lib/shipay/request.rb', line 83 def self.delete(url, ={}) self.new url, 'DELETE', end |
.get(url, options = {}) ⇒ Object
62 63 64 |
# File 'lib/shipay/request.rb', line 62 def self.get(url, ={}) self.new url, 'GET', end |
.patch(url, options = {}) ⇒ Object
79 80 81 |
# File 'lib/shipay/request.rb', line 79 def self.patch(url, ={}) self.new url, 'PATCH', end |
.post(url, options = {}) ⇒ Object
71 72 73 |
# File 'lib/shipay/request.rb', line 71 def self.post(url, ={}) self.new url, 'POST', end |
.put(url, options = {}) ⇒ Object
75 76 77 |
# File 'lib/shipay/request.rb', line 75 def self.put(url, ={}) self.new url, 'PUT', end |
Instance Method Details
#call(ressource_name) ⇒ Object
58 59 60 |
# File 'lib/shipay/request.rb', line 58 def call(ressource_name) ShipayObject.convert run, ressource_name, @client_key end |
#full_api_url ⇒ Object
109 110 111 |
# File 'lib/shipay/request.rb', line 109 def full_api_url Shipay.api_endpoint + path end |
#request_params ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/shipay/request.rb', line 87 def request_params aux = { method: method, url: full_api_url, } @parameters&.except_nested!("client_key") if !@auth && @parameters && Shipay.callback_url && Shipay::TokenManager.client_type_for(@client_key) == :e_comerce && method == 'POST' aux.merge!({ payload: MultiJson.encode(@parameters.merge({callback_url: Shipay.callback_url}))}) elsif @parameters aux.merge!({ payload: MultiJson.encode(@parameters)}) end extra_headers = DEFAULT_HEADERS extra_headers[:authorization] = "Bearer #{Shipay::TokenManager.token_for @client_key}" unless @auth extra_headers["x-shipay-order-type"] = "e-order" if (!@auth && Shipay::TokenManager.client_type_for(@client_key) == :e_comerce) aux.merge!({ headers: extra_headers }) aux end |
#run ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/shipay/request.rb', line 26 def run response = RestClient::Request.execute request_params MultiJson.decode response.body rescue RestClient::Exception => error begin parsed_error = MultiJson.decode error.http_body if error.is_a? RestClient::ResourceNotFound if parsed_error['message'] raise Shipay::NotFound.new(parsed_error, request_params, error) else raise Shipay::NotFound.new(nil, request_params, error) end else if parsed_error['message'] raise Shipay::ResponseError.new(request_params, error, parsed_error['message']) else raise Shipay::ValidationError.new parsed_error end end rescue MultiJson::ParseError raise Shipay::ResponseError.new(request_params, error) end rescue MultiJson::ParseError raise Shipay::ResponseError.new(request_params, response) rescue SocketError raise Shipay::ConnectionError.new $! rescue RestClient::ServerBrokeConnection raise Shipay::ConnectionError.new $! end |