screencap 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.9.3
3
+ script: "bundle exec rake"
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in screencap.gemspec
4
+ gem 'guard'
5
+ gem 'guard-rspec'
6
+ gem 'guard-bundler'
7
+
8
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ guard 'bundler' do
2
+ watch('Gemfile')
3
+ watch(/^.+\.gemspec/)
4
+ end
5
+
6
+ guard 'rspec', :version => 2 do
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Maxwell Salzberg
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,46 @@
1
+ # Screencap
2
+
3
+ A screenshot gem you can use from your ruby application. Uses Phantom.js under the hood.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'screencap'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install screencap
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'screencap'
23
+
24
+ f = Screencap::Fetcher.new('http://google.com')
25
+ screenshot = f.fetch
26
+ ```
27
+
28
+ it also currently supports a couple of options
29
+
30
+ ```ruby
31
+ f = Screencap::Fetcher.new('http://google.com')
32
+ screenshot = f.fetch(:div => '.header', :output => '~/my_directory.png') #dont forget the extension!
33
+ ```
34
+
35
+ ## To-do
36
+
37
+ more tests
38
+ better configuration
39
+ expose more options
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+ task :default => :spec
data/lib/screencap.rb ADDED
@@ -0,0 +1,18 @@
1
+ require "screencap/version"
2
+ require 'phantomjs.rb'
3
+
4
+
5
+
6
+ module Screencap
7
+ SCREENCAP_ROOT = Pathname.new(File.dirname(__FILE__))
8
+ TMP_DIRECTORY = SCREENCAP_ROOT.join('..', 'tmp')
9
+ end
10
+
11
+ #config
12
+
13
+ #tmp directory to store files
14
+
15
+ #should return a file handle to tmp director where it is stored
16
+
17
+ require 'screencap/fetcher'
18
+ require 'screencap/phantom'
@@ -0,0 +1,29 @@
1
+ module Screencap
2
+ class Fetcher
3
+ def initialize(url)
4
+ @url = url
5
+ end
6
+
7
+ def fetch(opts = {})
8
+ @filename = opts.fetch(:output, clean_filename)
9
+ raster(@url, @filename, opts[:div])
10
+ fetched_file
11
+ end
12
+
13
+ def filename
14
+ @filename
15
+ end
16
+
17
+ def fetched_file
18
+ File.open(filename)
19
+ end
20
+
21
+ def raster(*args)
22
+ Screencap::Phantom.rasterize(*args)
23
+ end
24
+
25
+ def clean_filename
26
+ "#{TMP_DIRECTORY}/#{@url.delete('/.:?!')}.png"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ module Screencap
2
+ class Phantom
3
+ RASTERIZE = SCREENCAP_ROOT.join('screencap', 'raster.js')
4
+
5
+ def self.rasterize(url, path, *args)
6
+ # puts RASTERIZE.to_s, url, path, *args # Your code goes here...
7
+ Phantomjs.run(RASTERIZE.to_s, url, path, *args)
8
+ end
9
+
10
+
11
+ def quoted_args(args)
12
+ args.map{|x| quoted_arg(x)}
13
+ end
14
+
15
+ def quoted_arg(arg)
16
+ return arg if arg.starts_with?("'") && arg.ends_with?("'")
17
+ arg = "'" + arg unless arg.starts_with?("'")
18
+ arg = arg + "'" unless arg.ends_with?("'")
19
+ arg
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,52 @@
1
+ var page = new WebPage(),
2
+ address, output, div;
3
+
4
+ if (phantom.args.length < 2 || phantom.args.length > 4) {
5
+ console.log('Usage: rasterize.js URL filename div(optional)');
6
+ phantom.exit();
7
+ }
8
+
9
+ address = phantom.args[0];
10
+ output = phantom.args[1];
11
+ div = phantom.args[2];
12
+
13
+ //console.log(address, output, div)
14
+
15
+ function evaluate(page, func) {
16
+ var args = [].slice.call(arguments, 2);
17
+ var fn = "function() { return (" + func.toString() + ").apply(this, " + JSON.stringify(args) + ");}";
18
+ return page.evaluate(fn);
19
+ }
20
+
21
+ function returnDivDimensions(div){
22
+ var $el = jQuery(div);
23
+ var box = $el.offset()
24
+ box.height = $el.height();
25
+ box.width = $el.width();
26
+ return box;
27
+ }
28
+
29
+ page.onConsoleMessage = function (msg) {
30
+ //console.log("from page:" + msg);
31
+ };
32
+
33
+ page.viewportSize = { width: 1000, height: 550 }
34
+
35
+ page.open(address, function (status) {
36
+ if (status !== 'success') {
37
+ console.log('Unable to load:' + address);
38
+ phantom.exit();
39
+ }
40
+ //once page loaded, include jQuery from cdn
41
+ page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
42
+ page.evaluate(function(){jQuery.noConflict()})
43
+
44
+ if(div) {
45
+ var clip = evaluate(page, returnDivDimensions, div);
46
+ page.clipRect = clip;
47
+ }
48
+
49
+ page.render(output);
50
+ phantom.exit();
51
+ });
52
+ });
@@ -0,0 +1,3 @@
1
+ module Screencap
2
+ VERSION = "0.0.1"
3
+ end
data/screencap.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/screencap/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Maxwell Salzberg"]
6
+ gem.email = ["maxwell@joindiaspora.com"]
7
+ gem.description = %q{a gem to grab screenshots of webpages, or just parts of webpages}
8
+ gem.summary = %q{uses Phantom.js to grab pages, or parts of pages. Simple API.}
9
+ gem.homepage = "http://github.com/maxwell/screencap"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "screencap"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Screencap::VERSION
17
+
18
+ gem.add_dependency 'phantomjs.rb'
19
+ gem.add_development_dependency 'rspec', '~> 2.10'
20
+ gem.add_development_dependency 'rake'
21
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Screencap::Fetcher do
4
+ it 'takes a url' do
5
+ Screencap::Fetcher.new('http://google.com').should_not be_nil
6
+ end
7
+
8
+ it 'supports a custom filename' do
9
+ screenshot = Screencap::Fetcher.new('http://yahoo.com').fetch(:output => Screencap::TMP_DIRECTORY + 'kats.png')
10
+ File.exists?(screenshot).should == true
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Screencap do
4
+ it 'works' do
5
+ Screencap::Fetcher.new('http://google.com').fetch
6
+ end
7
+
8
+ end
@@ -0,0 +1,12 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'screencap'
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
8
+
9
+ config.before(:all) do
10
+ system("rm #{Screencap::TMP_DIRECTORY}/*.png")
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: screencap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Maxwell Salzberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: phantomjs.rb
16
+ requirement: &70163076869600 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70163076869600
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70163076869000 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.10'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70163076869000
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70163076868540 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70163076868540
47
+ description: a gem to grab screenshots of webpages, or just parts of webpages
48
+ email:
49
+ - maxwell@joindiaspora.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - .travis.yml
57
+ - Gemfile
58
+ - Guardfile
59
+ - LICENSE
60
+ - README.md
61
+ - Rakefile
62
+ - lib/screencap.rb
63
+ - lib/screencap/fetcher.rb
64
+ - lib/screencap/phantom.rb
65
+ - lib/screencap/raster.js
66
+ - lib/screencap/version.rb
67
+ - screencap.gemspec
68
+ - spec/fetcher_spec.rb
69
+ - spec/screencap_spec.rb
70
+ - spec/spec_helper.rb
71
+ homepage: http://github.com/maxwell/screencap
72
+ licenses: []
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.17
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: uses Phantom.js to grab pages, or parts of pages. Simple API.
95
+ test_files:
96
+ - spec/fetcher_spec.rb
97
+ - spec/screencap_spec.rb
98
+ - spec/spec_helper.rb
99
+ has_rdoc: