devise_user_metering 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: 235ac1530efa4f108ed266bc062097c61c1eb5ba
4
- data.tar.gz: 9f2705da414110def3942b74846971a48c1ff1ea
3
+ metadata.gz: b675bc0b1c171194964694c2778391ef28c721ce
4
+ data.tar.gz: ae2576392b6882d541d9ac4145a3548a45aad6f8
5
5
  SHA512:
6
- metadata.gz: 3154a5e58bafcd8ecabf4717d0dacdf174f92ae60c3a8e386cbe45001890b42d6060586b1fd60cc37487527d7549f49c524c64e3f2f2d0897d91ce9737939f9c
7
- data.tar.gz: 52ca71e1d748802a99da12e7cf17a4517cfcb55b3558d4d735e8635143ed81afe727a6908c795a3c6ef3aa81a2a1d155d0ef0508721d4e08f8aa5ece8e85541b
6
+ metadata.gz: 5bfa5e33ddaf0f79dac99c6199fa0f04a8bf2319009bed519f744b677d9e1645b0a0caac2d08075b567affe2fe864f04119d670737d8ba0ca07b05629b6266a2
7
+ data.tar.gz: 99877f71bfa566b774af1b4db49298dc2a82ae612258135f255516e4c9024237544fe43b4000ce118aca86fc664da016c8e0c3a6a7df24bc7bd64af99e6eb9bc
data/Gemfile CHANGED
@@ -16,4 +16,5 @@ group :development do
16
16
  gem "simplecov", ">= 0"
17
17
  gem "timecop"
18
18
  gem 'test-unit'
19
+ gem 'pry-byebug'
19
20
  end
@@ -40,6 +40,10 @@ GEM
40
40
  arel (6.0.2)
41
41
  bcrypt (3.1.10)
42
42
  builder (3.2.2)
43
+ byebug (5.0.0)
44
+ columnize (= 0.9.0)
45
+ coderay (1.1.0)
46
+ columnize (0.9.0)
43
47
  descendants_tracker (0.0.4)
44
48
  thread_safe (~> 0.3, >= 0.3.1)
45
49
  devise (3.5.1)
@@ -82,6 +86,7 @@ GEM
82
86
  nokogiri (>= 1.5.9)
83
87
  mail (2.6.3)
84
88
  mime-types (>= 1.16, < 3)
89
+ method_source (0.8.2)
85
90
  mime-types (2.6.1)
86
91
  mini_portile (0.6.2)
87
92
  minitest (5.7.0)
@@ -98,6 +103,13 @@ GEM
98
103
  rack (~> 1.2)
99
104
  orm_adapter (0.5.0)
100
105
  power_assert (0.2.2)
106
+ pry (0.10.1)
107
+ coderay (~> 1.1.0)
108
+ method_source (~> 0.8.1)
109
+ slop (~> 3.4)
110
+ pry-byebug (3.2.0)
111
+ byebug (~> 5.0)
112
+ pry (~> 0.10)
101
113
  rack (1.6.4)
102
114
  rack-test (0.6.3)
103
115
  rack (>= 1.0)
@@ -141,6 +153,7 @@ GEM
141
153
  json (~> 1.8)
142
154
  simplecov-html (~> 0.10.0)
143
155
  simplecov-html (0.10.0)
156
+ slop (3.6.0)
144
157
  sprockets (3.2.0)
145
158
  rack (~> 1.0)
146
159
  sprockets-rails (2.3.2)
@@ -164,6 +177,7 @@ DEPENDENCIES
164
177
  bundler (~> 1.0)
165
178
  devise
166
179
  jeweler (~> 2.0.1)
180
+ pry-byebug
167
181
  rails (>= 3.0.4)
168
182
  rdoc (~> 3.12)
169
183
  shoulda
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: devise_user_metering 0.0.1 ruby lib
5
+ # stub: devise_user_metering 0.0.2 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "devise_user_metering"
9
- s.version = "0.0.1"
9
+ s.version = "0.0.2"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Mike Ihbe"]
14
- s.date = "2015-07-29"
14
+ s.date = "2015-09-04"
15
15
  s.description = "Add methods to devise User models that account for active time during a month. Useful for SAAS billings"
16
16
  s.email = "we@mustwin.com"
17
17
  s.extra_rdoc_files = [
@@ -53,6 +53,7 @@ Gem::Specification.new do |s|
53
53
  s.add_development_dependency(%q<simplecov>, [">= 0"])
54
54
  s.add_development_dependency(%q<timecop>, [">= 0"])
55
55
  s.add_development_dependency(%q<test-unit>, [">= 0"])
56
+ s.add_development_dependency(%q<pry-byebug>, [">= 0"])
56
57
  else
57
58
  s.add_dependency(%q<rails>, [">= 3.0.4"])
58
59
  s.add_dependency(%q<devise>, [">= 0"])
@@ -63,6 +64,7 @@ Gem::Specification.new do |s|
63
64
  s.add_dependency(%q<simplecov>, [">= 0"])
64
65
  s.add_dependency(%q<timecop>, [">= 0"])
65
66
  s.add_dependency(%q<test-unit>, [">= 0"])
67
+ s.add_dependency(%q<pry-byebug>, [">= 0"])
66
68
  end
67
69
  else
68
70
  s.add_dependency(%q<rails>, [">= 3.0.4"])
@@ -74,6 +76,7 @@ Gem::Specification.new do |s|
74
76
  s.add_dependency(%q<simplecov>, [">= 0"])
75
77
  s.add_dependency(%q<timecop>, [">= 0"])
76
78
  s.add_dependency(%q<test-unit>, [">= 0"])
79
+ s.add_dependency(%q<pry-byebug>, [">= 0"])
77
80
  end
78
81
  end
79
82
 
@@ -3,38 +3,44 @@
3
3
  module Devise
4
4
  module Models
5
5
  module UserMetering
6
- # This function returns a decimal between 0 and 1 that reflects the amount of the month this user has been 'active'
6
+
7
+ # takes a time, returns the active interval in that time's month
7
8
  def active_proportion_of_month(time)
8
- month = time.beginning_of_month
9
- end_month = time.end_of_month
9
+ active_proportion_of_interval(time.beginning_of_month, time.end_of_month)
10
+ end
10
11
 
11
- if end_month > Time.now
12
- raise StandardError.new("You can't get meter data for incomplete months")
12
+ # takes an interval start and interval end
13
+ # returns a decimal between 0 and 1 that reflects the proportion of time in the given interval
14
+ # that the user has been 'active'
15
+ def active_proportion_of_interval(interval_start, interval_end)
16
+ if interval_end > Time.now
17
+ raise StandardError.new("You can't get meter data for partial intervals")
13
18
  end
14
- if month < self.activated_at.beginning_of_month
15
- raise StandardError.new("No usage data retained for that month")
19
+ if usage_in_interval?(interval_start, interval_end)
20
+ raise StandardError.new('No usage data retained for this period of time')
16
21
  end
17
22
 
18
- in_month = ->(time) { (month..end_month).cover?(time) }
19
- if in_month.call(self.activated_at) || in_month.call(self.deactivated_at)
20
- if !active && self.deactivated_at < month
23
+ in_interval = ->(time) { (interval_start..interval_end).cover?(time) }
24
+ if in_interval.call(self.activated_at) || in_interval.call(self.deactivated_at)
25
+ if !active && self.deactivated_at < interval_start
21
26
  return 0
22
27
  end
23
- month_duration = end_month - month
24
- remainder = self.active ? [end_month - self.activated_at, 0].max : 0
25
- (remainder + self.rollover_active_duration) / month_duration
28
+ interval_duration = interval_end - interval_start
29
+ remainder = self.active ? [interval_end - self.activated_at, 0].max : 0
30
+ (remainder + self.rollover_active_duration) / interval_duration
26
31
  else
27
32
  self.active ? 1 : 0
28
- end
29
-
33
+ end
30
34
  end
31
35
 
36
+ #activates the user to indicate the start of metering
32
37
  def activate!
33
38
  self.activated_at = Time.new
34
39
  self.active = true
35
40
  self.save!
36
41
  end
37
42
 
43
+ #deactivates the user to indicate the end of metering
38
44
  def deactivate!
39
45
  now = Time.new
40
46
  self.deactivated_at = now
@@ -43,10 +49,18 @@ module Devise
43
49
  self.save!
44
50
  end
45
51
 
52
+ #indicates the user has been accounted for said month/interval and resets the rollover_active_duration to zero
46
53
  def billed!
47
54
  self.rollover_active_duration = 0
48
55
  self.save!
49
56
  end
57
+
58
+ private
59
+
60
+ def usage_in_interval?(interval_start, interval_end)
61
+ (self.deactivated_at && self.deactivated_at < interval_start) ||
62
+ (self.activated_at && self.activated_at > interval_end)
63
+ end
50
64
  end
51
65
  end
52
66
  end
@@ -1,6 +1,6 @@
1
1
  module DeviseUserMetering
2
2
  module Version
3
- VERSION = "0.0.1".freeze
3
+ VERSION = "0.0.2".freeze
4
4
  end
5
5
  end
6
6
 
@@ -1,6 +1,7 @@
1
1
  class DeviseAddLastseenable<%= table_name.camelize.singularize %> < ActiveRecord::Migration
2
2
  def self.up
3
3
  add_column :<%= table_name %>, :active, :boolean, default: true
4
+ add_column :<%= table_name %>, :billed_at, :datetime
4
5
  add_column :<%= table_name %>, :activated_at, :datetime
5
6
  add_column :<%= table_name %>, :deactivated_at, :datetime
6
7
  add_column :<%= table_name %>, :rollover_active_duration, :int
@@ -11,7 +12,9 @@ class DeviseAddLastseenable<%= table_name.camelize.singularize %> < ActiveRecord
11
12
 
12
13
  def self.down
13
14
  remove_column :<%= table_name %>, :active, :boolean
15
+ remove_column :<%= table_name %>, :billed_at, :datetime
14
16
  remove_column :<%= table_name %>, :activated_at, :datetime
17
+ remove_column :<%= table_name %>, :deactivated_at, :datetime
15
18
  remove_column :<%= table_name %>, :rollover_active_duration, :int
16
19
  end
17
20
  end
@@ -17,6 +17,13 @@ class UserTest < Test::Unit::TestCase
17
17
  assert_in_delta 17.0/31, u.active_proportion_of_month(Time.parse("2015-01-01")), ACCEPTABLE_DELTA # 15 days of 31 in jan 2015
18
18
  end
19
19
  end
20
+
21
+ should "Track users with an optional custom time span passed in" do
22
+ u = new_user(activated_at: Time.parse("2015-01-01"), active: true, rollover_active_duration: 0)
23
+ Timecop.freeze(Date.parse("2015-03-20")) do
24
+ assert_equal 1, u.active_proportion_of_interval(Time.parse("2015-01-15"), Time.parse("2015-02-15"))
25
+ end
26
+ end
20
27
 
21
28
  should "Track users activated this month and deactivated this month as a partial month" do
22
29
  u = new_user(activated_at: Time.parse("2015-01-10"), active: true, rollover_active_duration: 0)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise_user_metering
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Ihbe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-29 00:00:00.000000000 Z
11
+ date: 2015-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry-byebug
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
139
153
  description: Add methods to devise User models that account for active time during
140
154
  a month. Useful for SAAS billings
141
155
  email: we@mustwin.com