Module: LamAuth::Model::ClassMethods

Defined in:
lib/lam_auth/model.rb

Instance Method Summary collapse

Instance Method Details

#create_or_update_by_auth_data(data) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/lam_auth/model.rb', line 26

def create_or_update_by_auth_data(data)
  user = (data['login'])
  user.update_attributes! data.slice(*%w{email first_name last_name}).merge(
    'userpic' => data['userpic']['icon'], 
    'profile' => data.except(*%w{login email first_name last_name userpic})
  )
  user
end

#find_by_access_token(access_token) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lam_auth/model.rb', line 10

def find_by_access_token(access_token)
  Rails.logger.info("Trying to authorize a token #{access_token.inspect} ")

  uri = URI.parse(LamAuth.url)
  http = Net::HTTP.new(uri.host, uri.port)
  response = http.get("#{uri.path}/users/me", {'Authorization' => "Token token=\"#{access_token}\""})
  if response.code == "200"
    data = ActiveSupport::JSON.decode(response.body)
    Rails.logger.info("...success: #{data.inspect}")
    create_or_update_by_auth_data(data)
  else
    Rails.logger.info("...failed with #{response.code}!")
    nil
  end
end