Class: TeamSnap::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/teamsnap/api.rb

Constant Summary collapse

CRUD_METHODS =
[:find, :create, :update, :delete]
CRUD_VIAS =
[:get,  :post,   :patch,  :delete]
INTEGER_CLASS =

To handle Fixnum on Ruby 2.3 and Integer on 2.4+

1.class

Class Method Summary collapse

Class Method Details

.args(method, sent_args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



37
38
39
# 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 = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/teamsnap/api.rb', line 89

def self.template_args?(method)
  [:create, :update].include?(method)
end

.template_attributes(attributes) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object



108
109
110
111
112
113
114
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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