Class: EngagingNetworks::Response::Wrapper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/engaging_networks/response/wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Wrapper

Returns a new instance of Wrapper.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/engaging_networks/response/wrapper.rb', line 13

def initialize(response)
  @response = response

  if ao_xml_response?
    data = response.body['AOXmlResponse']['rows']['row']
  elsif ea_data_response?
    data = response.body['EaData']
  else
    return
  end

  # check for multiple returned rows
  if collection_response? data
    @kind = :collection
    @obj = EngagingNetworks::Response::Collection.new(rows_for(data))
  elsif object_response? data
    @kind = :object
    @obj = EngagingNetworks::Response::Object.new(data['EaRow'] ? data['EaRow'] : data)
  else
    @kind = :empty
    @obj = nil
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

if a raw object, just delegate



131
132
133
134
135
136
137
# File 'lib/engaging_networks/response/wrapper.rb', line 131

def method_missing(method_name, *args, &block)
  if object?
    obj.send(method_name, &block)
  else
    super
  end
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



8
9
10
# File 'lib/engaging_networks/response/wrapper.rb', line 8

def kind
  @kind
end

#objObject (readonly)

Returns the value of attribute obj.



9
10
11
# File 'lib/engaging_networks/response/wrapper.rb', line 9

def obj
  @obj
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/engaging_networks/response/wrapper.rb', line 7

def response
  @response
end

Instance Method Details

#[](key) ⇒ Object

Lookup an attribute from the object if hash, otherwise behave like array index. Convert any key to string before calling.



104
105
106
107
108
109
110
# File 'lib/engaging_networks/response/wrapper.rb', line 104

def [](key)
  if self.obj.is_a?(Array)
    self.obj[key]
  else
    self.obj.send(:"#{key}")
  end
end

#ao_xml_response?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/engaging_networks/response/wrapper.rb', line 37

def ao_xml_response?
  @response.body.respond_to?('has_key?') && @response.body.has_key?('AOXmlResponse')
end

#bodyObject

Response raw body



76
77
78
# File 'lib/engaging_networks/response/wrapper.rb', line 76

def body
  @body ? @body : response.body
end

#body=(value) ⇒ Object



70
71
72
73
# File 'lib/engaging_networks/response/wrapper.rb', line 70

def body=(value)
  @body = value
  @env[:body] = value
end

#client_error?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/engaging_networks/response/wrapper.rb', line 93

def client_error?
  status.to_i >= 400 && status.to_i < 500
end

#collection?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/engaging_networks/response/wrapper.rb', line 57

def collection?
  kind == :collection
end

#collection_response?(data) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/engaging_networks/response/wrapper.rb', line 45

def collection_response? data
  data.is_a?(Array) || (data.respond_to?('has_key?') && data.has_key?('EaRow') && data['EaRow'].is_a?(Array))
end

#ea_data_response?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/engaging_networks/response/wrapper.rb', line 41

def ea_data_response?
  @response.body.respond_to?('has_key?') && @response.body.has_key?('EaData')
end

#each(&block) ⇒ Object

Iterate over each resource inside the collection



141
142
143
144
145
146
147
148
149
# File 'lib/engaging_networks/response/wrapper.rb', line 141

def each(&block)
  if collection?
    obj.each do |o|
      block.call(o)
    end
  else
    raise("can only iterate over collections")
  end
end

#object?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/engaging_networks/response/wrapper.rb', line 61

def object?
  kind == :object
end

#object_response?(data) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/engaging_networks/response/wrapper.rb', line 49

def object_response? data
  data.respond_to?('has_key?')
end

#redirect?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/engaging_networks/response/wrapper.rb', line 89

def redirect?
  status.to_i >= 300 && status.to_i < 400
end

#rows_for(data) ⇒ Object



53
54
55
# File 'lib/engaging_networks/response/wrapper.rb', line 53

def rows_for data
  data.is_a?(Array) ? data : data['EaRow']
end

#server_error?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/engaging_networks/response/wrapper.rb', line 97

def server_error?
  status.to_i >= 500 && status.to_i < 600
end

#statusObject

Response status



81
82
83
# File 'lib/engaging_networks/response/wrapper.rb', line 81

def status
  response.status
end

#success?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/engaging_networks/response/wrapper.rb', line 85

def success?
  response.success?
end

#to_aryObject

Convert the ResponseWrapper into an Array



126
127
128
# File 'lib/engaging_networks/response/wrapper.rb', line 126

def to_ary
  body.to_ary
end

#to_hashObject

Convert the ResponseWrapper into a Hash



120
121
122
# File 'lib/engaging_networks/response/wrapper.rb', line 120

def to_hash
  body.to_hash
end

#to_sObject

Return response body as string



114
115
116
# File 'lib/engaging_networks/response/wrapper.rb', line 114

def to_s
  body.to_s
end

#urlObject

Request url



66
67
68
# File 'lib/engaging_networks/response/wrapper.rb', line 66

def url
  response.env[:url].to_s
end