mechanized_session 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +43 -9
- data/Rakefile +1 -1
- data/lib/mechanized_session.rb +38 -3
- metadata +62 -17
data/README.rdoc
CHANGED
@@ -1,32 +1,66 @@
|
|
1
1
|
= mechanized_session
|
2
2
|
|
3
|
-
* http://github.com
|
3
|
+
* http://github.com/dsboulder/mechanized_session
|
4
4
|
|
5
5
|
== DESCRIPTION:
|
6
6
|
|
7
|
-
|
7
|
+
A gem which uses the `mechanize` gem to make it easier to execute remote requests that require an authenticated session.
|
8
|
+
It allows the programmer to provide a `:login` action callback which is executed whenever mechanized_session determines that the
|
9
|
+
session has gone bad or authentication is required. The programmer also provides other actions which perform useful tasks.
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
* FIX (list of features or problems)
|
11
|
+
To use this with rails, check out `acts_as_other_website`.
|
12
12
|
|
13
13
|
== SYNOPSIS:
|
14
14
|
|
15
|
-
|
15
|
+
class SomeSiteSession < MechanizedSession
|
16
|
+
action :login do |session, options|
|
17
|
+
session.get('http://www.google.com/login') do |page|
|
18
|
+
next_page = page.form_with(:action =>"/login") do |form|
|
19
|
+
form["email"] = options[:username]
|
20
|
+
form["password"] = options[:password]
|
21
|
+
end.click_button
|
22
|
+
return false if next_page.uri.path.include?("/login")
|
23
|
+
end
|
24
|
+
true
|
25
|
+
end
|
26
|
+
|
27
|
+
action :get_widgets do |session|
|
28
|
+
links = []
|
29
|
+
session.get('http://www.google.com/ig") do |page| # might throw InvalidSession if not logged in
|
30
|
+
links += page.links
|
31
|
+
end
|
32
|
+
links
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.requires_login?(page) # explaining to mechanized_session when a page was a redirect to the login page
|
36
|
+
page.uri.path.downcase.include?("/login")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
@session = SomeSiteSession.new
|
41
|
+
begin
|
42
|
+
@session.get_widgets
|
43
|
+
rescue InvalidSession => e
|
44
|
+
# deal with invalid session, ask the user for credentials
|
45
|
+
@session = SomeSiteSession.new(:username => "bob", :password => "password")
|
46
|
+
retry
|
47
|
+
end
|
16
48
|
|
17
49
|
== REQUIREMENTS:
|
18
50
|
|
19
|
-
*
|
51
|
+
* mechanize gem
|
20
52
|
|
21
53
|
== INSTALL:
|
22
54
|
|
23
|
-
*
|
55
|
+
* sudo gem install mechanized_session
|
56
|
+
|
57
|
+
See blog post at: http://flouri.sh/2009/12/7/introducing-mechanized_session-and-acts_as_other_website
|
24
58
|
|
25
59
|
== LICENSE:
|
26
60
|
|
27
61
|
(The MIT License)
|
28
62
|
|
29
|
-
Copyright (c) 2009
|
63
|
+
Copyright (c) 2009 David Stevenson
|
30
64
|
|
31
65
|
Permission is hereby granted, free of charge, to any person obtaining
|
32
66
|
a copy of this software and associated documentation files (the
|
data/Rakefile
CHANGED
@@ -14,7 +14,7 @@ $hoe = Hoe.spec 'mechanized_session' do
|
|
14
14
|
self.developer 'David Stevenson', 'stellar256@hotmail.com'
|
15
15
|
# self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
16
16
|
# self.rubyforge_name = self.name # TODO this is default value
|
17
|
-
self.extra_deps = [['mechanize','>= 0.
|
17
|
+
self.extra_deps = [['mechanize','>= 1.0.0']]
|
18
18
|
|
19
19
|
end
|
20
20
|
|
data/lib/mechanized_session.rb
CHANGED
@@ -19,7 +19,7 @@ class MechanizedSession
|
|
19
19
|
attr_accessor :inner
|
20
20
|
end
|
21
21
|
|
22
|
-
VERSION = '0.0
|
22
|
+
VERSION = '0.1.0'
|
23
23
|
attr_accessor :agent
|
24
24
|
attr_accessor :disable_session_check
|
25
25
|
attr_accessor :logger
|
@@ -79,6 +79,33 @@ class MechanizedSession
|
|
79
79
|
page
|
80
80
|
end
|
81
81
|
|
82
|
+
def post(uri, params = {}, &block)
|
83
|
+
logger.debug "POST #{uri} #{params.inspect}"
|
84
|
+
page = agent.post(uri, params)
|
85
|
+
logger.debug "Successfully got page #{page.uri}"
|
86
|
+
check_for_invalid_session! unless disable_session_check?
|
87
|
+
yield page if block_given?
|
88
|
+
page
|
89
|
+
end
|
90
|
+
|
91
|
+
def put(uri, entity, &block)
|
92
|
+
logger.debug "PUT #{uri} #{entity.inspect}"
|
93
|
+
page = agent.put(uri, entity)
|
94
|
+
logger.debug "Successfully got page #{page.uri}"
|
95
|
+
check_for_invalid_session! unless disable_session_check?
|
96
|
+
yield page if block_given?
|
97
|
+
page
|
98
|
+
end
|
99
|
+
|
100
|
+
def delete(uri, params = {}, &block)
|
101
|
+
logger.debug "DELETE #{uri} #{params.inspect}"
|
102
|
+
page = agent.delete(uri, params)
|
103
|
+
logger.debug "Successfully got page #{page.uri}"
|
104
|
+
check_for_invalid_session! unless disable_session_check?
|
105
|
+
yield page if block_given?
|
106
|
+
page
|
107
|
+
end
|
108
|
+
|
82
109
|
def login(username, password)
|
83
110
|
raise "#{self.class} must declare action :login describing how to log in a session"
|
84
111
|
end
|
@@ -87,6 +114,10 @@ class MechanizedSession
|
|
87
114
|
agent.cookie_jar.to_yaml
|
88
115
|
end
|
89
116
|
|
117
|
+
def basic_auth
|
118
|
+
nil
|
119
|
+
end
|
120
|
+
|
90
121
|
private
|
91
122
|
def disable_session_check?
|
92
123
|
@disable_session_check
|
@@ -104,6 +135,10 @@ class MechanizedSession
|
|
104
135
|
end
|
105
136
|
|
106
137
|
def create_agent
|
107
|
-
self.agent =
|
138
|
+
self.agent = Mechanize.new
|
139
|
+
self.agent.log = logger
|
140
|
+
if auth = basic_auth
|
141
|
+
self.agent.auth(*auth)
|
142
|
+
end
|
108
143
|
end
|
109
|
-
end
|
144
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mechanized_session
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- David Stevenson
|
@@ -9,30 +15,63 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2010-10-02 00:00:00 -07:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
22
|
name: mechanize
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 1.0.0
|
17
35
|
type: :runtime
|
18
|
-
|
19
|
-
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rubyforge
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
20
42
|
requirements:
|
21
43
|
- - ">="
|
22
44
|
- !ruby/object:Gem::Version
|
23
|
-
|
24
|
-
|
45
|
+
hash: 7
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 0
|
49
|
+
- 4
|
50
|
+
version: 2.0.4
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
25
53
|
- !ruby/object:Gem::Dependency
|
26
54
|
name: hoe
|
27
|
-
|
28
|
-
|
29
|
-
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
30
58
|
requirements:
|
31
59
|
- - ">="
|
32
60
|
- !ruby/object:Gem::Version
|
33
|
-
|
34
|
-
|
35
|
-
|
61
|
+
hash: 21
|
62
|
+
segments:
|
63
|
+
- 2
|
64
|
+
- 6
|
65
|
+
- 1
|
66
|
+
version: 2.6.1
|
67
|
+
type: :development
|
68
|
+
version_requirements: *id003
|
69
|
+
description: |-
|
70
|
+
A gem which uses the `mechanize` gem to make it easier to execute remote requests that require an authenticated session.
|
71
|
+
It allows the programmer to provide a `:login` action callback which is executed whenever mechanized_session determines that the
|
72
|
+
session has gone bad or authentication is required. The programmer also provides other actions which perform useful tasks.
|
73
|
+
|
74
|
+
To use this with rails, check out `acts_as_other_website`.
|
36
75
|
email:
|
37
76
|
- stellar256@hotmail.com
|
38
77
|
executables: []
|
@@ -56,7 +95,7 @@ files:
|
|
56
95
|
- test/test_helper.rb
|
57
96
|
- test/test_mechanized_session.rb
|
58
97
|
has_rdoc: true
|
59
|
-
homepage: http://github.com
|
98
|
+
homepage: http://github.com/dsboulder/mechanized_session
|
60
99
|
licenses: []
|
61
100
|
|
62
101
|
post_install_message:
|
@@ -66,24 +105,30 @@ rdoc_options:
|
|
66
105
|
require_paths:
|
67
106
|
- lib
|
68
107
|
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
69
109
|
requirements:
|
70
110
|
- - ">="
|
71
111
|
- !ruby/object:Gem::Version
|
112
|
+
hash: 3
|
113
|
+
segments:
|
114
|
+
- 0
|
72
115
|
version: "0"
|
73
|
-
version:
|
74
116
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
75
118
|
requirements:
|
76
119
|
- - ">="
|
77
120
|
- !ruby/object:Gem::Version
|
121
|
+
hash: 3
|
122
|
+
segments:
|
123
|
+
- 0
|
78
124
|
version: "0"
|
79
|
-
version:
|
80
125
|
requirements: []
|
81
126
|
|
82
127
|
rubyforge_project: mechanized_session
|
83
|
-
rubygems_version: 1.3.
|
128
|
+
rubygems_version: 1.3.7
|
84
129
|
signing_key:
|
85
130
|
specification_version: 3
|
86
|
-
summary:
|
131
|
+
summary: A gem which uses the `mechanize` gem to make it easier to execute remote requests that require an authenticated session
|
87
132
|
test_files:
|
88
133
|
- test/test_helper.rb
|
89
134
|
- test/test_mechanized_session.rb
|