Class: RubyLokaliseApi::OAuth2::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_lokalise_api/oauth2/auth.rb

Overview

This class defines OAuth2 flow

Constant Summary collapse

OAUTH2_ENDPOINT =
RubyLokaliseApi::Endpoints::OAuth2::OAuth2Endpoint

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, params = {}) ⇒ Auth

Returns a new instance of Auth.



11
12
13
14
15
16
# File 'lib/ruby_lokalise_api/oauth2/auth.rb', line 11

def initialize(client_id, client_secret, params = {})
  @client_id = client_id
  @client_secret = client_secret
  @timeout = params[:timeout]
  @open_timeout = params[:open_timeout]
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



7
8
9
# File 'lib/ruby_lokalise_api/oauth2/auth.rb', line 7

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



7
8
9
# File 'lib/ruby_lokalise_api/oauth2/auth.rb', line 7

def client_secret
  @client_secret
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



7
8
9
# File 'lib/ruby_lokalise_api/oauth2/auth.rb', line 7

def open_timeout
  @open_timeout
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



7
8
9
# File 'lib/ruby_lokalise_api/oauth2/auth.rb', line 7

def timeout
  @timeout
end

Instance Method Details

#auth(scope:, redirect_uri: nil, state: nil) ⇒ String

Builds an OAuth2 link that customers have to visit in order to obtain a special code

Parameters:

  • scope (Array, String)
  • redirect_uri (String) (defaults to: nil)
  • state (String) (defaults to: nil)

Returns:

  • (String)


29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_lokalise_api/oauth2/auth.rb', line 29

def auth(scope:, redirect_uri: nil, state: nil)
  get_params = {
    client_id: client_id,
    scope: scope_to_string(scope),
    state: state,
    redirect_uri: redirect_uri
  }

  oauth2_endpoint.new(self, query: 'auth', get: get_params).full_uri
end

#oauth2_endpointObject

Returns OAuth2 endpoint URI



19
20
21
# File 'lib/ruby_lokalise_api/oauth2/auth.rb', line 19

def oauth2_endpoint
  self.class.const_get(:OAUTH2_ENDPOINT)
end

#refresh(refresh_token) ⇒ RubyLokaliseApi::Resources::OAuth2RefreshedToken

Refreshes an expired OAuth2 token

Parameters:

  • refresh_token (String)

    The refresh token

Returns:



50
51
52
# File 'lib/ruby_lokalise_api/oauth2/auth.rb', line 50

def refresh(refresh_token)
  request_token(grant_type: 'refresh_token', refresh_token: refresh_token)
end

#token(code) ⇒ RubyLokaliseApi::Resources::OAuth2Token

Requests an OAuth2 access token using a code

Parameters:

  • code (String)

    The authorization code

Returns:



43
44
45
# File 'lib/ruby_lokalise_api/oauth2/auth.rb', line 43

def token(code)
  request_token(grant_type: 'authorization_code', code: code)
end