Class: Fakturoid::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/fakturoid/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(faraday_response, caller, request_method) ⇒ Response

Returns a new instance of Response.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fakturoid/response.rb', line 7

def initialize(faraday_response, caller, request_method)
  @response = faraday_response
  @caller = caller
  @request_method = request_method.to_sym

  env = response.env

  if !(env.body.nil? || env.body.empty? || (json? && env.body =~ /\A\s+\z/))
    @body = json? ? MultiJson.load(env.body) : env.body
  end

  handle_response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



52
53
54
55
56
57
58
# File 'lib/fakturoid/response.rb', line 52

def method_missing(method, *args, &block)
  if body_has_key?(method)
    body[method.to_s]
  else
    super
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/fakturoid/response.rb', line 5

def body
  @body
end

#callerObject (readonly)

Returns the value of attribute caller.



5
6
7
# File 'lib/fakturoid/response.rb', line 5

def caller
  @caller
end

#request_methodObject (readonly)

Returns the value of attribute request_method.



5
6
7
# File 'lib/fakturoid/response.rb', line 5

def request_method
  @request_method
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/fakturoid/response.rb', line 5

def response
  @response
end

Instance Method Details

#headersObject



29
30
31
# File 'lib/fakturoid/response.rb', line 29

def headers
  response.env.response_headers.transform_keys(&:downcase)
end

#inspectObject



33
34
35
# File 'lib/fakturoid/response.rb', line 33

def inspect
  "#<#{self.class.name}:#{object_id} @body=\"#{body}\" @status_code=\"#{status_code}\">"
end

#json?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/fakturoid/response.rb', line 25

def json?
  headers["content-type"] =~ %r{\Aapplication/json}
end

#status_codeObject



21
22
23
# File 'lib/fakturoid/response.rb', line 21

def status_code
  response.env["status"]
end