photograph 0.0.4 → 0.0.6
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 +4 -4
- data/.gitignore +1 -0
- data/README.md +42 -11
- data/lib/photograph.rb +0 -1
- data/lib/photograph/artist.rb +33 -26
- data/lib/photograph/version.rb +1 -1
- data/photograph.gemspec +5 -7
- data/spec/photograph/artist_spec.rb +5 -0
- metadata +21 -63
- data/lib/photograph/service.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57e2e388307ebd1a8b4f5778ddacc96ae7e775a1
|
4
|
+
data.tar.gz: 0c524964148c4cdcfb53422bd294eb6743f58f6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a69d29af237a5728510ded57476604fe5ef7f3b9235bb2dccb36975b63919d48a4ff763222843a8ef7b33fe916b7a45e8308018b6e0cb42f6f909b9a264e670
|
7
|
+
data.tar.gz: af29eb62cc97c3939be416510b6cb29ceed71c62d2fd08deb8acdbf2fcf04ca7a09b4b15de8b4ecaafc21216c96839fbe3b4a3c7a1f4eb48d863a4b51313ebcf
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
# Photograph
|
2
2
|
|
3
3
|
Photograph allows to take screenshots of webpages, the rendering being
|
4
|
-
done by
|
4
|
+
done by webkit thanks to [PhantomJS](http://phantomjs.org/) and [Poltergeist](https://github.com/jonleighton/poltergeist).
|
5
5
|
|
6
|
-
Typically, this can be used to generate
|
6
|
+
Typically, this can be used to generate previews for DOM based
|
7
7
|
documents.
|
8
8
|
|
9
|
-
Please remind that having a
|
10
|
-
is slow, around 600ms.
|
9
|
+
Please remind that having a webkit instance doing the rendering, even if it is launched only once then reused, is slow, around 600ms!
|
11
10
|
|
12
11
|
## Installation
|
13
12
|
|
14
|
-
|
13
|
+
PhantomJS is required and must be available in your path. See [installation instructions](http://phantomjs.org/download.html).
|
14
|
+
|
15
|
+
Then you can add this line to your application's Gemfile:
|
15
16
|
|
16
17
|
gem 'photograph'
|
17
18
|
|
@@ -24,24 +25,54 @@ Or install it yourself as:
|
|
24
25
|
$ gem install photograph
|
25
26
|
|
26
27
|
## Usage
|
28
|
+
Photograph can be used either directly through the Photograph::Artist
|
29
|
+
class or by its little Sinatra app.
|
27
30
|
|
31
|
+
### Basics
|
28
32
|
**Using `Artist.shoot!` without a block had been deprecated in 0.3 and
|
29
33
|
will raise an exception.**
|
30
34
|
|
31
|
-
|
32
|
-
class or by its little sinatra app.
|
35
|
+
Artist can be instanciated with an url to screenshot, calling ``#shoot!`` will take the screenshot and yields it. The screenshot is only accessible within the block as it is always deleted afterward.
|
33
36
|
|
34
37
|
@artist = Photograph::Artist.new(:url => "http://github.com")
|
35
38
|
@artist.shoot! do |image|
|
36
39
|
image # => MiniMagick instance you can toy with
|
37
40
|
|
38
|
-
|
39
|
-
|
41
|
+
# Sinatra? Serve screenshot to client with:
|
42
|
+
send_file image.path, :type => :png
|
43
|
+
|
44
|
+
# Jobs or Rake task ? Export screenshot with:
|
45
|
+
FileUtils.cp(image.path, "/somewhere/image.png")
|
40
46
|
end
|
41
47
|
|
42
|
-
|
48
|
+
### Cropping
|
49
|
+
To crop the screenshot, ``Artist.new`` accepts ``:x``, ``y``, ``w`` and ``h`` options:
|
50
|
+
|
51
|
+
@artist = Photograph::Artist.new(:url => "http://github.com", :x => 100, :y => 100, :w => 800, :h => 600)
|
52
|
+
|
53
|
+
This will take a 800x600 screenshot, skipping 100 pixels top and left.
|
54
|
+
|
55
|
+
### Interacting with the page before shooting
|
56
|
+
If authentication is required (or any browser action), ``Artist#before`` allows to supply actions to be done before shooting. It yields a ``Capybara`` browser instance that respond to the usual methods, ``fill_in``, ``click_link`` [see for more details](https://github.com/jonleighton/poltergeist).
|
57
|
+
|
58
|
+
@artist = Photograph::Artist.new(:url => "http://rubygems.org")
|
59
|
+
@artist.before do |browser|
|
60
|
+
browser.click_button("Sign in")
|
61
|
+
browser.fill_in("Username", :with => "MyUsername")
|
62
|
+
# ...
|
63
|
+
end
|
64
|
+
|
65
|
+
@artist.shoot! do |image|
|
66
|
+
send_file image, :type => :png
|
67
|
+
end
|
68
|
+
|
69
|
+
### RDOC
|
70
|
+
|
71
|
+
[Full Documentation](http://rdoc.info/gems/photograph/frames)
|
72
|
+
|
73
|
+
### As a web service
|
43
74
|
|
44
|
-
|
75
|
+
As having the web service builtin requires Sinatra and Thin, it had been extracted to [Photograph-Service](https://github.com/jhchabran/photograph-service).
|
45
76
|
|
46
77
|
## Contributing
|
47
78
|
|
data/lib/photograph.rb
CHANGED
data/lib/photograph/artist.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
require 'capybara/poltergeist'
|
2
2
|
require 'mini_magick'
|
3
3
|
|
4
|
-
Capybara.default_wait_time = 15
|
5
|
-
|
6
4
|
module Photograph
|
7
5
|
class Artist
|
8
6
|
attr_accessor :options
|
@@ -12,14 +10,15 @@ module Photograph
|
|
12
10
|
class DeprecationError < RuntimeError; end
|
13
11
|
|
14
12
|
DefaultOptions = {
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
:x => 0, # top left position
|
14
|
+
:y => 0,
|
15
|
+
:w => 1280, # width
|
16
|
+
:h => 1024, # height
|
19
17
|
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
:sleep => 0.5, # Sleep 0.5 seconds before taking the screenshot
|
19
|
+
:capybara_wait_time => 15, # Default Capybara wait time
|
20
|
+
:selector => nil # wait until the selector matches to take the screenshot
|
21
|
+
}
|
23
22
|
|
24
23
|
##
|
25
24
|
# Instanciate a browser instance.
|
@@ -56,8 +55,9 @@ module Photograph
|
|
56
55
|
#
|
57
56
|
# Options:
|
58
57
|
# * +url+ mandatory, location you want to screenshot
|
59
|
-
# * +
|
58
|
+
# * +sleep+ sleep amount of seconds before screenshotting. *this is option is ignored if +selector+ is provided.
|
60
59
|
# * +selector+ wait until the provided +selector+ matches a dom node before screenshotting. Typically faster than an arbritrary +wait+ amount, used when your page has some dynamically inserted nodes.
|
60
|
+
# * +capybara_wait_time+ time capybara will wait for a a selector to appear before timing out
|
61
61
|
# * +x+ top coordinate of the screenshot, default to 0
|
62
62
|
# * +y+ left coordinate of the screenshot, default to 0
|
63
63
|
# * +w+ width of the screenshot, default to 1280
|
@@ -65,6 +65,9 @@ module Photograph
|
|
65
65
|
# * +browser+ Capybara instance to use, typically instanciated by +Artist.create_browser+
|
66
66
|
def initialize options={}
|
67
67
|
raise MissingUrlError.new('missing argument :url') unless options[:url]
|
68
|
+
if options[:wait]
|
69
|
+
$stderr.puts "DEPRECATED: the :wait option had been deprecated and will be ignored in further version, please use :sleep instead."
|
70
|
+
end
|
68
71
|
|
69
72
|
@options = DefaultOptions.merge(options)
|
70
73
|
@options[:url] = normalize_url(options[:url])
|
@@ -75,27 +78,31 @@ module Photograph
|
|
75
78
|
def shoot! &block
|
76
79
|
raise DeprecationError.new('Using Artist#shoot! without a block had been deprecated') unless block_given?
|
77
80
|
|
78
|
-
|
81
|
+
Capybara.using_wait_time @options[:capybara_wait_time] do
|
82
|
+
begin
|
83
|
+
browser.visit @options[:url]
|
79
84
|
|
80
|
-
|
85
|
+
@before_hook.call(browser) if @before_hook
|
81
86
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
87
|
+
if @options[:selector]
|
88
|
+
browser.wait_until do
|
89
|
+
browser.has_css? @options[:selector]
|
90
|
+
end
|
91
|
+
else
|
92
|
+
sleep @options[:sleep]
|
93
|
+
end
|
89
94
|
|
90
|
-
|
95
|
+
tempfile = Tempfile.new(['photograph','.png'])
|
91
96
|
|
92
|
-
|
93
|
-
|
94
|
-
|
97
|
+
browser.driver.render tempfile.path,
|
98
|
+
:width => options[:w] + options[:x],
|
99
|
+
:height => options[:h] + options[:y]
|
95
100
|
|
96
|
-
|
97
|
-
|
98
|
-
|
101
|
+
yield adjust_image(tempfile)
|
102
|
+
ensure
|
103
|
+
tempfile.unlink if tempfile
|
104
|
+
end
|
105
|
+
end
|
99
106
|
end
|
100
107
|
|
101
108
|
##
|
data/lib/photograph/version.rb
CHANGED
data/photograph.gemspec
CHANGED
@@ -2,11 +2,12 @@
|
|
2
2
|
require File.expand_path('../lib/photograph/version', __FILE__)
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
|
-
gem.authors = ["
|
5
|
+
gem.authors = ["Jean Hadrien Chabran"]
|
6
6
|
gem.email = ["jh@chabran.fr"]
|
7
|
-
gem.description = %q{
|
8
|
-
gem.summary = %q{
|
7
|
+
gem.description = %q{Small library to take screenshots of web pages}
|
8
|
+
gem.summary = %q{Small library to take screenshots of web pages}
|
9
9
|
gem.homepage = "https://github.com/jhchabran/photograph"
|
10
|
+
gem.license = "MIT"
|
10
11
|
|
11
12
|
gem.files = `git ls-files`.split($\)
|
12
13
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
@@ -17,12 +18,9 @@ Gem::Specification.new do |gem|
|
|
17
18
|
|
18
19
|
gem.add_dependency 'poltergeist'
|
19
20
|
gem.add_dependency 'mini_magick'
|
20
|
-
gem.add_dependency 'sinatra'
|
21
|
-
gem.add_dependency 'sinatra-contrib'
|
22
|
-
gem.add_dependency 'thin'
|
23
21
|
|
24
22
|
gem.add_development_dependency 'rspec', '~> 2.14'
|
25
23
|
gem.add_development_dependency 'rake'
|
26
24
|
|
27
|
-
gem.post_install_message = 'DEPRECATION: Artist#
|
25
|
+
gem.post_install_message = 'DEPRECATION: Photograph::Artist#new :wait option had been renamed to :sleep. :wait will be ignored in the next version.'
|
28
26
|
end
|
@@ -43,6 +43,11 @@ module Photograph
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
+
it 'should locally set capybara default time' do
|
47
|
+
subject.shoot!{ expect(Capybara.default_wait_time).to eq(15) }
|
48
|
+
expect(Capybara.default_wait_time).to eq(2)
|
49
|
+
end
|
50
|
+
|
46
51
|
it('should accept a block when shooting') do
|
47
52
|
subject.shoot!{|image| image.should respond_to(:path) }
|
48
53
|
end
|
metadata
CHANGED
@@ -1,114 +1,72 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: photograph
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Jean Hadrien Chabran
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: poltergeist
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: mini_magick
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: sinatra
|
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: sinatra-contrib
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - '>='
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :runtime
|
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: thin
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - '>='
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :runtime
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - '>='
|
38
|
+
- - ">="
|
81
39
|
- !ruby/object:Gem::Version
|
82
40
|
version: '0'
|
83
41
|
- !ruby/object:Gem::Dependency
|
84
42
|
name: rspec
|
85
43
|
requirement: !ruby/object:Gem::Requirement
|
86
44
|
requirements:
|
87
|
-
- - ~>
|
45
|
+
- - "~>"
|
88
46
|
- !ruby/object:Gem::Version
|
89
47
|
version: '2.14'
|
90
48
|
type: :development
|
91
49
|
prerelease: false
|
92
50
|
version_requirements: !ruby/object:Gem::Requirement
|
93
51
|
requirements:
|
94
|
-
- - ~>
|
52
|
+
- - "~>"
|
95
53
|
- !ruby/object:Gem::Version
|
96
54
|
version: '2.14'
|
97
55
|
- !ruby/object:Gem::Dependency
|
98
56
|
name: rake
|
99
57
|
requirement: !ruby/object:Gem::Requirement
|
100
58
|
requirements:
|
101
|
-
- -
|
59
|
+
- - ">="
|
102
60
|
- !ruby/object:Gem::Version
|
103
61
|
version: '0'
|
104
62
|
type: :development
|
105
63
|
prerelease: false
|
106
64
|
version_requirements: !ruby/object:Gem::Requirement
|
107
65
|
requirements:
|
108
|
-
- -
|
66
|
+
- - ">="
|
109
67
|
- !ruby/object:Gem::Version
|
110
68
|
version: '0'
|
111
|
-
description:
|
69
|
+
description: Small library to take screenshots of web pages
|
112
70
|
email:
|
113
71
|
- jh@chabran.fr
|
114
72
|
executables:
|
@@ -117,7 +75,7 @@ executables:
|
|
117
75
|
extensions: []
|
118
76
|
extra_rdoc_files: []
|
119
77
|
files:
|
120
|
-
- .gitignore
|
78
|
+
- ".gitignore"
|
121
79
|
- Gemfile
|
122
80
|
- LICENSE
|
123
81
|
- MIT-LICENSE
|
@@ -127,35 +85,35 @@ files:
|
|
127
85
|
- bin/photograph
|
128
86
|
- lib/photograph.rb
|
129
87
|
- lib/photograph/artist.rb
|
130
|
-
- lib/photograph/service.rb
|
131
88
|
- lib/photograph/version.rb
|
132
89
|
- photograph.gemspec
|
133
90
|
- spec/photograph/artist_spec.rb
|
134
91
|
- spec/spec_helper.rb
|
135
92
|
homepage: https://github.com/jhchabran/photograph
|
136
|
-
licenses:
|
93
|
+
licenses:
|
94
|
+
- MIT
|
137
95
|
metadata: {}
|
138
|
-
post_install_message: 'DEPRECATION: Artist#
|
139
|
-
|
96
|
+
post_install_message: 'DEPRECATION: Photograph::Artist#new :wait option had been renamed
|
97
|
+
to :sleep. :wait will be ignored in the next version.'
|
140
98
|
rdoc_options: []
|
141
99
|
require_paths:
|
142
100
|
- lib
|
143
101
|
required_ruby_version: !ruby/object:Gem::Requirement
|
144
102
|
requirements:
|
145
|
-
- -
|
103
|
+
- - ">="
|
146
104
|
- !ruby/object:Gem::Version
|
147
105
|
version: '0'
|
148
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
107
|
requirements:
|
150
|
-
- -
|
108
|
+
- - ">="
|
151
109
|
- !ruby/object:Gem::Version
|
152
110
|
version: '0'
|
153
111
|
requirements: []
|
154
112
|
rubyforge_project:
|
155
|
-
rubygems_version: 2.
|
113
|
+
rubygems_version: 2.2.2
|
156
114
|
signing_key:
|
157
115
|
specification_version: 4
|
158
|
-
summary:
|
116
|
+
summary: Small library to take screenshots of web pages
|
159
117
|
test_files:
|
160
118
|
- spec/photograph/artist_spec.rb
|
161
119
|
- spec/spec_helper.rb
|
data/lib/photograph/service.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'sinatra/base'
|
2
|
-
require 'sinatra/json'
|
3
|
-
|
4
|
-
module Photograph
|
5
|
-
class Service < ::Sinatra::Base
|
6
|
-
helpers Sinatra::JSON
|
7
|
-
|
8
|
-
# Reuse the same browser instance between requests.
|
9
|
-
def browser
|
10
|
-
@browser ||= Artist.create_browser
|
11
|
-
end
|
12
|
-
|
13
|
-
get '/' do
|
14
|
-
json :version => Photograph::VERSION
|
15
|
-
end
|
16
|
-
|
17
|
-
get '/shoot' do
|
18
|
-
artist = Artist.new :url => params["url"],
|
19
|
-
:x => params["x"].to_i,
|
20
|
-
:y => params["y"].to_i,
|
21
|
-
:w => params["w"].to_i,
|
22
|
-
:h => params["h"].to_i,
|
23
|
-
:wait => params["wait"].to_f,
|
24
|
-
:selector => params["selector"],
|
25
|
-
:browser => browser
|
26
|
-
|
27
|
-
artist.shoot! do |image|
|
28
|
-
send_file image.path,
|
29
|
-
:type => :png
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|