friendly_timestamp 1.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: 46ffda3910a6655ce1efc5b0b890a5dd8c356625
4
+ data.tar.gz: 0492b42d48ce76723331b5eaf4b6a7ae5a08ee97
5
+ SHA512:
6
+ metadata.gz: 7debde53f8dcaead2f86f0c60e02a5fadcc1f8fd3f960c842722b96b1ba89d8e62d6f864ea6228f637d4485c800a858c3588c5572b9daf36ee9355b0ec2bceed
7
+ data.tar.gz: 22f1c27987239c89bdc16eb0e5b9ae6afd1aea48598b77fd9493a555b8db8f4df4d7c123333e977e6c89cdb297e88825298db10a3dc54355e7366444992be1e8
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kuroun.seung@rackspace.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in friendly_timestamp.gemspec
4
+ gem 'activesupport'
5
+ gem 'rspec-rails', '3.5.2'
6
+ gem 'pry-byebug', '3.4.0'
7
+ gem 'rubocop'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Kuroun
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ var get_cookie, set_cookie;
2
+
3
+ $(document).ready(function() {
4
+ var d, n;
5
+ d = new Date;
6
+ n = d.getTimezoneOffset();
7
+ return set_cookie('fts_offset', n, 30);
8
+ });
9
+
10
+ set_cookie = function(cname, cvalue, exdays) {
11
+ var d;
12
+ d = new Date;
13
+ d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
14
+ return document.cookie = cname + '=' + cvalue + ';expires=' + d.toUTCString() + ';path=/';
15
+ };
16
+
17
+ get_cookie = function(cname) {
18
+ var c, ca, i, name;
19
+ name = cname + '=';
20
+ ca = document.cookie.split(';');
21
+ i = 0;
22
+ while (i < ca.length) {
23
+ c = ca[i];
24
+ while (c.charAt(0) === ' ') {
25
+ c = c.substring(1);
26
+ }
27
+ if (c.indexOf(name) === 0) {
28
+ return c.substring(name.length, c.length);
29
+ }
30
+ i++;
31
+ }
32
+ return '';
33
+ };
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "friendly_timestamp"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,87 @@
1
+ require 'friendly_timestamp/engine'
2
+ require 'friendly_timestamp/version'
3
+ require 'active_support/all'
4
+ require 'pry'
5
+
6
+ ONE_MINUTE = 60
7
+ ONE_HOUR = ONE_MINUTE * 60
8
+ ONE_DAY = ONE_HOUR * 24
9
+ ONE_WEEK = ONE_DAY * 7
10
+ ONE_MONTH = ONE_DAY * 30
11
+ ONE_YEAR = ONE_DAY * 365
12
+
13
+ class Time
14
+ def friendly_format
15
+ # convert time to local time
16
+ # using minute format if less hour
17
+ # using hour format if less than a day
18
+ # using full format otherwise (Thurs, Nov 12th 2016)
19
+ case (Time.now - self).to_i.abs
20
+ when 0...ONE_MINUTE
21
+ 'a moment ago'
22
+ when ONE_MINUTE...ONE_HOUR
23
+ minute_format
24
+ when ONE_HOUR...ONE_DAY
25
+ hour_format
26
+ when ONE_DAY...ONE_WEEK
27
+ day_format
28
+ when ONE_WEEK...ONE_MONTH
29
+ week_format
30
+ when ONE_MONTH...ONE_YEAR
31
+ month_format
32
+ when ONE_YEAR...ONE_YEAR * 5
33
+ year_format
34
+ else
35
+ full_format
36
+ end
37
+ end
38
+
39
+ def minute_format
40
+ in_minutes = diff_utc / ONE_MINUTE
41
+ "#{word_form(in_minutes, 'minute')} ago"
42
+ end
43
+
44
+ def hour_format
45
+ in_hours = diff_utc / ONE_HOUR
46
+ "#{word_form(in_hours, 'hour')} ago"
47
+ end
48
+
49
+ def day_format
50
+ in_days = diff_utc / ONE_DAY
51
+ "#{word_form(in_days, 'day')} ago"
52
+ end
53
+
54
+ def week_format
55
+ in_weeks = diff_utc / ONE_WEEK
56
+ "#{word_form(in_weeks, 'week')} ago"
57
+ end
58
+
59
+ def month_format
60
+ in_months = diff_utc / ONE_MONTH
61
+ "#{word_form(in_months, 'month')} ago"
62
+ end
63
+
64
+ def year_format
65
+ in_years = diff_utc / ONE_YEAR
66
+ "#{word_form(in_years, 'year')} ago"
67
+ end
68
+
69
+ def full_format(mn_offset = 0)
70
+ # multiplle -1 here because js represent time difference varies from how rails represents
71
+ # get offset in hour
72
+ hour_offset = (-1 * mn_offset.to_f / 60)
73
+ fts_local_time = in_time_zone hour_offset
74
+ fts_local_time.strftime("%a, %b #{fts_local_time.day.ordinalize} %Y at %H:%M %Z")
75
+ end
76
+
77
+ private
78
+
79
+ def word_form(num, word)
80
+ word = num.round > 1 ? word.pluralize : word.singularize
81
+ num > num.round ? "more than #{num.round} #{word}" : "less than #{num.round} #{word}"
82
+ end
83
+
84
+ def diff_utc
85
+ (Time.now - self).to_f.abs
86
+ end
87
+ end
@@ -0,0 +1,5 @@
1
+ require 'rails/engine'
2
+
3
+ module FriendlyTimestamp
4
+ class Engine < Rails::Engine; end
5
+ end
@@ -0,0 +1,3 @@
1
+ module FriendlyTimestamp
2
+ VERSION = :'1.0.1'
3
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe Time do
4
+ it 'has a version number' do
5
+ expect(FriendlyTimestamp::VERSION).not_to be nil
6
+ end
7
+
8
+ describe 'word_form' do
9
+ # make sure that return more than or less than phrase
10
+ # make sure that number is no decimal form
11
+ # make sure that it returns appropriate singular and plural form of word
12
+ word = 'minute'
13
+ context 'when number after decimal point is equal or more than 5' do
14
+ num = 1.5
15
+ it 'is supposed to be rounded up' do
16
+ expect(Time.now.send(:word_form, num, word)).to eq 'less than 2 minutes'
17
+ end
18
+ end
19
+
20
+ context 'when number after decimal point is less than 5' do
21
+ num = 1.4
22
+ it 'is supposed to be rounded down' do
23
+ expect(Time.now.send(:word_form, num, word)).to eq 'more than 1 minute'
24
+ end
25
+ end
26
+ end
27
+
28
+ describe '#friendly_format' do
29
+ context 'when less than a minute' do
30
+ time = Time.now - 30
31
+ it 'returns string of moment ago' do
32
+ expect(time.friendly_format).to eq 'a moment ago'
33
+ end
34
+ end
35
+
36
+ context 'when from 1 minute to less 60 minute' do
37
+ time = Time.now - 60
38
+ it 'returns in minute format' do
39
+ expect(time.friendly_format).to include '1 minute ago'
40
+ end
41
+ end
42
+
43
+ context 'when from 1 hour to less than 24 hours' do
44
+ time = Time.now - 60 * 60
45
+ it 'returns in hour format' do
46
+ expect(time.friendly_format).to include '1 hour ago'
47
+ end
48
+ end
49
+
50
+ context 'when from 1 day to less than 7 days' do
51
+ time = Time.now - 60 * 60 * 24
52
+ it 'return in day format' do
53
+ expect(time.friendly_format).to include '1 day ago'
54
+ end
55
+ end
56
+
57
+ context 'when from 1 week to less than 4 weeks' do
58
+ time = Time.now - 60 * 60 * 24 * 7
59
+ it 'returns in week format' do
60
+ expect(time.friendly_format).to include '1 week ago'
61
+ end
62
+ end
63
+
64
+ context 'when from 1 month to less than 12 months' do
65
+ time = Time.now - 60 * 60 * 24 * 30
66
+ it 'returns in month format' do
67
+ expect(time.friendly_format).to include '1 month ago'
68
+ end
69
+ end
70
+
71
+ context 'when from 1 year to less than 10 years' do
72
+ time = Time.now - 60 * 60 * 24 * 365
73
+ it 'returns in year format' do
74
+ expect(time.friendly_format).to include '1 year ago'
75
+ end
76
+ end
77
+
78
+ context 'when more than or equal 5 years' do
79
+ time = Time.now - 60 * 60 * 24 * 365 * 5
80
+ it 'returns full format' do
81
+ # match regular expression (Thu, Nov 17th 2016 at 17:50 UTC)
82
+ expect(time.friendly_format).to match(/((Sun)|(Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat))(,\s)((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec))(\s)[0-9]{1,2}((st)|(nd)|(rd)|(th))(\s)[0-9]{4}(\s)(at)(\s)[0-9]{1,2}(:)[0-9]{1,2}(\s)[A-Z]{3}/)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'friendly_timestamp'
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: friendly_timestamp
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kuroun
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: 3.5.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.5.2
55
+ description:
56
+ email:
57
+ - kuroun.seung@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - CODE_OF_CONDUCT.md
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - app/assets/javascripts/friendly_timestamp.js
66
+ - bin/console
67
+ - bin/setup
68
+ - lib/friendly_timestamp.rb
69
+ - lib/friendly_timestamp/engine.rb
70
+ - lib/friendly_timestamp/version.rb
71
+ - spec/friendly_timestamp_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: https://github.com/kuroun/friendly_timestamp
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '2.0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.5.1
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A gem for display time and date based on local time zone in verbal informal
97
+ saying like more than two day ago, less one week ago...etc. It also provides method
98
+ for full format of date and time in local time zone as well.
99
+ test_files: []