Class: Regaliator::Request

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

Constant Summary collapse

CONTENT_TYPE =
'application/json'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, endpoint, params = {}) ⇒ Request

Returns a new instance of Request.



15
16
17
18
19
20
21
22
# File 'lib/regaliator/request.rb', line 15

def initialize(config, endpoint, params = {})
  @config    = config
  @timestamp = Time.now.utc.httpdate.to_s
  @endpoint  = endpoint
  @params    = params
  @uri       = build_uri
  @http      = build_http
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/regaliator/request.rb', line 13

def config
  @config
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



13
14
15
# File 'lib/regaliator/request.rb', line 13

def endpoint
  @endpoint
end

#httpObject (readonly)

Returns the value of attribute http.



13
14
15
# File 'lib/regaliator/request.rb', line 13

def http
  @http
end

#http_requestObject (readonly)

Returns the value of attribute http_request.



13
14
15
# File 'lib/regaliator/request.rb', line 13

def http_request
  @http_request
end

#paramsObject (readonly)

Returns the value of attribute params.



13
14
15
# File 'lib/regaliator/request.rb', line 13

def params
  @params
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



13
14
15
# File 'lib/regaliator/request.rb', line 13

def timestamp
  @timestamp
end

#uriObject (readonly)

Returns the value of attribute uri.



13
14
15
# File 'lib/regaliator/request.rb', line 13

def uri
  @uri
end

Instance Method Details

#getObject



31
32
33
34
35
36
# File 'lib/regaliator/request.rb', line 31

def get
  uri.query = URI.encode_www_form(params) if !params.empty?
  @http_request = Net::HTTP::Get.new(uri.request_uri)

  send_request
end

#patchObject



38
39
40
41
42
43
# File 'lib/regaliator/request.rb', line 38

def patch
  @http_request = Net::HTTP::Patch.new(uri.request_uri)
  @http_request.body = params.to_json

  send_request
end

#postObject



24
25
26
27
28
29
# File 'lib/regaliator/request.rb', line 24

def post
  @http_request = Net::HTTP::Post.new(uri.request_uri)
  @http_request.body = params.to_json

  send_request
end

#send_requestObject



45
46
47
48
49
50
# File 'lib/regaliator/request.rb', line 45

def send_request
  apply_headers

  response = http.request(http_request)
  Regaliator::Response.new(response)
end