grenache-ruby-base 0.2.9 → 0.2.11
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 +4 -4
- data/lib/grenache/base.rb +10 -1
- data/lib/grenache/configurable.rb +29 -40
- data/lib/grenache/link.rb +3 -64
- data/lib/grenache/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 262059ec64c44d41d769ff4003848cfc33abc23f
|
4
|
+
data.tar.gz: da4517e84d0a5340f90c33176f436dec5e6e5f69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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)
|
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
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
33
|
-
|
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
|
43
|
-
|
26
|
+
def self.set_default &block
|
27
|
+
yield default
|
28
|
+
end
|
44
29
|
|
45
|
-
|
46
|
-
|
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
|
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
|
-
|
31
|
-
|
32
|
-
|
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
|
data/lib/grenache/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2017-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: eventmachine
|