a_moment_ago 1.1.4 → 1.1.5

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/locales/en.yml +14 -0
  3. data/lib/time_ago.rb +37 -43
  4. metadata +27 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 344f1a3b780de59d28c4104a8c0074ee10058de453b4b1e3bf12ab529dfe7f95
4
- data.tar.gz: 3c90ac591b0d2bd2eadd41f24bbce7a039bb71c0fefb09ff615d96b0946763cc
3
+ metadata.gz: 3a792ffaa8445ffe070a75deed346de34e1da6089a6bcb2a310ee8ea6d65f11c
4
+ data.tar.gz: 74834ab399dfdf547836b660ada9d144b8c5490939b0dcdeaa411128ea4da88e
5
5
  SHA512:
6
- metadata.gz: dc011d5771a93426bbc37093f17b1d4a545f3ca38e96b32239e4b1e258617db272a750da7d3c564ba9edc255e4d8d59c34adb5d70a25e3384c3b3dc6b2ceb169
7
- data.tar.gz: 0bb5caf2f2121c4b9a85b3405c9c71970f96c04ae157f4d9b22ed5b246bb090ce29c05f29f08d3ec2fa3e0c507b3d33529274dfeccba39a63d4d0419b0ed7480
6
+ metadata.gz: fe0dabfd50e598d14a81911e17688cb236d1cd278db4a8b10a66bafa67c29924baf4ee6409f6db4c0e66c40beac21cd057efecddc801f08b6f6a0aaa9bff5fa0
7
+ data.tar.gz: 8ef97d1ab9cfcc54ce672e307629f8cefbe1500caa04079bf42032b99fee07687a0293aefd285b60ae696c75bf68ba60477ec3e6df64e3ccc8c7ffa47103ac1d
@@ -0,0 +1,14 @@
1
+ en:
2
+ time_ago:
3
+ just_now: "just now"
4
+ seconds_ago:
5
+ one: "1 second ago"
6
+ other: "%{count} seconds ago"
7
+ minutes_ago:
8
+ one: "1 minute ago"
9
+ other: "%{count} minutes ago"
10
+ hours_ago:
11
+ one: "1 hour ago"
12
+ other: "%{count} hours ago"
13
+ yesterday_at: "Yesterday at %{time}"
14
+ other: "%{time}"
data/lib/time_ago.rb CHANGED
@@ -1,45 +1,39 @@
1
- class TimeAgo
2
- def initialize
3
- @now = Time.now
4
- end
5
-
6
- def time_ago
7
- time = ""
8
- if @now.sec === 0
9
- time = "just now"
10
- elsif @now.sec < 60
11
- time = @now.sec.to_s + " s ago"
12
- elsif @now.min === 0
13
- time = @now.hour.to_s + " h ago"
14
- elsif @now.min < 60
15
- time = @now.min.to_s + " m ago"
16
- elsif @now.hour === 0
17
- time = "today at " + @now.hour.to_s + ":" + @now.min.to_s
18
- elsif @now.hour < 24
19
- time = @now.hour.to_s + " hrs ago"
20
- elsif @now.day === 0
21
- time = "yesterday at " + @now.hour.to_s + ":" + @now.min.to_s
22
- elsif @now.day === 1
23
- time = "Monday at " + @now.hour.to_s + ":" + @now.min.to_s
24
- elsif @now.day === 2
25
- time = "Tuesday at " + @now.hour.to_s + ":" + @now.min.to_s
26
- elsif @now.day === 3
27
- time = "Wednesday at " + @now.hour.to_s + ":" + @now.min.to_s
28
- elsif @now.day === 4
29
- time = "Thursday at " + @now.hour.to_s + ":" + @now.min.to_s
30
- elsif @now.day === 5
31
- time = "Friday at " + @now.hour.to_s + ":" + @now.min.to_s
32
- elsif @now.day === 6
33
- time = "Saturday at " + @now.hour.to_s + ":" + @now.min.to_s
34
- elsif @now.day === 7
35
- time = "Sunday at " + @now.hour.to_s + ":" + @now.min.to_s
36
- else
37
- time = "on " + @now.strftime("%A") + " at " + @now.hour.to_s + ":" + @now.min.to_s
38
- end
39
-
40
- puts "#{time}"
1
+ require "i18n"
2
+
3
+ module TimeAgo
4
+ DEFAULT_LOCALE = :en
5
+
6
+ def self.format(time, locale: DEFAULT_LOCALE)
7
+ I18n.load_path << Dir[File.expand_path("locales/*.yml", __dir__)]
8
+ I18n.default_locale = DEFAULT_LOCALE
9
+ I18n.locale = locale
10
+
11
+ distance_in_seconds = (Time.current - time).to_i
12
+
13
+ if distance_in_seconds < 5
14
+ I18n.t("time_ago.just_now")
15
+ elsif distance_in_seconds < 60
16
+ I18n.t("time_ago.seconds_ago", count: distance_in_seconds)
17
+ elsif distance_in_seconds < 3600
18
+ minutes_ago = (distance_in_seconds / 60).to_i
19
+ I18n.t("time_ago.minutes_ago", count: minutes_ago)
20
+ elsif distance_in_seconds < 86400
21
+ hours_ago = (distance_in_seconds / 3600).to_i
22
+ I18n.t("time_ago.hours_ago", count: hours_ago)
23
+ elsif distance_in_seconds < 172_800
24
+ I18n.t("time_ago.yesterday_at", time: I18n.l(time, format: :time))
25
+ else
26
+ I18n.t("time_ago.other", time: I18n.l(time, format: :short))
41
27
  end
42
28
  end
43
-
44
- # created = TimeAgo.new
45
- # created.time_ago
29
+ end
30
+
31
+ class Time
32
+ def self.localized_format(time, locale: TimeAgo::DEFAULT_LOCALE)
33
+ TimeAgo.format(time, locale: locale)
34
+ end
35
+
36
+ def ago(locale: TimeAgo::DEFAULT_LOCALE)
37
+ TimeAgo.format(self, locale: locale)
38
+ end
39
+ end
metadata CHANGED
@@ -1,28 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: a_moment_ago
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Webster Avosa
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-11 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Rendering time in words, e.g. 'just now', '1 minute ago', '2 hours ago',
14
- etc.
15
- email: webster@simplyvirtual.io
11
+ date: 2023-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A gem that formats time intervals as relative dates in Ruby/Rails
28
+ email:
29
+ - webster@webstercodes.com
16
30
  executables: []
17
31
  extensions: []
18
32
  extra_rdoc_files: []
19
33
  files:
34
+ - lib/locales/en.yml
20
35
  - lib/time_ago.rb
21
- homepage: https://rubygems.org/gems/hola
36
+ homepage: https://rubygems.org/gems/a_moment_ago
22
37
  licenses:
23
38
  - MIT
24
39
  metadata: {}
25
- post_install_message:
40
+ post_install_message:
26
41
  rdoc_options: []
27
42
  require_paths:
28
43
  - lib
@@ -37,8 +52,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
52
  - !ruby/object:Gem::Version
38
53
  version: '0'
39
54
  requirements: []
40
- rubygems_version: 3.2.3
41
- signing_key:
55
+ rubygems_version: 3.4.13
56
+ signing_key:
42
57
  specification_version: 4
43
- summary: Rendering time in words
58
+ summary: Localized relative date/time formatting gem
44
59
  test_files: []