Class: Nuvemshop::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, formatter = nil) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
10
11
12
# File 'lib/nuvemshop/response.rb', line 5

def initialize(response, formatter = nil)
  @response = response
  @code = response.code
  @body = response.body
  @headers = response.headers
  @parsed_response = response.parsed_response
  @formatter = formatter
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



55
56
57
58
59
60
61
62
63
# File 'lib/nuvemshop/response.rb', line 55

def method_missing(name, *args, &block)
  if parsed_response.respond_to?(name)
    parsed_response.send(name, *args, &block)
  elsif response.respond_to?(name)
    response.send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/nuvemshop/response.rb', line 3

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



3
4
5
# File 'lib/nuvemshop/response.rb', line 3

def code
  @code
end

#formatterObject (readonly)

Returns the value of attribute formatter.



3
4
5
# File 'lib/nuvemshop/response.rb', line 3

def formatter
  @formatter
end

#headersObject (readonly)

Returns the value of attribute headers.



3
4
5
# File 'lib/nuvemshop/response.rb', line 3

def headers
  @headers
end

#parsed_responseObject (readonly)

Returns the value of attribute parsed_response.



3
4
5
# File 'lib/nuvemshop/response.rb', line 3

def parsed_response
  @parsed_response
end

#responseObject (readonly)

Returns the value of attribute response.



3
4
5
# File 'lib/nuvemshop/response.rb', line 3

def response
  @response
end

Instance Method Details

#formatObject



14
15
16
17
18
19
20
21
22
# File 'lib/nuvemshop/response.rb', line 14

def format
  return unless formatter

  @parsed_response = if parsed_response.is_a?(Array)
                       parsed_response.map { |pr| formatter.new(pr) }
                     else
                       formatter.new(parsed_response)
                     end
end

#inspectObject



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

def inspect
  inspect_id = ::Kernel.format '%x', (object_id * 2)
  %(#<#{self.class}:0x#{inspect_id} code, @response=#{response.inspect}, @headers=#{headers.inspect}>)
end

#pretty_print(pp) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/nuvemshop/response.rb', line 37

def pretty_print(pp)
  format

  if !parsed_response.nil? && parsed_response.respond_to?(:pretty_print)
    parsed_response.pretty_print(pp)
  else
    super
  end
end

#respond_to_missing?(name, *args) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/nuvemshop/response.rb', line 47

def respond_to_missing?(name, *args)
  return true if super

  parsed_response.respond_to?(name) || response.respond_to?(name)
end

#to_sObject



24
25
26
27
28
29
30
# File 'lib/nuvemshop/response.rb', line 24

def to_s
  if !response.nil? && !response.body.nil? && response.body.respond_to?(:to_s)
    response.body.to_s
  else
    inspect
  end
end