Module: TeamSnap

Defined in:
lib/teamsnap.rb,
lib/teamsnap/version.rb

Defined Under Namespace

Modules: Collection, Item Classes: AuthMiddleware

Constant Summary collapse

EXCLUDED_RELS =
%w(me apiv2_root root self dude sweet random xyzzy schemas)
DEFAULT_URL =
"https://apiv3.teamsnap.com"
Error =
Class.new(StandardError)
NotFound =
Class.new(TeamSnap::Error)
VERSION =
"1.3.3"

Class Method Summary collapse

Class Method Details

.apply_endpoints(obj, collection) ⇒ Object



250
251
252
253
254
255
256
257
258
# File 'lib/teamsnap.rb', line 250

def apply_endpoints(obj, collection)
  collection
    .fetch(:queries) { [] }
    .each { |endpoint| register_endpoint(obj, endpoint, :via => :get) }

  collection
    .fetch(:commands) { [] }
    .each { |endpoint| register_endpoint(obj, endpoint, :via => :post) }
end

.client_send(via, href, args) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/teamsnap.rb', line 212

def client_send(via, href, args)
  case via
  when :get
    client.send(via, href, args)
  when :post
    client.send(via, href) do |req|
      req.body = Oj.dump(args)
    end
  else
    raise TeamSnap::Error.new("Don't know how to run `#{via}`")
  end
end

.collection(href, resp, parsed_collection) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/teamsnap.rb', line 98

def collection(href, resp, parsed_collection)
  Module.new do
    define_singleton_method(:included) do |descendant|
      descendant.send(:include, TeamSnap::Item)
      descendant.extend(TeamSnap::Collection)
      descendant.instance_variable_set(:@href, href)
      descendant.instance_variable_set(:@resp, resp)
      descendant.instance_variable_set(:@parsed_collection, parsed_collection)
    end
  end
end

.hashify(arr) ⇒ Object



110
111
112
113
114
# File 'lib/teamsnap.rb', line 110

def hashify(arr)
  arr.to_h
rescue NoMethodError
  arr.inject({}) { |hash, (key, value)| hash[key] = value; hash }
end

.init(opts = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/teamsnap.rb', line 116

def init(opts = {})
  opts[:url] ||= DEFAULT_URL
  unless opts.include?(:token) || (
    opts.include?(:client_id) && opts.include?(:client_secret)
  )
    raise ArgumentError.new(
      "You must provide a :token or :client_id and :client_secret pair to '.init'"
    )
  end

  self.client = Faraday.new(
    :url => opts.fetch(:url),
    :parallel_manager => Typhoeus::Hydra.new
  ) do |c|
    c.request :teamsnap_auth_middleware, {
      :token => opts[:token],
      :client_id => opts[:client_id],
      :client_secret => opts[:client_secret]
    }
    c.adapter :typhoeus
  end

  collection = TeamSnap.run_init(:get, "/", {})

  classes = []

  schema = collection
    .fetch(:links) { [] }
    .find { |link| link[:rel] == "schemas" } || {}

  if schema[:href]
    href_to_rel = collection
      .fetch(:links) { [] }
      .reject { |link| EXCLUDED_RELS.include?(link[:rel]) }
      .map { |link| [link[:href], link[:rel]]}
      .to_h

    resp = client.get(schema[:href])
    if resp.status == 200
      collections = Oj.load(resp.body)
      classes = collections.map { |collection|
        col = collection.fetch(:collection) { {} }
        if rel = href_to_rel[col[:href]]
          create_collection_class(rel, col[:href], nil, col)
        end
      }
      .compact
    else
      error_message = TeamSnap.parse_error(resp)
      raise TeamSnap::Error.new(error_message)
    end
  else
    client.in_parallel do
      classes = collection
        .fetch(:links) { [] }
        .map { |link| classify_rel(link) }
        .compact
    end
  end
  classes.each { |cls| cls.parse_collection }

  apply_endpoints(self, collection) && true
end

.load_items(collection) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/teamsnap.rb', line 236

def load_items(collection)
  collection
    .fetch(:items) { [] }
    .map { |item|
      data = parse_data(item).merge(:href => item[:href])
      type = type_of(item)
      cls = load_class(type, data)

      cls.new(data).tap { |obj|
        obj.send(:load_links, item.fetch(:links) { [] })
      }
    }
end

.parse_error(resp) ⇒ Object



225
226
227
228
229
230
231
232
233
234
# File 'lib/teamsnap.rb', line 225

def parse_error(resp)
  begin
    Oj.load(resp.body)
      .fetch(:collection)
      .fetch(:error)
      .fetch(:message)
  rescue KeyError
    resp.body
  end
end

.run(via, href, args = {}) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/teamsnap.rb', line 196

def run(via, href, args = {})
  resp = client_send(via, href, args)
  if resp.success?
    Oj.load(resp.body).fetch(:collection)
  else
    if resp.headers["content-type"].match("json")
      error_message = parse_error(resp)
      raise TeamSnap::Error.new(error_message)
    else
      raise TeamSnap::Error.new("`#{via}` call was unsuccessful. " +
        "Unexpected response content-type. " +
        "Check TeamSnap APIv3 connection")
    end
  end
end

.run_init(via, href, args = {}, opts = {}) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/teamsnap.rb', line 180

def run_init(via, href, args = {}, opts = {})
  begin
    resp = client_send(via, href, args)
  rescue Faraday::TimeoutError
    warn("Connection to API failed. Initializing with empty class structure")
    {:links => []}
  else
    if resp.success?
      Oj.load(resp.body).fetch(:collection)
    else
      error_message = parse_error(resp)
      raise TeamSnap::Error.new(error_message)
    end
  end
end