webshot 0.0.1 → 0.0.2
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/lib/webshot.rb +2 -0
- data/lib/webshot/screenshot.rb +13 -3
- data/lib/webshot/version.rb +1 -1
- metadata +1 -1
data/lib/webshot.rb
CHANGED
@@ -21,10 +21,12 @@ module Webshot
|
|
21
21
|
mattr_accessor :user_agent
|
22
22
|
@@user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31"
|
23
23
|
|
24
|
+
# Customize settings
|
24
25
|
def self.setup
|
25
26
|
yield self
|
26
27
|
end
|
27
28
|
|
29
|
+
# Capibara setup
|
28
30
|
def self.capybara_setup!
|
29
31
|
# By default Capybara will try to boot a rack application
|
30
32
|
# automatically. You might want to switch off Capybara's
|
data/lib/webshot/screenshot.rb
CHANGED
@@ -6,12 +6,15 @@ module Webshot
|
|
6
6
|
width = opts.fetch(:width, Webshot.width)
|
7
7
|
height = opts.fetch(:height, Webshot.height)
|
8
8
|
user_agent = opts.fetch(:user_agent, Webshot.user_agent)
|
9
|
+
|
10
|
+
# Browser settings
|
9
11
|
page.driver.resize(width, height)
|
10
12
|
page.driver.headers = {
|
11
13
|
"User-Agent" => user_agent,
|
12
14
|
}
|
13
15
|
end
|
14
16
|
|
17
|
+
# Captures a screenshot of +url+ saving it to +path+.
|
15
18
|
def capture(url, path, opts = {})
|
16
19
|
# Default settings
|
17
20
|
width = opts.fetch(:width, 120)
|
@@ -19,20 +22,27 @@ module Webshot
|
|
19
22
|
gravity = opts.fetch(:gravity, "north")
|
20
23
|
quality = opts.fetch(:quality, 85)
|
21
24
|
|
25
|
+
# Open page
|
22
26
|
visit url
|
27
|
+
|
23
28
|
if page.driver.status_code == 200
|
29
|
+
# Save screenshot
|
24
30
|
page.driver.save_screenshot(path, :full => true)
|
31
|
+
|
32
|
+
# Resize screenshot
|
25
33
|
thumb = MiniMagick::Image.open(path)
|
26
34
|
thumb.combine_options do |c|
|
27
35
|
c.thumbnail "#{width}x"
|
28
36
|
c.background "white"
|
29
37
|
c.extent "#{width}x#{height}"
|
30
|
-
c.gravity
|
31
|
-
c.quality
|
38
|
+
c.gravity gravity
|
39
|
+
c.quality quality
|
32
40
|
end
|
41
|
+
|
42
|
+
# Save thumbnail
|
33
43
|
thumb.write path
|
34
44
|
else
|
35
|
-
raise WebshotError.new("Could not fetch page: #{url.inspect}")
|
45
|
+
raise WebshotError.new("Could not fetch page: #{url.inspect}, error code: #{page.driver.status_code}")
|
36
46
|
end
|
37
47
|
end
|
38
48
|
end
|
data/lib/webshot/version.rb
CHANGED