class LinkedinSignIn::Identity

Public Class Methods

new(token) click to toggle source
# File lib/linkedin_sign_in/identity.rb, line 7
def initialize(token)
  set_extracted_payload(token)
end

Public Instance Methods

avatar_url() click to toggle source
# File lib/linkedin_sign_in/identity.rb, line 27
def avatar_url
  @payload["pictureUrl"]
end
current_company_name() click to toggle source
# File lib/linkedin_sign_in/identity.rb, line 31
def current_company_name
  positions = @payload.dig("positions", "values")
  current_position = positions.find { |position| position["isCurrent"] }
  current_position.dig("company", "name")
end
email_address() click to toggle source
# File lib/linkedin_sign_in/identity.rb, line 23
def email_address
  @payload["emailAddress"]
end
first_name() click to toggle source
# File lib/linkedin_sign_in/identity.rb, line 15
def first_name
  @payload["firstName"]
end
last_name() click to toggle source
# File lib/linkedin_sign_in/identity.rb, line 19
def last_name
  @payload["lastName"]
end
user_id() click to toggle source
# File lib/linkedin_sign_in/identity.rb, line 11
def user_id
  @payload["id"]
end

Private Instance Methods

set_extracted_payload(token) click to toggle source
# File lib/linkedin_sign_in/identity.rb, line 38
def set_extracted_payload(token)
  uri = URI("https://api.linkedin.com/v1/people/~:(id,firstName,lastName,picture-url,email-address,positions)?format=json")
  request = Net::HTTP::Get.new uri
  request['Authorization'] = "Bearer #{token}"
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(request)
  end

  case response
  when Net::HTTPSuccess
    @payload = JSON(response.body)
  else
    raise ValidationError, response.body
  end
end