Class: Fragmentary::ExternalUserSession

Inherits:
Object
  • Object
show all
Defined in:
lib/fragmentary/user_session.rb

Instance Method Summary collapse

Constructor Details

#initialize(target, user = nil) ⇒ ExternalUserSession

Returns a new instance of ExternalUserSession.



105
106
107
108
109
110
111
112
# File 'lib/fragmentary/user_session.rb', line 105

def initialize(target, user=nil)
  @target = URI.parse(target)
  @relative_url_root = @target.path
  @session = HTTP.persistent(target)
  @cookie = nil
  @authenticity_token = nil
   if @credentials = session_credentials(user)
end

Instance Method Details

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/fragmentary/user_session.rb', line 114

def send_request(method:, path:, parameters: nil, options: {})
  if options.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
  cookies = @cookie ? {@cookie.name.to_sym => @cookie.value} : {}
  headers = options.try(:delete, :headers) || {}
  headers.merge!({:'X-Requested-With' => 'XMLHttpRequest'}) if options.try(:delete, :xhr)
  response = @session.cookies(cookies).headers(headers).send(method, path, {:json => parameters})
  @cookie = response.cookies.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]
    options = {:headers => {:accept => "text/html,application/xhtml+xml,application/xml"}}
    response = send_request(:method => :get, :path => location, :parameters => nil, :options => options)
  end
  response
end

#session_credentials(user) ⇒ Object



137
138
139
140
# File 'lib/fragmentary/user_session.rb', line 137

def session_credentials(user)
  credentials = user.try(:credentials)
  credentials.is_a?(Proc) ? credentials.call : credentials
end

#sign_inObject



142
143
144
145
146
147
148
149
150
# File 'lib/fragmentary/user_session.rb', line 142

def 
  # The first request retrieves the authentication token
  response = send_request(:method => :get, :path => Fragmentary.config.)
  puts "      * Signing in as #{@credentials.inspect}"
  Rails.logger.info "      * Signing in as #{@credentials.inspect}"
  response = send_request(:method => :post, :path => Fragmentary.config.,
                          :parameters => @credentials.merge(:authenticity_token => @authenticity_token),
                          :options => {:headers => {:accept => "text/html,application/xhtml+xml,application/xml"}})
end

#sign_outObject



152
153
154
155
# File 'lib/fragmentary/user_session.rb', line 152

def sign_out
  send_request(:method => :delete, :path => Fragmentary.config.sign_out_path, :parameters => {:authenticity_token => @authenticity_token})
  @session.close
end