phantoshot 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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NmFmMzIyNDE2NmIyZTQyMDBmNzhmMDI3MjY4NzBkOTk1MDlhYmVmOQ==
5
+ data.tar.gz: !binary |-
6
+ ZDY1MDIzMzY2YWYyNjFkMjBiNThhYmU3ODI5ZGRhOWNiZTJmMDljMw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NGQ2YTQ1Y2EwNjExNWQyNDFmODllNWU4ZWM2ZmIwMzI1NGEzODdiNGI1M2Vl
10
+ ODEzYjg3ZTMyZGIzOGU3ZmJjODBiNDAxZDY4YjNmOWM5NjI2Y2YxYmM2ZWJl
11
+ MmIzYWEzYjZlNTJiODJmMWYzZGI5ZGM2MGQ1OWQ1YTYxNzUwZmE=
12
+ data.tar.gz: !binary |-
13
+ ZDg3Y2FmNjY4NjQ2ZTgyYWYxMDlkZTMyOTNhYmJjMGU0NjFhNjhlNTY2ZDY2
14
+ YzI1MGFmMjAyNjc1NzMzMWIzZmE4OTQzOTY4MTRiZWQ2ZTlmN2E3MmM1ZTYx
15
+ NWY4Y2M2YWE3ZGIwMGE0Yjc3MTE1OGJmNWVkYTgwMWNjNzliYzE=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+ .DS_Store
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in phantoshot.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'foreman', require: false
8
+ gem 'rspec', "~>2.8"
9
+ gem 'fakeweb'
10
+ gem 'pry'
11
+ gem 'fastimage'
12
+ # gem 'debugger'
13
+ # gem 'better_errors'
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ phantoshot (0.0.1)
5
+ hashie
6
+ multi_json
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ coderay (1.0.9)
12
+ diff-lcs (1.2.4)
13
+ dotenv (0.7.0)
14
+ fakeweb (1.3.0)
15
+ fastimage (1.4.0)
16
+ foreman (0.63.0)
17
+ dotenv (>= 0.7)
18
+ thor (>= 0.13.6)
19
+ hashie (2.0.5)
20
+ method_source (0.8.1)
21
+ multi_json (1.7.6)
22
+ pry (0.9.12.2)
23
+ coderay (~> 1.0.5)
24
+ method_source (~> 0.8)
25
+ slop (~> 3.4)
26
+ rake (10.0.4)
27
+ rspec (2.13.0)
28
+ rspec-core (~> 2.13.0)
29
+ rspec-expectations (~> 2.13.0)
30
+ rspec-mocks (~> 2.13.0)
31
+ rspec-core (2.13.1)
32
+ rspec-expectations (2.13.0)
33
+ diff-lcs (>= 1.1.3, < 2.0)
34
+ rspec-mocks (2.13.1)
35
+ slop (3.4.5)
36
+ thor (0.18.1)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ bundler (~> 1.3)
43
+ fakeweb
44
+ fastimage
45
+ foreman
46
+ phantoshot!
47
+ pry
48
+ rake
49
+ rspec (~> 2.8)
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dan Nguyen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ phantoshot
2
+ ==========
3
+
4
+ A website snapshot utility, using Phantom.js
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'rubygems/package_task'
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :rspec
7
+
8
+ desc 'Run the specs'
9
+ RSpec::Core::RakeTask.new(:rspec) do |t|
10
+ t.rspec_opts = ['--color']
11
+ t.pattern = './spec/**/*_spec.rb'
12
+ end
13
+
14
+ spec = Gem::Specification.load("#{File.dirname(__FILE__)}/phantoshot.gemspec")
15
+
16
+ desc "Package gem."
17
+ Gem::PackageTask.new(spec) do |pkg|
18
+ pkg.gem_spec = spec
19
+ end
@@ -0,0 +1,24 @@
1
+ var page = require('webpage').create();
2
+ var system = require('system');
3
+
4
+ var result = {
5
+ url: system.args[1],
6
+ width: parseInt(system.args[2]),
7
+ height: parseInt(system.args[3])
8
+ };
9
+
10
+
11
+ page.viewportSize = {
12
+ width: result.width,
13
+ height: result.height
14
+ };
15
+
16
+ page.open(result.url, function() {
17
+
18
+ result.file_format = 'PNG';
19
+ result.image_data = page.renderBase64(result.file_format);
20
+
21
+ console.log( JSON.stringify(result) );
22
+
23
+ phantom.exit();
24
+ });
@@ -0,0 +1,24 @@
1
+ module Phantoshot
2
+ def make_screenshot(url, opts={})
3
+
4
+ width = opts[:width] || 600
5
+ height = opts[:height] || 400
6
+
7
+ res = MultiJson.load( phantom_command(url, width, height) )
8
+
9
+ hsh = Hashie::Mash.new(res)
10
+
11
+ # Decode it for writing out to binary
12
+ hsh[:image_data] = Base64.decode64( hsh[:image_data] )
13
+ return hsh
14
+ end
15
+
16
+
17
+ # returns JSON string with these attributes:
18
+ # :image_data (in base64), :width, :height, :file_format (default is PNG), and :url
19
+ #
20
+ def phantom_command(url, width, height)
21
+ cmd = "phantomjs #{JS_LIB_NAME} #{url} #{width} #{height}"
22
+ `#{cmd}`
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Phantoshot
2
+ VERSION = "0.0.1"
3
+ end
data/lib/phantoshot.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "phantoshot/version"
2
+ require "phantoshot/make_screenshot"
3
+
4
+ require 'hashie'
5
+ require 'multi_json'
6
+ require 'base64'
7
+
8
+ module Phantoshot
9
+ JS_LIB_NAME = File.join(File.dirname(__FILE__), "/phantom-shot-script.js")
10
+ end
11
+
12
+ include Phantoshot
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'phantoshot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "phantoshot"
8
+ spec.version = Phantoshot::VERSION
9
+ spec.authors = ["Andrew Callahan", "Dan Nguyen"]
10
+ spec.email = ["dn@gskift.com"]
11
+ spec.description = %q{Take screenshots with phantom.js}
12
+ spec.summary = %q{A simple Ruby wrapper around phantom.js}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "hashie"
24
+ spec.add_dependency "multi_json"
25
+
26
+
27
+ end
@@ -0,0 +1,13 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Document</title>
6
+ </head>
7
+ <body>
8
+ <h1>Example</h1>
9
+ <p>This is a webpage</p>
10
+
11
+ <h2>Another headline</h2>
12
+ </body>
13
+ </html>
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ include Phantoshot
4
+
5
+ describe Phantoshot do
6
+ it "should run this test" do
7
+ expect(true).to be true
8
+ end
9
+
10
+
11
+
12
+ describe "make_screenshot" do
13
+
14
+ context "basic setup" do
15
+ it "should require a single String parameter" do
16
+ expect{ make_screenshot }.to raise_error
17
+ end
18
+
19
+ it "should return Hashie::Mash object" do
20
+ # expect( make_screenshot('http://www.example.com') ).to be_a Hashie::Mash
21
+ end
22
+ end
23
+
24
+ context "image production" do
25
+ it "should return an image_data attribute in the data_hash" do
26
+ hsh = make_screenshot(HTML_FIXTURE_FILENAME)
27
+ expect( hsh.image_data ).to be_a String
28
+ end
29
+ end
30
+
31
+
32
+ context "call with parameters" do
33
+ before(:each) do
34
+ @imgfile = Tempfile.new('foo.png')
35
+ end
36
+
37
+ it "should accept width and height options" do
38
+ response = make_screenshot(HTML_FIXTURE_FILENAME, width: 300, height: 200)
39
+
40
+ ## let's have the hash contain height, width attributes
41
+ expect(response.width).to eq 300
42
+ expect(response.height).to eq 200
43
+ expect(response.url).to eq HTML_FIXTURE_FILENAME
44
+ expect(response.file_format).to eq 'PNG'
45
+
46
+ @imgfile.write( response.image_data )
47
+ @imgfile.close
48
+
49
+ expect( FastImage.size(@imgfile.path)[0] ).to eq 300
50
+ expect( FastImage.size(@imgfile.path)[1] ).to eq 200
51
+ end
52
+
53
+ after(:each) do
54
+ @imgfile.unlink
55
+ end
56
+ end
57
+
58
+
59
+ context "Error handling: should have an :error attribute when phantom_command fails" do
60
+
61
+ it "should return 'Error': Invalid source address' when nonexistent address is given"
62
+
63
+ end
64
+
65
+
66
+ end
67
+ end
@@ -0,0 +1,22 @@
1
+ require 'phantoshot'
2
+ require 'fakeweb'
3
+ require 'fastimage'
4
+ require 'tempfile'
5
+
6
+ HTML_FIXTURE_FILENAME = File.expand_path "./spec/fixtures/example-fixture.html"
7
+
8
+ RSpec.configure do |config|
9
+ config.before(:each) do
10
+ #
11
+ end
12
+ config.after(:each) do
13
+ #
14
+ end
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phantoshot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Callahan
8
+ - Dan Nguyen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: hashie
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: multi_json
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: Take screenshots with phantom.js
71
+ email:
72
+ - dn@gskift.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/phantom-shot-script.js
84
+ - lib/phantoshot.rb
85
+ - lib/phantoshot/make_screenshot.rb
86
+ - lib/phantoshot/version.rb
87
+ - phantoshot.gemspec
88
+ - spec/fixtures/example-fixture.html
89
+ - spec/lib/phantoshot_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.0.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: A simple Ruby wrapper around phantom.js
115
+ test_files:
116
+ - spec/fixtures/example-fixture.html
117
+ - spec/lib/phantoshot_spec.rb
118
+ - spec/spec_helper.rb