Class: Shipay::Request

Inherits:
Object show all
Defined in:
lib/shipay/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options={})
    @path       = path
    @method     = method
    @parameters = options[:params]      || nil
    @query      = options[:query]       || Hash.new
    @headers    = options[:headers]     || Hash.new
    @auth       = options[:auth]        || false
    @client_key = options[:client_key]  || @parameters && ( @parameters[:client_key] || @parameters["client_key"] ) || Shipay.default_client_key
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



14
15
16
# File 'lib/shipay/request.rb', line 14

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



14
15
16
# File 'lib/shipay/request.rb', line 14

def method
  @method
end

#parametersObject

Returns the value of attribute parameters.



14
15
16
# File 'lib/shipay/request.rb', line 14

def parameters
  @parameters
end

#pathObject

Returns the value of attribute path.



14
15
16
# File 'lib/shipay/request.rb', line 14

def path
  @path
end

#queryObject

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, options={})
  options[:auth] = true
  self.new url, 'POST', options
end

.delete(url, options = {}) ⇒ Object



83
84
85
# File 'lib/shipay/request.rb', line 83

def self.delete(url, options={})
  self.new url, 'DELETE', options
end

.get(url, options = {}) ⇒ Object



62
63
64
# File 'lib/shipay/request.rb', line 62

def self.get(url, options={})
  self.new url, 'GET', options
end

.patch(url, options = {}) ⇒ Object



79
80
81
# File 'lib/shipay/request.rb', line 79

def self.patch(url, options={})
  self.new url, 'PATCH', options
end

.post(url, options = {}) ⇒ Object



71
72
73
# File 'lib/shipay/request.rb', line 71

def self.post(url, options={})
  self.new url, 'POST', options
end

.put(url, options = {}) ⇒ Object



75
76
77
# File 'lib/shipay/request.rb', line 75

def self.put(url, options={})
  self.new url, 'PUT', options
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_urlObject



109
110
111
# File 'lib/shipay/request.rb', line 109

def full_api_url
  Shipay.api_endpoint + path
end

#request_paramsObject



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

#runObject



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