key_store 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d9f84301f0857cc3b7b0090b554914a680ada02b
4
+ data.tar.gz: 49de7e8df07c0730d79fbbca051742e282956abe
5
+ SHA512:
6
+ metadata.gz: 79b5ad463d132b39486f9fa7e82150f764861d74823b1e3595b8c7ae2c43001939c3394038edb8728407b91208c3c238415db5691c0a820aabc7cd82f86e5fb5
7
+ data.tar.gz: 76f666c6ca5375da6e83df7b01f162182bb354142a3ef9626f418a97c639082993ae93f2c840bdd472be1e1ef7cbb38c4c46b55dd8cc492ac71edcf4ba1046da
data/lib/key_store.rb ADDED
@@ -0,0 +1,55 @@
1
+ require "yaml/store"
2
+ require "thread"
3
+ require "fileutils"
4
+ require_relative "key_store/key"
5
+ require_relative "key_store/version"
6
+
7
+ module KeyStore
8
+
9
+ class << self
10
+
11
+ attr_reader :file_path
12
+
13
+ def set_file_path(value)
14
+ @file_path = value
15
+ end
16
+
17
+ def file
18
+ if @file.nil? || modified?
19
+ Mutex.new.synchronize do
20
+ @file = begin
21
+ raise "file_path not set" if file_path.nil?
22
+ FileUtils.touch(file_path) unless File.exists?(file_path)
23
+ self.mtime = File.mtime(file_path)
24
+ YAML::Store.new(file_path, true)
25
+ end
26
+ end
27
+ end
28
+ @file
29
+ end
30
+
31
+ def exists?(name)
32
+ !!(file.transaction { file[name.to_s] })
33
+ end
34
+
35
+ def find(name)
36
+ exists?(name) ? KeyStore::Key.new(name) : nil
37
+ end
38
+
39
+ def delete!(name)
40
+ file.transaction { file.delete(name.to_s) }
41
+ end
42
+
43
+ private
44
+
45
+ attr_accessor :mtime
46
+
47
+ def modified?
48
+ return true if mtime.nil? || !File.exists?(file_path)
49
+ File.mtime(file_path) != mtime
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
@@ -0,0 +1,52 @@
1
+ require "forwardable"
2
+
3
+ module KeyStore
4
+ class Key
5
+ extend Forwardable
6
+
7
+ def_delegators :KeyStore, :file, :exists?
8
+ attr_reader :name, :roles
9
+ attr_accessor :notes
10
+ alias_method :to_s, :name
11
+
12
+ def initialize(name, roles: nil, notes: nil)
13
+ @name = name.to_s
14
+
15
+ if exists?(name)
16
+ set_roles_from_file if roles.nil?
17
+ set_notes_from_file if notes.nil?
18
+ end
19
+
20
+ @roles ||= (roles || []).map(&:to_s)
21
+ @notes ||= notes.to_s
22
+ end
23
+
24
+ def http_header
25
+ "Authorization: Token token=\"#{name}\""
26
+ end
27
+
28
+ def save!
29
+ file.transaction do
30
+ file[name] ||= {}
31
+ file[name][:roles] = roles.map(&:to_s)
32
+ file[name][:notes] = notes.to_s
33
+ file[name][:http_header] = http_header
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def set_roles_from_file
40
+ file.transaction do
41
+ @roles = file[name][:roles]
42
+ end
43
+ end
44
+
45
+ def set_notes_from_file
46
+ file.transaction do
47
+ @notes = file[name][:notes]
48
+ end
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module KeyStore
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,45 @@
1
+ require_relative "test_helper"
2
+
3
+ class KeyStoreTest < MicroTest::Test
4
+
5
+ before do
6
+ @file_path = File.expand_path("../keys.yml", __FILE__)
7
+ KeyStore.set_file_path @file_path
8
+ KeyStore.delete!(:foobar)
9
+ end
10
+
11
+ test "set_file_path" do
12
+ assert KeyStore.file_path == @file_path
13
+ end
14
+
15
+ test "file errors with nil file_path" do
16
+ KeyStore.instance_eval do
17
+ @file_path = @file = nil
18
+ end
19
+
20
+ begin
21
+ assert KeyStore.file
22
+ rescue Exception => e
23
+ error = e
24
+ end
25
+
26
+ assert !error.nil?
27
+ end
28
+
29
+ test "file success with valid file_path" do
30
+ assert KeyStore.file
31
+ end
32
+
33
+ test "unknown key does not exist" do
34
+ assert KeyStore.exists?(:foobar) == false
35
+ end
36
+
37
+ test "find returns nil on unknown key" do
38
+ assert KeyStore.find(:foobar).nil?
39
+ end
40
+
41
+ test "ok to call delete! on unknown key" do
42
+ assert KeyStore.delete!(:foobar).nil?
43
+ end
44
+
45
+ end
data/test/key_test.rb ADDED
@@ -0,0 +1,84 @@
1
+ require_relative "test_helper"
2
+
3
+ class KeyTest < MicroTest::Test
4
+
5
+ before do
6
+ @file_path = File.expand_path("../keys.yml", __FILE__)
7
+ KeyStore.set_file_path @file_path
8
+ KeyStore.delete!(:foobar)
9
+ end
10
+
11
+ test "name set on initialize" do
12
+ key = KeyStore::Key.new(:foobar)
13
+ assert key.name == "foobar"
14
+ end
15
+
16
+ test "initialize doesn't save key" do
17
+ assert KeyStore.exists?(:foobar) == false
18
+ KeyStore::Key.new(:foobar)
19
+ assert KeyStore.exists?(:foobar) == false
20
+ end
21
+
22
+ test "initialize with roles" do
23
+ roles = [:foo, :bar, :baz]
24
+ key = KeyStore::Key.new(:foobar, roles: roles)
25
+ assert key.roles == roles.map(&:to_s)
26
+ end
27
+
28
+ test "initialize with notes" do
29
+ notes = "There are notes."
30
+ key = KeyStore::Key.new(:foobar, notes: notes)
31
+ assert key.notes == notes
32
+ end
33
+
34
+ test "http_header after initialize" do
35
+ key = KeyStore::Key.new(:foobar)
36
+ assert key.http_header == "Authorization: Token token=\"foobar\""
37
+ end
38
+
39
+ test "basic save" do
40
+ key = KeyStore::Key.new(:foobar)
41
+ key.save!
42
+ assert KeyStore.exists?(:foobar)
43
+ end
44
+
45
+ test "save with roles" do
46
+ roles = [:foo, :bar, :baz]
47
+ key = KeyStore::Key.new(:foobar, roles: roles)
48
+ key.save!
49
+ key = KeyStore.find(:foobar)
50
+ assert key.roles == roles.map(&:to_s)
51
+ end
52
+
53
+ test "save with notes" do
54
+ notes = "There are notes."
55
+ key = KeyStore::Key.new(:foobar, notes: notes)
56
+ key.save!
57
+ key = KeyStore.find(:foobar)
58
+ assert key.notes == notes
59
+ end
60
+
61
+ test "append roles & multiple saves" do
62
+ key = KeyStore::Key.new(:foobar, roles: [:foo])
63
+ key.save!
64
+ key = KeyStore.find(:foobar)
65
+ key.roles << :bar
66
+ key.roles << :baz
67
+ key.save!
68
+ key = KeyStore.find(:foobar)
69
+ assert key.roles == [:foo, :bar, :baz].map(&:to_s)
70
+ end
71
+
72
+ test "update notes" do
73
+ key = KeyStore::Key.new(:foobar, notes: "note 1")
74
+ key.save!
75
+ key = KeyStore.find(:foobar)
76
+ assert key.notes == "note 1"
77
+ key.notes = "note 2"
78
+ key.save!
79
+ key = KeyStore.find(:foobar)
80
+ assert key.notes == "note 2"
81
+ end
82
+
83
+ end
84
+
@@ -0,0 +1,2 @@
1
+ require "micro_test"
2
+ require_relative "../lib/key_store"
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: key_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Hopkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: micro_test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.0
55
+ description: A simple api-key storage engine
56
+ email:
57
+ - natehop@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/key_store/key.rb
63
+ - lib/key_store/version.rb
64
+ - lib/key_store.rb
65
+ - test/key_store_test.rb
66
+ - test/key_test.rb
67
+ - test/test_helper.rb
68
+ homepage: https://github.com/hopsoft/key_store
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: 2.0.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.0.3
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: A simple api-key storage engine
92
+ test_files:
93
+ - test/key_store_test.rb
94
+ - test/key_test.rb
95
+ - test/test_helper.rb
96
+ has_rdoc: