Class: Fragmentary::InternalUserSession

Inherits:
Object
  • Object
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
23
# File 'lib/fragmentary/user_session.rb', line 11

def initialize(target, user=nil, &block)
  # app is from Rails::ConsoleMethods. It returns an object ActionDispatch::Integration::Session.new(Rails.application)
  # with some extensions. See https://github.com/rails/rails/blob/master/railties/lib/rails/console/app.rb
  # The session object has instance methods get, post etc.
  # See https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/integration.rb
  @session = app
  @user = user
  @target = URI.parse(target)
  @session.host! session_host
  @session.https! if (@target.scheme == 'https')
   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



54
55
56
# File 'lib/fragmentary/user_session.rb', line 54

def method_missing(method, *args)
  @session.send(method, *args)
end

Instance Method Details

#follow_redirect!Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/fragmentary/user_session.rb', line 73

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_rootObject



45
46
47
48
# File 'lib/fragmentary/user_session.rb', line 45

def relative_url_root
  @relative_url_root ||= Rails.application.config.relative_url_root
  @relative_url_root ||= Rails.application.routes.default_url_options[:relative_url_root].try(:gsub, /\/$/,'')
end

#send_request(method:, path:, parameters: nil, options: {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fragmentary/user_session.rb', line 84

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_credentialsObject



37
38
39
40
41
42
43
# File 'lib/fragmentary/user_session.rb', line 37

def session_credentials
  return nil unless @user
  @credentials ||= begin
    credentials = @user.credentials
    credentials.is_a?(Proc) ? credentials.call : credentials
  end
end

#session_hostObject



25
26
27
# File 'lib/fragmentary/user_session.rb', line 25

def session_host
  @session_host ||= @target.host + ((port=@target.port) ? ":#{port}" : "")
end

#session_optionsObject



50
51
52
# File 'lib/fragmentary/user_session.rb', line 50

def session_options
  @session_options ||= relative_url_root ? {:env => {'SCRIPT_NAME' => relative_url_root}} : {}
end

#session_sign_in_pathObject



29
30
31
# File 'lib/fragmentary/user_session.rb', line 29

def 
  @sign_in_path ||= Fragmentary.config.
end

#session_sign_out_pathObject



33
34
35
# File 'lib/fragmentary/user_session.rb', line 33

def session_sign_out_path
  @sign_out_path ||= Fragmentary.config.sign_out_path
end

#sign_inObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fragmentary/user_session.rb', line 58

def 
  raise "Can't sign in without user credentials" unless session_credentials
  send_request(:method => :get, :path => , :options => session_options)  # necessary in order to get the csrf token
  # Note that request is called on session, returning an ActionDispatch::Request; request.session is an ActionDispatch::Request::Session
  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 => , :parameters => parameters, :options => session_options)
  if @session.redirect?
    follow_redirect!
  else
    raise "Sign in failed with credentials #{@credentials.inspect}"
  end
end

#sign_outObject



97
98
99
100
101
# File 'lib/fragmentary/user_session.rb', line 97

def sign_out
  # request is called on session, returning an ActionDispatch::Request; request.session is an ActionDispatch::Request::Session
  parameters = {:_method => 'delete', :authenticity_token => request.session[:_csrf_token]}
  send_request(:method => :post, :path => session_sign_out_path, :parameters => parameters, :options => session_options)
end