savvy 0.1.1 → 0.2.0

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: 95186ba3a6494096fcac558c89fc868d8f790bde
4
- data.tar.gz: 598b24403bac739d12b0d3c0b5b3ef9db3e275e4
3
+ metadata.gz: 28fa85fba714b4b39fd3723a0e82d9e9f8cc738f
4
+ data.tar.gz: 62d432c2a84b8b971f22470e8e31b22956e712d1
5
5
  SHA512:
6
- metadata.gz: 4093c079265cfa65d1bdd34ed9111c86d360727a4a58d5664bf943ba0013bac696593a65d7dee8f9825f2d9fbe6719c67be920b9898d335bca9cf9d775c097fe
7
- data.tar.gz: 0aed5f4081e6f689ea71471bbc85d1758e91cc06f7958a9f498e2af9e0cf758c6b929fa858a54c7666150337cd767d9c0810419e9245b9ee8683d393c2ee25f8
6
+ metadata.gz: 36c6c67963c9becef1b00424d30ac738e6de263ba21bff7b9dda27d2b90a7944237ead68804e6a3e7a001775d2015588644c12faaff663d287b7749494388e74
7
+ data.tar.gz: 9d6df66f86c22c58e82fc24951fdf2e88e606ffc6344958f391b510a15147d9ba1dd8a57f1452d7962f0ec828c229c6b1e29c1a951d47a95747e3eefb4579808
@@ -3,7 +3,15 @@ dist: trusty
3
3
  cache: bundler
4
4
  language: ruby
5
5
  rvm:
6
- - 2.2
7
- - 2.3
8
- - 2.4.0
6
+ - 2.2
7
+ - 2.3
8
+ - 2.4.0
9
9
  before_install: gem install bundler -v 1.14.6
10
+ services:
11
+ - redis-server
12
+ notifications:
13
+ email:
14
+ on_success: never
15
+ on_failure: always
16
+ slack:
17
+ secure: d4aMjEF4rS1UGUilgyxObX/t1ZGJfrunNbZiDTssB2GjRvg1g6Z9sO3YrslK4BHlW+PcijDvH7eIhGheOgKA21LaPSnxlp/rqzPeIEh0yEZMmto2rPwecQicIjq0vu1C/EV/HPv8twzX65YpBYQHf9xZnX9y54J7QRrYpKIogJoEFsErwqOGvjQA4cWCfQQH3TdDrg+NdayMaWFiINdMNGqyIE1pxM80m2bT+nlE6o+YxOBd1lZZgh+3+Mm7r8PLjq3q7U8U3Avw+dS45PoEXQbnoKpdaXR5SUWRFyx+uH5itygiYb6F6rpPG9xuM7iUzz7PIJsRRkPO7tVXCPvlmGgWEiTFDEGc9Tsw9Kp739iGplWKtOBQzbrvf9mJfV/gViJdOpfpCOG5JCJWqTK8iSHe/m1LA36NOFll7tzd1NR6QBPLN4nb2y3bYjFdDlEh5N91ftIJAhkJEvg0+uMRZb4gd2ez4KxwzfGXSyYjS3HswBXmeCHuLFsEWyHaq5KjSdA3DfREUdU7B8PwP0sbEvTnzCKMnrUem4RJMwhNg8A+TtbXqLJsrxfjbKA0TkPNKpHqos74BJ+kh+ZvfnR7gD3yuG/WCXAIYzx4ET7OY97etpaqK3qVDrz/Bd1K/SEh4TtCE+lMkk6tFMtse6kUgg+tBzpA4mKkl0yVUoEMQfE=
@@ -9,6 +9,7 @@ require 'savvy/version'
9
9
  require 'savvy/configuration'
10
10
  require 'savvy/configuration_dsl'
11
11
  require 'savvy/environment_reader'
12
+ require 'savvy/error'
12
13
  require 'savvy/root_finder'
13
14
  require 'savvy/utility'
14
15
 
@@ -43,9 +43,15 @@ module Savvy
43
43
  load_from_file!
44
44
  end
45
45
 
46
+ # Build a namespace that takes environmental and application factors into account.
47
+ #
48
+ # @param [<String>] parts additional parts that will be suffixed after the generated namespace
49
+ # @param [String] prefix a component that will appear before the savvy-generated namepsace
50
+ # @param [String] separator what separates the components used in the namespace
46
51
  # @return [String]
47
- def build_namespace(*parts, separator: ?.)
52
+ def build_namespace(*parts, prefix: nil, separator: ?.)
48
53
  [
54
+ prefix,
49
55
  app_name,
50
56
  ( app_env if include_app_env? ),
51
57
  *parts
@@ -3,41 +3,156 @@ module Savvy
3
3
  class Redis
4
4
  DEFAULT_SEPARATOR = ?:
5
5
 
6
- # @!attribute [r] url
7
- # @return [String]
8
- attr_reader :url
6
+ PATH_PATTERN = %r{\A/(?<db>\d{1,2})(?:/(?<namespace_prefix>[^/]+))?}
7
+
8
+ # This is the namespace portion of a redis URI that
9
+ # is possibly set by the environment and will prefix
10
+ # the Savvy-generated namespace.
11
+ #
12
+ # @return [String, nil]
13
+ attr_reader :namespace_prefix
9
14
 
10
15
  # @param [Savvy::Config] config
11
16
  def initialize(config: Savvy.config)
12
17
  @config = config
13
- @url = read_url_from_config
18
+
19
+ @db = 0
20
+ @namespace_prefix = nil
21
+
22
+ parse_url!
23
+ end
24
+
25
+ # @param [<String>] parts
26
+ # @param [Boolean] without_namespace return a configuration hash *only* as defined
27
+ # @return [{Symbol => Object}]
28
+ def config_hash(*parts, without_namespace: false)
29
+ check_validity!
30
+
31
+ {
32
+ host: @host,
33
+ port: @port,
34
+ db: @db,
35
+ }.tap do |h|
36
+ ns = without_namespace ? @namespace_prefix : namespace(*parts)
37
+
38
+ h[:namespace] = ns unless ns.nil?
39
+ end
40
+ end
41
+
42
+ alias to_h config_hash
43
+
44
+ # @!attribute [r] db
45
+ # The database number used by redis.
46
+ # @return [Integer]
47
+ def db
48
+ check_validity!
49
+
50
+ @db
51
+ end
52
+
53
+ # @!attribute [r] host
54
+ # @return [String]
55
+ def host
56
+ check_validity!
57
+
58
+ @host
14
59
  end
15
60
 
16
61
  def namespace(*parts, separator: DEFAULT_SEPARATOR)
17
- @config.build_namespace(*parts, separator: separator)
62
+ check_validity!
63
+
64
+ @config.build_namespace(*parts, prefix: @namespace_prefix, separator: separator)
18
65
  end
19
66
 
67
+ # @param [<String>] parts parts to add to the namespace
68
+ # @return [String]
20
69
  def namespaced_url(*parts)
21
- "#{url}/#{namespace(*parts)}"
70
+ check_validity!
71
+
72
+ build_redis_url *parts
73
+ end
74
+
75
+ # @!attribute [r] port
76
+ # @return [Integer]
77
+ def port
78
+ check_validity!
79
+
80
+ @port
81
+ end
82
+
83
+ # The url as configured by the environment (sans namespace)
84
+ #
85
+ # @return [String]
86
+ def url
87
+ check_validity!
88
+
89
+ build_redis_url without_namespace: true
22
90
  end
23
91
 
24
92
  # @return [ConnectionPool]
25
93
  def build_connection_pool(*namespace_parts, size: 5, timeout: 5)
26
- raise 'Requires connection_pool gem' unless defined?(ConnectionPool)
94
+ raise Savvy::RedisError, 'Requires connection_pool gem' unless defined?(ConnectionPool)
27
95
 
28
96
  ::ConnectionPool.new size: size, timeout: timeout do
29
97
  build_connection(*namespace_parts)
30
98
  end
31
99
  end
32
100
 
101
+ # @return [Redis::Namespace]
33
102
  def build_connection(*namespace_parts)
34
- raise 'Requires redis-namespace gem' unless defined?(::Redis::Namespace)
103
+ raise Savvy::RedisError, 'Requires redis-namespace gem' unless defined?(::Redis::Namespace)
35
104
 
36
105
  ::Redis::Namespace.new(namespace(*namespace_parts), redis: ::Redis.new(url: url))
37
106
  end
38
107
 
108
+ def valid?
109
+ @error.nil?
110
+ end
111
+
39
112
  private
40
113
 
114
+ # @return [String]
115
+ def build_redis_url(*parts, without_namespace: false)
116
+ ns = without_namespace ? @namespace_prefix : namespace(*parts)
117
+
118
+ path = ?/ + [db, ns].compact.join(?/)
119
+
120
+ URI.join(@base_uri, path).to_s
121
+ end
122
+
123
+ def check_validity!
124
+ raise Savvy::RedisError, "Redis configuration is invalid: #{@error}", caller unless valid?
125
+ end
126
+
127
+ # @return [void]
128
+ def parse_url!
129
+ @provided_url = read_url_from_config.freeze
130
+
131
+ @parsed_url = URI.parse @provided_url
132
+
133
+ @host = @parsed_url.host
134
+ @port = @parsed_url.port
135
+
136
+ path_match = PATH_PATTERN.match @parsed_url.path
137
+
138
+ if path_match
139
+ captures =
140
+ if path_match.respond_to?(:named_captures)
141
+ path_match.named_captures
142
+ else
143
+ Hash[path_match.names.zip(path_match.captures)]
144
+ end
145
+
146
+ @db = Integer(captures['db'])
147
+
148
+ @namespace_prefix = captures['namespace_prefix']
149
+ end
150
+
151
+ @base_uri = URI::Generic.new 'redis', nil, @host, @port, nil, '', nil, nil, nil
152
+ rescue URI::InvalidURIError => e
153
+ @error = "could not parse redis URL (#{@provided_url})"
154
+ end
155
+
41
156
  def read_url_from_config
42
157
  @config.read_from_env *@config.redis_env_vars, fallback: @config.redis_default_url
43
158
  end
@@ -0,0 +1,5 @@
1
+ module Savvy
2
+ class Error < StandardError; end
3
+
4
+ class RedisError < Error; end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module Savvy
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: savvy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexa Grey
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-10 00:00:00.000000000 Z
11
+ date: 2017-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cleanroom
@@ -169,6 +169,7 @@ files:
169
169
  - lib/savvy/configurators/redis.rb
170
170
  - lib/savvy/configurators/sidekiq.rb
171
171
  - lib/savvy/environment_reader.rb
172
+ - lib/savvy/error.rb
172
173
  - lib/savvy/root_finder.rb
173
174
  - lib/savvy/templates/Savvyfile.rb.erb
174
175
  - lib/savvy/utility.rb
@@ -194,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
195
  version: '0'
195
196
  requirements: []
196
197
  rubyforge_project:
197
- rubygems_version: 2.6.8
198
+ rubygems_version: 2.4.5
198
199
  signing_key:
199
200
  specification_version: 4
200
201
  summary: ENV-backed configuration helper for Redis, Sidekiq, etc