class Nimbu::Response::Wrapper

A class responsible for proxing to faraday response

Attributes

current_api[R]
env[R]
response[R]

Public Class Methods

new(response, current_api) click to toggle source
# File lib/nimbu-api/response/wrapper.rb, line 17
def initialize(response, current_api)
  @response    = response
  @current_api = current_api
  @env         = response.env
end

Public Instance Methods

==(other) click to toggle source

Compare the wrapper with other wrapper for equality

# File lib/nimbu-api/response/wrapper.rb, line 143
def ==(other)
  self.env == other.env &&
  self.body == other.body
end
[](key) click to toggle source

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

# File lib/nimbu-api/response/wrapper.rb, line 71
def [](key)
  if self.body.is_a?(Array)
    self.body[key]
  else
    self.body.send(:"#{key}")
  end
end
body() click to toggle source

Response raw body

# File lib/nimbu-api/response/wrapper.rb, line 36
def body
  @body ? @body : response.body
end
body=(value) click to toggle source
# File lib/nimbu-api/response/wrapper.rb, line 29
def body=(value)
  @body = value
  @env[:body] = value
end
client_error?() click to toggle source
# File lib/nimbu-api/response/wrapper.rb, line 54
def client_error?
  status.to_i >= 400 && status.to_i < 500
end
each() { |part| ... } click to toggle source

Iterate over each resource inside the body

# File lib/nimbu-api/response/wrapper.rb, line 103
def each
  body_parts = self.body.respond_to?(:each) ? self.body : [self.body]
  return body_parts.to_enum unless block_given?
  body_parts.each { |part| yield(part) }
end
has_key?(key) click to toggle source

Check if body has an attribute

# File lib/nimbu-api/response/wrapper.rb, line 111
def has_key?(key)
  self.body.is_a?(Hash) && self.body.has_key?(key)
end
headers() click to toggle source

Return response headers

# File lib/nimbu-api/response/wrapper.rb, line 64
def headers
  Nimbu::Response::Header.new(env)
end
inspect() click to toggle source

Print only response body

# File lib/nimbu-api/response/wrapper.rb, line 137
def inspect
  "#<#{self.class.name} @body=\"#{self.body}\">"
end
last() click to toggle source
# File lib/nimbu-api/response/wrapper.rb, line 79
def last
  self[self.length-1] if self.length.to_i > 0
end
method_missing(method_name, *args, &block) click to toggle source

Coerce any method calls for body attributes

Calls superclass method
# File lib/nimbu-api/response/wrapper.rb, line 117
def method_missing(method_name, *args, &block)
  if self.has_key?(method_name.to_s)
    self.[](method_name, &block)
  else
    super
  end
end
redirect?() click to toggle source
# File lib/nimbu-api/response/wrapper.rb, line 50
def redirect?
  status.to_i >= 300 && status.to_i < 400
end
respond_to?(method_name) click to toggle source

Check if method is defined on the body

Calls superclass method
# File lib/nimbu-api/response/wrapper.rb, line 127
def respond_to?(method_name)
  if self.has_key?(method_name.to_s)
    true
  else
    super
  end
end
server_error?() click to toggle source
# File lib/nimbu-api/response/wrapper.rb, line 58
def server_error?
  status.to_i >= 500 && status.to_i < 600
end
status() click to toggle source

Response status

# File lib/nimbu-api/response/wrapper.rb, line 42
def status
  response.status
end
success?() click to toggle source
# File lib/nimbu-api/response/wrapper.rb, line 46
def success?
  response.success?
end
to_ary() click to toggle source

Convert the ResponseWrapper into an Array

# File lib/nimbu-api/response/wrapper.rb, line 97
def to_ary
  body.to_ary
end
to_hash() click to toggle source

Convert the ResponseWrapper into a Hash

# File lib/nimbu-api/response/wrapper.rb, line 91
def to_hash
  body.to_hash
end
to_s() click to toggle source

Return response body as string

# File lib/nimbu-api/response/wrapper.rb, line 85
def to_s
  body.to_s
end
url() click to toggle source

Request url

# File lib/nimbu-api/response/wrapper.rb, line 25
def url
  response.env[:url].to_s
end