billable 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/billable +7 -0
  3. data/lib/billable.rb +56 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 09d18d6e86ff15f204e80c683f13373814e1b9b4
4
+ data.tar.gz: e21c42876712a021201dbdad07a65c8aace28e18
5
+ SHA512:
6
+ metadata.gz: 16658d792c3b447d77d534e1a9752ec99c298dbc5d3cdd7a6ef2119ed1b53566c98c2aa36a19e41bebffdf0dec4608d380e5347449888e1d3f0675fe58682cd0
7
+ data.tar.gz: 50f75ef8930a557ea7ae8036c2dafaaa2913383ad82eaddb61d5132f87287ffc3377b00e7ef24dabe5a19f506c38683c2ec2fb30fd9c3a12ba7def96e2bb8df8
data/bin/billable ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/billable'
3
+
4
+ billable = Billable.new
5
+
6
+ sum = billable.sum_multiple(timestamps: ARGV)
7
+ puts billable.charge(45, sum)
data/lib/billable.rb ADDED
@@ -0,0 +1,56 @@
1
+ class Billable
2
+ def charge(rate, timestamp)
3
+ hours, minutes = split_timestamp(timestamp)
4
+ decimal = minutes / 60.0
5
+
6
+ rate * (hours + decimal)
7
+ end
8
+
9
+ def sum(args={})
10
+ period_x = args.fetch(:period_x, '0:00') || '0:00'
11
+ period_y = args.fetch(:period_y, '0:00') || '0:00'
12
+
13
+ hour_x, min_x = split_timestamp(period_x)
14
+ hour_y, min_y = split_timestamp(period_y)
15
+
16
+ hours_sum = hour_x + hour_y
17
+ min_sum = min_x + min_y
18
+
19
+ hours, minutes = accumulate_hours_from_minutes(hours_sum, min_sum)
20
+
21
+ format_timestamp(hours, minutes)
22
+ end
23
+
24
+ def sum_multiple(args)
25
+ timestamps = args.fetch(:timestamps, [])
26
+ reduce_timestamps(timestamps)
27
+ end
28
+
29
+ def accumulate_hours_from_minutes(hours, minutes)
30
+ if minutes >= 60
31
+ return accumulate_hours_from_minutes(hours + 1, minutes - 60)
32
+ end
33
+
34
+ [hours, minutes]
35
+ end
36
+
37
+ private
38
+
39
+ def reduce_timestamps(timestamps)
40
+ return sum(period_x: timestamps.first) if timestamps.length == 1
41
+ return reduce_timestamps(map_sum_to(timestamps))
42
+ end
43
+
44
+ def map_sum_to(timestamps)
45
+ timestamps.each_slice(2).map { |x, y| sum(period_x: x, period_y: y) }
46
+ end
47
+
48
+ def split_timestamp(timestamp)
49
+ timestamp.split(':').map(&:to_i)
50
+ end
51
+
52
+ def format_timestamp(hours, minutes)
53
+ minutes = minutes.to_s.rjust(2, '0')
54
+ "#{hours}:#{minutes}"
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: billable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Newton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Billable takes a list of timestamps and calculates a total billable amount
14
+ email: newtoncodes@gmail.com
15
+ executables:
16
+ - billable
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/billable
21
+ - lib/billable.rb
22
+ homepage: https://github.com/newton1337/billable
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.4.8
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: billable hours and rate calculator
46
+ test_files: []