module TeamSnap

Constants

DEFAULT_URL
EXCLUDED_RELS
Error
InitializationError
NotFound
VERSION

Attributes

client_id[RW]
client_secret[RW]
headers[RW]
root_client[RW]
token[RW]
url[RW]

Public Class Methods

client_send(client, via, href, args) click to toggle source
# File lib/teamsnap.rb, line 77
def client_send(client, via, href, args)
  case via
  when :get, :delete
    client.send(via, href, args)
  when :patch, :post
    client.send(via, href) do |req|
      if use_multipart?(args)
        req.body = args
      else
        req.body = JSON.generate(args)
      end
    end
  else
    raise TeamSnap::Error.new("Don't know how to run `#{via}`")
  end
end
default_timeout_error() click to toggle source
# File lib/teamsnap.rb, line 70
def default_timeout_error
  -> {
    warn("Connection to API failed with TimeoutError")
    {:links => []}
  }
end
init(opts = {}) click to toggle source
# File lib/teamsnap.rb, line 28
def init(opts = {})
  unless opts[:token] || (opts[:client_id] && opts[:client_secret])
    raise ArgumentError.new("You must provide a :token or :client_id and :client_secret pair to '.init'")
  end

  ##   setup variables required
  self.client_id = opts.fetch(:client_id) {}
  self.client_secret = opts.fetch(:client_secret) {}
  self.token = opts.fetch(:token) {}
  self.url = opts.fetch(:url) { DEFAULT_URL }

  ##   create universally accessible TeamSnap.root_client
  self.root_client = TeamSnap::Client.new(:token => token)

  ##   include any feature headers
  if opts[:headers]
    if headers = opts.fetch(:headers)
      self.root_client.headers = self.root_client.headers.merge(headers)
    end
  end

  ##   Make the apiv3 root call. collection is parsed JSON
  collection = TeamSnap.run(root_client, :get, self.url, {}) do
    self.root_client = nil
    raise TeamSnap::InitializationError
  end

  ##   Setup Dynamic Classes from the collection
  TeamSnap::Structure.init(root_client, collection)

  ##   Queries and Commands parsing for shortcut methods
  TeamSnap::Collection.apply_endpoints(self, collection) && true
end
run(client, via, href, args = {}, &block) click to toggle source
# File lib/teamsnap.rb, line 62
def run(client, via, href, args = {}, &block)
  timeout_error = block || default_timeout_error
  resp = client_send(client, via, href, args)
  return TeamSnap::Response.load_collection(resp)
rescue Faraday::TimeoutError
  timeout_error.call
end

Private Class Methods

is_file?(arg) click to toggle source
# File lib/teamsnap.rb, line 100
def is_file?(arg)
  arg.respond_to?(:path) && File.file?(arg)
end
use_multipart?(args) click to toggle source
# File lib/teamsnap.rb, line 96
def use_multipart?(args)
  args.values.any? { |a| is_file?(a) }
end