omg_image 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
+ SHA256:
3
+ metadata.gz: 2e1e0241bab9c052e42bc9319cac7fcaf7ea68999c8032f8578d92876f4cd5b2
4
+ data.tar.gz: 7c8a7684a69bc13d0bdcaf91aa08565dfbe49d84f55da595666459066fb49501
5
+ SHA512:
6
+ metadata.gz: bcef2a421d5106171d8c269a50bf353ce676953a9a45c2cced6cf667352a9dfcd36f8bc977c0c440314fc30e826e95c55a205319f0c2797b21a1775afb73038a
7
+ data.tar.gz: 91f6c00b8688af4a216dd5b18c9e2b30b06d8b4265b9248611179b1262722e2eb95548a3f95bd6f6220fd166249630c46cd51cb4fb109fbd11a0b973cbe8abb5
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Igor Kasyanchuk
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,39 @@
1
+ # OmgImage
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'omg_image'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install omg_image
22
+ ```
23
+
24
+ ## Development
25
+
26
+ - install chrome headless
27
+ - add to `/etc/hosts` new host `127.0.0.0 site.com`
28
+ - in `puma.rb` put `workers ENV.fetch("WEB_CONCURRENCY") { 3 }` (required because screenshot is made from another request)
29
+ -
30
+
31
+ ## Issues
32
+
33
+ - if you process too many requests and because of timeouts dead processes may appear. To kill them `parents_of_dead_kids=$(ps -ef | grep [d]efunct | awk '{print $3}' | sort | uniq | egrep -v '^1$'); echo "$parents_of_dead_kids" | xargs kill`
34
+
35
+ ## Contributing
36
+ Contribution directions go here.
37
+
38
+ ## License
39
+ 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,32 @@
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 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'OmgImage'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/omg_image .js
2
+ //= link_directory ../stylesheets/omg_image .css
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require rails-ujs
14
+ //= require activestorage
15
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ module OmgImage
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module OmgImage
2
+ class PreviewsController < ApplicationController
3
+ layout nil
4
+
5
+ def show
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,75 @@
1
+ require 'uri'
2
+ require 'tempfile'
3
+ require 'timeout'
4
+ require 'open4'
5
+
6
+ module OmgImage
7
+ module ApplicationHelper
8
+ include OmgHelper
9
+
10
+ def omg_image_preview(template, options)
11
+ options ||= {}
12
+ options[:size] ||= '800,400'
13
+ options[:key] ||= options.to_s
14
+
15
+ image = OmgImage::Image.find_by(key: options[:key])
16
+ if image && !image.file.attached?
17
+ image.destroy
18
+ image = nil
19
+ end
20
+
21
+ file = image&.file || create_screenshot(template, options)
22
+
23
+ url_for(file) if file
24
+ end
25
+
26
+ private
27
+
28
+ def create_screenshot(template, options)
29
+ begin
30
+ start = Time.now
31
+ body = OmgImage::Renderer.render(template, locals: options)
32
+ log_smt " to_html: #{(Time.now - start).round(2)}"
33
+
34
+ input = BetterTempfile.new("input.html")
35
+ input.write(body)
36
+ input.flush
37
+
38
+ output = BetterTempfile.new("image.png")
39
+
40
+ options = { file: output, size: options[:size], path: input.path }
41
+ command = build_chrome_command(options)
42
+ log_smt " => #{command}"
43
+
44
+ start = Time.now
45
+ begin
46
+ process = open4.spawn(command, timeout: 10)
47
+ rescue Timeout::Error
48
+ Process.kill('KILL', process.pid) rescue nil
49
+ end
50
+ log_smt " to_image: #{(Time.now - start).round(2)}"
51
+
52
+ image = OmgImage::Image.find_or_create_by(key: options[:key])
53
+ if !image.file.attached?
54
+ image.file.attach(io: File.open(output.path), filename: "image.png", content_type: "image/png")
55
+ image.save
56
+ end
57
+
58
+ image.file
59
+ ensure
60
+ output&.close!
61
+ input&.close!
62
+ end
63
+ end
64
+
65
+ def build_chrome_command(options)
66
+ size_opts = options[:size] == :auto ? nil : "--window-size=#{options[:size]}"
67
+ "google-chrome --headless --disable-gpu --no-sandbox --ignore-certificate-errors --screenshot=#{options[:file].path} #{size_opts} \"file://#{options[:path]}\""
68
+ end
69
+
70
+ def log_smt(str)
71
+ Rails.logger.debug(str)
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,58 @@
1
+ module OmgImage
2
+ module OmgHelper
3
+ COLORS = [
4
+ {
5
+ bg: "#19063A",
6
+ text: "#dce9f3",
7
+ },
8
+ {
9
+ bg: "#080E3B",
10
+ text: "#ffffff",
11
+ },
12
+ {
13
+ bg: "#010C1F",
14
+ text: "#aebcd5",
15
+ },
16
+ {
17
+ bg: "#d7dee2",
18
+ text: "#022235",
19
+ },
20
+ {
21
+ bg: "#161616",
22
+ text: "#66e2d5",
23
+ },
24
+ {
25
+ bg: "#1c0bba",
26
+ text: "#c9d2dd",
27
+ },
28
+ {
29
+ bg: '#449833',
30
+ text: '#fff'
31
+ },
32
+ {
33
+ bg: '#f31e25',
34
+ text: '#fff'
35
+ },
36
+ {
37
+ bg: '#f5681d',
38
+ text: '#fff'
39
+ },
40
+ {
41
+ bg: '#3d9fe1',
42
+ text: '#fff'
43
+ },
44
+ {
45
+ bg: '#930b99',
46
+ text: '#fff'
47
+ },
48
+ {
49
+ bg: '#f2169a',
50
+ text: '#fff'
51
+ },
52
+ ].shuffle.freeze
53
+
54
+ def assigned_color(id = rand(COLORS.size))
55
+ COLORS[id % COLORS.size]
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,4 @@
1
+ module OmgImage
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module OmgImage
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module OmgImage
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: omg_image_images
4
+ #
5
+ # id :integer not null, primary key
6
+ # key :string
7
+ # created_at :datetime
8
+ #
9
+
10
+ module OmgImage
11
+ class Image < ApplicationRecord
12
+ has_one_attached :file
13
+ validates :key, presence: true
14
+ end
15
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ OmgImage::Engine.routes.draw do
2
+ end
@@ -0,0 +1,8 @@
1
+ class CreateOmgImageImages < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :omg_image_images do |t|
4
+ t.string :key, index: true, unique: true
5
+ t.datetime :created_at
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ class OmgGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ def create_omg_file
5
+ directory "app/omg"
6
+ end
7
+ end
@@ -0,0 +1,102 @@
1
+ <% accent_color = '#FFF' %>
2
+ <% color = '#FFF' %>
3
+ <% dark_color = "#000" %>
4
+ <% description_bg_color = 'yellow' %>
5
+ <% description_color = '#000' %>
6
+ <% border_color = '#000' %>
7
+ <% shadow_color = '#555' %>
8
+
9
+ <style>
10
+ body{
11
+ background: white;
12
+ margin:0;
13
+ padding:0;
14
+ overflow: hidden;
15
+ position:relative;
16
+ }
17
+ .preview-div-wrapper {
18
+ overflow: hidden;
19
+ position:relative;
20
+ }
21
+ .preview-div {
22
+ background: #fff;
23
+ border: 0.25vw solid <%= border_color %>;
24
+ box-shadow: 0.8vw 1vw 0px <%= shadow_color %>;
25
+ width: 88vw;
26
+ height: 44vw;
27
+ margin: 2.5vw auto 5vw;
28
+ overflow: hidden;
29
+ position:relative;
30
+ }
31
+ .preview-info-header {
32
+ color: black;
33
+ margin: 2vw auto 0vw;
34
+ font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
35
+ font-size: 3.5vw;
36
+ width: 92%;
37
+ }
38
+ h1 {
39
+ color: <%= dark_color %>;
40
+ width: 92%;
41
+ margin: 1vw auto;
42
+ font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
43
+ }
44
+ .preview-description {
45
+ font-size: 3.5vw;
46
+ position: absolute;
47
+ left: 1.5vw;
48
+ bottom: 1.5vw;
49
+ padding: 1vw 2vw;
50
+ font-weight: 300;
51
+ display: flex;
52
+ flex-flow: row;
53
+ height: 4vh;
54
+ align-items: center;
55
+ }
56
+ .preview-description img {
57
+ display: inline-block;
58
+ width: 3vw;
59
+ }
60
+ .preview-description span {
61
+ font-size: 2.0vw;
62
+ padding-top: 1vh;
63
+ padding-left: 0.5vw;
64
+ }
65
+ .preview-tags {
66
+ position: absolute;
67
+ right: 1.5vw;
68
+ bottom: 1.5vw;
69
+ padding: 1vw 2vw;
70
+ font-weight: 300;
71
+ }
72
+ .preview-tags .tag {
73
+ font-size: 2.0vw;
74
+ margin-right: 0.8vw;
75
+ padding: 0.4vw 0.8vw;
76
+ }
77
+ </style>
78
+
79
+ <div class="preview-div-wrapper">
80
+ <div class="preview-div">
81
+ <% font_size = 15 - (((title.size**0.89)+85)/16) %>
82
+ <h1 style="font-size:<%= font_size %>vw;"><%= raw title %><h1>
83
+
84
+ <div class="preview-description">
85
+ <%= image_tag 'logo.svg' %>
86
+ <span><%= description %></span>
87
+ </div>
88
+
89
+ <% if tags.any? %>
90
+ <div class="preview-tags">
91
+ <% shift = rand(10) %>
92
+ <% tags.each_with_index do |tag, index| %>
93
+ <% color = assigned_color(shift + index) %>
94
+ <span class='tag' style="background: <%= color[:bg] %>; color: <%= color[:text] %>">
95
+ <%= tag %>
96
+ </span>
97
+ <% end %>
98
+ </div>
99
+ <% end %>
100
+
101
+ </div>
102
+ </div>
@@ -0,0 +1,22 @@
1
+ <style>
2
+ body{
3
+ background: yellow;
4
+ display: flex;
5
+ flex-direction: columns;
6
+ align-items: center;
7
+ text-align: center;
8
+ }
9
+ h1 {
10
+ color: #000;
11
+ width: 92%;
12
+ margin: 1vw auto;
13
+ font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
14
+ }
15
+ </style>
16
+
17
+ <div class="preview-div-wrapper">
18
+ <div class="preview-div">
19
+ <% font_size = 15 - (((title.size**0.89)+85)/16) %>
20
+ <h1 style="font-size:<%= font_size %>vw;"><%= raw title %><h1>
21
+ </div>
22
+ </div>
@@ -0,0 +1,11 @@
1
+ module OmgImage
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace OmgImage
4
+
5
+ initializer 'rails_db.helpers' do
6
+ ActiveSupport.on_load :action_view do
7
+ ActionView::Base.send :include, OmgImage::ApplicationHelper
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module OmgImage
2
+ class Renderer
3
+ def Renderer.render(template, layout: nil, locals: {})
4
+ ::ApplicationController.render(
5
+ file: "/#{Rails.root}/app/omg/#{template}",
6
+ layout: layout,
7
+ locals: locals
8
+ )
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module OmgImage
2
+ VERSION = '0.0.1'
3
+ end
data/lib/omg_image.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "better_tempfile"
2
+
3
+ require "omg_image/engine"
4
+ require "omg_image/renderer"
5
+
6
+ module OmgImage
7
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omg_image
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Igor Kasyanchuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: open4
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: better_tempfile
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
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: puma
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Description of OmgImage.
98
+ email:
99
+ - igorkasyanchuk@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - MIT-LICENSE
105
+ - README.md
106
+ - Rakefile
107
+ - app/assets/config/omg_image_manifest.js
108
+ - app/assets/javascripts/omg_image/application.js
109
+ - app/assets/stylesheets/omg_image/application.css
110
+ - app/controllers/omg_image/application_controller.rb
111
+ - app/controllers/omg_image/previews_controller.rb
112
+ - app/helpers/omg_image/application_helper.rb
113
+ - app/helpers/omg_image/omg_helper.rb
114
+ - app/jobs/omg_image/application_job.rb
115
+ - app/mailers/omg_image/application_mailer.rb
116
+ - app/models/omg_image/application_record.rb
117
+ - app/models/omg_image/image.rb
118
+ - config/routes.rb
119
+ - db/migrate/20180928082949_create_omg_image_images.rb
120
+ - lib/generators/omg_generator.rb
121
+ - lib/generators/templates/app/omg/simple.html.erb
122
+ - lib/generators/templates/app/omg/square.html.erb
123
+ - lib/omg_image.rb
124
+ - lib/omg_image/engine.rb
125
+ - lib/omg_image/renderer.rb
126
+ - lib/omg_image/version.rb
127
+ homepage: https://github.com/igorkasyanchuk/omg_image
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.7.6
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Summary of OmgImage.
151
+ test_files: []