Class: Fragmentary::InternalUserSession
- Inherits:
-
Object
- Object
- Fragmentary::InternalUserSession
show all
- Includes:
- Rails::ConsoleMethods
- Defined in:
- lib/fragmentary/user_session.rb
Instance Method Summary
collapse
Constructor Details
#initialize(target, user = nil, &block) ⇒ InternalUserSession
Returns a new instance of InternalUserSession.
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/fragmentary/user_session.rb', line 11
def initialize(target, user=nil, &block)
@session = app
@user = user
@target = URI.parse(target)
@session.host! session_host
sign_in if session_credentials
instance_eval(&block) if block_given?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
52
53
54
|
# File 'lib/fragmentary/user_session.rb', line 52
def method_missing(method, *args)
@session.send(method, *args)
end
|
Instance Method Details
#follow_redirect! ⇒ Object
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/fragmentary/user_session.rb', line 71
def follow_redirect!
raise "not a redirect! #{status} #{status_message}" unless redirect?
if (url = response.location) =~ %r{://}
destination = URI.parse(url)
path = destination.query ? "#{destination.path}?#{destination.query}" : destination.path
end
path = relative_url_root ? path.gsub(Regexp.new("^#{relative_url_root}"), "") : path
send_request(:method => :get, :path => path, :options => session_options)
status
end
|
#relative_url_root ⇒ Object
44
45
46
|
# File 'lib/fragmentary/user_session.rb', line 44
def relative_url_root
@relative_url_root ||= Rails.application.config.relative_url_root
end
|
#send_request(method:, path:, parameters: nil, options: {}) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/fragmentary/user_session.rb', line 82
def send_request(method:, path:, parameters: nil, options: {})
options.merge!({:params => parameters})
options.merge!(session_options)
if options.try(:[], :xhr)
puts " * Sending xhr request '#{method.to_s} #{path}'" + (!parameters.nil? ? " with #{parameters.inspect}" : "")
Rails.logger.info " * Sending xhr request '#{method.to_s} #{path}'" + (!parameters.nil? ? " with #{parameters.inspect}" : "")
else
puts " * Sending request '#{method.to_s} #{path}'" + (!parameters.nil? ? " with #{parameters.inspect}" : "")
Rails.logger.info " * Sending request '#{method.to_s} #{path}'" + (!parameters.nil? ? " with #{parameters.inspect}" : "")
end
@session.send(method, path, options)
end
|
#session_credentials ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/fragmentary/user_session.rb', line 36
def session_credentials
return nil unless @user
@credentials ||= begin
credentials = @user.credentials
credentials.is_a?(Proc) ? credentials.call : credentials
end
end
|
#session_host ⇒ Object
24
25
26
|
# File 'lib/fragmentary/user_session.rb', line 24
def session_host
@session_host ||= @target.host + ((port=@target.port) ? ":#{port}" : "")
end
|
#session_options ⇒ Object
48
49
50
|
# File 'lib/fragmentary/user_session.rb', line 48
def session_options
@session_options ||= relative_url_root ? {:env => {'SCRIPT_NAME' => relative_url_root}} : {}
end
|
#session_sign_in_path ⇒ Object
28
29
30
|
# File 'lib/fragmentary/user_session.rb', line 28
def session_sign_in_path
@sign_in_path ||= Fragmentary.config.get_sign_in_path
end
|
#session_sign_out_path ⇒ Object
32
33
34
|
# File 'lib/fragmentary/user_session.rb', line 32
def session_sign_out_path
@sign_out_path ||= Fragmentary.config.sign_out_path
end
|
#sign_in ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/fragmentary/user_session.rb', line 56
def sign_in
raise "Can't sign in without user credentials" unless session_credentials
send_request(:method => :get, :path => session_sign_in_path, :options => session_options) puts " * Signing in as #{session_credentials.inspect}"
Rails.logger.info " * Signing in as #{session_credentials.inspect}"
parameters = session_credentials.merge(:authenticity_token => request.session[:_csrf_token])
send_request(:method => :post, :path => session_sign_in_path, :parameters => parameters, :options => session_options)
if @session.redirect?
follow_redirect!
else
raise "Sign in failed with credentials #{@credentials.inspect}"
end
end
|
#sign_out ⇒ Object
95
96
97
98
99
|
# File 'lib/fragmentary/user_session.rb', line 95
def sign_out
parameters = {:_method => 'delete', :authenticity_token => request.session[:_csrf_token]}
send_request(:method => :post, :path => session_sign_out_path, :parameters => parameters, :options => session_options)
end
|