class EasyMeli::AuthorizationClient

Constants

ACCESS_TOKEN_KEY
AUTH_PATH
AUTH_TOKEN_URL
BASE_AUTH_URLS

Attributes

logger[R]

Public Class Methods

access_token(refresh_token, logger: nil) click to toggle source
# File lib/easy_meli/authorization_client.rb, line 52
def self.access_token(refresh_token, logger: nil)
  response = self.new(logger: logger).access_token_with_response(refresh_token)
  if response.success?
    response.to_h[EasyMeli::AuthorizationClient::ACCESS_TOKEN_KEY]
  else
    exception = EasyMeli::ErrorParser.error_class(response) || EasyMeli::InvalidTokenError

    raise exception.new(response)
  end
end
authorization_url(country_code, redirect_uri) click to toggle source
# File lib/easy_meli/authorization_client.rb, line 34
def self.authorization_url(country_code, redirect_uri)
  params = {
    client_id: EasyMeli.configuration.application_id,
    response_type: 'code',
    redirect_uri: redirect_uri
  }
  HTTParty::Request.new(:get, country_auth_url(country_code), query: params).uri.to_s
end
create_token(code, redirect_uri, logger: nil) click to toggle source
# File lib/easy_meli/authorization_client.rb, line 43
def self.create_token(code, redirect_uri, logger: nil)
  response = self.new(logger: logger).create_token_with_response(code, redirect_uri)
  if response.success?
    response.to_h
  else
    raise EasyMeli::CreateTokenError.new(response)
  end
end
new(logger: nil) click to toggle source
# File lib/easy_meli/authorization_client.rb, line 30
def initialize(logger: nil)
  @logger = logger
end

Private Class Methods

country_auth_url(country_code) click to toggle source
# File lib/easy_meli/authorization_client.rb, line 88
def self.country_auth_url(country_code)
  url = BASE_AUTH_URLS[country_code.to_s.upcase.to_sym] ||
    (raise ArgumentError.new('%s is an invalid country code' % country_code))
  [url, AUTH_PATH].join
end

Public Instance Methods

access_token_with_response(refresh_token) click to toggle source
# File lib/easy_meli/authorization_client.rb, line 72
def access_token_with_response(refresh_token)
  query_params = merge_auth_params(
    grant_type: 'refresh_token',
    refresh_token: refresh_token
  )
  post_auth(query_params)
end
create_token_with_response(code, redirect_uri) click to toggle source
# File lib/easy_meli/authorization_client.rb, line 63
def create_token_with_response(code, redirect_uri)
  query_params = merge_auth_params(
    grant_type: 'authorization_code',
    code: code,
    redirect_uri: redirect_uri
  )
  post_auth(query_params)
end

Private Instance Methods

merge_auth_params(options = {}) click to toggle source
# File lib/easy_meli/authorization_client.rb, line 94
def merge_auth_params(options = {})
  options.merge(
    client_id: EasyMeli.configuration.application_id,
    client_secret: EasyMeli.configuration.secret_key
  )
end
post_auth(params) click to toggle source
# File lib/easy_meli/authorization_client.rb, line 82
def post_auth(params)
  self.class.post(AUTH_TOKEN_URL, query: params).tap do |response|
    logger&.log response
  end
end