time_humanizer 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c9c81ae13209741e2e9ccd010d555f910e481b3b
4
+ data.tar.gz: 4da41eca8a94f579fbecf2dffd10877faabc09c8
5
+ SHA512:
6
+ metadata.gz: 245640a3b5b7322e650f9d61b3f94910bcd1faf1ffa862f101a3053718e2a2e6348015e2734d1dd1f1b837944ad19b4359b1f7b91243f204c8f6191eec4eeb11
7
+ data.tar.gz: 39a4a6231e7a14bb1834fc8b9037fad83263c33ef5628d14fe46a9f3bdfbd72fd008c8381f82220941bcb9c4a4778df7635d2358e3f7a6f0e3ff4bb069e0c188
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in time_humanizer.gemspec
4
+ gemspec
5
+
6
+ gem 'pry'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 bart
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # TimeHumanizer
2
+
3
+ Formats seconds to readable time format i.e. 8h05m and vice versa. See examples below.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'time_humanizer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install time_humanizer
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ TimeHumanizer.to_seconds(nil) # nil
23
+ TimeHumanizer.to_seconds('0') # 0
24
+ TimeHumanizer.to_seconds('1') # 3600
25
+ TimeHumanizer.to_seconds('1h') # 3600
26
+ TimeHumanizer.to_seconds('1h30m') # 5400
27
+ TimeHumanizer.to_seconds('2m') # 120
28
+ TimeHumanizer.to_seconds('1h5m') # 3900
29
+ TimeHumanizer.to_seconds('1h06m') # 3960
30
+ TimeHumanizer.to_seconds('1h0m') # 3600
31
+ TimeHumanizer.to_seconds('') # nil
32
+ TimeHumanizer.to_seconds(' ') # nil
33
+ TimeHumanizer.to_seconds('x') # TimeHumanizer::InvalidArgument
34
+ TimeHumanizer.to_seconds(' x') # TimeHumanizer::InvalidArgument
35
+ TimeHumanizer.to_seconds('0f') # TimeHumanizer::InvalidArgument
36
+
37
+ TimeHumanizer.to_humanized_time(nil) # nil
38
+ TimeHumanizer.to_humanized_time(0) # '0'
39
+ TimeHumanizer.to_humanized_time(3600) # '1h'
40
+ TimeHumanizer.to_humanized_time(5400) # '1h30m'
41
+ TimeHumanizer.to_humanized_time(120) # '2m'
42
+ TimeHumanizer.to_humanized_time(3900) # '1h5m'
43
+ TimeHumanizer.to_humanized_time(3960) # '1h6m'
44
+ TimeHumanizer.to_humanized_time('0') # '0'
45
+ TimeHumanizer.to_humanized_time('3600') # '1h'
46
+ TimeHumanizer.to_humanized_time('5400') # '1h30m'
47
+ TimeHumanizer.to_humanized_time('120') # '2m'
48
+ TimeHumanizer.to_humanized_time('3900') # '1h5m'
49
+ TimeHumanizer.to_humanized_time('3960') # '1h6m'
50
+ TimeHumanizer.to_humanized_time('' # nil
51
+ TimeHumanizer.to_humanized_time(' ') # nil
52
+ TimeHumanizer.to_humanized_time('x') # TimeHumanizer::InvalidArgument
53
+ TimeHumanizer.to_humanized_time(' x') # TimeHumanizer::InvalidArgument
54
+ TimeHumanizer.to_humanized_time('0f') # TimeHumanizer::InvalidArgument
55
+ ```
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it ( https://github.com/b-wojtowicz/time_humanizer/fork )
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |task|
5
+ task.rspec_opts = ['--color']
6
+ end
7
+
8
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ module TimeHumanizer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,53 @@
1
+ require "time_humanizer/version"
2
+
3
+ module TimeHumanizer
4
+ class InvalidArgument < StandardError; end
5
+
6
+ VALID_TIME = /
7
+ ^
8
+ (
9
+ (?<hours> \d+?)h? |
10
+ (?<minutes> \d+?)m |
11
+ (?<hours> \d+?)h(?<minutes> \d+?)m
12
+ )
13
+ $
14
+ /x
15
+
16
+ VALID_SECONDS = /^\d+$/
17
+
18
+ # Converts human readable time format like 1h30m to seconds
19
+ def self.to_seconds(humanized_time_str)
20
+ unless blank_value?(humanized_time_str)
21
+ match_data = humanized_time_str.match VALID_TIME
22
+ raise InvalidArgument unless match_data
23
+
24
+ h, m = match_data[:hours], match_data[:minutes]
25
+ (h.to_i * 3600) + (m.to_i * 60)
26
+ end
27
+ end
28
+
29
+ # Converts seconds to human readable format like 1h30m
30
+ def self.to_humanized_time(seconds)
31
+ unless blank_value?(seconds)
32
+ raise InvalidArgument if VALID_SECONDS !~ seconds.to_s
33
+
34
+ seconds = seconds.to_i
35
+ hours = seconds / 3600
36
+ minutes = seconds % 3600 / 60
37
+
38
+ str = (hours > 0) ? "#{hours}h" : ""
39
+ str << "#{minutes}m" if minutes > 0
40
+ str.size == 0 ? '0' : str
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def self.blank_value?(value)
47
+ value.nil? ||
48
+ (String === value && (
49
+ value.empty? ||
50
+ value.lstrip.empty?
51
+ ))
52
+ end
53
+ end
@@ -0,0 +1,8 @@
1
+ require 'pry'
2
+ require 'time_humanizer'
3
+
4
+ Dir[File.expand_path("../support/**/*.rb", __FILE__)].each { |f| require f }
5
+
6
+ RSpec.configure do |config|
7
+ config.extend TimeHumanizer::TestHelper
8
+ end
@@ -0,0 +1,14 @@
1
+ module TimeHumanizer
2
+ module TestHelper
3
+ def conversion_with(opts = {})
4
+ context opts[:value].to_s do
5
+ let(:value) { opts[:value] }
6
+ if opts[:raises]
7
+ it { expect{ subject }.to raise_error opts[:raises] }
8
+ else
9
+ it { is_expected.to eq opts[:expects] }
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe TimeHumanizer do
4
+ subject { run }
5
+
6
+ describe '.to_seconds' do
7
+ let(:run) { described_class.to_seconds(value) }
8
+
9
+ conversion_with value: nil, expects: nil
10
+ conversion_with value: '0', expects: 0
11
+ conversion_with value: '1', expects: 3600
12
+ conversion_with value: '1h', expects: 3600
13
+ conversion_with value: '1h30m', expects: 5400
14
+ conversion_with value: '2m', expects: 120
15
+ conversion_with value: '1h5m', expects: 3900
16
+ conversion_with value: '1h06m', expects: 3960
17
+ conversion_with value: '1h0m', expects: 3600
18
+ conversion_with value: '', expects: nil
19
+ conversion_with value: ' ', expects: nil
20
+ conversion_with value: 'x', raises: TimeHumanizer::InvalidArgument
21
+ conversion_with value: ' x', raises: TimeHumanizer::InvalidArgument
22
+ conversion_with value: '0f', raises: TimeHumanizer::InvalidArgument
23
+ end
24
+
25
+ describe 'to_humanized_time' do
26
+ let(:run) { described_class.to_humanized_time(value) }
27
+
28
+ conversion_with value: nil, expects: nil
29
+ conversion_with value: 0, expects: '0'
30
+ conversion_with value: 3600, expects: '1h'
31
+ conversion_with value: 5400, expects: '1h30m'
32
+ conversion_with value: 120, expects: '2m'
33
+ conversion_with value: 3900, expects: '1h5m'
34
+ conversion_with value: 3960, expects: '1h6m'
35
+ conversion_with value: '0', expects: '0'
36
+ conversion_with value: '3600', expects: '1h'
37
+ conversion_with value: '5400', expects: '1h30m'
38
+ conversion_with value: '120', expects: '2m'
39
+ conversion_with value: '3900', expects: '1h5m'
40
+ conversion_with value: '3960', expects: '1h6m'
41
+ conversion_with value: '', expects: nil
42
+ conversion_with value: ' ', expects: nil
43
+ conversion_with value: 'x', raises: TimeHumanizer::InvalidArgument
44
+ conversion_with value: ' x', raises: TimeHumanizer::InvalidArgument
45
+ conversion_with value: '0f', raises: TimeHumanizer::InvalidArgument
46
+ end
47
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'time_humanizer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "time_humanizer"
8
+ spec.version = TimeHumanizer::VERSION
9
+ spec.authors = ["Bartłomiej Wójtowicz"]
10
+ spec.email = ["bart@softkiwi.com"]
11
+ spec.summary = %q{Formats seconds to readable time format i.e. 8h05m and vice versa}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/b-wojtowicz/time_humanizer"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_humanizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bartłomiej Wójtowicz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: ''
56
+ email:
57
+ - bart@softkiwi.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/time_humanizer.rb
69
+ - lib/time_humanizer/version.rb
70
+ - spec/spec_helper.rb
71
+ - spec/support/time_humanizer/test_helper.rb
72
+ - spec/time_humanizer_spec.rb
73
+ - time_humanizer.gemspec
74
+ homepage: https://github.com/b-wojtowicz/time_humanizer
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Formats seconds to readable time format i.e. 8h05m and vice versa
98
+ test_files:
99
+ - spec/spec_helper.rb
100
+ - spec/support/time_humanizer/test_helper.rb
101
+ - spec/time_humanizer_spec.rb