time_ago_in_words 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 17816bdfb96d3a2e1e9ba53f3dd6e2db69a31682
4
+ data.tar.gz: c68a369e2faeb1db7aed0dbe215eb63fbe72d492
5
+ SHA512:
6
+ metadata.gz: 8eac441414aecb182c726f1bf80e7398f642a3c74a461cf78843207abb69ad8878e04e9f0d183703b460e4fdc011ba69a9e7eb6c5d715c368a59ca7722e52ff6
7
+ data.tar.gz: e80b0d1598bacb957ac0d493c54a60cd47a974448a2426a516ba1722b312526bd0fa942d463989f2fd8277e7622c052f0671a9295d969a4c29b15875a3eb99c3
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --order random
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - ruby-head
8
+ script: 'bundle exec rake'
data/.yardopts ADDED
@@ -0,0 +1,6 @@
1
+ --readme README.md
2
+ --title 'TimeAgoInWords API Documentation'
3
+ --charset utf-8
4
+ -
5
+ LICENSE.md
6
+ CHANGELOG.md
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ ## [In git](https://github.com/elgalu/time_ago_in_words/compare/v0.0.1...HEAD)
2
+
3
+ ### New Features
4
+ * n/a
5
+
6
+ ### Bugfixes
7
+ * n/a
8
+
9
+ ### Chores
10
+ * n/a
11
+
12
+ ## [v0.0.1](https://github.com/elgalu/time_ago_in_words/tree/v0.0.1)
13
+
14
+ ## First gem release
15
+
16
+ ### Features:
17
+ * Humanize elapsed time from some Time instance to Time.now, e.g. '2 hours and 1 minute ago' (Leo Gallucci)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in time_ago_in_words.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Leo Gallucci
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,97 @@
1
+ # TimeAgoInWords
2
+
3
+ [![Gem Version][GV img]][Gem Version]
4
+ [![Build Status][BS img]][Build Status]
5
+ [![Dependency Status][DS img]][Dependency Status]
6
+ [![Code Climate][CC img]][Code Climate]
7
+ [![Coverage Status][CS img]][Coverage Status]
8
+
9
+ ## Description
10
+
11
+ Humanize elapsed time from some Time instance to Time.now, e.g. '2 hours and 1 minute ago'
12
+
13
+ ## Installation
14
+
15
+ `$ gem install time_ago_in_words` or add to your [Gemfile][] this line: `gem 'time_ago_in_words'` then run [bundle install][]
16
+
17
+ ## Usage
18
+
19
+ Just `require 'time_ago_in_words'` and then call Time#ago_in_words method:
20
+
21
+ ```ruby
22
+ require 'rubygems' # if ruby 1.8.7
23
+ require 'time_ago_in_words'
24
+
25
+ # Comparing from now:
26
+ (Time.now - 10).ago_in_words #=> "10 seconds ago"
27
+ (Time.now - 1).ago_in_words #=> "1 second ago"
28
+ (Time.now - 60).ago_in_words #=> "1 minute ago"
29
+ (Time.now - 63).ago_in_words #=> "1 minute and 3 seconds ago"
30
+
31
+ # This is my current time so you can compare
32
+ Time.now #=> 2013-03-06 02:19:23 -0300
33
+
34
+ Time.local(1981,03,03,20,30,40).ago_in_words #=> "690 days and 5 hours ago"
35
+ Time.local(2013,03,03,20,30,40).ago_in_words #=> "2 days and 5 hours ago"
36
+ Time.local(2013,03,04,20,30,40).ago_in_words #=> "1 day and 5 hours ago"
37
+ Time.local(2013,03,05,20,30,40).ago_in_words #=> "5 hours and 48 minutes ago"
38
+ Time.local(2013,03,05,21,13,40).ago_in_words #=> "5 hours and 5 minutes ago"
39
+ Time.local(2013,03,06,00,30,40).ago_in_words #=> "1 hour and 48 minutes ago"
40
+ Time.local(2013,03,06,01,11,40).ago_in_words #=> "1 hour and 7 minutes ago"
41
+ Time.local(2013,03,06,01,27,40).ago_in_words #=> "51 minutes and 43 seconds ago"
42
+ Time.local(2013,03,06,02,19,20).ago_in_words #=> "3 seconds ago"
43
+ ```
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it.
48
+ 2. Make your feature addition or bug fix and create your feature branch.
49
+ 3. Update the [Change Log][].
50
+ 3. Add specs/tests for it. This is important so I don't break it in a future version unintentionally.
51
+ 4. Commit, create a new Pull Request.
52
+ 5. Check that your pull request passes the [build][travis pull requests].
53
+
54
+ ### TODO
55
+ + Add DateTime support. Currently only available for Time objects.
56
+ + Add "N months ago"
57
+ + Add "N years ago"
58
+ + Add "N decades ago"
59
+ + Add "N centuries ago"
60
+ + Add more rspec Examples
61
+ + Implement Time mocking for testing, instead of abusing Time.now on spec
62
+ + Extract [time_ago_in_words][] and implement that alternative precision syntax, e.g. "less than a minute ago"
63
+
64
+ ## License
65
+
66
+ Released under the MIT License. See the [LICENSE][] file for further details.
67
+
68
+ ## Links
69
+
70
+ [RubyGems][] | [Documentation][] | [Source][] | [Bugtracker][] | [Build Status][] | [Dependency Status][] | [Code Climate][]
71
+
72
+
73
+ [bundle install]: http://gembundler.com/man/bundle-install.1.html
74
+ [Gemfile]: http://gembundler.com/man/gemfile.5.html
75
+ [LICENSE]: LICENSE.md
76
+ [Change Log]: CHANGELOG.md
77
+
78
+ [RubyGems]: https://rubygems.org/gems/time_ago_in_words
79
+ [Documentation]: http://rubydoc.info/gems/time_ago_in_words
80
+ [Source]: https://github.com/elgalu/time_ago_in_words
81
+ [Bugtracker]: https://github.com/elgalu/time_ago_in_words/issues
82
+
83
+ [travis pull requests]: https://travis-ci.org/elgalu/time_ago_in_words/pull_requests
84
+
85
+ [Gem Version]: https://rubygems.org/gems/time_ago_in_words
86
+ [Build Status]: https://travis-ci.org/elgalu/time_ago_in_words
87
+ [Dependency Status]: https://gemnasium.com/elgalu/time_ago_in_words
88
+ [Code Climate]: https://codeclimate.com/github/elgalu/time_ago_in_words
89
+ [Coverage Status]: https://coveralls.io/r/elgalu/time_ago_in_words
90
+
91
+ [GV img]: https://badge.fury.io/rb/time_ago_in_words.png
92
+ [BS img]: https://travis-ci.org/elgalu/time_ago_in_words.png
93
+ [DS img]: https://gemnasium.com/elgalu/time_ago_in_words.png
94
+ [CC img]: https://codeclimate.com/github/elgalu/time_ago_in_words.png
95
+ [CS img]: https://coveralls.io/repos/elgalu/time_ago_in_words/badge.png?branch=master
96
+
97
+ [time_ago_in_words]: http://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ require 'time_ago_in_words/version'
2
+ require 'time_ago_in_words/methods'
3
+ require 'time_ago_in_words/core_ext/time'
@@ -0,0 +1,4 @@
1
+ # Polite monkey patching to add Time#ago_in_words method
2
+ class Time
3
+ include TimeAgoInWords
4
+ end
@@ -0,0 +1,41 @@
1
+ module TimeAgoInWords
2
+ # Convert elapsed seconds from Time.now to the current Time instance
3
+ # into an English human readable text like '4 hours and 1 minute ago'
4
+ # It is not well suited for dates older than 1 year
5
+ #
6
+ # @return [String] the humanized elapsed time in pairs
7
+ # Always returns a pair of words like:
8
+ # '23 days and 3 hours ago'
9
+ # '5 hours and 44 minutes ago'
10
+ # ' 1 minute and 11 seconds ago'
11
+ #
12
+ # Or just 1 when no hours/minutes/seconds available:
13
+ # '23 days ago'
14
+ # '17 hours ago'
15
+ # '1 minute ago'
16
+ # '1 second ago'
17
+ #
18
+ # Ẃhen 0 seconds or Time instance is a future date, returns empty string:
19
+ # ""
20
+ #
21
+ # @note Inspired from http://stackoverflow.com/a/4136485/511069
22
+ def ago_in_words
23
+ secs = Time.now - self
24
+ return 'just now' if secs > -1 && secs < 1
25
+ return '' if secs <= -1
26
+ return 'a very very long time ago' if secs > 60*60*24*100_000
27
+ ary = [[60, :seconds], [60, :minutes], [24, :hours], [100_000, :days]].map{ |count, name|
28
+ if secs > 0
29
+ secs, n = secs.divmod(count)
30
+ "#{n.to_i} #{name}"
31
+ end
32
+ }.compact.reverse[0..1]
33
+ if ary.size == 1
34
+ ary.map! {|part| part[0, 2].to_i == 1 ? part.chomp('s') : part }
35
+ else
36
+ ary.map! {|part| part[0, 2].to_i == 1 ? part.chomp('s') : part[0, 2].to_i == 0 ? nil : part }
37
+ end
38
+ ary.compact!
39
+ ary.size == 0 ? '' : ary.join(' and ') << ' ago'
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module TimeAgoInWords
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start
9
+
10
+ require 'time_ago_in_words'
11
+
12
+ # Require this file using `require "spec_helper"` within each of your specs
13
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
14
+ RSpec.configure do |config|
15
+ config.treat_symbols_as_metadata_keys_with_true_values = true
16
+ config.run_all_when_everything_filtered = true
17
+ config.filter_run :focus
18
+
19
+ # Run specs in random order to surface order dependencies.
20
+ config.order = 'random'
21
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe TimeAgoInWords do
4
+ it 'should have a version number' do
5
+ TimeAgoInWords::VERSION.should_not be_nil
6
+ end
7
+
8
+ describe '#ago_in_words' do
9
+ context 'invalid dates' do
10
+ it "returns 'just now' when elapsed seconds are less than 1" do
11
+ Time.now.ago_in_words.should == 'just now'
12
+ (Time.now - 0.5).ago_in_words.should == 'just now'
13
+ (Time.now + 0.5).ago_in_words.should == 'just now'
14
+ end
15
+
16
+ it "returns empty string on future dates" do
17
+ (Time.now + 1.6).ago_in_words.should == ''
18
+ (Time.now + 100).ago_in_words.should == ''
19
+ (Time.now + 100_000).ago_in_words.should == ''
20
+ end
21
+
22
+ it "returns 'a very very long time ago' when more than 250 years ago" do
23
+ (Time.now - 60*60*24*100_001).ago_in_words.should == 'a very very long time ago'
24
+ end
25
+
26
+ it "can handle many days ago" do
27
+ (Time.now - 60*60*24*99_999).ago_in_words.should == '99999 days ago'
28
+ end
29
+
30
+ it "returns N seconds ago, N seconds ago!" do
31
+ (Time.now - 2).ago_in_words.should == '2 seconds ago'
32
+ (Time.now - 33).ago_in_words.should == '33 seconds ago'
33
+ end
34
+
35
+ it "returns 1 second/minute/hour/day ago when 1 ago!" do
36
+ (Time.now - 1).ago_in_words.should == '1 second ago'
37
+ (Time.now - 60).ago_in_words.should == '1 minute ago'
38
+ (Time.now - 60*60).ago_in_words.should == '1 hour ago'
39
+ (Time.now - 60*60*24).ago_in_words.should == '1 day ago'
40
+ end
41
+
42
+ it "returns 1 second/minute/hour/day and 1 second/minute/hour/day ago when appropriate" do
43
+ (Time.now - 1 - 60).ago_in_words.should == '1 minute and 1 second ago'
44
+ (Time.now - 1*60*60 - 60).ago_in_words.should == '1 hour and 1 minute ago'
45
+ (Time.now - 1*60*60*24 - 60*60).ago_in_words.should == '1 day and 1 hour ago'
46
+ end
47
+
48
+ it "returns 1 second/minute/hour/day and 1 second/minute/hour/day ago ignoring third elements (residual irrelevant precisions)" do
49
+ (Time.now - 1*60*60 - 60 - 10).ago_in_words.should == '1 hour and 1 minute ago'
50
+ (Time.now - 1*60*60*24 - 1*60*60 - 100).ago_in_words.should == '1 day and 1 hour ago'
51
+ (Time.now - 9*60*60*24 - 3*60*60 - 100).ago_in_words.should == '9 days and 3 hours ago'
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,35 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'time_ago_in_words/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+
7
+ spec.platform = Gem::Platform::RUBY
8
+ spec.name = "time_ago_in_words"
9
+ spec.version = TimeAgoInWords::VERSION
10
+ spec.summary = %q{Humanize elapsed time from some Time instance to Time.now, e.g. '2 hours and 1 minute ago'}
11
+ spec.description = spec.summary
12
+
13
+ spec.required_ruby_version = '>= 1.9.2'
14
+ spec.required_rubygems_version = '>= 2'
15
+
16
+ spec.license = 'MIT'
17
+
18
+ spec.authors = ["Leo Gallucci"]
19
+ spec.email = ["elgalu3@gmail.com"]
20
+ spec.homepage = "https://github.com/elgalu/time_ago_in_words"
21
+
22
+ spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
23
+ spec.files = `git ls-files`.split($/)
24
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", ">= 1.2"
28
+ spec.add_development_dependency "rake"
29
+ spec.add_development_dependency "rspec", "~> 2.13"
30
+ spec.add_development_dependency "redcarpet", ">= 2.2"
31
+ spec.add_development_dependency "yard", ">= 0.8.5.2"
32
+ spec.add_development_dependency "simplecov", ">= 0.7.1"
33
+ spec.add_development_dependency 'coveralls', '>= 0.5.8'
34
+
35
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_ago_in_words
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Leo Gallucci
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-06 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.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
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: '2.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redcarpet
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '2.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '2.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 0.8.5.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 0.8.5.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.7.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.7.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: 0.5.8
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: 0.5.8
111
+ description: Humanize elapsed time from some Time instance to Time.now, e.g. '2 hours
112
+ and 1 minute ago'
113
+ email:
114
+ - elgalu3@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .rspec
121
+ - .travis.yml
122
+ - .yardopts
123
+ - CHANGELOG.md
124
+ - Gemfile
125
+ - LICENSE.md
126
+ - README.md
127
+ - Rakefile
128
+ - lib/time_ago_in_words.rb
129
+ - lib/time_ago_in_words/core_ext/time.rb
130
+ - lib/time_ago_in_words/methods.rb
131
+ - lib/time_ago_in_words/version.rb
132
+ - spec/spec_helper.rb
133
+ - spec/time_ago_in_words_spec.rb
134
+ - time_ago_in_words.gemspec
135
+ homepage: https://github.com/elgalu/time_ago_in_words
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - '>='
146
+ - !ruby/object:Gem::Version
147
+ version: 1.9.2
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '2'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.0.0
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Humanize elapsed time from some Time instance to Time.now, e.g. '2 hours
159
+ and 1 minute ago'
160
+ test_files:
161
+ - spec/spec_helper.rb
162
+ - spec/time_ago_in_words_spec.rb
163
+ has_rdoc: