mechanized_session 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +11 -0
- data/PostInstall.txt +7 -0
- data/README.rdoc +48 -0
- data/Rakefile +26 -0
- data/lib/mechanized_session.rb +92 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/test/test_helper.rb +4 -0
- data/test/test_mechanized_session.rb +88 -0
- metadata +89 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/PostInstall.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= mechanized_session
|
2
|
+
|
3
|
+
* http://github.com/#{github_username}/#{project_name}
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
FIX (describe your package)
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* FIX (list of features or problems)
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
FIX (code sample of usage)
|
16
|
+
|
17
|
+
== REQUIREMENTS:
|
18
|
+
|
19
|
+
* FIX (list of requirements)
|
20
|
+
|
21
|
+
== INSTALL:
|
22
|
+
|
23
|
+
* FIX (sudo gem install, anything else)
|
24
|
+
|
25
|
+
== LICENSE:
|
26
|
+
|
27
|
+
(The MIT License)
|
28
|
+
|
29
|
+
Copyright (c) 2009 FIXME full name
|
30
|
+
|
31
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
32
|
+
a copy of this software and associated documentation files (the
|
33
|
+
'Software'), to deal in the Software without restriction, including
|
34
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
35
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
36
|
+
permit persons to whom the Software is furnished to do so, subject to
|
37
|
+
the following conditions:
|
38
|
+
|
39
|
+
The above copyright notice and this permission notice shall be
|
40
|
+
included in all copies or substantial portions of the Software.
|
41
|
+
|
42
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
43
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
44
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
45
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
46
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
47
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
48
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'hoe', '>= 2.1.0'
|
3
|
+
require 'hoe'
|
4
|
+
require 'fileutils'
|
5
|
+
require './lib/mechanized_session'
|
6
|
+
|
7
|
+
Hoe.plugin :newgem
|
8
|
+
# Hoe.plugin :website
|
9
|
+
# Hoe.plugin :cucumberfeatures
|
10
|
+
|
11
|
+
# Generate all the Rake tasks
|
12
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
13
|
+
$hoe = Hoe.spec 'mechanized_session' do
|
14
|
+
self.developer 'David Stevenson', 'stellar256@hotmail.com'
|
15
|
+
# self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
16
|
+
# self.rubyforge_name = self.name # TODO this is default value
|
17
|
+
self.extra_deps = [['mechanize','>= 0.9.3']]
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'newgem/tasks'
|
22
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
23
|
+
|
24
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
25
|
+
# remove_task :default
|
26
|
+
# task :default => [:spec, :features]
|
@@ -0,0 +1,92 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
gem "mechanize"
|
5
|
+
require "mechanize"
|
6
|
+
|
7
|
+
class MechanizedSession
|
8
|
+
class Error < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
class InvalidAuthentication < MechanizedSession::Error
|
12
|
+
end
|
13
|
+
|
14
|
+
class InvalidSession < MechanizedSession::Error
|
15
|
+
end
|
16
|
+
|
17
|
+
class MechanizeError < MechanizedSession::Error
|
18
|
+
attr_accessor :inner
|
19
|
+
end
|
20
|
+
|
21
|
+
VERSION = '0.0.1'
|
22
|
+
attr_accessor :agent
|
23
|
+
attr_accessor :disable_session_check
|
24
|
+
attr_reader :logged_in
|
25
|
+
|
26
|
+
def self.action(name, &block)
|
27
|
+
define_method name do |*args|
|
28
|
+
result = nil
|
29
|
+
begin
|
30
|
+
self.disable_session_check = true if name == :login
|
31
|
+
result = block.call(self, *args)
|
32
|
+
check_for_invalid_session! unless name == :login
|
33
|
+
rescue StandardError => e
|
34
|
+
if e.is_a?(MechanizedSession::Error)
|
35
|
+
raise e
|
36
|
+
else
|
37
|
+
ex = MechanizeError.new("Unable to execute action :#{name}, due to '#{e}'")
|
38
|
+
ex.inner = e
|
39
|
+
ex.set_backtrace(e.backtrace)
|
40
|
+
raise ex
|
41
|
+
end
|
42
|
+
ensure
|
43
|
+
self.disable_session_check = nil
|
44
|
+
end
|
45
|
+
result
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def initialize(options)
|
50
|
+
create_agent
|
51
|
+
if options[:session_data]
|
52
|
+
self.agent.cookie_jar = YAML.load(options[:session_data])
|
53
|
+
elsif options[:username]
|
54
|
+
result = self.login(options)
|
55
|
+
if result == false
|
56
|
+
raise InvalidAuthentication
|
57
|
+
elsif result == true
|
58
|
+
@logged_in = true
|
59
|
+
else
|
60
|
+
raise "the :login method of #{self.class} must return exactly true or false (depending on the success of the login)"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def get(uri, &block)
|
66
|
+
page = agent.get(uri)
|
67
|
+
check_for_invalid_session! unless disable_session_check?
|
68
|
+
yield page if block_given?
|
69
|
+
page
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
def disable_session_check?
|
74
|
+
@disable_session_check
|
75
|
+
end
|
76
|
+
|
77
|
+
def check_for_invalid_session!
|
78
|
+
raise InvalidSession if agent.current_page && self.class.requires_login?(agent.current_page)
|
79
|
+
end
|
80
|
+
|
81
|
+
def login(username, password)
|
82
|
+
raise "#{self.class} must declare action :login describing how to log in a session"
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.requires_login?(page)
|
86
|
+
page.uri.path.downcase.include?("login")
|
87
|
+
end
|
88
|
+
|
89
|
+
def create_agent
|
90
|
+
self.agent = WWW::Mechanize.new
|
91
|
+
end
|
92
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/mechanized_session.rb'}"
|
9
|
+
puts "Loading mechanized_session gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require "pp"
|
3
|
+
|
4
|
+
class TestMechanizedSession < Test::Unit::TestCase
|
5
|
+
class ExampleEmptyMechanizedSession < MechanizedSession
|
6
|
+
action :login do |session, options|
|
7
|
+
session.get('https://www.google.com/accounts/ManageAccount?hl=en') do |page|
|
8
|
+
end
|
9
|
+
if options[:username] == "bad implementation"
|
10
|
+
nil
|
11
|
+
else
|
12
|
+
@username = options[:username]
|
13
|
+
@password = options[:password]
|
14
|
+
options[:username] == "bad user" ? false : true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
action :do_something_requiring_login do |session|
|
19
|
+
session.get('https://www.google.com/accounts/ManageAccount?hl=en') do |page|
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def setup
|
25
|
+
@previous_session_data = <<-YAML
|
26
|
+
--- !ruby/object:WWW::Mechanize::CookieJar
|
27
|
+
jar:
|
28
|
+
google.com:
|
29
|
+
/:
|
30
|
+
PREF: !ruby/object:WWW::Mechanize::Cookie
|
31
|
+
comment:
|
32
|
+
comment_url:
|
33
|
+
discard:
|
34
|
+
domain: google.com
|
35
|
+
expires: Mon, 05 Dec 2011 20:13:27 GMT
|
36
|
+
max_age:
|
37
|
+
name: PREF
|
38
|
+
path: /
|
39
|
+
port:
|
40
|
+
secure: false
|
41
|
+
value: ID=81b036b3879502d3:TM=1260044007:LM=1260044007:S=gTZE41MSHOrSwy5S
|
42
|
+
version: 0
|
43
|
+
NID: !ruby/object:WWW::Mechanize::Cookie
|
44
|
+
comment:
|
45
|
+
comment_url:
|
46
|
+
discard:
|
47
|
+
domain: google.com
|
48
|
+
expires: Sun, 06 Jun 2010 20:13:27 GMT
|
49
|
+
max_age:
|
50
|
+
name: NID
|
51
|
+
path: /
|
52
|
+
port:
|
53
|
+
secure: false
|
54
|
+
value: 29=W6XhKG_4rDv709QX6oDGzHQ5y17pISryKo75MZuWOZ59HNZm011Htlk_TYKIdXEwau_4GK3jIjFHALrbDFjoJ7Pz-zmOc5evFZAp71Og7itAVYPDQukb8Z7DwBB9qLzt
|
55
|
+
version: 0
|
56
|
+
YAML
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_initialize_with_previous_session__sets_cookies
|
60
|
+
session = MechanizedSession.new(:session_data => @previous_session_data)
|
61
|
+
google_cookies = session.agent.cookie_jar.cookies(URI.parse("http://google.com/"))
|
62
|
+
assert_equal 2, google_cookies.length
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_initialize_with_username__calls_login
|
66
|
+
session = ExampleEmptyMechanizedSession.new(:username => "david", :password => "ponies")
|
67
|
+
assert session.logged_in
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_initialize_with_username__calls_login__raises_exception_if_returns_false
|
71
|
+
assert_raises(MechanizedSession::InvalidAuthentication) do
|
72
|
+
ExampleEmptyMechanizedSession.new(:username => "bad user", :password => "noponies")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_initialize_with_username__calls_login__raises_exception_if_returns_non_true
|
77
|
+
assert_raises(RuntimeError) do
|
78
|
+
ExampleEmptyMechanizedSession.new(:username => "bad implementation", :password => "noponies")
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_check_for_invalid_session__raises_error_when_doing_something_that_requires_login
|
83
|
+
session = ExampleEmptyMechanizedSession.new({})
|
84
|
+
assert_raises(MechanizedSession::InvalidSession) {
|
85
|
+
session.do_something_requiring_login
|
86
|
+
}
|
87
|
+
end
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mechanized_session
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Stevenson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-05 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mechanize
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.9.3
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: hoe
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.3.3
|
34
|
+
version:
|
35
|
+
description: FIX (describe your package)
|
36
|
+
email:
|
37
|
+
- stellar256@hotmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- History.txt
|
44
|
+
- Manifest.txt
|
45
|
+
- PostInstall.txt
|
46
|
+
files:
|
47
|
+
- History.txt
|
48
|
+
- Manifest.txt
|
49
|
+
- PostInstall.txt
|
50
|
+
- README.rdoc
|
51
|
+
- Rakefile
|
52
|
+
- lib/mechanized_session.rb
|
53
|
+
- script/console
|
54
|
+
- script/destroy
|
55
|
+
- script/generate
|
56
|
+
- test/test_helper.rb
|
57
|
+
- test/test_mechanized_session.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://github.com/#{github_username}/#{project_name}
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options:
|
64
|
+
- --main
|
65
|
+
- README.rdoc
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
version:
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project: mechanized_session
|
83
|
+
rubygems_version: 1.3.5
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: FIX (describe your package)
|
87
|
+
test_files:
|
88
|
+
- test/test_helper.rb
|
89
|
+
- test/test_mechanized_session.rb
|