clook 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dbfd09218f6f51578b6f64b039d95cb95f0b045d
4
+ data.tar.gz: 29043169335b2b53913c18fdd65929586b5212f0
5
+ SHA512:
6
+ metadata.gz: ca7a9b0376612534de98cd9dc22d8dbf8d7405af3aa9a583f0e54755bc80e213eb0659a5b76dc41774569b7ca0ff343007336014daabf17e2c0861a23c3a4a7a
7
+ data.tar.gz: c3b8a6483b91163c57b319505801194b5619b6f107f88b1dd5ea6eaecebb3b9ee2f0f651408d78fee713e5d252263bf9df23c8f8545807a59d598a2bc93ff592
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in clook.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
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.
@@ -0,0 +1,11 @@
1
+ .PHONY: test consul env
2
+
3
+ env:
4
+ eval $$(boot2docker shellinit)
5
+
6
+ test: env
7
+ docker run --rm -it -v $$(pwd):/home ruby:2.2 /bin/bash
8
+
9
+ consul: env
10
+ docker rm consul
11
+ docker run -d --publish 8500:8500 --publish 53:53 --publish 53:53/udp --name consul progrium/consul -server -bootstrap -ui-dir /ui
@@ -0,0 +1,66 @@
1
+ # Clook
2
+
3
+ Configurations made easy.
4
+
5
+ Fetch configurations from several backends using a common interface.
6
+
7
+ Backends supported:
8
+ * environment
9
+ * json
10
+ * yaml
11
+ * consul
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'clook'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install clook
28
+
29
+ ## Usage
30
+
31
+ Load a backend:
32
+
33
+ ```ruby
34
+ Clook.load_json(<filepath>)
35
+ Clook.load_yaml(<filepath>)
36
+ Clook.load_consul("https://<consul>:8500")
37
+
38
+ Clook.fetch("hostname")
39
+ ```
40
+
41
+
42
+ Change priority:
43
+
44
+ ```ruby
45
+ Clook.configure do |config|
46
+ config.order = [:consul, :json, :env]
47
+ end
48
+ ```
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it ( https://github.com/[my-github-username]/clook/fork )
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create a new Pull Request
63
+
64
+
65
+
66
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "clook"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'clook/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "clook"
8
+ spec.version = Clook::VERSION
9
+ spec.authors = ["José Coelho"]
10
+ spec.email = ["jose.alberto.coelho@gmail.com"]
11
+
12
+ spec.summary = %q{Fetch configurations, made easy}
13
+ spec.description = %q{Fetch configurations from several backends with a common interface}
14
+ spec.homepage = "https://github.com/jacoelho/clook"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "test-unit"
26
+ spec.add_development_dependency "webmock"
27
+ end
@@ -0,0 +1,55 @@
1
+ module Clook
2
+ require "clook/version"
3
+ require "clook/configuration"
4
+
5
+ @adapters = Hash.new { |h, k| h[k] = [] }
6
+
7
+ class << self
8
+ attr_writer :configuration
9
+ attr_accessor :adapters
10
+
11
+ def configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def configure
16
+ yield(configuration) if block_given?
17
+ end
18
+
19
+ def load(type, *args, &block)
20
+ require "clook/#{type.to_s}"
21
+
22
+ @adapter = const_get("#{type.to_s.capitalize}")
23
+ @adapter.send :initialize, *args, &block
24
+ @adapters[type.to_sym] << @adapter
25
+
26
+ unless @configuration.order.include? type.to_sym
27
+ @configuration.order << type.to_sym
28
+ end
29
+ self
30
+ rescue LoadError
31
+ raise "Unknown adapter #{type}"
32
+ end
33
+
34
+ def fetch(stuff, *args, &block)
35
+ value = nil
36
+ @configuration.order.each do |prio|
37
+ @adapters[prio].each do |adapter|
38
+ value ||= adapter.fetch(stuff, *args, &block)
39
+ end
40
+ end
41
+ value
42
+ end
43
+
44
+ def method_missing(method_sym, *args, &block)
45
+ if method_sym.to_s =~ /^load_(\w+)$/
46
+ Clook.load($1, *args, &block)
47
+ else
48
+ super
49
+ end
50
+ end
51
+
52
+ Clook.configuration
53
+ Clook.load("env")
54
+ end
55
+ end
@@ -0,0 +1,9 @@
1
+ module Clook
2
+ class Configuration
3
+ attr_accessor :order
4
+
5
+ def initialize(options = {})
6
+ @order = options[order] || []
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,96 @@
1
+ require "base64"
2
+ require "json"
3
+ require "uri"
4
+ require "net/http"
5
+ require "net/https"
6
+
7
+ # TODO search services too?
8
+ # /v1/catalog/service/<service>
9
+ # v1/catalog/nodes
10
+
11
+ module Clook
12
+ class Consul
13
+ class << self
14
+ DEFAULT_HEADERS = {
15
+ "Content-Type" => "application/json",
16
+ "Accept" => "application/json",
17
+ }
18
+
19
+ def initialize(consul="http://192.168.59.103:8500")
20
+ @consul ||= consul
21
+ end
22
+
23
+ def fetch(stuff)
24
+ uri = URI.parse(@consul)
25
+ http = Net::HTTP.new(uri.host, uri.port)
26
+ request = Net::HTTP::Get.new("/v1/kv/#{stuff}?recurse")
27
+
28
+ DEFAULT_HEADERS.each do |key, value|
29
+ request.add_field(key, value)
30
+ end
31
+
32
+ response = http.request(request)
33
+
34
+ if response.body && (response.content_type || '').include?("json")
35
+ data = JSON.parse(response.body)
36
+ end
37
+
38
+ parse(data) unless data.nil?
39
+ end
40
+
41
+ def parse(data)
42
+ results = Array.new
43
+ data.map do |item|
44
+ if item["Value"]
45
+ value = Base64.decode64(item["Value"])
46
+ results << (item["Key"].split('/').reverse.reduce(value){ |r, e| {e.to_sym => r} })
47
+ end
48
+ end
49
+
50
+ values = squeeze_array(results)
51
+ results.count == 1 ? find_value(values) : values
52
+ end
53
+
54
+ def merge(array)
55
+ array.group_by(&:keys).map{ |k, v| { k.first => v.flat_map(&:values).reduce(&:merge) } }.reduce(&:merge)
56
+ end
57
+
58
+ def squeeze_array(ary)
59
+ ary
60
+ .group_by(&:keys)
61
+ .map do |k, v|
62
+ flatten_values = v.flat_map(&:values)
63
+
64
+ {}.tap do |h|
65
+ h[k.first] = if flatten_values[0].is_a?(Hash)
66
+ squeeze_array(flatten_values)
67
+ else
68
+ flatten_values[0]
69
+ end
70
+ end
71
+ end
72
+ .reduce(&:merge)
73
+ end
74
+
75
+ def find_value(hash)
76
+ result = nil
77
+ hash.each do |key, value|
78
+ if hash[key].is_a? Hash
79
+ result = find_value(value)
80
+ else
81
+ result = value
82
+ end
83
+ end
84
+ result
85
+ end
86
+
87
+ def flat_hash(input)
88
+ Hash[*input.map { |key, value|
89
+ value.is_a?(Hash) ?
90
+ make_hash_one_dimensional(value).map { |nested_key, nested_value| ["#{key}/#{nested_key}",nested_value] } :
91
+ [key, value]
92
+ }.flatten]
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,12 @@
1
+ module Clook
2
+ class Env
3
+ class << self
4
+ def initialize
5
+ end
6
+
7
+ def fetch value
8
+ ENV[value.upcase]
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ module Clook
2
+ class Json
3
+ require "json"
4
+
5
+ class << self
6
+ def initialize path
7
+ @path = path
8
+ end
9
+
10
+ def fetch value
11
+ file = File.read(@path)
12
+ data = JSON.parse(file)
13
+ data[value]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Clook
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ module Clook
2
+ class Yaml
3
+ require "yaml"
4
+
5
+ class << self
6
+ def initialize path
7
+ @path = path
8
+ end
9
+
10
+ def fetch value
11
+ file = File.read(@path)
12
+ data = YAML::load(file)
13
+ data[value]
14
+ end
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - José Coelho
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-22 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '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'
55
+ - !ruby/object:Gem::Dependency
56
+ name: test-unit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Fetch configurations from several backends with a common interface
84
+ email:
85
+ - jose.alberto.coelho@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - Makefile
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - clook.gemspec
102
+ - lib/clook.rb
103
+ - lib/clook/configuration.rb
104
+ - lib/clook/consul.rb
105
+ - lib/clook/env.rb
106
+ - lib/clook/json.rb
107
+ - lib/clook/version.rb
108
+ - lib/clook/yaml.rb
109
+ homepage: https://github.com/jacoelho/clook
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.2.3
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Fetch configurations, made easy
133
+ test_files: []