Class: ActionKitRest::Response::Wrapper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/action_kit_rest/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
# File 'lib/action_kit_rest/response/wrapper.rb', line 13

def initialize(response)
  @response    = response

  if response.body.respond_to?(:meta) && response.body.meta
    @kind = :collection
    @obj = ActionKitRest::Response::Collection.new(response.body.meta, response.body.objects)
  else
    @kind = :object
    @obj = response.body
  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



99
100
101
102
103
104
105
# File 'lib/action_kit_rest/response/wrapper.rb', line 99

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/action_kit_rest/response/wrapper.rb', line 8

def kind
  @kind
end

#objObject (readonly)

Returns the value of attribute obj.



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

def obj
  @obj
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#[](key) ⇒ Object

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



72
73
74
75
76
77
78
# File 'lib/action_kit_rest/response/wrapper.rb', line 72

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

#bodyObject

Response raw body



44
45
46
# File 'lib/action_kit_rest/response/wrapper.rb', line 44

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

#body=(value) ⇒ Object



38
39
40
41
# File 'lib/action_kit_rest/response/wrapper.rb', line 38

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

#client_error?Boolean

Returns:

  • (Boolean)


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

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

#collection?Boolean

Returns:

  • (Boolean)


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

def collection?
  kind == :collection
end

#each(&block) ⇒ Object

Iterate over each resource inside the collection



109
110
111
112
113
114
115
116
117
# File 'lib/action_kit_rest/response/wrapper.rb', line 109

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)


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

def object?
  kind == :object
end

#redirect?Boolean

Returns:

  • (Boolean)


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

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

#server_error?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/action_kit_rest/response/wrapper.rb', line 65

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

#statusObject

Response status



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

def status
  response.status
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  response.success?
end

#to_aryObject

Convert the ResponseWrapper into an Array



94
95
96
# File 'lib/action_kit_rest/response/wrapper.rb', line 94

def to_ary
  body.to_ary
end

#to_hashObject

Convert the ResponseWrapper into a Hash



88
89
90
# File 'lib/action_kit_rest/response/wrapper.rb', line 88

def to_hash
  body.to_hash
end

#to_sObject

Return response body as string



82
83
84
# File 'lib/action_kit_rest/response/wrapper.rb', line 82

def to_s
  body.to_s
end

#urlObject

Request url



34
35
36
# File 'lib/action_kit_rest/response/wrapper.rb', line 34

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