Class: Namely::Authenticator
- Inherits:
-
Object
- Object
- Namely::Authenticator
- Defined in:
- lib/namely/authenticator.rb
Defined Under Namespace
Classes: URL
Instance Method Summary collapse
-
#authorization_code_url(options) ⇒ String
Returns a URL to begin the authorization code workflow.
-
#current_user(options) ⇒ Model
Return the profile of the user accessing the API.
-
#initialize(options) ⇒ Authenticator
constructor
Return a new Authenticator instance.
-
#refresh_access_token(options) ⇒ Hash
Get an updated access token using the refresh token.
-
#retrieve_tokens(options) ⇒ Hash
Request an access token and refresh token.
Constructor Details
#initialize(options) ⇒ Authenticator
Return a new Authenticator instance.
18 19 20 21 |
# File 'lib/namely/authenticator.rb', line 18 def initialize() @client_id = .fetch(:client_id) @client_secret = .fetch(:client_secret) end |
Instance Method Details
#authorization_code_url(options) ⇒ String
Returns a URL to begin the authorization code workflow. If you provide a redirect_uri you can receive the server’s response.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/namely/authenticator.rb', line 33 def () URL.new(.merge( path: "/api/v1/oauth2/authorize", params: { response_type: "code", approve: "true", client_id: client_id, }, )).to_s end |
#current_user(options) ⇒ Model
Return the profile of the user accessing the API.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/namely/authenticator.rb', line 116 def current_user() access_token = .fetch(:access_token) subdomain = .fetch(:subdomain) user_url = URL.new(.merge( params: { access_token: access_token, }, path: "/api/v1/profiles/me", )).to_s response = RestClient.get( user_url, accept: :json, ) build_profile( access_token, subdomain, JSON.parse(response)["profiles"].first ) end |
#refresh_access_token(options) ⇒ Hash
Get an updated access token using the refresh token.
101 102 103 104 105 106 107 |
# File 'lib/namely/authenticator.rb', line 101 def refresh_access_token() request_tokens( , grant_type: "refresh_token", refresh_token: .fetch(:refresh_token), ) end |
#retrieve_tokens(options) ⇒ Hash
Request an access token and refresh token.
69 70 71 72 73 74 75 |
# File 'lib/namely/authenticator.rb', line 69 def retrieve_tokens() request_tokens( , grant_type: "authorization_code", code: .fetch(:code), ) end |