consul-rb 0.0.2

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: 7aa41c5c7cdf1de482586293d03c6d636fa2e6fd
4
+ data.tar.gz: 127fa25ede589e931a54080852c8a8050c3d304b
5
+ SHA512:
6
+ metadata.gz: d794949c1c7df9bead7048ae1731886d22ef733224bd925467e55fd92413d11dbb4d3edc3360f4d8eda5ba6a59b4abfc79113581bdae905ccf06143aa7e4d5bf
7
+ data.tar.gz: 2063d204ccbea95806df55bca7f3da8c21b833ac52ec1b72456c58c7e992ac0844cae6e80fba61d33b47b28118dc66f70245b2032615f6f1cc13f6aa8c8f032a
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Supersonic Ltd.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/consul-rb.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require "consul/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "consul-rb"
8
+ s.version = Consul::Client::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["Alexander Piavlo"]
11
+ s.email = ["alex.p@supersonic.com"]
12
+ s.homepage = "https://github.com/SupersonicAds/consul-rb"
13
+ s.summary = "Consul HTTP Client"
14
+ s.description = "Consul HTTP Client Ruby Library"
15
+ s.license = 'MIT'
16
+ s.has_rdoc = false
17
+
18
+ s.add_development_dependency('rake-compiler', '~> 10')
19
+
20
+ s.files = Dir.glob("{lib}/**/*") + %w(consul-rb.gemspec LICENSE)
21
+ s.test_files = nil
22
+ s.require_paths = ['lib']
23
+ end
@@ -0,0 +1,16 @@
1
+ require 'consul/client/http'
2
+ require 'logger'
3
+
4
+ module Consul
5
+ module Client
6
+ def self.v1
7
+ V1.new
8
+ end
9
+
10
+ class V1
11
+ def http(host: "localhost", port: 8500, logger: Logger.new("/dev/null"))
12
+ HTTP.new(host: host, port: port, logger: logger)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,113 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'logger'
4
+
5
+ module Consul
6
+ module Client
7
+
8
+ NotFoundException = Class.new(StandardError)
9
+ ResponseException = Class.new(StandardError)
10
+ RpcErrorException = Class.new(StandardError)
11
+
12
+ class HTTP
13
+ def initialize(host:, port:, logger:)
14
+ @host = host
15
+ @port = port
16
+ @logger = logger
17
+ end
18
+
19
+ def get(request_uri)
20
+ url = base_uri + request_uri
21
+ logger.debug("GET #{url}")
22
+
23
+ uri = URI.parse(url)
24
+
25
+ response = http_request(:get, uri)
26
+
27
+ parse_body(response)
28
+ end
29
+
30
+ def exists?(request_uri)
31
+ url = base_uri + request_uri
32
+ logger.debug("GET #{url}")
33
+
34
+ uri = URI.parse(url)
35
+
36
+ begin
37
+ response = http_request(:get, uri)
38
+ rescue NotFoundException
39
+ return false
40
+ end
41
+ return true
42
+ end
43
+
44
+ def watch(request_uri, index: 0, wait: '60s', recurse: false)
45
+ url = base_uri + request_uri
46
+
47
+ uri = URI.parse(url)
48
+ uri.query ||= ""
49
+ uri.query += "&index=#{index}&wait=#{wait}"
50
+ uri.query += "&recurse" if recurse
51
+
52
+ while true do
53
+ begin
54
+ logger.debug("GET #{uri}")
55
+
56
+ response = http_request(:get, uri)
57
+ if response['x-consul-index'].to_i > index
58
+ return { index: response['x-consul-index'], body: parse_body(response) }
59
+ end
60
+ rescue RpcErrorException
61
+ logger.warn("Got 500 while watching #{uri}")
62
+ sleep 5
63
+ end
64
+ end
65
+ end
66
+
67
+ def put(request_uri, data = nil)
68
+ url = base_uri + request_uri
69
+ logger.debug("PUT #{url}")
70
+
71
+ uri = URI.parse(url)
72
+
73
+ response = http_request(:put, uri, request_uri.match('^/kv/') ? data : data.to_json)
74
+
75
+ parse_body(response)
76
+ end
77
+
78
+ attr_accessor :logger
79
+
80
+ protected
81
+
82
+ def base_uri
83
+ "http://#{@host}:#{@port}/v1"
84
+ end
85
+
86
+ def parse_body(response)
87
+ JSON.parse("[#{response.body}]")[0]
88
+ end
89
+
90
+ def http_request(method, uri, data = nil)
91
+ method = {
92
+ get: Net::HTTP::Get,
93
+ put: Net::HTTP::Put,
94
+ }.fetch(method)
95
+
96
+ http = Net::HTTP.new(uri.host, uri.port)
97
+ request = method.new(uri.request_uri)
98
+ request.body = data
99
+ response = http.request(request)
100
+
101
+ if response.code.to_i == 404
102
+ raise NotFoundException, "#{response.code} on #{uri}"
103
+ elsif response.code.to_i >= 500
104
+ raise RpcErrorException, "#{response.code} on #{uri}"
105
+ elsif response.code.to_i >= 400
106
+ raise ResponseException, "#{response.code} on #{uri}"
107
+ end
108
+
109
+ response
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,5 @@
1
+ module Consul
2
+ module Client
3
+ VERSION ||= '0.0.2'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: consul-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Piavlo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10'
27
+ description: Consul HTTP Client Ruby Library
28
+ email:
29
+ - alex.p@supersonic.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - consul-rb.gemspec
36
+ - lib/consul/client.rb
37
+ - lib/consul/client/http.rb
38
+ - lib/consul/version.rb
39
+ homepage: https://github.com/SupersonicAds/consul-rb
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.2.2
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Consul HTTP Client
63
+ test_files: []