module TeamSnap::Collection

Public Class Methods

apply_endpoints(obj, collection) click to toggle source
# File lib/teamsnap/collection.rb, line 6
def apply_endpoints(obj, collection)
  queries = collection.fetch(:queries) { [] }
  commands = collection.fetch(:commands) { [] }

  endpoint_creation_set(obj, queries, :get)
  endpoint_creation_set(obj, commands, :post)
end
endpoint_creation_set(obj, creation_set, via) click to toggle source
# File lib/teamsnap/collection.rb, line 14
def endpoint_creation_set(obj, creation_set, via)
  creation_set.each{ |endpoint| register_endpoint(obj, endpoint, :via => via) }
end
register_endpoint(obj, endpoint, opts) click to toggle source
# File lib/teamsnap/collection.rb, line 18
def register_endpoint(obj, endpoint, opts)
  rel = endpoint.fetch(:rel)
  href = endpoint.fetch(:href)
  valid_args = endpoint.fetch(:data) { [] }
    .map { |datum| datum.fetch(:name).to_sym }
  via = opts.fetch(:via)

  obj.define_singleton_method(rel) do |client, *args|
    args = Hash[*args]

    unless (args.keys & valid_args).any?
      raise ArgumentError.new(
        "Invalid argument(s). Valid argument(s) are #{valid_args.inspect}"
      )
    end

    resp = TeamSnap.run(client, via, href, args)
    TeamSnap::Item.load_items(client, resp)
  end
end

Public Instance Methods

actions() click to toggle source
# File lib/teamsnap/collection.rb, line 40
def actions
  actions = parsed_collection.fetch(:actions) {
    %w(create read update delete search)
  }
  return actions.map(&:to_sym)
end
command_names() click to toggle source
# File lib/teamsnap/collection.rb, line 59
def command_names
  commands.map{ |q| q[:rel].to_sym }
end
commands() click to toggle source
# File lib/teamsnap/collection.rb, line 55
def commands
  parsed_collection.fetch(:commands) { [] }
end
create(client, attributes = {}) click to toggle source
# File lib/teamsnap/collection.rb, line 63
def create(client, attributes = {})
  post_attributes = TeamSnap::Api.template_attributes(attributes)

  create_resp = TeamSnap.run(client, :post, href, post_attributes)
  TeamSnap::Item.load_items(client, create_resp).first
end
delete(client, id) click to toggle source
# File lib/teamsnap/collection.rb, line 77
def delete(client, id)
  TeamSnap.run(client, :delete, href+"/#{id}", {})
end
href() click to toggle source
# File lib/teamsnap/collection.rb, line 94
def href
  self.instance_variable_get(:@href)
end
items(client = TeamSnap.root_client) click to toggle source
# File lib/teamsnap/collection.rb, line 89
def items(client = TeamSnap.root_client)
  resp = TeamSnap.run(client, :get, href, {})
  TeamSnap::Item.load_items(client, resp)
end
parse_collection() click to toggle source
# File lib/teamsnap/collection.rb, line 106
def parse_collection
  if resp
    TeamSnap.response_check(resp, :get)
    collection = JSON.parse(resp.body, :symbolize_names => true).fetch(:collection) { [] }
  elsif parsed_collection
    collection = parsed_collection
  end

  TeamSnap::Collection.apply_endpoints(self, collection)
  enable_find if respond_to?(:search)
end
parsed_collection() click to toggle source
# File lib/teamsnap/collection.rb, line 102
def parsed_collection
  self.instance_variable_get(:@parsed_collection)
end
queries() click to toggle source
# File lib/teamsnap/collection.rb, line 47
def queries
  parsed_collection.fetch(:queries) { [] }
end
query_names() click to toggle source
# File lib/teamsnap/collection.rb, line 51
def query_names
  queries.map{ |q| q[:rel].to_sym }
end
resp() click to toggle source
# File lib/teamsnap/collection.rb, line 98
def resp
  self.instance_variable_get(:@resp)
end
template_attributes() click to toggle source
# File lib/teamsnap/collection.rb, line 81
def template_attributes
  template = parsed_collection.fetch(:template) {}
  data = template.fetch(:data) { [] }
  data
    .reject{ |col| col.fetch(:name) == "type" }
    .map{ |col| col.fetch(:name) }
end
update(client, id, attributes = {}) click to toggle source
# File lib/teamsnap/collection.rb, line 70
def update(client, id, attributes = {})
  patch_attributes = TeamSnap::Api.template_attributes(attributes)

  update_resp = TeamSnap.run(client, :patch, href+"/#{id}", patch_attributes)
  TeamSnap::Item.load_items(client, update_resp).first
end

Private Instance Methods

enable_find() click to toggle source
# File lib/teamsnap/collection.rb, line 120
def enable_find
  define_singleton_method(:find) do |client, id|
    search(client, :id => id).first.tap do |object|
      raise TeamSnap::NotFound.new(
        "Could not find a #{self} with an id of '#{id}'."
      ) unless object
    end
  end
end