Class: Fragmentary::ExternalUserSession
- Inherits:
-
Object
- Object
- Fragmentary::ExternalUserSession
- Defined in:
- lib/fragmentary/user_session.rb
Instance Method Summary collapse
-
#initialize(target, user = nil) ⇒ ExternalUserSession
constructor
A new instance of ExternalUserSession.
- #send_request(method:, path:, parameters: nil, options: {}) ⇒ Object
- #session_credentials(user) ⇒ Object
- #sign_in ⇒ Object
- #sign_out ⇒ Object
Constructor Details
#initialize(target, user = nil) ⇒ ExternalUserSession
Returns a new instance of ExternalUserSession.
107 108 109 110 111 112 113 114 |
# File 'lib/fragmentary/user_session.rb', line 107 def initialize(target, user=nil) @target = URI.parse(target) @relative_url_root = @target.path @session = HTTP.persistent(target) @cookie = nil @authenticity_token = nil sign_in if @credentials = session_credentials(user) end |
Instance Method Details
#send_request(method:, path:, parameters: nil, options: {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/fragmentary/user_session.rb', line 116 def send_request(method:, path:, parameters: nil, options: {}) if .try(:[], :xhr) Rails.logger.info " * Sending xhr request '#{method.to_s} #{path}'" + (!parameters.nil? ? " with #{parameters.inspect}" : "") else Rails.logger.info " * Sending request '#{method.to_s} #{path}'" + (!parameters.nil? ? " with #{parameters.inspect}" : "") end unless path =~ %r{://} path = @relative_url_root + path end = @cookie ? {@cookie.name.to_sym => @cookie.value} : {} headers = .try(:delete, :headers) || {} headers.merge!({:'X-Requested-With' => 'XMLHttpRequest'}) if .try(:delete, :xhr) response = @session.().headers(headers).send(method, path, {:json => parameters}) @cookie = response..first @authenticity_token = Nokogiri::HTML.parse(response.to_s).css('head meta[name="csrf-token"]').first.try(:[], 'content') if (response.code >=300) && (response.code <=399) location = response.headers[:location] = {:headers => {:accept => "text/html,application/xhtml+xml,application/xml"}} response = send_request(:method => :get, :path => location, :parameters => nil, :options => ) end response end |
#session_credentials(user) ⇒ Object
139 140 141 142 |
# File 'lib/fragmentary/user_session.rb', line 139 def session_credentials(user) credentials = user.try(:credentials) credentials.is_a?(Proc) ? credentials.call : credentials end |
#sign_in ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/fragmentary/user_session.rb', line 144 def sign_in # The first request retrieves the authentication token response = send_request(:method => :get, :path => Fragmentary.config.get_sign_in_path) puts " * Signing in as #{@credentials.inspect}" Rails.logger.info " * Signing in as #{@credentials.inspect}" response = send_request(:method => :post, :path => Fragmentary.config.post_sign_in_path, :parameters => @credentials.merge(:authenticity_token => @authenticity_token), :options => {:headers => {:accept => "text/html,application/xhtml+xml,application/xml"}}) end |
#sign_out ⇒ Object
154 155 156 157 |
# File 'lib/fragmentary/user_session.rb', line 154 def sign_out send_request(:method => :delete, :path => Fragmentary.config.sign_out_path, :parameters => {:authenticity_token => @authenticity_token}) @session.close end |