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.is_a?(Array) ? scope.join(' ') : 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 expired OAuth2 access token.

Parameters:

  • refresh_token (String)

Returns:



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby_lokalise_api/oauth2/auth.rb', line 60

def refresh(refresh_token)
  endpoint = oauth2_endpoint.new(
    self,
    query: :token,
    req: common_params.merge(
      grant_type: 'refresh_token',
      refresh_token: refresh_token
    )
  )

  RubyLokaliseApi::Resources::OAuth2RefreshedToken.new endpoint.do_post
end

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

Requests OAuth2 access token. Requires OAuth2 code obtained using the ‘.auth` method

Parameters:

  • code (String)

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby_lokalise_api/oauth2/auth.rb', line 44

def token(code)
  endpoint = oauth2_endpoint.new(
    self,
    query: :token,
    req: common_params.merge(
      grant_type: 'authorization_code',
      code: code
    )
  )

  RubyLokaliseApi::Resources::OAuth2Token.new endpoint.do_post
end