Class: EasyMeli::AuthorizationClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/easy_meli/authorization_client.rb

Constant Summary collapse

AUTH_TOKEN_URL =
'https://api.mercadolibre.com/oauth/token'
AUTH_PATH =
'/authorization'
BASE_AUTH_URLS =
{
  AR: 'https://auth.mercadolibre.com.ar',
  BR: 'https://auth.mercadolivre.com.br',
  CO: 'https://auth.mercadolibre.com.co',
  CR: 'https://auth.mercadolibre.com.cr',
  EC: 'https://auth.mercadolibre.com.ec',
  CL: 'https://auth.mercadolibre.cl',
  MX: 'https://auth.mercadolibre.com.mx',
  UY: 'https://auth.mercadolibre.com.uy',
  VE: 'https://auth.mercadolibre.com.ve',
  PA: 'https://auth.mercadolibre.com.pa',
  PE: 'https://auth.mercadolibre.com.pe',
  PT: 'https://auth.mercadolibre.com.pt',
  DO: 'https://auth.mercadolibre.com.do'
}
ACCESS_TOKEN_KEY =
'access_token'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ AuthorizationClient



30
31
32
# File 'lib/easy_meli/authorization_client.rb', line 30

def initialize(logger: nil)
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



28
29
30
# File 'lib/easy_meli/authorization_client.rb', line 28

def logger
  @logger
end

Class Method Details

.access_token(refresh_token, logger: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



34
35
36
37
38
39
40
41
# 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

.country_auth_url(country_code) ⇒ Object



88
89
90
91
92
# 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

.create_token(code, redirect_uri, logger: nil) ⇒ Object



43
44
45
46
47
48
49
50
# 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

Instance Method Details

#access_token_with_response(refresh_token) ⇒ Object



72
73
74
75
76
77
78
# 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) ⇒ Object



63
64
65
66
67
68
69
70
# 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