disco_client 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/disco_client.rb +71 -0
  3. metadata +58 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7121fbc0f23f8cab73ab2ee6764634d04327f94d
4
+ data.tar.gz: 3db0ea495fb6668e7a46019f6b4e7e46b447fe01
5
+ SHA512:
6
+ metadata.gz: 3cb1d898e692870c3d08090e736b5b5625e18bb8d4e72a7887787b17399e0fb6c3117bc04fbf5f596173dfc5a057dd9a1bbd911299805e6f61c3336cf829cc3a
7
+ data.tar.gz: 14dc5c20840c356c4d1adb945926404d9f723369bbd22018c9df90daedfd60e7434567cb96a4c1721b6fcaa7022c1cb64f3ec321fda061bdfc459fc7eb0875cd
@@ -0,0 +1,71 @@
1
+ require 'uri'
2
+ require 'json'
3
+ require 'httparty'
4
+
5
+ class DiscoClient
6
+ include HTTParty
7
+
8
+ headers 'Accept' => 'application/json'
9
+ headers 'Content-Type' => 'application/json'
10
+
11
+ DEFAULT_CONTAINER = {
12
+ id: ENV['CONTAINER_NAME'],
13
+ type: 'http',
14
+ port: 3000
15
+ }
16
+
17
+ def initialize(options = {})
18
+ @base_uri = options[:base_uri] || ENV['DISCO_BASE_URI']
19
+ end
20
+
21
+ def register(service_id, options = {})
22
+ body = DEFAULT_CONTAINER.merge(options)
23
+ body[:service] = { id: service_id };
24
+
25
+ @container = request('post', '1/containers', nil, body)
26
+ end
27
+
28
+ def get_services
29
+ request('get', '1/services')
30
+ end
31
+
32
+ def get_service(id)
33
+ URI.encode_www_form_component(id)
34
+ request('get', "1/services/#{id}")
35
+ end
36
+
37
+ def get_container(id = nil)
38
+ id = URI.encode_www_form_component(id || @container[:id])
39
+ request('get', "1/containers/#{id}")
40
+ end
41
+
42
+ def get_configuration(id = nil)
43
+ id = URI.encode_www_form_component(id || @container[:id])
44
+ request('get', "1/containers/#{id}/configuration")
45
+ end
46
+
47
+ private
48
+
49
+ def request(method, path, params = nil, body = nil)
50
+ uri = URI.join(@base_uri, path)
51
+
52
+ options = {}
53
+ options[:query] = params if params
54
+ options[:body] = JSON.generate(body) if body
55
+
56
+ res = self.class.send(method, uri, options)
57
+
58
+ # 1xx should be handled by httparty
59
+
60
+ # 2xx - success
61
+ if res.code < 300
62
+ return nil unless res.body && res.body.length
63
+ return JSON.parse(res.body, symbolize_names: true, create_additions: false)
64
+ end
65
+
66
+ # 3xx redirections are handled by httparty
67
+ # 304 is raised as an error
68
+
69
+ res.error!
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: disco_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Billingham
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.13.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.13.5
27
+ description:
28
+ email: james@jamesbillingham.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/disco_client.rb
34
+ homepage: https://github.com/cuvva/disco-client-ruby
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.4.6
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Ruby client for the Cuvva environment discovery API
58
+ test_files: []