class Crowdkit::API::ResponseWrapper

Attributes

client[R]
sawyer[R]

Public Class Methods

new(sawyer, client) click to toggle source
# File lib/crowdkit/api/response_wrapper.rb, line 12
def initialize(sawyer, client)
  @sawyer = sawyer
  @client = client
end

Public Instance Methods

[](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/crowdkit/api/response_wrapper.rb, line 28
def [](key)
  if self.data.is_a?(Array)
    self.data[key]
  else
    self.data.send(:"#{key}")
  end
end
current_status() click to toggle source
# File lib/crowdkit/api/response_wrapper.rb, line 36
def current_status
  if self.data["current_status"]
    do_get(self.data["current_status"].rels["self"].href)
  else
    nil
  end
end
data() click to toggle source
# File lib/crowdkit/api/response_wrapper.rb, line 17
def data
  @sawyer.data
end
each(to_enum = true, &block) click to toggle source

Iterate over each resource inside the body

# File lib/crowdkit/api/response_wrapper.rb, line 46
def each(to_enum = true, &block)
  body_parts = self.data.respond_to?(:each) ? self.data : [self.data]
  if block_given?
    body_parts.each &block
    if self.auto_pagination && self.has_next_page?
      next_page.each &block
    end
  else
    res = self
    if self.auto_pagination && res.has_next_page?
      res = next_page
      body_parts.concat(res.each(false))
    end
    result = to_enum ? body_parts.to_enum : body_parts
  end
  #Reset auto pagination option
  client.auto_pagination = nil
  result
end
has_key?(key) click to toggle source

Check if body has an attribute

# File lib/crowdkit/api/response_wrapper.rb, line 90
def has_key?(key)
  res = self.data.is_a?(Array) ? self.data.first : self.data
  res.key?(key.to_sym)
end
has_next_page?() click to toggle source
# File lib/crowdkit/api/response_wrapper.rb, line 72
def has_next_page?
  @sawyer.rels[:next]
end
headers() click to toggle source
# File lib/crowdkit/api/response_wrapper.rb, line 21
def headers
  @sawyer.headers
end
inspect() click to toggle source

Print only response body

# File lib/crowdkit/api/response_wrapper.rb, line 117
def inspect
  self.data.inspect
end
method_missing(method_name, *args, &block) click to toggle source

Coerce any method calls for body attributes

Calls superclass method
# File lib/crowdkit/api/response_wrapper.rb, line 97
def method_missing(method_name, *args, &block)
  if self.has_key?(method_name)
    self.[](method_name, &block)
  else
    super
  end
end
rate_limit_remaining() click to toggle source
# File lib/crowdkit/api/response_wrapper.rb, line 84
def rate_limit_remaining
  self.headers["X-RateLimit-Remaining"]
end
respond_to?(method_name) click to toggle source

Check if method is defined on the body

Calls superclass method
# File lib/crowdkit/api/response_wrapper.rb, line 107
def respond_to?(method_name)
  if self.has_key?(method_name)
    true
  else
    super
  end
end