Module: TeamSnap::Collection
- Defined in:
- lib/teamsnap/collection.rb
Class Method Summary collapse
- .apply_endpoints(obj, collection) ⇒ Object
- .endpoint_creation_set(obj, creation_set, via) ⇒ Object
- .register_endpoint(obj, endpoint, opts) ⇒ Object
Instance Method Summary collapse
- #actions ⇒ Object
- #command_names ⇒ Object
- #commands ⇒ Object
- #create(client, attributes = {}) ⇒ Object
- #delete(client, id) ⇒ Object
- #href ⇒ Object
- #items(client = TeamSnap.root_client) ⇒ Object
- #parse_collection ⇒ Object
- #parsed_collection ⇒ Object
- #queries ⇒ Object
- #query_names ⇒ Object
- #resp ⇒ Object
- #template_attributes ⇒ Object
- #update(client, id, attributes = {}) ⇒ Object
Class Method Details
.apply_endpoints(obj, collection) ⇒ Object
6 7 8 9 10 11 12 |
# 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) ⇒ Object
14 15 16 |
# 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) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# 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 |
Instance Method Details
#actions ⇒ Object
40 41 42 43 44 45 |
# 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 ⇒ Object
59 60 61 |
# File 'lib/teamsnap/collection.rb', line 59 def command_names commands.map{ |q| q[:rel].to_sym } end |
#commands ⇒ Object
55 56 57 |
# File 'lib/teamsnap/collection.rb', line 55 def commands parsed_collection.fetch(:commands) { [] } end |
#create(client, attributes = {}) ⇒ Object
63 64 65 66 67 68 |
# 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) ⇒ Object
77 78 79 |
# File 'lib/teamsnap/collection.rb', line 77 def delete(client, id) TeamSnap.run(client, :delete, href+"/#{id}", {}) end |
#href ⇒ Object
94 95 96 |
# File 'lib/teamsnap/collection.rb', line 94 def href self.instance_variable_get(:@href) end |
#items(client = TeamSnap.root_client) ⇒ Object
89 90 91 92 |
# 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 ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# 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 ⇒ Object
102 103 104 |
# File 'lib/teamsnap/collection.rb', line 102 def parsed_collection self.instance_variable_get(:@parsed_collection) end |
#queries ⇒ Object
47 48 49 |
# File 'lib/teamsnap/collection.rb', line 47 def queries parsed_collection.fetch(:queries) { [] } end |
#query_names ⇒ Object
51 52 53 |
# File 'lib/teamsnap/collection.rb', line 51 def query_names queries.map{ |q| q[:rel].to_sym } end |
#resp ⇒ Object
98 99 100 |
# File 'lib/teamsnap/collection.rb', line 98 def resp self.instance_variable_get(:@resp) end |
#template_attributes ⇒ Object
81 82 83 84 85 86 87 |
# 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 = {}) ⇒ Object
70 71 72 73 74 75 |
# 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 |