yabbie 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -4
- data/lib/yabbie/config.json +4 -0
- data/lib/yabbie/configuration.rb +44 -0
- data/lib/yabbie/default.js +13 -0
- data/lib/yabbie/exceptions.rb +22 -0
- data/lib/yabbie/slimer.rb +100 -0
- data/lib/yabbie/source.rb +27 -0
- data/lib/yabbie/version.rb +1 -1
- data/lib/yabbie.rb +5 -4
- data/spec/fixtures/source_file.html +2 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/yabbie/configuration_spec.rb +83 -0
- data/spec/yabbie/slimer_spec.rb +46 -0
- data/spec/yabbie/source_spec.rb +30 -0
- data/yabbie.gemspec +2 -2
- metadata +25 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 045f4caf54997ca331c1e5bd32c22987ce619283
|
4
|
+
data.tar.gz: fc9a0a723cdbbfd95cbff0b022bc266fc6819bae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a97981fbdf90fe1944d80ea2eea26b3b1eed1e5aa94ade71d3e377d631ee1ffa6ba6c7d9d8f8a424a4635c8003ad79a04287b533653b25d67c2e6f20ac16e001
|
7
|
+
data.tar.gz: fc17735fedb7704d334923456a77ede6083a1bdbd4dbebf49fba579a81b04953b604e512cc7455fbf0694cf37ef3c8408ca0e8b6a650f62b77f8fe30c6a6b9fd
|
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# Yabbie
|
2
2
|
|
3
|
-
|
3
|
+
Yabbie is a gem to wrap SlimerJS.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
gem 'yabbie'
|
9
|
+
gem 'yabbie', github: 'net-engine/yabbie'
|
10
10
|
|
11
11
|
And then execute:
|
12
12
|
|
@@ -16,14 +16,35 @@ Or install it yourself as:
|
|
16
16
|
|
17
17
|
$ gem install yabbie
|
18
18
|
|
19
|
+
## Dependency
|
20
|
+
|
21
|
+
It relies on [SlimerJS](http://slimerjs.org), and a script that contains what you want to achieve.
|
22
|
+
For the script, you can check our [default.js](https://github.com/net-engine/yabbie/blob/master/lib/yabbie/default.js). It does nothing but takes a png snapshot. It is just so that Yabbie does not fail if you don't provide one. Please don't rely on it.
|
23
|
+
|
19
24
|
## Usage
|
20
25
|
|
21
|
-
|
26
|
+
Yabbie::Slimer.new(html_file, options).to_file(file)
|
27
|
+
|
28
|
+
For more usage, please investigate in the code while we don't build a nice README
|
29
|
+
|
30
|
+
## Roadmap
|
31
|
+
|
32
|
+
1. Add tests, sorry for that. :)
|
33
|
+
|
34
|
+
## And the oscar goes to...
|
35
|
+
|
36
|
+
This gem is inspired on [Shrimp](https://github.com/adjust/shrimp) gem.
|
37
|
+
|
38
|
+
A huge thanks to all Shrimp contributors.
|
22
39
|
|
23
40
|
## Contributing
|
24
41
|
|
25
|
-
1. Fork it (
|
42
|
+
1. Fork it (https://github.com/net-engine/yabbie/fork)
|
26
43
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
44
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
45
|
4. Push to the branch (`git push origin my-new-feature`)
|
29
46
|
5. Create a new Pull Request
|
47
|
+
|
48
|
+
## License
|
49
|
+
|
50
|
+
MIT
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
|
3
|
+
module Yabbie
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :default_options
|
6
|
+
|
7
|
+
[:format, :tmpdir, :timeout, :config_file, :error_log_file, :width, :height, :prepend_command, :script].each do |m|
|
8
|
+
define_method("#{m}=") do |val|
|
9
|
+
@default_options[m] = val
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@default_options = {
|
15
|
+
format: 'png',
|
16
|
+
tmpdir: Dir.tmpdir,
|
17
|
+
timeout: 120000,
|
18
|
+
config_file: File.expand_path('../config.json', __FILE__),
|
19
|
+
width: 600,
|
20
|
+
height: 600,
|
21
|
+
error_log_file: Dir.tmpdir + "/log/yabbie_error.log",
|
22
|
+
prepend_command: '',
|
23
|
+
script: 'default.js'
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def slimerjs
|
28
|
+
@slimerjs ||= `which slimerjs`.chomp
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class << self
|
33
|
+
attr_accessor :configuration
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.configuration
|
37
|
+
@configuration ||= Configuration.new
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.configure
|
41
|
+
yield(configuration)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
var webpage = require('webpage'),
|
2
|
+
system = require('system');
|
3
|
+
|
4
|
+
var url = system.args[1];
|
5
|
+
|
6
|
+
webpage.open(url)
|
7
|
+
.then(function() {
|
8
|
+
webpage.viewportSize = { width: 650, height: 320 };
|
9
|
+
webpage.render('page.png', { onlyViewport: true });
|
10
|
+
|
11
|
+
slimer.exit();
|
12
|
+
});
|
13
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Yabbie
|
2
|
+
class NoExecutableError < StandardError
|
3
|
+
def initialize
|
4
|
+
msg = "No slimerjs executable found at #{Yabbie.configuration.slimerjs}\n"
|
5
|
+
msg << ">> Please install slimerjs"
|
6
|
+
super(msg)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class ImproperSourceError < StandardError
|
11
|
+
def initialize(msg = nil)
|
12
|
+
super("Improper Source: #{msg}")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class RenderingError < StandardError
|
17
|
+
def initialize(msg = nil)
|
18
|
+
super("Rendering Error: #{msg}")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
|
3
|
+
module Yabbie
|
4
|
+
class Slimer
|
5
|
+
attr_accessor :source, :configuration
|
6
|
+
attr_reader :options, :result, :error
|
7
|
+
|
8
|
+
def initialize(url_or_file, options = {})
|
9
|
+
@source = Source.new(url_or_file)
|
10
|
+
@options = Yabbie.configuration.default_options.merge(options)
|
11
|
+
|
12
|
+
raise NoExecutableError.new unless File.exists?(Yabbie.configuration.slimerjs)
|
13
|
+
end
|
14
|
+
|
15
|
+
def run
|
16
|
+
@error = nil
|
17
|
+
@result = `#{cmd}`
|
18
|
+
|
19
|
+
unless $?.exitstatus == 0
|
20
|
+
@error = @result
|
21
|
+
@result = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
@result
|
25
|
+
end
|
26
|
+
|
27
|
+
def cmd
|
28
|
+
[
|
29
|
+
prepend_command,
|
30
|
+
Yabbie.configuration.slimerjs,
|
31
|
+
command_config_file,
|
32
|
+
command_error_log,
|
33
|
+
script,
|
34
|
+
@source.to_s.shellescape,
|
35
|
+
outfile,
|
36
|
+
width,
|
37
|
+
height,
|
38
|
+
format,
|
39
|
+
timeout
|
40
|
+
].join(" ").strip
|
41
|
+
end
|
42
|
+
|
43
|
+
def render(path = nil)
|
44
|
+
@outfile = File.expand_path(path) if path
|
45
|
+
self.run
|
46
|
+
@outfile
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_file(path = nil)
|
50
|
+
self.render(path)
|
51
|
+
File.new(@outfile)
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_string(path = nil)
|
55
|
+
File.open(self.render(path)).read
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def format
|
61
|
+
options[:format]
|
62
|
+
end
|
63
|
+
|
64
|
+
def timeout
|
65
|
+
options[:timeout]
|
66
|
+
end
|
67
|
+
|
68
|
+
def width
|
69
|
+
options[:width]
|
70
|
+
end
|
71
|
+
|
72
|
+
def height
|
73
|
+
options[:height]
|
74
|
+
end
|
75
|
+
|
76
|
+
def script
|
77
|
+
options[:script]
|
78
|
+
end
|
79
|
+
|
80
|
+
def prepend_command
|
81
|
+
options[:prepend_command]
|
82
|
+
end
|
83
|
+
|
84
|
+
def command_config_file
|
85
|
+
"--config=#{options[:config_file]}"
|
86
|
+
end
|
87
|
+
|
88
|
+
def command_error_log
|
89
|
+
"--error-log-file=#{options[:error_log_file]}"
|
90
|
+
end
|
91
|
+
|
92
|
+
def outfile
|
93
|
+
if @outfile
|
94
|
+
File.expand_path(@outfile)
|
95
|
+
else
|
96
|
+
"#{options[:tmpdir]}/#{Digest::MD5.hexdigest((Time.now.to_i + rand(9001)).to_s)}.#{format}"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module Yabbie
|
4
|
+
class Source
|
5
|
+
def initialize(url_or_file)
|
6
|
+
@source = url_or_file
|
7
|
+
raise ImproperSourceError.new unless url? || file?
|
8
|
+
end
|
9
|
+
|
10
|
+
def url?
|
11
|
+
@source.is_a?(String) && @source.match(URI::regexp)
|
12
|
+
end
|
13
|
+
|
14
|
+
def file?
|
15
|
+
@source.kind_of?(File)
|
16
|
+
end
|
17
|
+
|
18
|
+
def html?
|
19
|
+
!(url? || file?)
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
file? ? @source.path : @source
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
data/lib/yabbie/version.rb
CHANGED
data/lib/yabbie.rb
CHANGED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Yabbie::Configuration do
|
4
|
+
subject { described_class.new }
|
5
|
+
|
6
|
+
describe 'accessors' do
|
7
|
+
context 'when values are not provided' do
|
8
|
+
it 'falls back to defaults' do
|
9
|
+
defaults = { format: 'png',
|
10
|
+
tmpdir: Dir.tmpdir,
|
11
|
+
timeout: 120000,
|
12
|
+
config_file: File.expand_path('../../../lib/yabbie/config.json', __FILE__),
|
13
|
+
width: 600,
|
14
|
+
height: 600,
|
15
|
+
error_log_file: Dir.tmpdir + "/log/yabbie_error.log",
|
16
|
+
prepend_command: '',
|
17
|
+
script: 'default.js' }
|
18
|
+
|
19
|
+
expect(subject.default_options).to eq(defaults)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'defines accessor to format' do
|
24
|
+
subject.format = 'jpg'
|
25
|
+
expect(subject.default_options[:format]).to eq('jpg')
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'defines accessor to tmpdir' do
|
29
|
+
subject.tmpdir = '/test'
|
30
|
+
expect(subject.default_options[:tmpdir]).to eq('/test')
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'defines accessor to timeout' do
|
34
|
+
subject.timeout = '9'
|
35
|
+
expect(subject.default_options[:timeout]).to eq('9')
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'defines accessor to config_file' do
|
39
|
+
subject.config_file = 'config.json'
|
40
|
+
expect(subject.default_options[:config_file]).to eq('config.json')
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'defines accessor to error_log_file' do
|
44
|
+
subject.error_log_file = 'error.log'
|
45
|
+
expect(subject.default_options[:error_log_file]).to eq('error.log')
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'defines accessor to width' do
|
49
|
+
subject.width = '100px'
|
50
|
+
expect(subject.default_options[:width]).to eq('100px')
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'defines accessor to height' do
|
54
|
+
subject.height = '100px'
|
55
|
+
expect(subject.default_options[:height]).to eq('100px')
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'defines accessor to prepend_command' do
|
59
|
+
subject.prepend_command = 'RAILS_ENV=production'
|
60
|
+
expect(subject.default_options[:prepend_command]).to eq('RAILS_ENV=production')
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'defines accessor to script' do
|
64
|
+
subject.script = 'config/cool_slimerjs_stuff.js'
|
65
|
+
expect(subject.default_options[:script]).to eq('config/cool_slimerjs_stuff.js')
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#slimerjs' do
|
70
|
+
it 'finds where slimerjs is installed' do
|
71
|
+
allow(subject).to receive(:`).with('which slimerjs').and_return('/usr/bin/slimerjs')
|
72
|
+
|
73
|
+
expect(subject.slimerjs).to eq('/usr/bin/slimerjs')
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe '.configuration' do
|
78
|
+
it 'returns instance of configuration class' do
|
79
|
+
expect(Yabbie.configuration).to be_an_instance_of(described_class)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Yabbie::Slimer do
|
4
|
+
let(:options) {
|
5
|
+
{
|
6
|
+
prepend_command: 'SLIMERJSLAUCHER=/app/firefox',
|
7
|
+
config_file: 'config.js',
|
8
|
+
error_log_file: 'error.log',
|
9
|
+
script: 'snapshot.js',
|
10
|
+
width: 10,
|
11
|
+
height: 10,
|
12
|
+
format: 'pdf',
|
13
|
+
tmpdir: '/tmp',
|
14
|
+
timeout: 10
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
subject { described_class.new('http://google.com', options) }
|
19
|
+
|
20
|
+
before(:each) do
|
21
|
+
# Assume slimerjs is installed
|
22
|
+
allow(File).to receive(:exists?).and_return(true)
|
23
|
+
allow(Yabbie.configuration).to receive(:slimerjs).and_return('/usr/bin/slimerjs')
|
24
|
+
allow(Digest::MD5).to receive(:hexdigest).and_return('md5')
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'initialization' do
|
28
|
+
context 'when slimerjs is not present in the system' do
|
29
|
+
it 'raises no executable error' do
|
30
|
+
allow(File).to receive(:exists?).and_return(false)
|
31
|
+
|
32
|
+
expect {
|
33
|
+
described_class.new('http://google.com')
|
34
|
+
}.to raise_error(Yabbie::NoExecutableError)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#cmd' do
|
40
|
+
it 'joins slimerjs options as a command' do
|
41
|
+
command = 'SLIMERJSLAUCHER=/app/firefox /usr/bin/slimerjs --config=config.js --error-log-file=error.log snapshot.js http://google.com /tmp/md5.pdf 10 10 pdf 10'
|
42
|
+
|
43
|
+
expect(subject.cmd).to eq(command)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Yabbie::Source do
|
4
|
+
describe '#url?' do
|
5
|
+
let(:source) { described_class.new('http://google.com') }
|
6
|
+
|
7
|
+
it 'matches urls' do
|
8
|
+
expect(source).to be_url
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#file?' do
|
13
|
+
let(:file) { File.open('spec/fixtures/source_file.html') }
|
14
|
+
let(:source) { described_class.new(file) }
|
15
|
+
|
16
|
+
it 'matches files' do
|
17
|
+
expect(source).to be_file
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'initialization' do
|
22
|
+
context 'when argument is not url nor file' do
|
23
|
+
it 'raises improper source error' do
|
24
|
+
expect {
|
25
|
+
described_class.new('what is this?')
|
26
|
+
}.to raise_error(Yabbie::ImproperSourceError)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/yabbie.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.requirements << 'slimerjs, v0.10 or greater'
|
21
21
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.6"
|
23
|
-
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.3"
|
24
24
|
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
-
spec.add_development_dependency "pry-meta", "~> 0.0
|
25
|
+
spec.add_development_dependency "pry-meta", "~> 0.0"
|
26
26
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yabbie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ricardo Bernardeli
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '10.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '10.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 0.0
|
61
|
+
version: '0.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 0.0
|
68
|
+
version: '0.0'
|
69
69
|
description: Delicious
|
70
70
|
email:
|
71
71
|
- ricardobcs@gmail.com
|
@@ -79,7 +79,18 @@ files:
|
|
79
79
|
- README.md
|
80
80
|
- Rakefile
|
81
81
|
- lib/yabbie.rb
|
82
|
+
- lib/yabbie/config.json
|
83
|
+
- lib/yabbie/configuration.rb
|
84
|
+
- lib/yabbie/default.js
|
85
|
+
- lib/yabbie/exceptions.rb
|
86
|
+
- lib/yabbie/slimer.rb
|
87
|
+
- lib/yabbie/source.rb
|
82
88
|
- lib/yabbie/version.rb
|
89
|
+
- spec/fixtures/source_file.html
|
90
|
+
- spec/spec_helper.rb
|
91
|
+
- spec/yabbie/configuration_spec.rb
|
92
|
+
- spec/yabbie/slimer_spec.rb
|
93
|
+
- spec/yabbie/source_spec.rb
|
83
94
|
- yabbie.gemspec
|
84
95
|
homepage: https://github.com/net-engine/yabbie
|
85
96
|
licenses:
|
@@ -106,4 +117,9 @@ rubygems_version: 2.2.2
|
|
106
117
|
signing_key:
|
107
118
|
specification_version: 4
|
108
119
|
summary: Yabbie. Like shrimp for PhantomJS, but for SlimerJS.
|
109
|
-
test_files:
|
120
|
+
test_files:
|
121
|
+
- spec/fixtures/source_file.html
|
122
|
+
- spec/spec_helper.rb
|
123
|
+
- spec/yabbie/configuration_spec.rb
|
124
|
+
- spec/yabbie/slimer_spec.rb
|
125
|
+
- spec/yabbie/source_spec.rb
|