Sha256: cc32aff6a339345282e341020fc77c2232147831db4e5d589ce1cc22c249efd3

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Lokalise
  module OAuth2
    class Auth
      include Lokalise::OAuth2::Request

      attr_reader :client_id, :client_secret

      def initialize(client_id, client_secret)
        @client_id = client_id
        @client_secret = client_secret
      end

      def auth(scope:, redirect_uri: nil, state: nil)
        scope = scope.join(' ') if scope.is_a?(Array)

        params = {
          client_id: client_id,
          scope: scope
        }
        params[:state] = state unless state.nil?
        params[:redirect_uri] = redirect_uri unless redirect_uri.nil?

        _build_url_from params
      end

      def token(code)
        params = base_params.merge({
                                     code: code,
                                     grant_type: 'authorization_code'
                                   })
        post 'token', params
      end

      def refresh(token)
        params = base_params.merge({
                                     refresh_token: token,
                                     grant_type: 'refresh_token'
                                   })
        post 'token', params
      end

      private

      def base_params
        {
          client_id: client_id,
          client_secret: client_secret
        }
      end

      def _build_url_from(params)
        URI::HTTPS.build(
          host: BASE_URL.host,
          path: "#{BASE_URL.path}auth",
          query: URI.encode_www_form(params)
        ).to_s
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-lokalise-api-5.0.0 lib/ruby-lokalise-api/oauth2/auth.rb