class TeamSnap::Api

Constants

CRUD_METHODS
CRUD_VIAS
INTEGER_CLASS

Public Class Methods

args(method, sent_args) click to toggle source
# File lib/teamsnap/api.rb, line 26
def self.args(method, sent_args)
  case method
  when :update
    sent_args.except(:id)
  when :find, :delete
    {}
  else
    sent_args
  end
end
get_class(sym) click to toggle source
# File lib/teamsnap/api.rb, line 37
def self.get_class(sym)
  "TeamSnap::#{sym.to_s.singularize.camelcase}".constantize
end
href(base_href, method, args = {}) click to toggle source
# File lib/teamsnap/api.rb, line 41
def self.href(base_href, method, args = {})
  case method
  when :find, :delete
    if [INTEGER_CLASS, String].include?(args.class)
      base_href + "/#{args}"
    elsif args.class == Hash
      base_href + "/#{args.fetch(:id)}"
    else
      raise TeamSnap::Error.new("You must pass in the `id` of the object you would like to :find or :delete")
    end
  when :create
    base_href
  when :update
    base_href + "/#{args.fetch(:id)}"
  else
    base_href + "/#{method}"
  end
end
parse_error(resp) click to toggle source
# File lib/teamsnap/api.rb, line 75
def self.parse_error(resp)
  return "Object Not Found (404)" if resp.status == 404
  return "Forbidden (403)" if resp.status == 403 && resp.body == ""

  begin
    JSON.parse(resp.body, :symbolize_names => true)
      .fetch(:collection)
      .fetch(:error)
      .fetch(:message)
  rescue KeyError
    resp.body
  end
end
run(client, method, klass, args = {}, template_args = false) click to toggle source
# File lib/teamsnap/api.rb, line 8
def self.run(client, method, klass, args = {}, template_args = false)
  klass = klass.class == Symbol ? get_class(klass) : klass
  via = via(klass, method)
  href = href(klass.href, method, args)
  args = args(method, args)
  client_send_args = template_args ? template_attributes(args) : args
  resp = TeamSnap.client_send(client, via, href, client_send_args)
  TeamSnap::Response.new(
    :args => args,
    :client => client,
    :client_send_args => client_send_args,
    :href => href,
    :resp => resp,
    :status => resp.status,
    :via => via
  )
end
template_args?(method) click to toggle source
# File lib/teamsnap/api.rb, line 89
def self.template_args?(method)
  [:create, :update].include?(method)
end
template_attributes(attributes) click to toggle source
# File lib/teamsnap/api.rb, line 93
def self.template_attributes(attributes)
  request_attributes = {
    :template => {
      :data => []
    }
  }
  attributes.each do |key, value|
    request_attributes[:template][:data] << {
      "name" => key,
      "value" => value
    }
  end
  return request_attributes
end
untemplate_attributes(request_attributes) click to toggle source
# File lib/teamsnap/api.rb, line 108
def self.untemplate_attributes(request_attributes)
  attributes = {}
  request_attributes.fetch(:template).fetch(:data).each do |datum|
    attributes[datum.fetch(:name).to_sym] = datum.fetch(:value)
  end
  return attributes
end
via(klass, method) click to toggle source
# File lib/teamsnap/api.rb, line 60
def self.via(klass, method)
  queries = klass.query_names
  commands = klass.command_names

  method_map = CRUD_METHODS + queries + commands
  via_map = CRUD_VIAS + ([:get] * queries.count) + ([:post] * commands.count)

  # SET VIA
  if method_index = method_map.index(method)
    return via_map[method_index]
  else
    raise TeamSnap::Error.new("Method Missing: `#{method}` for Collection Class: `#{klass}`")
  end
end