flux_capacitor 0.1.3 → 0.1.4

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: 4f897fe1d5f357c010758842c3407fddbf9f7aa1
4
- data.tar.gz: 4d5d8cf4934e2429eec97d3071dbd6bc99da91b9
3
+ metadata.gz: 76a7dee4f3e4464adaebd86fe7a8a0af5f88700a
4
+ data.tar.gz: 98a96b818381b2707f80aabf9a1ce703665d3d74
5
5
  SHA512:
6
- metadata.gz: 8bd742e6e19b0d46e9ae0e8891a9c0ec59df0ea78629e51f488c09c736fe2f0160fa7fdb8f9ab93daaf9a1e85c83eba01ca5779f2a63dc2ba684eafe328f912c
7
- data.tar.gz: b7fef45c25100bc5448d432bcb2bd1e9c05dfdd3dffde97716d249c37e3843bce06bcea35887945cd75b1acd638e4d45eef686004caf167d4e73f16c9398707b
6
+ metadata.gz: b0f08d256f5d3519b0fec3956e4a8270e96d22fe5b56b4784c6dd72e14b22d763457f7ac5ce95db3a6dda4f0c6f150f47f39d16b60094f4579ea1ff0e1c8492c
7
+ data.tar.gz: 345fbe63eb956576ec934127ead9412c0a59ca20f0abc5d7c761b62240e31fdffecbd6ad639d10bba112f0a1f032b6054c303282d3ced35d1b4aa7737e6ffc02
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/flux_capacitor.svg)](https://badge.fury.io/rb/flux_capacitor)
2
+ [![security](https://hakiri.io/github/raphaeleidus/flux_capacitor/master.svg)](https://hakiri.io/github/raphaeleidus/flux_capacitor/master)
2
3
 
3
4
  # FluxCapacitor
4
5
 
@@ -25,13 +26,13 @@ Or install it yourself as:
25
26
  ```ruby
26
27
  require 'flux_capacitor'
27
28
 
28
- pivot = DateTime.parse('2017/08/14 00:00:00-000') # when do you want to start rolling out the feature
29
+ start = DateTime.parse('2017/08/14 00:00:00-000') # when do you want to start rolling out the feature
29
30
  oldest = MyModel.first.created_at # If you are using active record finding your oldest item is pretty easy
30
31
  # otherwise if you know the date of your first item, just use that
31
32
  end_point = DateTime.parse('2017/09/14') # The point where the feature is fully rolled out/safe to remove the Flux Capacitor.
32
33
  # This dictates how quickly the feature rolls out. If you are concerned about overloading a required service set this to farther in the future
33
34
 
34
- FEATURE_1_CAPACITOR = Flux::Capacitor.new(pivot, end_point, oldest)
35
+ FEATURE_1_CAPACITOR = Flux::Capacitor.new(start, end_point, oldest)
35
36
 
36
37
  def controller_method
37
38
  model = MyModel.find(params[:id])
@@ -46,15 +47,15 @@ end
46
47
  If your feature doesn't map well to something where you have a date for each piece of content you can still use flux capacitor. It can also take strings and distribute them evenly over your rollout period using the murmur3 hashing algorithm.
47
48
  ```ruby
48
49
  require 'flux_capacitor'
49
- pivot = DateTime.parse('2017/08/14 00:00:00-000') # when do you want to start rolling out the feature
50
+ start = DateTime.parse('2017/08/14 00:00:00-000') # when do you want to start rolling out the feature
50
51
  end_point = DateTime.parse('2017/09/14') # when do you want the rollout to finish
51
52
 
52
53
  # NOTE: We don't need an oldest date when using strings
53
- FEATURE_1_CAPACITOR = Flux::Capacitor.new(pivot, end_point)
54
+ FEATURE_1_CAPACITOR = Flux::Capacitor.new(start, end_point)
54
55
 
55
56
  def controller_method
56
57
  model = MyModel.find(params[:id])
57
- if FEATURE_1_CAPACITOR.travel_to?(model.uuid)
58
+ if FEATURE_1_CAPACITOR.travel_to?(model.uuid) # Any string will work here
58
59
  use_new_feature
59
60
  else
60
61
  use_old_feature
@@ -1,51 +1,3 @@
1
1
  require 'flux_capacitor/version'
2
- require 'date'
3
- require 'murmurhash3'
4
-
5
- module Flux
6
-
7
- class Capacitor
8
- attr_reader :pivot, :time_dilation
9
- def initialize (start_time, completion_target, oldest_target = Capacitor.oldest_string_time(start_time))
10
- @pivot = start_time
11
- time_to_complete = seconds_between(completion_target, pivot)
12
- dilation = seconds_between(pivot, oldest_target).to_f / time_to_complete.to_f
13
- @time_dilation = dilation
14
- end
15
-
16
- def limit
17
- diff = seconds_between(DateTime.now, pivot)
18
- datetime_minus_seconds(pivot, (diff * time_dilation).to_i)
19
- end
20
-
21
- def travel_to?(destination)
22
- destination = destination.is_a?(DateTime) ? destination : string_to_time(destination)
23
- limit < destination
24
- end
25
-
26
- def string_to_time(str)
27
- Capacitor.string_to_time(pivot, str)
28
- end
29
-
30
- def self.string_to_time(pivot, str)
31
- timestamp = pivot.strftime("%s").to_i - (MurmurHash3::V32.str_hexdigest(str).to_i(16)/8)
32
- DateTime.strptime(timestamp.to_s, "%s")
33
- end
34
-
35
- def self.oldest_string_time(pivot)
36
- timestamp = pivot.strftime("%s").to_i - ("ffffffff".to_i(16)/8)
37
- DateTime.strptime(timestamp.to_s, "%s")
38
- end
39
-
40
- private
41
-
42
- def seconds_between(date_time_1, date_time_2)
43
- date_time_1.strftime("%s").to_i - date_time_2.strftime("%s").to_i
44
- end
45
-
46
- def datetime_minus_seconds(datetime, seconds)
47
- ts = (datetime.strftime("%s").to_i - seconds).to_s
48
- DateTime.strptime(ts, "%s")
49
- end
50
- end
51
- end
2
+ require 'flux_capacitor/capacitor'
3
+ require 'flux_capacitor/testing'
@@ -0,0 +1,50 @@
1
+ require 'date'
2
+ require 'murmurhash3'
3
+
4
+ module Flux
5
+
6
+ class Capacitor
7
+ attr_reader :pivot, :time_dilation
8
+ def initialize (start_time, completion_target, oldest_target = Capacitor.oldest_string_time(start_time))
9
+ @pivot = start_time
10
+ time_to_complete = seconds_between(completion_target, pivot)
11
+ dilation = seconds_between(pivot, oldest_target).to_f / time_to_complete.to_f
12
+ @time_dilation = dilation
13
+ end
14
+
15
+ def limit
16
+ diff = seconds_between(DateTime.now, pivot)
17
+ datetime_minus_seconds(pivot, (diff * time_dilation).to_i)
18
+ end
19
+
20
+ def travel_to?(destination)
21
+ destination = destination.is_a?(DateTime) ? destination : string_to_time(destination)
22
+ limit < destination
23
+ end
24
+
25
+ def string_to_time(str)
26
+ Capacitor.string_to_time(pivot, str)
27
+ end
28
+
29
+ def self.string_to_time(pivot, str)
30
+ timestamp = pivot.strftime("%s").to_i - (MurmurHash3::V32.str_hexdigest(str).to_i(16)/8)
31
+ DateTime.strptime(timestamp.to_s, "%s")
32
+ end
33
+
34
+ def self.oldest_string_time(pivot)
35
+ timestamp = pivot.strftime("%s").to_i - ("ffffffff".to_i(16)/8)
36
+ DateTime.strptime(timestamp.to_s, "%s")
37
+ end
38
+
39
+ private
40
+
41
+ def seconds_between(date_time_1, date_time_2)
42
+ date_time_1.strftime("%s").to_i - date_time_2.strftime("%s").to_i
43
+ end
44
+
45
+ def datetime_minus_seconds(datetime, seconds)
46
+ ts = (datetime.strftime("%s").to_i - seconds).to_s
47
+ DateTime.strptime(ts, "%s")
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,23 @@
1
+ module Flux
2
+ class Falsey < Capacitor
3
+ def initialize(*args)
4
+ @pivot = DateTime.now
5
+ @time_dilation = 2.0
6
+ end
7
+
8
+ def travel_to?(point)
9
+ false
10
+ end
11
+ end
12
+
13
+ class Truethy < Capacitor
14
+ def initialize(*args)
15
+ @pivot = DateTime.now
16
+ @time_dilation = 2.0
17
+ end
18
+
19
+ def travel_to?(point)
20
+ true
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Flux
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flux_capacitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raphael Eidus
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-22 00:00:00.000000000 Z
11
+ date: 2017-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: murmurhash3
@@ -86,6 +86,8 @@ files:
86
86
  - bin/setup
87
87
  - flux_capacitor.gemspec
88
88
  - lib/flux_capacitor.rb
89
+ - lib/flux_capacitor/capacitor.rb
90
+ - lib/flux_capacitor/testing.rb
89
91
  - lib/flux_capacitor/version.rb
90
92
  homepage: https://github.com/raphaeleidus/flux_capacitor
91
93
  licenses: []