Class: Namely::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/namely/authenticator.rb

Defined Under Namespace

Classes: URL

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Authenticator

Return a new Authenticator instance.

Examples:

authenticator = Authenticator.new(
  client_id: "my-client-id",
  client_secret: "my-client-secret"
)

Parameters:

  • options (Hash)

Options Hash (options):

  • client_id (String)
  • client_secret (String)


18
19
20
21
# File 'lib/namely/authenticator.rb', line 18

def initialize(options)
  @client_id = options.fetch(:client_id)
  @client_secret = options.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.

Parameters:

  • options (Hash)

Options Hash (options):

  • host (String) — default: optional
  • protocol (String) — default: optional, defaults to "https"
  • subdomain (String) — default: required
  • redirect_uri (String) — default: optional

Returns:

  • (String)


33
34
35
36
37
38
39
40
41
42
# File 'lib/namely/authenticator.rb', line 33

def authorization_code_url(options)
  URL.new(options.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.

Parameters:

  • options (Hash)

Options Hash (options):

  • access_token (String) — default: required
  • subdomain (String) — default: required

Returns:

  • (Model)

    the profile of the current user.



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(options)
  access_token = options.fetch(:access_token)
  subdomain = options.fetch(:subdomain)

  user_url = URL.new(options.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.

Examples:

authenticator = Authenticator.new(
  client_id: "my-client-id",
  client_secret: "my-client-secret"
)

tokens = authenticator.refresh_access_token(
  redirect_uri: "my-redirect-uri",
  refresh_token: "my-refresh-token",
  subdomain: "my-subdomain"
)

tokens["access_token"] # => "my-access-token"
tokens["expires_in"] # => 1234
tokens["token_type"] # => "bearer"

Parameters:

  • options (Hash)

Options Hash (options):

  • redirect_uri (String) — default: required
  • refresh_token (String) — default: required
  • subdomain (String) — default: required

Returns:

  • (Hash)


101
102
103
104
105
106
107
# File 'lib/namely/authenticator.rb', line 101

def refresh_access_token(options)
  request_tokens(
    options,
    grant_type: "refresh_token",
    refresh_token: options.fetch(:refresh_token),
  )
end

#retrieve_tokens(options) ⇒ Hash

Request an access token and refresh token.

Examples:

authenticator = Authenticator.new(
  client_id: "my-client-id",
  client_secret: "my-client-secret"
)

tokens = authenticator.retrieve_tokens(
  code: "my-code",
  subdomain: "my-subdomain",
  redirect_uri: "my-redirect-uri"
)

tokens["access_token"] # => "my-access-token"
tokens["refresh_token"] # => "my-refresh-token"
tokens["expires_in"] # => 1234
tokens["token_type"] # => "bearer"

Parameters:

  • options (Hash)

Options Hash (options):

  • code (String) — default: required
  • redirect_uri (String) — default: required
  • subdomain (String) — default: required

Returns:

  • (Hash)


69
70
71
72
73
74
75
# File 'lib/namely/authenticator.rb', line 69

def retrieve_tokens(options)
  request_tokens(
    options,
    grant_type: "authorization_code",
    code: options.fetch(:code),
  )
end