teamsnap_rb 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f9511231be3d578db265dfe57fef9b8144fba8f
4
- data.tar.gz: 75edb7b7469b24e1e58211e2489a45aba6039317
3
+ metadata.gz: 2c81581a206de0d99dea75777830a764ba63b965
4
+ data.tar.gz: 284dc197b286c7bfa8041b5810332fe5717ccfc0
5
5
  SHA512:
6
- metadata.gz: bcecd865a782c7f3925cea7ffc0ac533476e7f545727a434d540c5b467663a4df3feebfcdf92c52a4de947299cf960b939b976d6b917788e9eaac4d035c96cb1
7
- data.tar.gz: 889f6cd95ee3257a8164787852bf63e36f19d2ebf07e6e75a98513ad041e7dd091bb536da13fa2dd8481904cd00fabb30481801c38b914c91902d20c20952477
6
+ metadata.gz: cf8b9e6b81e6fc05ffaa5270d9a5bc6b0ae32cd98106abe1e4af8029e9327111f59588acc007967343636c19e8d5636624058e4f39cf016101e8f31bed3e68dd
7
+ data.tar.gz: 9bd33d443efa7a176dda48c92227f3a8cf60c71292e9d54b7c0a1347f3e93348f7decb54ccf4f5f9d628524ea7b24838fc2598ff21e4538255195e535beea3e1
data/README.md CHANGED
@@ -29,6 +29,18 @@ _Note: You'll need an OAuth2 Token from TeamSnap. Checkout our API docs
29
29
  => "Andrew"
30
30
  ```
31
31
 
32
+ ## Backup Cache
33
+
34
+ By default when calling TeamSnap.init(...), there is a backup cache of the API root response used
35
+ to setup all relevant classes. This is primarily used so CI testing and deploy jobs do not have to
36
+ connect to the API to complete.
37
+
38
+ This creates a .teamsnap\_rb file in your ./tmp directory (./tmp/.teamsnap_rb). If you would like to turn this functionality
39
+ off, just set ```:backup_cache => false``` in your init options. Alternatively, you can set the location
40
+ of the backup by passing a string location of where to store the file. e.g.
41
+ ```:backup_cache => "./another/location/.teamsnap_rb_file"```
42
+
43
+
32
44
  ## Todo
33
45
 
34
46
  - Literate style docs?
@@ -1,3 +1,3 @@
1
1
  module TeamSnap
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/teamsnap.rb CHANGED
@@ -104,6 +104,11 @@ module TeamSnap
104
104
  )
105
105
  end
106
106
 
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
+
107
112
  self.client = Faraday.new(
108
113
  :url => opts.fetch(:url),
109
114
  :parallel_manager => Typhoeus::Hydra.new
@@ -116,7 +121,7 @@ module TeamSnap
116
121
  c.adapter :typhoeus
117
122
  end
118
123
 
119
- collection = TeamSnap.run(:get, "/")
124
+ collection = TeamSnap.run(:get, "/", {}, opts)
120
125
 
121
126
  classes = []
122
127
  client.in_parallel do
@@ -131,18 +136,45 @@ module TeamSnap
131
136
  apply_endpoints(self, collection) && true
132
137
  end
133
138
 
134
- def run(via, href, args = {})
139
+ def run(via, href, args = {}, opts = {})
135
140
  resp = client.send(via, href, args)
136
141
 
137
142
  if resp.status == 200
138
- Oj.load(resp.body)
139
- .fetch(:collection)
143
+ collection = Oj.load(resp.body).fetch(:collection)
144
+ TeamSnap.write_backup_file(opts[:backup_cache_file], collection)
145
+ collection
140
146
  else
141
- error_message = parse_error(resp)
142
- raise TeamSnap::Error.new(error_message)
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]))
150
+ else
151
+ error_message = parse_error(resp)
152
+ raise TeamSnap::Error.new(error_message)
153
+ end
154
+ end
155
+ end
156
+
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) }
166
+ else
167
+ warn(
168
+ "WARNING: Directory '#{dir_location}' does not exist. " +
169
+ "Backup cache functionality will not work until this is resolved."
170
+ )
143
171
  end
144
172
  end
145
173
 
174
+ def backup_file_exists?(backup_cache_file)
175
+ !!backup_cache_file && File.exist?(backup_cache_file)
176
+ end
177
+
146
178
  def parse_error(resp)
147
179
  Oj.load(resp.body)
148
180
  .fetch(:collection)
@@ -6,7 +6,8 @@ RSpec.describe "teamsnap_rb", :vcr => true do
6
6
  VCR.use_cassette("apiv3-init") do
7
7
  TeamSnap.init(
8
8
  :url => "http://localhost:3000",
9
- :token => "1-classic-dont_tell_the_cops"
9
+ :token => "1-classic-dont_tell_the_cops",
10
+ :backup_cache => false
10
11
  )
11
12
  end
12
13
  end
@@ -161,4 +162,71 @@ RSpec.describe "teamsnap_rb", :vcr => true do
161
162
  end
162
163
  end
163
164
  end
165
+
166
+ context "supports using a backup file on init for when API cannot be reached" do
167
+ context ".backup_file_for" do
168
+ let(:default_file_location) { "./tmp/.teamsnap_rb" }
169
+
170
+ it "responds with the given file location if provided" do
171
+ file_location = "./some_dir/some_file.testing"
172
+ expect(TeamSnap.backup_file_for(file_location)).to eq(file_location)
173
+ end
174
+
175
+ it "responds with the default file location if set to true" do
176
+ expect(TeamSnap.backup_file_for(true)).to eq(default_file_location)
177
+ end
178
+ end
179
+
180
+ context ".write_backup_file" do
181
+ let(:collection) { {:one => 1} }
182
+
183
+ context "when the given directory exists" do
184
+ let(:test_file_location) { "./tmp/.teamsnap_rb" }
185
+ let(:file_contents) { Oj.dump(collection) }
186
+
187
+ it "returns the number of characters written to the file" do
188
+ expect(TeamSnap.write_backup_file(test_file_location, collection)).to eq(file_contents.length)
189
+ end
190
+
191
+ it "writes the file inside the directory provided" do
192
+ expect(File).to receive(:open).with(test_file_location, "w+")
193
+ TeamSnap.write_backup_file(test_file_location, collection)
194
+ end
195
+
196
+ it "writes the file with the correct information" do
197
+ TeamSnap.write_backup_file(test_file_location, collection)
198
+ file_contents = Oj.load(IO.read(test_file_location))
199
+ expect(file_contents).to eq(collection)
200
+ end
201
+ end
202
+
203
+ context "when the given directory does NOT exist" do
204
+ let(:test_file_location) { "./directory_doesnt_exist/file" }
205
+ let(:test_dir_location) { File.dirname(test_file_location) }
206
+ let(:warning_message) {
207
+ "WARNING: Directory '#{test_dir_location}' does not exist. " +
208
+ "Backup cache functionality will not work until this is resolved."
209
+ }
210
+
211
+ it "issues a warning that the directory does not exist" do
212
+ expect(TeamSnap).to receive(:warn).with(warning_message)
213
+ TeamSnap.write_backup_file(test_file_location, collection)
214
+ end
215
+ end
216
+ end
217
+
218
+ context ".backup_file_exists?" do
219
+ it "returns false if backup_cache_file is NOT set" do
220
+ expect(TeamSnap.backup_file_exists?(nil)).to eq(false)
221
+ end
222
+
223
+ it "returns false if the file does NOT exist" do
224
+ expect(TeamSnap.backup_file_exists?("./some_file_that_does_not_exist")).to eq(false)
225
+ end
226
+
227
+ it "returns true is the file exists" do
228
+ expect(TeamSnap.backup_file_exists?("./Gemfile")).to eq(true)
229
+ end
230
+ end
231
+ end
164
232
  end
data/tmp/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ # Ignore everything in this directory
2
+ *
3
+ # Except this file
4
+ !.gitignore
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: teamsnap_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Emmons
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-05-08 00:00:00.000000000 Z
12
+ date: 2015-05-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -174,6 +174,7 @@ files:
174
174
  - spec/spec_helper.rb
175
175
  - spec/teamsnap_spec.rb
176
176
  - teamsnap_rb.gemspec
177
+ - tmp/.gitignore
177
178
  homepage: http://developer.teamsnap.com/
178
179
  licenses:
179
180
  - MIT
@@ -194,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
195
  version: '0'
195
196
  requirements: []
196
197
  rubyforge_project:
197
- rubygems_version: 2.4.5
198
+ rubygems_version: 2.2.2
198
199
  signing_key:
199
200
  specification_version: 4
200
201
  summary: A gem to interact with TeamSnap's API