mechanized_session 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) ||
3
3
  require "rubygems"
4
4
  gem "mechanize"
5
5
  require "mechanize"
6
+ require "logger"
6
7
 
7
8
  class MechanizedSession
8
9
  class Error < StandardError
@@ -18,20 +19,23 @@ class MechanizedSession
18
19
  attr_accessor :inner
19
20
  end
20
21
 
21
- VERSION = '0.0.1'
22
+ VERSION = '0.0.2'
22
23
  attr_accessor :agent
23
24
  attr_accessor :disable_session_check
25
+ attr_accessor :logger
24
26
  attr_reader :logged_in
25
27
 
26
28
  def self.action(name, &block)
27
29
  define_method name do |*args|
28
30
  result = nil
31
+ logger.debug "Executing action :#{name}"
29
32
  begin
30
33
  self.disable_session_check = true if name == :login
31
34
  result = block.call(self, *args)
32
35
  check_for_invalid_session! unless name == :login
33
36
  rescue StandardError => e
34
- if e.is_a?(MechanizedSession::Error)
37
+ logger.debug "Exception #{e} (#{e.class}) raised in action :#{name}"
38
+ if e.is_a?(MechanizedSession::Error) || e.is_a?(WWW::Mechanize::ResponseCodeError) && e.response_code.to_s == "401"
35
39
  raise e
36
40
  else
37
41
  ex = MechanizeError.new("Unable to execute action :#{name}, due to '#{e}'")
@@ -48,13 +52,17 @@ class MechanizedSession
48
52
 
49
53
  def initialize(options)
50
54
  create_agent
55
+ @logger = options[:logger] || Logger.new($stdout)
51
56
  if options[:session_data]
57
+ logger.debug "Initializing session from previous data"
52
58
  self.agent.cookie_jar = YAML.load(options[:session_data])
53
59
  elsif options[:username]
54
60
  result = self.login(options)
55
61
  if result == false
62
+ logger.debug "Login returned false, due to invalid credentials we hope"
56
63
  raise InvalidAuthentication
57
64
  elsif result == true
65
+ logger.debug "Login returned true, assuming session established"
58
66
  @logged_in = true
59
67
  else
60
68
  raise "the :login method of #{self.class} must return exactly true or false (depending on the success of the login)"
@@ -63,23 +71,32 @@ class MechanizedSession
63
71
  end
64
72
 
65
73
  def get(uri, &block)
74
+ logger.debug "GET #{uri}"
66
75
  page = agent.get(uri)
76
+ logger.debug "Successfully got page #{page.uri}"
67
77
  check_for_invalid_session! unless disable_session_check?
68
78
  yield page if block_given?
69
79
  page
70
80
  end
71
81
 
82
+ def login(username, password)
83
+ raise "#{self.class} must declare action :login describing how to log in a session"
84
+ end
85
+
86
+ def session_data
87
+ agent.cookie_jar.to_yaml
88
+ end
89
+
72
90
  private
73
91
  def disable_session_check?
74
92
  @disable_session_check
75
93
  end
76
94
 
77
95
  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"
96
+ if agent.current_page && self.class.requires_login?(agent.current_page)
97
+ logger.info "MechanizedSession is no longer valid"
98
+ raise InvalidSession
99
+ end
83
100
  end
84
101
 
85
102
  def self.requires_login?(page)
@@ -22,6 +22,7 @@ class TestMechanizedSession < Test::Unit::TestCase
22
22
  end
23
23
 
24
24
  def setup
25
+ @logger = Logger.new(StringIO.new)
25
26
  @previous_session_data = <<-YAML
26
27
  --- !ruby/object:WWW::Mechanize::CookieJar
27
28
  jar:
@@ -57,30 +58,30 @@ jar:
57
58
  end
58
59
 
59
60
  def test_initialize_with_previous_session__sets_cookies
60
- session = MechanizedSession.new(:session_data => @previous_session_data)
61
+ session = MechanizedSession.new(:session_data => @previous_session_data, :logger =>@logger)
61
62
  google_cookies = session.agent.cookie_jar.cookies(URI.parse("http://google.com/"))
62
63
  assert_equal 2, google_cookies.length
63
64
  end
64
65
 
65
66
  def test_initialize_with_username__calls_login
66
- session = ExampleEmptyMechanizedSession.new(:username => "david", :password => "ponies")
67
+ session = ExampleEmptyMechanizedSession.new(:username => "david", :password => "ponies", :logger => @logger)
67
68
  assert session.logged_in
68
69
  end
69
70
 
70
71
  def test_initialize_with_username__calls_login__raises_exception_if_returns_false
71
72
  assert_raises(MechanizedSession::InvalidAuthentication) do
72
- ExampleEmptyMechanizedSession.new(:username => "bad user", :password => "noponies")
73
+ ExampleEmptyMechanizedSession.new(:username => "bad user", :password => "noponies", :logger => @logger)
73
74
  end
74
75
  end
75
76
 
76
77
  def test_initialize_with_username__calls_login__raises_exception_if_returns_non_true
77
78
  assert_raises(RuntimeError) do
78
- ExampleEmptyMechanizedSession.new(:username => "bad implementation", :password => "noponies")
79
+ ExampleEmptyMechanizedSession.new(:username => "bad implementation", :password => "noponies", :logger => @logger)
79
80
  end
80
81
  end
81
82
 
82
83
  def test_check_for_invalid_session__raises_error_when_doing_something_that_requires_login
83
- session = ExampleEmptyMechanizedSession.new({})
84
+ session = ExampleEmptyMechanizedSession.new(:logger => @logger)
84
85
  assert_raises(MechanizedSession::InvalidSession) {
85
86
  session.do_something_requiring_login
86
87
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mechanized_session
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Stevenson
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-05 00:00:00 -08:00
12
+ date: 2009-12-06 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency