octoauth 1.0.2 → 1.1.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: 9aa2e61a7ffe64cb1ca0a5c992a62afad06ba3f2
4
- data.tar.gz: 3eea475acbea688c561c178afd6418f83695618a
3
+ metadata.gz: a2fc370fac48f586e3832626f1979f7cea1b5649
4
+ data.tar.gz: e0cd4359ebe9fb0d4a06c9e1544edb2bd1cd3b72
5
5
  SHA512:
6
- metadata.gz: 34b473862aff7ce51daf7f2d3c17c2dfd98bd01bba08a521e9a93bac0fcbfa81ffa80b39f00baf17fb1fe56bbf024b80adc0017383aeea609ff60284745675ef
7
- data.tar.gz: c9d43a74f10613864ec54a04382465f80772a5ecd463cb6277648fd44d6d0b0f46c866727757d46f2fb970a287bfcf1f76ab34bc5f0707794799f9fb3608e69a
6
+ metadata.gz: fce5750ecd54f28ab452f227a6ac685ee251619e1f0e76e1620add2b6c4d64d84cb7943f0fcd195abcb6f24ae3760088eca66205783a69b420c3280d66eae752
7
+ data.tar.gz: 0abe260a1a69248c6f0c7d086a6db8c267fef7e3a97000a94ba5b0e6b747ff6b0e9ad77815381ecc3f5307ff23d26a3d7104550d6a43b1fbb9582756f3e1c6ac
data/.gitignore CHANGED
@@ -6,3 +6,4 @@ Gemfile.lock
6
6
  spec/examples/tmp.yml
7
7
  spec/examples/priv_test.yml
8
8
  spec/examples/autosave.yml
9
+ spec/examples/nil.yml
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 1.1.0 / 2015-05-03
2
+
3
+ * [FEATURE] Support hierarchy of file paths for configs
4
+
1
5
  # 1.0.2 / 2015-05-03
2
6
 
3
7
  * [ENHANCEMENT] Fix option/param handling to be cleaner
data/README.md CHANGED
@@ -35,6 +35,12 @@ puts "The other token is #{other_auth.token}"
35
35
  other_auth.save
36
36
  ```
37
37
 
38
+ Multiple files can be specified, and it will try them in order looking for a valid token. If this fails, it will use the first file listed for any future saving of tokens:
39
+
40
+ ```
41
+ auth = Octoauth.new note: 'my_app', files: ['./.octoauth.yml', '/etc/octoauth', :default]
42
+ ```
43
+
38
44
  The above examples get us the basic scope, which means some read-only public access. For other scopes, specify them when creating the token:
39
45
 
40
46
  ```
data/lib/octoauth/auth.rb CHANGED
@@ -21,15 +21,14 @@ module Octoauth
21
21
  class Auth
22
22
  def initialize(params = {})
23
23
  parse_params!(params)
24
- @config = ConfigFile.new file: @options[:file], note: config_note
25
24
  save if @options[:autosave]
26
25
  end
27
26
 
28
27
  def save
29
28
  fail 'No token to save' unless token
30
- fail 'No file given for config' unless @config.file
31
- @config.token = @token
32
- @config.write
29
+ fail 'No file given for config' unless config.file
30
+ config.token = token
31
+ config.write
33
32
  end
34
33
 
35
34
  def token
@@ -38,9 +37,21 @@ module Octoauth
38
37
 
39
38
  private
40
39
 
40
+ def config
41
+ return @config if @config
42
+ configs = config_files.map do |file|
43
+ ConfigFile.new file: file, note: config_note
44
+ end
45
+ @config = configs.find(&:token) || configs.first
46
+ end
47
+
48
+ def config_files
49
+ @options[:files] ||= [@options[:file]]
50
+ end
51
+
41
52
  def parse_params!(params)
42
53
  @options = params.subset(
43
- :file, :note, :autosave, :scopes,
54
+ :file, :files, :note, :autosave, :scopes,
44
55
  :login, :password, :twofactor, :api_endpoint
45
56
  )
46
57
  end
@@ -60,7 +71,7 @@ module Octoauth
60
71
  end
61
72
 
62
73
  def load_token(needs2fa = false)
63
- return @config.token if @config.token
74
+ return config.token if config.token
64
75
  prompt!(needs2fa)
65
76
  authenticate
66
77
  end
data/octoauth.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'octoauth'
3
- s.version = '1.0.2'
3
+ s.version = '1.1.0'
4
4
  s.date = Time.now.strftime("%Y-%m-%d")
5
5
 
6
6
  s.summary = 'Auth token helper for GitHub API'
@@ -139,6 +139,61 @@ describe Octoauth do
139
139
  expect(new_auth.token).to eql random
140
140
  end
141
141
  end
142
+ context 'when given multiple file paths' do
143
+ context 'when the first file exists' do
144
+ it 'reads the first file' do
145
+ auth = Octoauth::Auth.new(
146
+ note: 'foo',
147
+ files: ['spec/examples/conf_a.yml', 'spec/examples/bogus.yml']
148
+ )
149
+ expect(auth.token).to eql 'bcdebcdebcdebcdebcdebcdebcde'
150
+ end
151
+ end
152
+ context 'when the first file does not exist' do
153
+ context 'when the second file exists' do
154
+ it 'reads the second file' do
155
+ FileUtils.rm_f 'spec/examples/nil.yml'
156
+ auth = Octoauth::Auth.new(
157
+ note: 'foo',
158
+ files: ['spec/examples/bogus.yml', 'spec/examples/conf_a.yml']
159
+ )
160
+ expect(auth.token).to eql 'bcdebcdebcdebcdebcdebcdebcde'
161
+ end
162
+ end
163
+ context 'when the second file does not exist' do
164
+ it 'reads nil' do
165
+ FileUtils.rm_f ['spec/examples/nil.yml', 'spec/examples/nil2.yml']
166
+ auth = Octoauth::Auth.new(
167
+ note: 'foo',
168
+ files: ['spec/examples/nil.yml', 'spec/examples/nil2.yml']
169
+ )
170
+ expect(auth.send(:config).token).to be_nil
171
+ end
172
+ it 'writes to the first file' do
173
+ random = rand(36**30).to_s(30)
174
+ stub_request(:post, 'https://user:pw@api.github.com/authorizations')
175
+ .with(body: "{\"note\":\"foo\",\"scopes\":[]}")
176
+ .to_return(
177
+ status: 200,
178
+ body: AuthShim.new('foo', random)
179
+ )
180
+ FileUtils.rm_f ['spec/examples/nil.yml', 'spec/examples/nil2.yml']
181
+ auth = Octoauth::Auth.new(
182
+ note: 'foo',
183
+ files: ['spec/examples/nil.yml', 'spec/examples/nil2.yml'],
184
+ login: 'user',
185
+ password: 'pw'
186
+ )
187
+ auth.save
188
+ new_auth = Octoauth::Auth.new(
189
+ note: 'foo',
190
+ file: 'spec/examples/nil.yml'
191
+ )
192
+ expect(new_auth.token).to eql random
193
+ end
194
+ end
195
+ end
196
+ end
142
197
 
143
198
  describe '#save' do
144
199
  it 'saves the config to disk' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octoauth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Les Aker