webshot 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -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 "north"
31
- c.quality 85
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
@@ -1,3 +1,3 @@
1
1
  module Webshot
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: