grenache-ruby-base 0.2.9 → 0.2.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c584733c4084dbb644918ed0b484941df6f579dd
4
- data.tar.gz: d2e816d767f7d3162c01b5fd89a349251500b8a7
3
+ metadata.gz: 262059ec64c44d41d769ff4003848cfc33abc23f
4
+ data.tar.gz: da4517e84d0a5340f90c33176f436dec5e6e5f69
5
5
  SHA512:
6
- metadata.gz: d484d57baee1bcffdb298489905a7cb18b544a85d4bf0a21722ebeba379ed0fc2cc682b627eaea6e5a042669bf47411a53283f3edfe14bae31c57e1da1562e06
7
- data.tar.gz: 1f200b0bf1633669e4cf066968f0dbe2d73b5b80f2b1f64460a7bcc058acc48d3478b69b47437ed821a5c2db4549d68a400729b57fc64376433ceff7e9d80bef
6
+ metadata.gz: d97d7cd9e20382f1367c85e6aa1307c0e78792e6f0e6f48594763f5d0109dcbcd5cd6f95853efbadf4a42f03e0330e73c6f892543f131262d7c1d7d1810206fa
7
+ data.tar.gz: d84e9c844a2bff695f005898e220ce52521e904cb2ea3ed77a727b65faee9f6ea0038247e91e7856854b50b25b6f04b4cf88817ddb310716ca87fe6f5689881b
data/lib/grenache/base.rb CHANGED
@@ -2,9 +2,18 @@ module Grenache
2
2
  class Base
3
3
  include Grenache::Configurable
4
4
 
5
+ # Set default configuration values for BASE
6
+ default_conf do |conf|
7
+ conf.grape_address = "http://127.0.0.1:40001/"
8
+ conf.auto_announce = true
9
+ conf.timeout = 5
10
+ conf.auto_announce_interval = 5
11
+ conf.service_host = "0.0.0.0"
12
+ end
13
+
5
14
  # Initialize can accept custom configuration parameters
6
15
  def initialize(params = {})
7
- @configuration = Configuration.new(params) unless params.empty?
16
+ @configuration = Configuration.new(params)
8
17
  end
9
18
 
10
19
  # Lookup for a specific service `key`
@@ -1,53 +1,36 @@
1
1
  module Grenache
2
+ class BaseConfiguration
3
+ def values
4
+ @values ||= {}
5
+ end
2
6
 
3
- # Encapsulate Configuration parameters
4
- class Configuration
5
- # grape configuration
6
- attr_accessor :grape_address, :timeout
7
-
8
- # service configuration parameters
9
- attr_accessor :service_timeout, :auto_announce_interval, :auto_announce, :service_host
7
+ def method_missing(name, *args, &block)
8
+ if name[-1] == "="
9
+ values[name[0,name.size-1]] = args.first
10
+ else
11
+ values[name.to_s]
12
+ end
13
+ end
14
+ end
10
15
 
11
- # service SSL specific configuration
12
- # Cert is supposed to be PKCS12
13
- attr_accessor :key, :cert_pem, :ca, :reject_unauthorized, :verify_mode
16
+ class Configuration <BaseConfiguration
14
17
 
15
- # Initialize default values
16
18
  def initialize(params = {})
17
- set_val :grape_address, params, "http://127.0.0.1:40001/"
18
- set_val :timeout, params, 5
19
-
20
- set_val :auto_announce_interval, params, 5
21
- set_bool :auto_announce, params, true
22
- set_val :service_timeout, params, 5
23
- set_val :service_host, params, "0.0.0.0"
24
-
25
- set_val :key, params, nil
26
- set_val :cert_pem, params, nil
27
- set_val :ca, params, nil
28
- set_val :reject_unauthorized, params, nil
29
- set_val :verify_mode, params, Grenache::SSL_VERIFY_PEER
30
- end
19
+ @values = self.class.default.values
31
20
 
32
- private
33
- def set_bool(name, params, default)
34
- method = "#{name}=".to_sym
35
- if params[name].nil?
36
- send(method, default)
37
- else
38
- send(method, params[name])
21
+ params.keys.each do |k|
22
+ @values[k.to_s] = params[k]
39
23
  end
40
24
  end
41
25
 
42
- def set_val(name, params, default)
43
- method = "#{name}=".to_sym
26
+ def self.set_default &block
27
+ yield default
28
+ end
44
29
 
45
- if params[name]
46
- send(method, params[name])
47
- else
48
- send(method, default)
49
- end
30
+ def self.default
31
+ @defaults ||= BaseConfiguration.new
50
32
  end
33
+
51
34
  end
52
35
 
53
36
  # Configuration helpers
@@ -56,8 +39,9 @@ module Grenache
56
39
  base.extend(ClassMethods)
57
40
  end
58
41
 
42
+ # Instance configuration, can be altered indipendently
59
43
  def config
60
- @configuration || self.class.config
44
+ @configuration ||= Configuration.new
61
45
  end
62
46
 
63
47
  module ClassMethods
@@ -65,9 +49,14 @@ module Grenache
65
49
  yield config
66
50
  end
67
51
 
52
+ # Class configuration
68
53
  def config
69
54
  @configuration ||= Configuration.new
70
55
  end
56
+
57
+ def default_conf &block
58
+ Grenache::Configuration.set_default &block
59
+ end
71
60
  end
72
61
  end
73
62
  end
data/lib/grenache/link.rb CHANGED
@@ -7,57 +7,19 @@ module Grenache
7
7
  @config = config
8
8
  end
9
9
 
10
- # Connect to grape
11
- def connect
12
- unless connected?
13
- ws_connect
14
- end
15
- end
16
-
17
- # Return true if it's connected to Grape
18
- def connected?
19
- @connected
20
- end
21
-
22
- # Disconnect from grape
23
- def disconnect
24
- @ws.close
25
- @ws = nil
26
- end
27
-
28
10
  # Send a message to grape
29
11
  def send(type, payload, opts = {}, &block)
30
- if http?
31
- res = http_send type, Oj.dump({"rid" => uuid, "data" => payload})
32
- block.call(res) if block
33
- res
34
- else
35
- m = GrapeMessage.new(type,payload,opts, &block)
36
- messages[m.rid] = m
37
- ws_send m.to_json
38
- end
12
+ res = http_send type, Oj.dump({"rid" => uuid, "data" => payload})
13
+ block.call(res) if block
14
+ res
39
15
  end
40
16
 
41
17
  private
42
18
 
43
- def messages
44
- @messages ||= {}
45
- end
46
-
47
19
  def grape_url
48
20
  @grape_url ||= @config.grape_address
49
21
  end
50
22
 
51
- def http?
52
- grape_url.start_with? "http"
53
- end
54
-
55
- def ws_send(payload)
56
- ws_connect unless connected?
57
- ensure
58
- @ws.send(payload)
59
- end
60
-
61
23
  def http_send(type, payload)
62
24
  url = grape_url + type
63
25
  options = {
@@ -72,29 +34,6 @@ module Grenache
72
34
  end
73
35
  end
74
36
 
75
- def ws_connect
76
- @ws = Faye::WebSocket::Client.new(grape_url)
77
- @ws.on(:open, method(:on_open))
78
- @ws.on(:message, method(:on_message))
79
- @ws.on(:close, method(:on_close))
80
- end
81
-
82
- def on_open(ev)
83
- @connected = true
84
- end
85
-
86
- def on_message(ev)
87
- msg = Oj.load(ev.data)
88
- if req = messages[msg[0]]
89
- messages.delete(msg[0])
90
- req.yield(msg[1]) if req.block_given?
91
- end
92
- end
93
-
94
- def on_close(ev)
95
- @connected = false
96
- end
97
-
98
37
  def uuid
99
38
  SecureRandom.uuid
100
39
  end
@@ -1,3 +1,3 @@
1
1
  module Grenache
2
- VERSION = '0.2.9'
2
+ VERSION = '0.2.11'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grenache-ruby-base
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bitfinex
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-21 00:00:00.000000000 Z
11
+ date: 2017-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine