Class: ShippingEasy::Http::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/shipping_easy/http/request.rb

Direct Known Subclasses

PartnerRequest

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Request

Returns a new instance of Request.



5
6
7
8
9
10
# File 'lib/shipping_easy/http/request.rb', line 5

def initialize(options = {})
  @http_method = options.fetch(:http_method, :get)
  @params = options.fetch(:params, {})
  @body = options[:payload] && options[:payload].to_json
  @relative_path = options.delete(:relative_path)
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



3
4
5
# File 'lib/shipping_easy/http/request.rb', line 3

def body
  @body
end

#http_methodObject

Returns the value of attribute http_method.



3
4
5
# File 'lib/shipping_easy/http/request.rb', line 3

def http_method
  @http_method
end

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'lib/shipping_easy/http/request.rb', line 3

def params
  @params
end

#relative_pathObject

Returns the value of attribute relative_path.



3
4
5
# File 'lib/shipping_easy/http/request.rb', line 3

def relative_path
  @relative_path
end

Class Method Details

.connect!(options = {}) ⇒ Object



12
13
14
# File 'lib/shipping_easy/http/request.rb', line 12

def self.connect!(options = {})
  new(options).connect!
end

Instance Method Details

#adapterObject



35
36
37
# File 'lib/shipping_easy/http/request.rb', line 35

def adapter
  ShippingEasy.configuration.http_adapter.new(self)
end

#api_keyObject



43
44
45
# File 'lib/shipping_easy/http/request.rb', line 43

def api_key
  ShippingEasy.api_key
end

#api_secretObject



39
40
41
# File 'lib/shipping_easy/http/request.rb', line 39

def api_secret
  ShippingEasy.api_secret
end

#base_urlObject



47
48
49
# File 'lib/shipping_easy/http/request.rb', line 47

def base_url
  ShippingEasy.base_url
end

#connect!Object



16
17
18
19
# File 'lib/shipping_easy/http/request.rb', line 16

def connect!
  sign_request!
  adapter.connect!
end

#sign_request!Object



21
22
23
24
25
# File 'lib/shipping_easy/http/request.rb', line 21

def sign_request!
  params[:api_key] = api_key
  params[:api_timestamp] = Time.now.to_i
  params[:api_signature] = signature.to_s
end

#signatureObject



31
32
33
# File 'lib/shipping_easy/http/request.rb', line 31

def signature
  ShippingEasy::Signature.new(api_secret: api_secret, method: http_method, path: uri, params: params, body: body)
end

#uriObject



27
28
29
# File 'lib/shipping_easy/http/request.rb', line 27

def uri
  "/api#{relative_path}"
end