teamsnap_rb 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/teamsnap/version.rb +1 -1
  4. data/lib/teamsnap.rb +64 -46
  5. data/spec/cassettes/apiv3-init.yml +1358 -1229
  6. data/spec/cassettes/teamsnap_rb/adds_find_if_search_is_available.yml +19 -21
  7. data/spec/cassettes/teamsnap_rb/adds_href_to_items.yml +8 -8
  8. data/spec/cassettes/teamsnap_rb/can_follow_plural_links.yml +44 -55
  9. data/spec/cassettes/teamsnap_rb/can_follow_singular_links.yml +37 -41
  10. data/spec/cassettes/teamsnap_rb/can_handle_an_empty_bulk_load.yml +14 -22
  11. data/spec/cassettes/teamsnap_rb/can_handle_an_error_with_bulk_load.yml +12 -14
  12. data/spec/cassettes/teamsnap_rb/can_handle_errors_generated_by_command.yml +14 -16
  13. data/spec/cassettes/teamsnap_rb/can_handle_links_with_no_data.yml +29 -45
  14. data/spec/cassettes/teamsnap_rb/can_use_bulk_load.yml +28 -31
  15. data/spec/cassettes/teamsnap_rb/handles_executing_an_action_via_commands.yml +18 -26
  16. data/spec/cassettes/teamsnap_rb/handles_executing_an_action_via_commands_with_multiple_params.yml +2918 -0
  17. data/spec/cassettes/teamsnap_rb/handles_fetching_data_via_queries.yml +18 -26
  18. data/spec/cassettes/teamsnap_rb/handles_queries_with_no_data.yml +15 -23
  19. data/spec/cassettes/teamsnap_rb/raises_an_exception_if_find_returns_nothing.yml +15 -23
  20. data/spec/cassettes/teamsnap_rb/supports_relations_with_expected_behaviors/when_a_plural_relation_is_called/responds_with_an_array_of_objects_when_successful.yml +28 -40
  21. data/spec/cassettes/teamsnap_rb/supports_relations_with_expected_behaviors/when_a_plural_relation_is_called/responds_with_an_empty_array_when_no_objects_exist.yml +22 -34
  22. data/spec/cassettes/teamsnap_rb/supports_relations_with_expected_behaviors/when_a_singular_relation_is_called/responds_with_nil_if_it_does_NOT_exist.yml +22 -34
  23. data/spec/cassettes/teamsnap_rb/supports_relations_with_expected_behaviors/when_a_singular_relation_is_called/responds_with_the_object_if_it_exists.yml +25 -37
  24. data/spec/spec_helper.rb +10 -0
  25. data/spec/teamsnap_spec.rb +72 -9
  26. metadata +4 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c81581a206de0d99dea75777830a764ba63b965
4
- data.tar.gz: 284dc197b286c7bfa8041b5810332fe5717ccfc0
3
+ metadata.gz: f5c247d6f1854cf336cecb619650aeee8874707f
4
+ data.tar.gz: 3a025c42980dad4d3da35f8795360042e157388a
5
5
  SHA512:
6
- metadata.gz: cf8b9e6b81e6fc05ffaa5270d9a5bc6b0ae32cd98106abe1e4af8029e9327111f59588acc007967343636c19e8d5636624058e4f39cf016101e8f31bed3e68dd
7
- data.tar.gz: 9bd33d443efa7a176dda48c92227f3a8cf60c71292e9d54b7c0a1347f3e93348f7decb54ccf4f5f9d628524ea7b24838fc2598ff21e4538255195e535beea3e1
6
+ metadata.gz: 4053c6f511dd48f576400bf8283cca14a01ff46b3677341392dc772724142e126b81bb1a5c0eb0dbbb255415aac1d47e8d2d5b28891474738598fa44b68e4ef5
7
+ data.tar.gz: 86e36409184178ef0c1397c4e5cc10bf34ac31232bfdaca7b362d19a538a283d8a491b704d77ed65be7f8c4be8e490c3f685b5ca8d4d86d9001593b3cff07cfc
data/README.md CHANGED
@@ -15,7 +15,7 @@ _Note: You'll need an OAuth2 Token from TeamSnap. Checkout our API docs
15
15
  ```
16
16
  λ gem install teamsnap_rb
17
17
  λ irb
18
- > TeamSnap.init(:token => "abc123...", :url => "https://apiv3.teamsnap.com")
18
+ > TeamSnap.init(:token => "abc123...")
19
19
  > t = TeamSnap::Team.find(1)
20
20
  => #<TeamSnap::Team::...>
21
21
  > t.name
@@ -1,3 +1,3 @@
1
1
  module TeamSnap
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
data/lib/teamsnap.rb CHANGED
@@ -32,7 +32,7 @@ end
32
32
 
33
33
  module TeamSnap
34
34
  EXCLUDED_RELS = %w(me apiv2_root root self dude sweet random xyzzy)
35
- DEFAULT_URL = "http://apiv3.teamsnap.com"
35
+ DEFAULT_URL = "https://apiv3.teamsnap.com"
36
36
  Error = Class.new(StandardError)
37
37
  NotFound = Class.new(TeamSnap::Error)
38
38
 
@@ -43,10 +43,6 @@ module TeamSnap
43
43
  end
44
44
 
45
45
  def call(env)
46
- token = @options[:token]
47
- client_id = @options[:client_id]
48
- client_secret = @options[:client_secret]
49
-
50
46
  if token
51
47
  env[:request_headers].merge!({"Authorization" => "Bearer #{token}"})
52
48
  elsif client_id && client_secret
@@ -58,22 +54,43 @@ module TeamSnap
58
54
  })
59
55
  env.url.query = URI.encode_www_form(query_params)
60
56
 
61
- body = if env.body.is_a?(Hash)
62
- Faraday::Utils::ParamsHash.new.merge(env.body).to_query
63
- else
64
- env.body
65
- end
66
- message = "/?" + env.url.query.to_s + (body || "")
67
- digest = OpenSSL::Digest.new("sha256")
68
- message_hash = digest.hexdigest(message)
69
-
70
57
  env.request_headers["X-Teamsnap-Hmac"] = OpenSSL::HMAC.hexdigest(
71
- digest, client_secret, message_hash
58
+ digest, client_secret, message_hash(env)
72
59
  )
73
60
  end
74
61
 
75
62
  @app.call(env)
76
63
  end
64
+
65
+ def token
66
+ @token ||= @options[:token]
67
+ end
68
+
69
+ def client_id
70
+ @client_id ||= @options[:client_id]
71
+ end
72
+
73
+ def client_secret
74
+ @client_secret ||= @options[:client_secret]
75
+ end
76
+
77
+ def digest
78
+ OpenSSL::Digest.new("sha256")
79
+ end
80
+
81
+ def message_hash(env)
82
+ digest.hexdigest(
83
+ query_string(env) + message(env)
84
+ )
85
+ end
86
+
87
+ def query_string(env)
88
+ "/?" + env.url.query.to_s
89
+ end
90
+
91
+ def message(env)
92
+ env.body || ""
93
+ end
77
94
  end
78
95
 
79
96
  class << self
@@ -104,11 +121,6 @@ module TeamSnap
104
121
  )
105
122
  end
106
123
 
107
- opts[:backup_cache] = opts.fetch(:backup_cache) { true }
108
- if opts[:backup_cache]
109
- opts[:backup_cache_file] = TeamSnap.backup_file_for(opts[:backup_cache])
110
- end
111
-
112
124
  self.client = Faraday.new(
113
125
  :url => opts.fetch(:url),
114
126
  :parallel_manager => Typhoeus::Hydra.new
@@ -121,7 +133,7 @@ module TeamSnap
121
133
  c.adapter :typhoeus
122
134
  end
123
135
 
124
- collection = TeamSnap.run(:get, "/", {}, opts)
136
+ collection = TeamSnap.run_init(:get, "/", {})
125
137
 
126
138
  classes = []
127
139
  client.in_parallel do
@@ -136,17 +148,15 @@ module TeamSnap
136
148
  apply_endpoints(self, collection) && true
137
149
  end
138
150
 
139
- def run(via, href, args = {}, opts = {})
140
- resp = client.send(via, href, args)
141
-
142
- if resp.status == 200
143
- collection = Oj.load(resp.body).fetch(:collection)
144
- TeamSnap.write_backup_file(opts[:backup_cache_file], collection)
145
- collection
151
+ def run_init(via, href, args = {}, opts = {})
152
+ begin
153
+ resp = client_send(via, href, args)
154
+ rescue Faraday::TimeoutError
155
+ warn("Connection to API failed. Initializing with empty class structure")
156
+ {:links => []}
146
157
  else
147
- if TeamSnap.backup_file_exists?(opts[:backup_cache_file])
148
- warn("Connection to API failed.. using backup cache file to initialize endpoints")
149
- Oj.load(IO.read(opts[:backup_cache_file]))
158
+ if resp.success?
159
+ Oj.load(resp.body).fetch(:collection)
150
160
  else
151
161
  error_message = parse_error(resp)
152
162
  raise TeamSnap::Error.new(error_message)
@@ -154,25 +164,33 @@ module TeamSnap
154
164
  end
155
165
  end
156
166
 
157
- def backup_file_for(backup_cache)
158
- backup_cache == true ? "./tmp/.teamsnap_rb" : backup_cache
159
- end
160
-
161
- def write_backup_file(file_location, collection)
162
- return unless file_location
163
- dir_location = File.dirname(file_location)
164
- if Dir.exist?(dir_location)
165
- File.open(file_location, "w+") { |f| f.write Oj.dump(collection) }
167
+ def run(via, href, args = {})
168
+ resp = client_send(via, href, args)
169
+ if resp.success?
170
+ Oj.load(resp.body).fetch(:collection)
166
171
  else
167
- warn(
168
- "WARNING: Directory '#{dir_location}' does not exist. " +
169
- "Backup cache functionality will not work until this is resolved."
170
- )
172
+ if resp.headers["content-type"].match("json")
173
+ error_message = parse_error(resp)
174
+ raise TeamSnap::Error.new(error_message)
175
+ else
176
+ raise TeamSnap::Error.new("`#{via}` call was unsuccessful. " +
177
+ "Unexpected response content-type. " +
178
+ "Check TeamSnap APIv3 connection")
179
+ end
171
180
  end
172
181
  end
173
182
 
174
- def backup_file_exists?(backup_cache_file)
175
- !!backup_cache_file && File.exist?(backup_cache_file)
183
+ def client_send(via, href, args)
184
+ case via
185
+ when :get
186
+ client.send(via, href, args)
187
+ when :post
188
+ client.send(via, href) do |req|
189
+ req.body = Oj.dump(args)
190
+ end
191
+ else
192
+ raise TeamSnap::Error.new("Don't know how to run `#{via}`")
193
+ end
176
194
  end
177
195
 
178
196
  def parse_error(resp)