config_hub-client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +60 -0
- data/LICENSE +21 -0
- data/README.md +33 -0
- data/Rakefile +6 -0
- data/config_hub-client.gemspec +40 -0
- data/lib/config_hub/client.rb +79 -0
- data/lib/config_hub/client/version.rb +5 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4b8169b5e906c21b7695d49bb9c831917f5f6922
|
4
|
+
data.tar.gz: a7ceed5f4468b65403e6f542734fd62415ab5061
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a1920c664eeb099169630c56ff247d5d04c0845d062df7c5f13be767555f54602e4acb214a014120aa92966109f84c8a511fe1145248463c23bcce91bf08cac8
|
7
|
+
data.tar.gz: 89b1ac3f3d67f3400ddc7239ef9d5e344950a1c5d30bc184ebae59728d83ae81f5c46be2efde61ea482f149afe55cadb0bcb59562205e6b959d297f065728b8e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
config_hub-client (0.1.0)
|
5
|
+
activerecord (>= 4.2.0)
|
6
|
+
faraday (>= 0.12.2)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (5.1.2)
|
12
|
+
activesupport (= 5.1.2)
|
13
|
+
activerecord (5.1.2)
|
14
|
+
activemodel (= 5.1.2)
|
15
|
+
activesupport (= 5.1.2)
|
16
|
+
arel (~> 8.0)
|
17
|
+
activesupport (5.1.2)
|
18
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
19
|
+
i18n (~> 0.7)
|
20
|
+
minitest (~> 5.1)
|
21
|
+
tzinfo (~> 1.1)
|
22
|
+
arel (8.0.0)
|
23
|
+
awesome_print (1.7.0)
|
24
|
+
concurrent-ruby (1.0.5)
|
25
|
+
diff-lcs (1.3)
|
26
|
+
faraday (0.12.2)
|
27
|
+
multipart-post (>= 1.2, < 3)
|
28
|
+
i18n (0.8.6)
|
29
|
+
minitest (5.10.3)
|
30
|
+
multipart-post (2.0.0)
|
31
|
+
rake (10.4.2)
|
32
|
+
rspec (3.6.0)
|
33
|
+
rspec-core (~> 3.6.0)
|
34
|
+
rspec-expectations (~> 3.6.0)
|
35
|
+
rspec-mocks (~> 3.6.0)
|
36
|
+
rspec-core (3.6.0)
|
37
|
+
rspec-support (~> 3.6.0)
|
38
|
+
rspec-expectations (3.6.0)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.6.0)
|
41
|
+
rspec-mocks (3.6.0)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.6.0)
|
44
|
+
rspec-support (3.6.0)
|
45
|
+
thread_safe (0.3.6)
|
46
|
+
tzinfo (1.2.3)
|
47
|
+
thread_safe (~> 0.1)
|
48
|
+
|
49
|
+
PLATFORMS
|
50
|
+
ruby
|
51
|
+
|
52
|
+
DEPENDENCIES
|
53
|
+
awesome_print
|
54
|
+
bundler (~> 1.15)
|
55
|
+
config_hub-client!
|
56
|
+
rake (~> 10.0)
|
57
|
+
rspec (~> 3.0)
|
58
|
+
|
59
|
+
BUNDLED WITH
|
60
|
+
1.15.3
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2017 Vineti, Inc.
|
2
|
+
|
3
|
+
MIT License
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Ruby ConfigHub client
|
2
|
+
|
3
|
+
### Installation
|
4
|
+
Add this to your Gemfile:
|
5
|
+
```ruby
|
6
|
+
gem 'config_hub-client'
|
7
|
+
```
|
8
|
+
then `bundle install`
|
9
|
+
|
10
|
+
Or install globally:
|
11
|
+
|
12
|
+
`gem install config_hub-client`
|
13
|
+
|
14
|
+
### Setup
|
15
|
+
```ruby
|
16
|
+
# create a client instance
|
17
|
+
client = ConfigHub::Client.new(
|
18
|
+
'https://config.example.com',
|
19
|
+
'your-confighub-token',
|
20
|
+
'context1;context2;context3'
|
21
|
+
)
|
22
|
+
|
23
|
+
# request config for your context from the server and store it locally
|
24
|
+
# you can call it again to refresh the data
|
25
|
+
client.pull
|
26
|
+
```
|
27
|
+
|
28
|
+
### Usage
|
29
|
+
```ruby
|
30
|
+
# get a configuration value from local cache
|
31
|
+
# does not request data from the server
|
32
|
+
value = client.fetch('your.config.key') { 'default value' }
|
33
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "config_hub/client/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "config_hub-client"
|
8
|
+
spec.version = ConfigHub::Client::VERSION
|
9
|
+
spec.authors = ["Jeremy Dunn"]
|
10
|
+
spec.email = ["jeremy.dunn@vineti.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple Ruby client for ConfigHub}
|
13
|
+
spec.homepage = "https://github.com/vinetiworks/config_hub-client"
|
14
|
+
|
15
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
16
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
17
|
+
if spec.respond_to?(:metadata)
|
18
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
19
|
+
else
|
20
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
21
|
+
"public gem pushes."
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
f.match(%r{^(test|spec|features)/})
|
26
|
+
end
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_dependency "faraday", ">= 0.12.2"
|
32
|
+
spec.add_dependency "activerecord", ">= 4.2.0"
|
33
|
+
# spec.add_dependency "pg", "~> 0.21"
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
38
|
+
spec.add_development_dependency "awesome_print"
|
39
|
+
# spec.add_development_dependency "config"
|
40
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_support/core_ext/string'
|
4
|
+
require 'active_support/json/encoding'
|
5
|
+
|
6
|
+
module ConfigHub
|
7
|
+
class Client
|
8
|
+
def initialize(server_url, token, context, options = {})
|
9
|
+
@token = token
|
10
|
+
@context = context
|
11
|
+
@options = options
|
12
|
+
env = options[:environment] || defined?(Rails) ? Rails.env : 'development'
|
13
|
+
faraday_opts = options[:faraday] || {}
|
14
|
+
@conn = Faraday.new(server_url, ssl: {verify: env == 'production'}.merge(faraday_opts), headers: headers)
|
15
|
+
end
|
16
|
+
|
17
|
+
def pull
|
18
|
+
@data = retrieve_remote_config
|
19
|
+
end
|
20
|
+
|
21
|
+
def config_pulled?
|
22
|
+
!@data.nil?
|
23
|
+
end
|
24
|
+
|
25
|
+
def fetch(key)
|
26
|
+
if @data
|
27
|
+
val = @data.dig('properties', key.to_s, 'val')
|
28
|
+
if val.nil?
|
29
|
+
if block_given? && !@data['properties'].key?(key.to_s)
|
30
|
+
yield
|
31
|
+
end
|
32
|
+
else
|
33
|
+
val
|
34
|
+
end
|
35
|
+
else
|
36
|
+
raise ConfigNotPulledError
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def retrieve_remote_config
|
43
|
+
res = @conn.get('/rest/pull')
|
44
|
+
if res.status == 200
|
45
|
+
JSON.parse(res.body)
|
46
|
+
else
|
47
|
+
raise RequestError.new('Could not pull config', res)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def headers
|
52
|
+
header_format({
|
53
|
+
client_token: @token,
|
54
|
+
context: @context
|
55
|
+
}.merge(@options[:headers] || {}))
|
56
|
+
end
|
57
|
+
|
58
|
+
def header_format(hash)
|
59
|
+
hash.map { |k, v| [k.to_s.titleize.tr(' ', '-'), v] }.to_h
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class RequestError < StandardError
|
64
|
+
def initialize(message, response)
|
65
|
+
@message = message
|
66
|
+
@reason = "#{response.reason_phrase} - #{response.headers['etag']}"
|
67
|
+
end
|
68
|
+
|
69
|
+
def message
|
70
|
+
"#{@message}: #{@reason}"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class ConfigNotPulledError < StandardError
|
75
|
+
def message
|
76
|
+
'Configuration was not loaded from ConfigHub server (use .pull)'
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: config_hub-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeremy Dunn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.12.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.12.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.2.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.2.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.15'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.15'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: awesome_print
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- jeremy.dunn@vineti.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- Gemfile
|
106
|
+
- Gemfile.lock
|
107
|
+
- LICENSE
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- config_hub-client.gemspec
|
111
|
+
- lib/config_hub/client.rb
|
112
|
+
- lib/config_hub/client/version.rb
|
113
|
+
homepage: https://github.com/vinetiworks/config_hub-client
|
114
|
+
licenses: []
|
115
|
+
metadata:
|
116
|
+
allowed_push_host: https://rubygems.org
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.5.1
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: Simple Ruby client for ConfigHub
|
137
|
+
test_files: []
|