webshooter 0.0.1a → 0.0.2a

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  *.png
2
+ pkg/*
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- webshooter (0.0.1a)
4
+ webshooter (0.0.2a)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/bin/webshooter.rb CHANGED
@@ -1,18 +1,18 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
 
4
- require 'webshotr'
4
+ require 'webshooter'
5
5
 
6
- module Webshotr
6
+ module Webshooter
7
7
  class CLI
8
8
  #Ruby CLI tool - http://rubylearning.com/blog/2011/01/03/how-do-i-make-a-command-line-tool-in-ruby/
9
9
  def self.execute(args)
10
10
  url=args[0]
11
11
  filename=args[1]
12
12
  size=args[2]
13
- webshotr=Webshotr.capture(url, filename, size )
13
+ webshotr=Webshooter.capture(url, filename, size )
14
14
  end
15
15
  end
16
16
  end
17
17
 
18
- Webshotr::CLI.execute(ARGV)
18
+ Webshooter::CLI.execute(ARGV)
@@ -0,0 +1,58 @@
1
+ # Inspired by http://railstips.org/blog/archives/2009/03/04/following-redirects-with-nethttp/
2
+ # But needs some of this - http://www.ruby-forum.com/topic/142745
3
+
4
+ require 'net/http'
5
+ require 'net/https'
6
+
7
+ module Webshooter
8
+
9
+ class RedirectFollower
10
+ class TooManyRedirects < StandardError; end
11
+
12
+ attr_accessor :url, :body, :redirect_limit, :response
13
+
14
+ def initialize(url, limit=5)
15
+ @url, @redirect_limit = url, limit
16
+ end
17
+
18
+ def resolve
19
+ raise TooManyRedirects if redirect_limit < 0
20
+
21
+ uri=URI.parse(url)
22
+
23
+ http = Net::HTTP.new(uri.host, uri.port)
24
+
25
+ if uri.scheme == "https"
26
+ http.use_ssl = true
27
+ #http.verify_mode = OpenSSL::SSL::VERIFY_NONE # Not setting this explicitly will result in an error and the value being set anyway
28
+ end
29
+
30
+ if (uri.path=="")
31
+ request = Net::HTTP::Get.new("/")
32
+ else
33
+ request = Net::HTTP::Get.new(uri.path)
34
+ end
35
+ self.response = http.start {|http| http.request(request) }
36
+
37
+ if response.kind_of?(Net::HTTPRedirection)
38
+ self.url = redirect_url
39
+ self.redirect_limit -= 1
40
+
41
+ puts "redirect found, headed to #{url}"
42
+ resolve
43
+ end
44
+ return url
45
+
46
+ end
47
+
48
+ def redirect_url
49
+ if response['location'].nil?
50
+ response.body.match(/<a href=\"([^>]+)\">/i)[1]
51
+ else
52
+ response['location']
53
+ end
54
+ end
55
+ end
56
+
57
+ end
58
+
@@ -1,3 +1,3 @@
1
1
  module Webshooter
2
- VERSION = "0.0.1a"
2
+ VERSION = "0.0.2a"
3
3
  end
@@ -3,6 +3,8 @@ require 'logger'
3
3
  require 'uri'
4
4
 
5
5
  require 'webshooter/newnsurlrequest'
6
+ require 'webshooter/redirectfollower'
7
+
6
8
  OSX.require_framework 'WebKit'
7
9
 
8
10
  #Respond to URL not found....
@@ -52,7 +54,6 @@ class WebShotProcessor
52
54
  # Replace the window's content @webView with the web @webView
53
55
  @window.setContentView(@webView)
54
56
  @webView.release
55
- puts "its not in the release"
56
57
  end
57
58
 
58
59
  def capture(uri, path, dimensions = "1024x768" )
@@ -61,14 +62,16 @@ class WebShotProcessor
61
62
  # Tell the frame to load the URL we want
62
63
  @webView.window.setContentSize(snapshot_dimension)
63
64
  @webView.setFrameSize(snapshot_dimension)
65
+
66
+ final_link = RedirectFollower.new(uri).resolve
64
67
 
65
- myURI = URI.parse(uri)
68
+ myURI = URI.parse(final_link)
66
69
 
67
70
  #Allow all https certificates
68
71
  NewNSURLRequest.setAllowsAnyHTTPSCertificate_forHost(true,myURI.host)
69
72
 
70
73
  #puts "Getting ready for the loadRequest"+uri
71
- @webView.mainFrame.loadRequest(NewNSURLRequest.requestWithURL(OSX::NSURL.URLWithString(uri)))
74
+ @webView.mainFrame.loadRequest(NewNSURLRequest.requestWithURL(OSX::NSURL.URLWithString(final_link)))
72
75
 
73
76
  #
74
77
  # Run the main event loop until the frame loads
@@ -82,12 +85,11 @@ class WebShotProcessor
82
85
  #OSX.CFRunLoopRun
83
86
  #puts "first loop enters here"
84
87
 
85
- puts "its not in the after upon success"
86
88
 
87
89
  upon_success do |view| # Capture the content of the view as an image
88
90
 
89
91
  view.window.orderFront(nil)
90
- view.window.display
92
+ #view.window.display
91
93
 
92
94
  #puts "-------------------------------------------------"
93
95
  #puts "We got success"
data/webshooter.gemspec CHANGED
@@ -9,12 +9,13 @@ Gem::Specification.new do |s|
9
9
  s.email = ["patrick.debois@jedi.be"]
10
10
  s.homepage = "http://github.com/jedi4ever/webshooter/"
11
11
  s.summary = %q{Create webshot using webkit on MacOSX}
12
- s.description = %q{This library allows you to create webshots using webkit on MacOSX. A webshot is a screenshot taking inside the browser. The advantage of this library is that it is headless and gives you the real view not a parsed view}
12
+ s.description = %q{This library allows you to create webshots using webkit on MacOSX. A webshot is a screenshot taken inside the browser. The advantage of this library is that it is headless and gives you the real view not a parsed view}
13
13
 
14
14
  s.required_rubygems_version = ">= 1.3.6"
15
15
  s.rubyforge_project = "webshooter"
16
16
 
17
17
  s.add_development_dependency "bundler", ">= 1.0.0"
18
+ #s.add_dependency "responsalizr", "~>1.0.2"
18
19
 
19
20
  s.files = `git ls-files`.split("\n")
20
21
  s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1a
9
- version: 0.0.1a
8
+ - 2a
9
+ version: 0.0.2a
10
10
  platform: ruby
11
11
  authors:
12
12
  - Patrick Debois
@@ -31,7 +31,7 @@ dependencies:
31
31
  name: bundler
32
32
  prerelease: false
33
33
  type: :development
34
- description: This library allows you to create webshots using webkit on MacOSX. A webshot is a screenshot taking inside the browser. The advantage of this library is that it is headless and gives you the real view not a parsed view
34
+ description: This library allows you to create webshots using webkit on MacOSX. A webshot is a screenshot taken inside the browser. The advantage of this library is that it is headless and gives you the real view not a parsed view
35
35
  email:
36
36
  - patrick.debois@jedi.be
37
37
  executables:
@@ -49,6 +49,7 @@ files:
49
49
  - bin/webshooter.rb
50
50
  - lib/webshooter.rb
51
51
  - lib/webshooter/newnsurlrequest.rb
52
+ - lib/webshooter/redirectfollower.rb
52
53
  - lib/webshooter/version.rb
53
54
  - lib/webshooter/webshotprocessor.rb
54
55
  - trials/darkroom.orig.rb