consul-kv 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 842a67923f31d892cf220492577113c88ac02d5e
4
+ data.tar.gz: 77783ecfb709cc07d99ca71b90a7011ea585912d
5
+ SHA512:
6
+ metadata.gz: 0fb84ba82a9c7c0b5d934cae5e7db2fe3150c41cb473e99d03e992eb54159405198c468fbc6b6280024fa9b013223bbcee32468e263010cb3d55ca61c5244240
7
+ data.tar.gz: 652125a6a738bf706aef0df66712059ba6799e5c4d7a9de7aa943917b09f38da860b576c09daf10238a2d7b40dd7eb05e2362a6a3b76c911756f3549a5d2f48b
data/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*]
4
+ end_of_line = lf
5
+ insert_final_newline = true
6
+ indent_style = space
7
+ indent_size = 2
8
+ trim_trailing_whitespace = true
9
+ charset = utf-8
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .irbrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in consul-kv.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Board Intelligence
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Consul::KV
2
+
3
+ Consul::KV is a simple wrapper around Consul's KV store.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'consul-kv'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install consul-kv
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ Consul::KV.configure do |x|
25
+ x.consul_host = 'http://127.0.0.1'
26
+ x.consul_port = '8500'
27
+ x.consul_prefix = 'v1/kv'
28
+ end
29
+
30
+ store = Consul::KV::Store.new
31
+ store['foo'] = bar #=> { 'foo' => 'bar' }
32
+ store['foo'] #=> { 'foo' => 'bar' }
33
+ store.delete('foo') #=> true
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( https://github.com/[my-github-username]/consul-kv/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/consul-kv.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'consul/kv/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "consul-kv"
8
+ spec.version = Consul::KV::VERSION
9
+ spec.authors = ["JGW Maxwell"]
10
+ spec.email = ["john.maxwell@boardintelligence.co.uk"]
11
+ spec.summary = %q{Simple wrapper around the Consul KV store.}
12
+ spec.description = %q{Simple wrapper around the Consul KV store.}
13
+ spec.homepage = "https://github.com/boardiq/consul-kv"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,14 @@
1
+ module Consul
2
+ module KV
3
+ class Configuration
4
+ attr_accessor :consul_host, :consul_port,
5
+ :consul_prefix
6
+
7
+ def initialize
8
+ @consul_host = 'http://127.0.0.1'
9
+ @consul_port = '8500'
10
+ @consul_prefix = '/v1/kv'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,44 @@
1
+ require 'net/http'
2
+
3
+ module Consul
4
+ module KV
5
+ class Connection
6
+ attr_reader :http
7
+
8
+ def initialize
9
+ raise 'Must configure Consul::KV' unless Consul::KV.config
10
+ @http = Net::HTTP.new KV.config.consul_host, KV.config.consul_port
11
+ end
12
+
13
+ def put(key, value)
14
+ response = standard_request :Put, key, false do |x|
15
+ x.body = value
16
+ end
17
+ end
18
+
19
+ def get(key, recurse = false)
20
+ response = standard_request :Get, key, recurse
21
+ end
22
+
23
+ def delete(key, recurse = false)
24
+ response = standard_request :Delete, key, recurse
25
+ end
26
+
27
+ protected
28
+
29
+ def standard_request(verb, key, recurse)
30
+ key = normalize_key key, recurse
31
+ request = Net::HTTP.const_get(verb).new(key)
32
+ yield(request) if block_given?
33
+ http.request(request)
34
+ end
35
+
36
+ def normalize_key(key, recurse)
37
+ key = "#{KV.config.consul_prefix}/#{key}"
38
+ key = "#{key}?recurse" if recurse
39
+ key
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,58 @@
1
+ require 'json'
2
+ require 'base64'
3
+
4
+ module Consul
5
+ module KV
6
+ class Store
7
+ CannotReadError = Class.new(StandardError)
8
+ CannotWriteError = Class.new(StandardError)
9
+ CannotDeleteError = Class.new(StandardError)
10
+
11
+ def initialize
12
+ @mutex = Mutex.new
13
+ @conn = Connection.new
14
+ end
15
+
16
+ def get(key)
17
+ resp = nil
18
+ @mutex.synchronize do
19
+ resp = conn.get key, true
20
+ end
21
+ return nil if resp.kind_of?(Net::HTTPNotFound)
22
+ raise CannotReadError unless resp.kind_of?(Net::HTTPSuccess)
23
+ json = JSON.parse(resp.body)
24
+ if json.size > 1 || json.first['Key'] != key
25
+ json.each_with_object({}) do |item, hsh|
26
+ hsh[item['Key'].gsub(/\A#{key}\//, '')] = Base64.decode64(item['Value'])
27
+ end
28
+ else
29
+ Base64.decode64 json.first['Value']
30
+ end
31
+ end
32
+ alias :[] :get
33
+
34
+ def put(key, value)
35
+ resp = nil
36
+ @mutex.synchronize do
37
+ resp = conn.put key, value
38
+ end
39
+ raise CannotWriteError unless resp.kind_of?(Net::HTTPSuccess)
40
+ get key
41
+ end
42
+ alias :[]= :put
43
+
44
+ def delete(key)
45
+ resp = nil
46
+ @mutex.synchronize do
47
+ resp = conn.delete key, true
48
+ end
49
+ raise CannotDeleteError unless resp.kind_of?(Net::HTTPSuccess)
50
+ end
51
+
52
+ private
53
+
54
+ attr_reader :conn
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ module Consul
2
+ module KV
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/lib/consul/kv.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'consul/kv/version'
2
+ require 'consul/kv/configuration'
3
+ require 'consul/kv/connection'
4
+ require 'consul/kv/store'
5
+
6
+ module Consul
7
+ module KV
8
+ class << self
9
+ attr_accessor :config
10
+
11
+ def configure(&block)
12
+ self.config ||= Configuration.new
13
+ yield(config)
14
+ end
15
+ end
16
+ end
17
+ end
data/lib/consul-kv.rb ADDED
@@ -0,0 +1 @@
1
+ require 'consul/kv'
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: consul-kv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - JGW Maxwell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-27 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Simple wrapper around the Consul KV store.
42
+ email:
43
+ - john.maxwell@boardintelligence.co.uk
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".editorconfig"
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - consul-kv.gemspec
55
+ - lib/consul-kv.rb
56
+ - lib/consul/kv.rb
57
+ - lib/consul/kv/configuration.rb
58
+ - lib/consul/kv/connection.rb
59
+ - lib/consul/kv/store.rb
60
+ - lib/consul/kv/version.rb
61
+ homepage: https://github.com/boardiq/consul-kv
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.4.1
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Simple wrapper around the Consul KV store.
85
+ test_files: []