class TeamSnap::Response

Attributes

args[RW]
client[RW]
collection[RW]
href[RW]
message[RW]
objects[RW]
resp[RW]
status[RW]
via[RW]

Public Class Methods

load_collection(resp) click to toggle source
# File lib/teamsnap/response.rb, line 5
def load_collection(resp)
  if resp.success?
    return JSON.parse(resp.body, :symbolize_names => true).fetch(:collection)
  else
    content_type = resp.headers["content-type"]
    if content_type && content_type.match("json")
      if resp.status == 404
        raise TeamSnap::NotFound.new("Object not found.")
      else
        raise TeamSnap::Error.new(
          TeamSnap::Api.parse_error(resp)
        )
      end
    else
      raise TeamSnap::Error.new(
        "`#{resp.env.method}` call was unsuccessful. " +
        "Unexpected response content-type. " +
        "Check TeamSnap APIv3 connection")
    end
  end
end
new(opts = {}) click to toggle source
# File lib/teamsnap/response.rb, line 51
def initialize(opts = {})
  [
    :args, :client, :collection, :href, :message,
    :objects, :resp, :status, :via
  ].each do |attribute_name|
    instance_variable_set("@#{attribute_name}", opts.fetch(attribute_name, nil))
  end
  if resp
    if resp.success?
      if via == :get
        process_info
      else
        process_action
      end
    else
      process_error
    end
  end
end
process(client, resp, via, href, args) click to toggle source
# File lib/teamsnap/response.rb, line 27
def process(client, resp, via, href, args)
  response_object = self.new(
    :args => args,
    :client => client,
    :href => href,
    :resp => resp,
    :status => resp.status,
    :via => via
  )
  if resp.success?
    if via == :get
      response_object.process_info
    else
      response_object.process_action
    end
  else
    response_object.process_error
  end
end

Public Instance Methods

errors?() click to toggle source
# File lib/teamsnap/response.rb, line 107
def errors?
  @status / 100 != 2
end
process_action() click to toggle source
# File lib/teamsnap/response.rb, line 78
def process_action
  body = if @resp.body.nil? || @resp.body.empty?
           {}
         else
           JSON.parse(@resp.body, :symbolize_names => true) || {}
         end
  @collection = body.fetch(:collection) { {} }
  @message = "`#{@via}` call was successful"
  @objects = TeamSnap::Item.load_items(@client, @collection)
end
process_error() click to toggle source
# File lib/teamsnap/response.rb, line 89
def process_error
  if @resp.headers["content-type"].match("json")
    body = JSON.parse(@resp.body, :symbolize_names => true) || {}
    @collection = body.fetch(:collection) { {} }
    @message = TeamSnap::Api.parse_error(@resp)
    @objects = TeamSnap::Item.load_items(@client, @collection)
  else
    raise TeamSnap::Error.new(
      "`#{@via}` call with arguments #{@args} was unsuccessful. " +
        "The server returned a status of #{@status}."
    )
  end
end
process_info() click to toggle source
# File lib/teamsnap/response.rb, line 71
def process_info
  body = JSON.parse(@resp.body, :symbolize_names => true)
  @collection = body.fetch(:collection) { {} }
  @message = "Data retrieved successfully"
  @objects = TeamSnap::Item.load_items(@client, @collection)
end
success?() click to toggle source
# File lib/teamsnap/response.rb, line 103
def success?
  @status / 100 == 2
end