gif_countdown 0.1.0

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
+ SHA256:
3
+ metadata.gz: 05a548ff4d76da19d2715dd9427d7110ede683c572c09212128d250023b4f97e
4
+ data.tar.gz: d831c3ef65065ce5fa268d59e6da763d4ac1571597b3aeb4fd65a59f734a1988
5
+ SHA512:
6
+ metadata.gz: 1e335003414fd5b0a3d4fc1ab0e2248bfc7cc49547744790fc2309ff4bde4bc0268908f9c59244b2adc000c59d7a4152fc5d03ff50e31cdcee0ff9d6e8222591
7
+ data.tar.gz: 5d08fe0e3dc334109e2c6e534ca5a5778bb08455ce52f55851be0c2aaa2beb5d2d496a1be258d683aff52edd874f36c97b646e091390936ddf9cd002fe3358c1
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Stefan Wienert
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # GifCountdown
2
+
3
+ Create a animated gif countdown picture using RMagick.
4
+
5
+ Each frame is a second.
6
+
7
+ Useful for mailings campaign / event notifications.
8
+
9
+ ![Example](https://raw.githubusercontent.com/zealot128/gif_countdown/main/doc/example.gif)
10
+
11
+ ## Usage
12
+
13
+ ```
14
+ blob = GifCountdown.generate(distance_in_seconds: distance_in_seconds, duration: 5)
15
+ ```
16
+
17
+ Usage in a Rails controller:
18
+
19
+ ```ruby
20
+ def show
21
+ distance_in_seconds = (some_future_event - Time.zone.now).round
22
+
23
+ blob = GifCountdown.generate(distance_in_seconds: distance_in_seconds, duration: 10)
24
+ send_data blob, type: 'image/gif', disposition: 'inline'
25
+ end
26
+ ```
27
+
28
+ Make sure to build a minute cache or something, as building that images might take a couple of seconds every time!
29
+
30
+ ### Available Keyword Arguments:
31
+
32
+ With defaults
33
+
34
+ ```
35
+ distance_in_seconds: nil #Number of seconds to countdown
36
+ width: 535
37
+ height: 61
38
+ fontsize: 30
39
+ padding_x: 10
40
+ duration: 60 # how many seconds to generate - linearily takes longer to generator
41
+ font: 'Ubuntu' # That font must be installed on the target host that generates the image!
42
+ ```
43
+
44
+ ### I18n
45
+
46
+ It uses "I18n" to build up the words "Days", "Hours", "Seconds", "Minutes" with plurarlization. The Gem ships with en/de.
47
+
48
+
49
+ ## Installation
50
+ Add this line to your application's Gemfile:
51
+
52
+ ```ruby
53
+ gem 'gif_countdown'
54
+ ```
55
+
56
+ And then execute:
57
+ ```bash
58
+ $ bundle
59
+ ```
60
+
61
+ Or install it yourself as:
62
+ ```bash
63
+ $ gem install gif_countdown
64
+ ```
65
+
66
+ ## Contributing
67
+ Contribution directions go here.
68
+
69
+ ## License
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'bundler/gem_tasks'
@@ -0,0 +1,14 @@
1
+ de:
2
+ gif_countdown:
3
+ days:
4
+ one: Tag
5
+ other: Tage
6
+ hours:
7
+ one: Stunde
8
+ other: Stunden
9
+ minutes:
10
+ one: Minute
11
+ other: Minuten
12
+ seconds:
13
+ one: Sekunde
14
+ other: Sekunden
@@ -0,0 +1,15 @@
1
+ en:
2
+ gif_countdown:
3
+ days:
4
+ one: day
5
+ other: days
6
+ hours:
7
+ one: hour
8
+ other: hours
9
+ minutes:
10
+ one: minute
11
+ other: minutes
12
+ seconds:
13
+ one: second
14
+ other: seconds
15
+
@@ -0,0 +1,11 @@
1
+ if defined?(Rails::Engine)
2
+ require "gif_countdown/engine"
3
+ end
4
+
5
+ require 'gif_countdown/generator'
6
+
7
+ module GifCountdown
8
+ def self.generate(**args)
9
+ GifCountdown::Generator.new(**args).call
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module GifCountdown
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace GifCountdown
4
+ end
5
+ end
@@ -0,0 +1,101 @@
1
+ require 'rmagick'
2
+
3
+ class GifCountdown::Generator
4
+ attr_reader :width, :height, :fontsize, :padding_x, :font
5
+
6
+ def initialize(distance_in_seconds:, width: 535, height: 61, fontsize: 30, padding_x: 10, duration: 60, font: 'Ubuntu')
7
+ @distance_in_seconds = distance_in_seconds
8
+ @width = width
9
+ @height = height
10
+ @fontsize = fontsize
11
+ @padding_x = padding_x
12
+ @duration = duration
13
+ @font = font
14
+ end
15
+
16
+ def call
17
+ gif = Magick::ImageList.new
18
+ gif.delay = 100
19
+ gif.ticks_per_second = 100
20
+
21
+ @duration.times do |i|
22
+ image = Magick::Image.new(width, height)
23
+ image.delay = 100
24
+ annotate_with_countdown(image: image, seconds: @distance_in_seconds - i)
25
+ gif << image
26
+ end
27
+ gif.to_blob { self.format = 'gif' }
28
+ end
29
+
30
+ def pluralize(count, key)
31
+ I18n.t("gif_countdown.#{key}", count: count)
32
+ end
33
+
34
+ def annotate_with_countdown(image:, seconds:)
35
+ this = self
36
+ draw = Magick::Draw.new
37
+ draw.gravity = Magick::NorthGravity
38
+ boxwidth = (width - padding_x * 5) / 4.0
39
+
40
+ parts = seconds_to_countdown(seconds)
41
+
42
+ text_y = height / 2 + padding_x / 2
43
+ x = padding_x
44
+ image.annotate(draw, boxwidth, height / 2, x, 0, parts[:days]) {
45
+ self.pointsize = this.fontsize
46
+ self.font_family = this.font
47
+ }
48
+ image.annotate(draw, boxwidth, height / 2, x, text_y, pluralize(parts[:days], :days)) {
49
+ self.pointsize = (this.fontsize * 0.7).round
50
+ self.font_family = this.font
51
+ }
52
+
53
+ x = padding_x * 2 + boxwidth
54
+ image.annotate(draw, boxwidth, height / 2, x, 0, parts[:hours]) {
55
+ self.pointsize = this.fontsize
56
+ self.font_family = this.font
57
+ }
58
+ image.annotate(draw, boxwidth, height / 2, x, text_y, pluralize(parts[:hours], :hours)) {
59
+ self.pointsize = (this.fontsize * 0.7).round
60
+ self.font_family = this.font
61
+ }
62
+
63
+ x = padding_x * 3 + boxwidth * 2
64
+ image.annotate(draw, boxwidth, height / 2, x, 0, parts[:minutes]) {
65
+ self.pointsize = this.fontsize
66
+ self.font_family = this.font
67
+ }
68
+ image.annotate(draw, boxwidth, height / 2, x, text_y, pluralize(parts[:minutes], :minutes)) {
69
+ self.pointsize = (this.fontsize * 0.7).round
70
+ self.font_family = this.font
71
+ }
72
+
73
+ x = padding_x * 4 + boxwidth * 3
74
+ image.annotate(draw, boxwidth, height / 2, x, 0, parts[:seconds]) {
75
+ self.pointsize = this.fontsize
76
+ self.font_family = this.font
77
+ }
78
+ image.annotate(draw, boxwidth, height / 2, x, text_y, pluralize(parts[:seconds], :seconds)) {
79
+ self.pointsize = (this.fontsize * 0.7).round
80
+ self.font_family = this.font
81
+ }
82
+ end
83
+
84
+ def seconds_to_countdown(seconds)
85
+ days = seconds / 1.day
86
+ seconds -= days * 1.day.to_i
87
+
88
+ hours = seconds / 1.hour
89
+ seconds -= hours * 1.hour.to_i
90
+
91
+ minutes = seconds / 1.minute
92
+ seconds -= minutes * 1.minute.to_i
93
+
94
+ {
95
+ days: sprintf("%02d", days),
96
+ hours: sprintf("%02d", hours),
97
+ minutes: sprintf("%02d", minutes),
98
+ seconds: sprintf("%02d", seconds)
99
+ }
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ module GifCountdown
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gif_countdown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stefan Wienert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-02-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rmagick
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: i18n
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Create a countdown timer animated GIF
56
+ email:
57
+ - info@stefanwienert.de
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - config/locales/gif_countdown.de.yml
66
+ - config/locales/gif_countdown.en.yml
67
+ - lib/gif_countdown.rb
68
+ - lib/gif_countdown/engine.rb
69
+ - lib/gif_countdown/generator.rb
70
+ - lib/gif_countdown/version.rb
71
+ homepage: https://github.com/pludoni/gif_countdown
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.7.6
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Create a countdown timer animated GIF
95
+ test_files: []