unleash 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e17e0aeb7e67031de9d1088f574059c27e897f7a4d1e14106274f4b2aed5b14
4
- data.tar.gz: c12cf65f061e004f8b040b4bffa09c1515d432d678ed8e12d4baabf5ef36ba5c
3
+ metadata.gz: a27b367d8f33a853c46e239a81c0366c8d07d335b3faf58049a63412bec87e26
4
+ data.tar.gz: a1cfc2279f3a5d8859e482beaecb11b39f65dff4f903bb62329e48e3bb718df8
5
5
  SHA512:
6
- metadata.gz: d76a97471f879db4a8458f916833923e31e6ea2b671d5d9421122bf54275ca27183ee18b064eae61e7a406b268d6a4c58cdc8ac2b617f00d67dc7c2015c0ceff
7
- data.tar.gz: ed965bd8bb3c3d561e7eb93a0c44f91f9186fddb7b2932da025bc571ffdf3a4d9691a41a112375df51a01f19a22b81c4c1b849eccf00ba6ed5baed45020c6393
6
+ metadata.gz: e34aca4b62e43363279be75caf42b61f545a0b459329ba919bf74bc3c38ed3f54392107e0a90ed55f6e0a5b49e4a80d336475ca383a2fc97a9c402af45eab39f
7
+ data.tar.gz: 25e28af7767bed78cc54f3dd0390b8e4ddc5f987f7ff432d8e740646524483f55f1ca994a3cf59d089322b71007cc8828c8ffbe131847821d88715e35faa9b8a
@@ -0,0 +1,11 @@
1
+ Naming/PredicateName:
2
+ NameWhitelist:
3
+ - is_enabled?
4
+
5
+ Metrics/LineLength:
6
+ Max: 120
7
+
8
+ Style/RedundantSelf:
9
+ Enabled: false
10
+ Style/PreferredHashMethods:
11
+ Enabled: false
data/README.md CHANGED
@@ -12,7 +12,7 @@ Leverage the [Unleash Server](https://github.com/Unleash/unleash) for powerful f
12
12
  Add this line to your application's Gemfile:
13
13
 
14
14
  ```ruby
15
- gem 'unleash', '~> 0.1.1'
15
+ gem 'unleash', '~> 0.1.3'
16
16
  ```
17
17
 
18
18
  And then execute:
@@ -12,6 +12,7 @@ module Unleash
12
12
 
13
13
  STRATEGIES = Unleash::Strategy.constants
14
14
  .select { |c| Unleash::Strategy.const_get(c).is_a? Class }
15
+ .select { |c| !['NotImplemented', 'Base'].include?(c.to_s) }
15
16
  .map { |c|
16
17
  lowered_c = c.to_s
17
18
  lowered_c[0] = lowered_c[0].downcase
@@ -16,6 +16,7 @@ module Unleash
16
16
  Unleash.logger.level = Unleash.configuration.log_level
17
17
 
18
18
  Unleash.toggle_fetcher = Unleash::ToggleFetcher.new
19
+ register
19
20
 
20
21
  unless Unleash.configuration.disable_metrics
21
22
  Unleash.toggle_metrics = Unleash::Metrics.new
@@ -25,7 +26,6 @@ module Unleash
25
26
  Unleash.reporter.send
26
27
  end
27
28
  end
28
- register
29
29
  end
30
30
 
31
31
  def is_enabled?(feature, context = nil, default_value = false)
@@ -61,14 +61,23 @@ module Unleash
61
61
 
62
62
  uri = URI(Unleash.configuration.client_register_url)
63
63
  http = Net::HTTP.new(uri.host, uri.port)
64
+ http.use_ssl = true if uri.scheme == 'https'
64
65
  http.open_timeout = Unleash.configuration.timeout # in seconds
65
66
  http.read_timeout = Unleash.configuration.timeout # in seconds
66
- headers = {'Content-Type' => 'application/json'}
67
+
68
+ headers = (Unleash.configuration.custom_http_headers || {}).dup
69
+ headers['Content-Type'] = 'application/json'
70
+
67
71
  request = Net::HTTP::Post.new(uri.request_uri, headers)
68
72
  request.body = info.to_json
69
73
 
70
- # Send the request
71
- response = http.request(request)
74
+ # Send the request, if possible
75
+ begin
76
+ response = http.request(request)
77
+ rescue Exception => e
78
+ Unleash.logger.error "unable to register client with unleash server due to exception #{e.class}:'#{e}'."
79
+ Unleash.logger.error "stacktrace: #{e.backtrace}"
80
+ end
72
81
  end
73
82
  end
74
83
  end
@@ -4,6 +4,7 @@ require 'tmpdir'
4
4
  module Unleash
5
5
  class Configuration
6
6
  attr_accessor :url, :app_name, :instance_id,
7
+ :custom_http_headers,
7
8
  :disable_metrics, :timeout, :retry_limit,
8
9
  :refresh_interval, :metrics_interval,
9
10
  :backup_file, :logger, :log_level
@@ -13,6 +14,11 @@ module Unleash
13
14
  self.url = opts[:url] || nil
14
15
  self.instance_id = opts[:instance_id] || SecureRandom.uuid
15
16
 
17
+ if opts[:custom_http_headers].is_a?(Hash) || opts[:custom_http_headers].nil?
18
+ self.custom_http_headers = opts[:custom_http_headers] || {}
19
+ else
20
+ raise ArgumentError, "custom_http_headers must be a hash."
21
+ end
16
22
  self.disable_metrics = opts[:disable_metrics] || false
17
23
  self.refresh_interval = opts[:refresh_interval] || 15
18
24
  self.metrics_interval = opts[:metrics_interval] || 10
@@ -42,7 +48,11 @@ module Unleash
42
48
 
43
49
  def validate!
44
50
  if self.app_name.nil? or self.url.nil?
45
- raise ArgumentError, "URL and app_name are required"
51
+ raise ArgumentError, "URL and app_name are required parameters."
52
+ end
53
+
54
+ if ! self.custom_http_headers.is_a?(Hash)
55
+ raise ArgumentError, "custom_http_headers must be a hash."
46
56
  end
47
57
  end
48
58
 
@@ -40,9 +40,12 @@ module Unleash
40
40
 
41
41
  uri = URI(Unleash.configuration.client_metrics_url)
42
42
  http = Net::HTTP.new(uri.host, uri.port)
43
+ http.use_ssl = true if uri.scheme == 'https'
43
44
  http.open_timeout = Unleash.configuration.timeout # in seconds
44
45
  http.read_timeout = Unleash.configuration.timeout # in seconds
45
- headers = {'Content-Type' => 'application/json'}
46
+
47
+ headers = (Unleash.configuration.custom_http_headers || {}).dup
48
+ headers['Content-Type'] = 'application/json'
46
49
  request = Net::HTTP::Post.new(uri.request_uri, headers)
47
50
  request.body = generated_report.to_json
48
51
 
@@ -58,4 +61,4 @@ module Unleash
58
61
 
59
62
  end
60
63
  end
61
- end
64
+ end
@@ -24,7 +24,8 @@ module Unleash
24
24
  self.retry_count = 0
25
25
  rescue Exception => e
26
26
  self.retry_count += 1
27
- Unleash.logger.error "thread #{name} throwing exception (#{self.retry_count} of #{self.max_exceptions})", e
27
+ Unleash.logger.error "thread #{name} threw exception #{e.class}:'#{e}' (#{self.retry_count}/#{self.max_exceptions})"
28
+ Unleash.logger.error "stacktrace: #{e.backtrace}"
28
29
  end
29
30
 
30
31
  break if self.retry_count > self.max_exceptions
@@ -32,4 +33,4 @@ module Unleash
32
33
  end
33
34
  end
34
35
  end
35
- end
36
+ end
@@ -14,7 +14,7 @@ module Unleash
14
14
  end
15
15
 
16
16
  # need: :params[:hostnames]
17
- def is_enabled?(params = {}, context = nil)
17
+ def is_enabled?(params = {}, _context = nil)
18
18
  return false unless params.is_a?(Hash) && params.has_key?('hostnames')
19
19
 
20
20
  params['hostnames'].split(",").map(&:strip).map{|h| h.downcase }.include?(self.hostname)
@@ -8,7 +8,7 @@ module Unleash
8
8
  end
9
9
 
10
10
  # need: params['percentage']
11
- def is_enabled?(params = {}, context = nil)
11
+ def is_enabled?(params = {}, _context = nil)
12
12
  return false unless params.is_a?(Hash) && params.has_key?('percentage')
13
13
 
14
14
  begin
@@ -17,9 +17,7 @@ module Unleash
17
17
  return false
18
18
  end
19
19
 
20
- randomNumber = Random.rand(100) + 1
21
-
22
- (percentage >= randomNumber)
20
+ (percentage >= Random.rand(1..100))
23
21
  end
24
22
  end
25
23
  end
@@ -11,7 +11,7 @@ module Unleash
11
11
  return false unless params.fetch('ips', nil).is_a? String
12
12
  return false unless context.class.name == 'Unleash::Context'
13
13
 
14
- params['ips'].split(',').map(&:strip).include?( context.remote_address )
14
+ params['ips'].split(',').map(&:strip).include?(context.remote_address)
15
15
  end
16
16
  end
17
17
  end
@@ -22,7 +22,6 @@ module Unleash
22
22
  rescue Exception => e
23
23
  Unleash.logger.warn "ToggleFetcher was unable to fetch from the network, attempting to read from backup file."
24
24
  read!
25
- raise e
26
25
  end
27
26
 
28
27
  # once we have initialized, start the fetcher loop
@@ -39,16 +38,22 @@ module Unleash
39
38
  end
40
39
 
41
40
  # rename to refresh_from_server! ??
41
+ # TODO: should simplify by moving uri / http initialization to class initialization
42
42
  def fetch
43
43
  Unleash.logger.debug "fetch()"
44
44
  Unleash.logger.debug "ETag: #{self.etag}" unless self.etag.nil?
45
45
 
46
46
  uri = URI(Unleash.configuration.fetch_toggles_url)
47
47
  http = Net::HTTP.new(uri.host, uri.port)
48
+ http.use_ssl = true if uri.scheme == 'https'
48
49
  http.open_timeout = Unleash.configuration.timeout # in seconds
49
50
  http.read_timeout = Unleash.configuration.timeout # in seconds
50
- request = Net::HTTP::Get.new(uri.request_uri)
51
- request['If-None-Match'] = self.etag unless self.etag.nil?
51
+
52
+ headers = (Unleash.configuration.custom_http_headers || {}).dup
53
+ headers['Content-Type'] = 'application/json'
54
+ headers['If-None-Match'] = self.etag unless self.etag.nil?
55
+
56
+ request = Net::HTTP::Get.new(uri.request_uri, headers)
52
57
 
53
58
  response = http.request(request)
54
59
 
@@ -110,7 +115,8 @@ module Unleash
110
115
  end
111
116
  rescue Exception => e
112
117
  # This is not really the end of the world. Swallowing the exception.
113
- Unleash.logger.error "Unable to save backup file."
118
+ Unleash.logger.error "Unable to save backup file. Exception thrown #{e.class}:'#{e}'"
119
+ Unleash.logger.error "stacktrace: #{e.backtrace}"
114
120
  ensure
115
121
  file.close unless file.nil?
116
122
  end
@@ -129,16 +135,18 @@ module Unleash
129
135
 
130
136
  backup_as_hash = JSON.parse(line_cache)
131
137
  synchronize_with_local_cache!(backup_as_hash)
138
+ update_client!
132
139
 
133
140
  rescue IOError => e
134
141
  Unleash.logger.error "Unable to read the backup_file."
135
142
  rescue JSON::ParserError => e
136
143
  Unleash.logger.error "Unable to parse JSON from existing backup_file."
137
144
  rescue Exception => e
138
- Unleash.logger.error "Unable to extract valid data from backup_file."
145
+ Unleash.logger.error "Unable to extract valid data from backup_file. Exception thrown #{e.class}:'#{e}'"
146
+ Unleash.logger.error "stacktrace: #{e.backtrace}"
139
147
  ensure
140
148
  file.close unless file.nil?
141
149
  end
142
150
  end
143
151
  end
144
- end
152
+ end
@@ -1,3 +1,3 @@
1
1
  module Unleash
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency "bundler", "~> 1.14"
29
29
  spec.add_development_dependency "rake", "~> 10.0"
30
30
  spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "webmock", "~> 3.0"
31
32
  spec.add_development_dependency "coveralls"
32
33
 
33
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unleash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Renato Arruda
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-03 00:00:00.000000000 Z
11
+ date: 2018-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: murmurhash3
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: coveralls
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -91,6 +105,7 @@ extra_rdoc_files: []
91
105
  files:
92
106
  - ".gitignore"
93
107
  - ".rspec"
108
+ - ".rubocop.yml"
94
109
  - ".travis.yml"
95
110
  - Gemfile
96
111
  - LICENSE
@@ -116,7 +131,6 @@ files:
116
131
  - lib/unleash/strategy/gradual_rollout_sessionid.rb
117
132
  - lib/unleash/strategy/gradual_rollout_userid.rb
118
133
  - lib/unleash/strategy/remote_address.rb
119
- - lib/unleash/strategy/unknown.rb
120
134
  - lib/unleash/strategy/user_with_id.rb
121
135
  - lib/unleash/strategy/util.rb
122
136
  - lib/unleash/toggle_fetcher.rb
@@ -1,13 +0,0 @@
1
- module Unleash
2
- module Strategy
3
- class Unknown < Base
4
- def name
5
- 'unknown'
6
- end
7
-
8
- def is_enabled?(params = {})
9
- false
10
- end
11
- end
12
- end
13
- end