browserly 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: 6a8d15b23d265199e1b11d6cb144ca1493255a8beaa7f28207534c9c3c93f277
4
+ data.tar.gz: 24a4800b88f94c1fca2ca42145457fb2cdd730bf3e396d441e2eb5a81617c3ce
5
+ SHA512:
6
+ metadata.gz: 4dcce2144fdd52a4347a13557e16380c4289329e7ccc9c2178b797374afd6928b2997d98008f1ade04c31d2f60cd82bd47a570908d3ae3b40bf1bbbac4cfe294
7
+ data.tar.gz: 771b69fbff3bc8b9f33ed134b4436685dda56e8e15b3a013b082205b6fec5480a610fa16a12411ba45d62da1d5a8d4293bcbcd9cadaf2f0849ea5c27303b4886
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Alexander Selivanov
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,28 @@
1
+ # Browserly
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 'browserly'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install browserly
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,33 @@
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 = 'Browserly'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
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
+
33
+ task default: :test
data/lib/browserly.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'singleton'
2
+ require 'comandor'
3
+ require 'selenium-webdriver'
4
+ require 'browserly/configuration'
5
+ require 'browserly/pool'
6
+ require 'browserly/screenshot'
7
+ require 'browserly/version'
8
+
9
+ module Browserly
10
+ class << self
11
+ attr_writer :configuration
12
+ end
13
+
14
+ def self.configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ def self.reset
19
+ @configuration = Configuration.new
20
+ end
21
+
22
+ def self.configure
23
+ yield(configuration)
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ module Browserly
2
+ class Configuration
3
+ attr_accessor :tmp_path
4
+
5
+ def initialize
6
+ @tmp_path = '../../tmp/screenshots'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Browserly
4
+ class Pool
5
+ include ::Singleton
6
+ attr_reader :browsers
7
+
8
+ def initialize
9
+ @browsers = SizedQueue.new(pool_size)
10
+ pool_size.times { @browsers.push(new_browser) }
11
+ end
12
+
13
+ def take_browser
14
+ @browsers.pop
15
+ end
16
+
17
+ def release!(browser)
18
+ @browsers.push(browser)
19
+ end
20
+
21
+ def terminate_all!
22
+ @browsers.each(&:quit)
23
+ end
24
+
25
+ private
26
+
27
+ def pool_size
28
+ ENV.fetch('BROWSER_POOL_SIZE') { 1 }.to_i
29
+ end
30
+
31
+ def new_browser
32
+ options = Selenium::WebDriver::Chrome::Options.new(
33
+ args: [
34
+ '--headless',
35
+ '--hide-scrollbars',
36
+ '--ignore-certificate-errors',
37
+ '--disable-popup-blocking',
38
+ '--disable-translate',
39
+ '--window-size=1280,780',
40
+ '--enable-font-antialiasing',
41
+ '--font-cache-shared-handle[6]'
42
+ ]
43
+ )
44
+ Selenium::WebDriver.for :chrome, options: options
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Browserly
4
+ class Screenshot
5
+ extend Comandor
6
+
7
+ attr_reader :browser, :pool, :file, :selector
8
+
9
+ def initialize(url, selector = '')
10
+ @url = url
11
+ @pool = Browserly::Pool.instance
12
+ @browser = pool.take_browser
13
+ @selector = selector
14
+ end
15
+
16
+ def perform
17
+ take_screenshot! && release_browser!
18
+ end
19
+
20
+ private
21
+
22
+ def take_screenshot!
23
+ browser.navigate.to @url
24
+ height = browser.execute_script(js) || 780
25
+ browser.manage.window.resize_to(1280, height)
26
+ @file = browser.save_screenshot(filename)
27
+ end
28
+
29
+ def release_browser!
30
+ pool.release!(browser)
31
+ end
32
+
33
+ def js
34
+ <<-JAVASCRIPT
35
+ element = document.getElementById('#{selector}');
36
+
37
+ if(element != null) {
38
+ return element.clientHeight;
39
+ } else {
40
+ return null
41
+ };
42
+ JAVASCRIPT
43
+ end
44
+
45
+ def filename
46
+ File.join(Browserly.configuration.tmp_path, "#{Time.now.to_i}.png")
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Browserly
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: browserly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Selivanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-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.1.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: comandor
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: selenium-webdriver
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Website screenshot for Ruby uses Selenium with Chrome Driver
56
+ email:
57
+ - selivandex@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/browserly.rb
66
+ - lib/browserly/configuration.rb
67
+ - lib/browserly/pool.rb
68
+ - lib/browserly/screenshot.rb
69
+ - lib/browserly/version.rb
70
+ homepage: http://github.com/softeamco/browserly
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.7.3
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Website screenshot for Ruby
94
+ test_files: []