Class: Justa::Request
Constant Summary collapse
- DEFAULT_HEADERS =
{ "Content-Type" => "application/json", "Accept" => "application/json", "User-Agent" => "justa-ruby/#{Justa::VERSION}" }.freeze
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.
- #must_append_document? ⇒ Boolean
- #request_params ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(path, method, options = {}) ⇒ Request
Returns a new instance of Request.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/justa/request.rb', line 15 def initialize(path, method, = {}) @path = path @method = method @parameters = [:params] || nil @query = [:query] || {} @headers = [:headers] || {} @auth = [:auth] || false @append_document = .fetch(:append_document, true) @client_key = [:client_key] || @parameters && (@parameters[:client_key] || @parameters["client_key"]) || Justa.default_client_key end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
13 14 15 |
# File 'lib/justa/request.rb', line 13 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
13 14 15 |
# File 'lib/justa/request.rb', line 13 def method @method end |
#parameters ⇒ Object
Returns the value of attribute parameters.
13 14 15 |
# File 'lib/justa/request.rb', line 13 def parameters @parameters end |
#path ⇒ Object
Returns the value of attribute path.
13 14 15 |
# File 'lib/justa/request.rb', line 13 def path @path end |
#query ⇒ Object
Returns the value of attribute query.
13 14 15 |
# File 'lib/justa/request.rb', line 13 def query @query end |
Class Method Details
.auth(url, options = {}) ⇒ Object
65 66 67 68 |
# File 'lib/justa/request.rb', line 65 def self.auth(url, = {}) [:auth] = true new url, "POST", end |
.delete(url, options = {}) ⇒ Object
82 83 84 |
# File 'lib/justa/request.rb', line 82 def self.delete(url, = {}) new url, "DELETE", end |
.get(url, options = {}) ⇒ Object
61 62 63 |
# File 'lib/justa/request.rb', line 61 def self.get(url, = {}) new url, "GET", end |
.patch(url, options = {}) ⇒ Object
78 79 80 |
# File 'lib/justa/request.rb', line 78 def self.patch(url, = {}) new url, "PATCH", end |
.post(url, options = {}) ⇒ Object
70 71 72 |
# File 'lib/justa/request.rb', line 70 def self.post(url, = {}) new url, "POST", end |
.put(url, options = {}) ⇒ Object
74 75 76 |
# File 'lib/justa/request.rb', line 74 def self.put(url, = {}) new url, "PUT", end |
Instance Method Details
#call(ressource_name) ⇒ Object
57 58 59 |
# File 'lib/justa/request.rb', line 57 def call(ressource_name) JustaObject.convert run, ressource_name, @client_key end |
#full_api_url ⇒ Object
110 111 112 |
# File 'lib/justa/request.rb', line 110 def full_api_url Justa.api_endpoint + "/payment-provider/api" + @path + (!@auth && @append_document ? ("/" + TokenManager.client_for(@client_key).document.to_s) : "") end |
#must_append_document? ⇒ Boolean
108 |
# File 'lib/justa/request.rb', line 108 def must_append_document?; end |
#request_params ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/justa/request.rb', line 86 def request_params aux = { method: method, url: full_api_url } @parameters if !@auth && @parameters && method == "POST" aux.merge!({ payload: MultiJson.encode(@parameters.to_request_params) }) elsif @parameters aux.merge!({ payload: @parameters }) end extra_headers = DEFAULT_HEADERS.merge(@headers) extra_headers[:authorization] = "Bearer #{Justa::TokenManager.token_for @client_key}" unless @auth extra_headers["integratorId"] = Justa.integrator_id unless @auth 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 |
# File 'lib/justa/request.rb', line 26 def run response = RestClient::Request.execute request_params MultiJson.decode response.body rescue RestClient::Exception => e begin parsed_error = MultiJson.decode e.http_body if e.is_a? RestClient::ResourceNotFound if parsed_error["message"] raise Justa::NotFound.new(parsed_error, request_params, e) else raise Justa::NotFound.new(nil, request_params, e) end elsif parsed_error["message"] raise Justa::ResponseError.new(request_params, e, parsed_error["message"]) else raise Justa::ValidationError, parsed_error end rescue MultiJson::ParseError raise Justa::ResponseError.new(request_params, e) end rescue MultiJson::ParseError return {} unless response.code < 200 && response.code > 299 raise Justa::ResponseError.new(request_params, response) rescue SocketError raise Justa::ConnectionError, $! rescue RestClient::ServerBrokeConnection raise Justa::ConnectionError, $! end |