class PortaText::Client::BaseClient

A generic PortaText client.

Author

Marcelo Gornstein (marcelog@portatext.com)

Copyright

Copyright © 2015 PortaText

License

Apache-2.0

rubocop:disable Metrics/ClassLength

Attributes

api_key[W]
credentials[W]
endpoint[W]
executor[W]
logger[W]

Public Class Methods

new() click to toggle source

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize rubocop:enable Metrics/ParameterLists

# File lib/portatext/client/base_client.rb, line 71
def initialize
  @logger = Logger.new nil
  @endpoint = DEFAULT_ENDPOINT
  @api_key = nil
  @credentials = nil
  @session_token = nil
  @executor = self
end

Public Instance Methods

command_class_name(method) click to toggle source
# File lib/portatext/client/base_client.rb, line 31
def command_class_name(method)
  method = method.to_s.split('_').map(&:capitalize)
  Object.const_get('PortaText')
        .const_get('Command')
        .const_get('Api')
        .const_get(method.join(''))
end
method_missing(method, *_arguments, &_block) click to toggle source
Calls superclass method
# File lib/portatext/client/base_client.rb, line 23
def method_missing(method, *_arguments, &_block)
  class_name = command_class_name(method)
  super unless defined?(class_name)
  command = class_name.new
  command.client = self
  command
end
respond_to_missing?(method, *_arguments, &_block) click to toggle source
Calls superclass method
# File lib/portatext/client/base_client.rb, line 19
def respond_to_missing?(method, *_arguments, &_block)
  defined?(command_class_name method) || super
end
run( endpoint, method, content_type, accept_content_type, body, output_file = nil, auth = nil ) click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize rubocop:disable Metrics/ParameterLists

# File lib/portatext/client/base_client.rb, line 42
def run(
  endpoint, method, content_type, accept_content_type,
  body, output_file = nil, auth = nil
)
  true_endpoint = "#{@endpoint}/#{endpoint}"
  auth ||= auth_method(auth)
  headers = form_headers content_type, accept_content_type, auth
  @logger.debug "Calling #{method} #{true_endpoint} with #{auth}"
  descriptor = PortaText::Command::Descriptor.new(
    true_endpoint, method, headers, body, output_file
  )
  ret_code, ret_headers, ret_body = @executor.execute descriptor
  @logger.debug "Got: #{ret_code} / #{ret_headers} / #{ret_body}"
  ret_body = '{}' if ret_body.nil?
  ret_body = JSON.parse ret_body
  result = PortaText::Command::Result.new ret_code, ret_headers, ret_body
  if ret_code.eql?(401) && auth.eql?(:session_token)
    login!
    result = run(
      endpoint, method, content_type, accept_content_type,
      body, output_file, auth
    )
  end
  assert_result descriptor, result
end

Private Instance Methods

assert_result(descriptor, result) click to toggle source
# File lib/portatext/client/base_client.rb, line 90
def assert_result(descriptor, result)
  error = error_for result.code
  return result if error.nil?
  raise error, [descriptor, result]
end
auth_method(auth_suggested) click to toggle source
# File lib/portatext/client/base_client.rb, line 96
def auth_method(auth_suggested)
  return auth_suggested unless auth_suggested.nil?
  return :session_token unless @session_token.nil?
  return :api_key unless @api_key.nil?
  login!
  :session_token
end
error_for(code) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/portatext/client/base_client.rb, line 105
def error_for(code)
  errors = {
    400 => PortaText::Exception::ClientError,
    401 => PortaText::Exception::InvalidCredentials,
    402 => PortaText::Exception::PaymentRequired,
    403 => PortaText::Exception::Forbidden,
    404 => PortaText::Exception::NotFound,
    405 => PortaText::Exception::InvalidMethod,
    406 => PortaText::Exception::NotAcceptable,
    415 => PortaText::Exception::InvalidMedia,
    429 => PortaText::Exception::RateLimited,
    500 => PortaText::Exception::ServerError
  }
  errors[code]
end
form_headers(content_type, accept_content_type, auth) click to toggle source
# File lib/portatext/client/base_client.rb, line 121
def form_headers(content_type, accept_content_type, auth)
  headers = {
    'Content-Type' => content_type,
    'Accept' => accept_content_type
  }
  case auth
  when :session_token
    headers['X-Session-Token'] = @session_token
  when :basic
    auth_string = Base64.encode64 "#{@credentials[0]}:#{@credentials[1]}"
    headers['Authorization'] = "Basic #{auth_string}"
  when :api_key
    headers['X-Api-Key'] = @api_key
  else
    raise "Invalid auth type: #{auth}"
  end
  headers
end
login!() click to toggle source
# File lib/portatext/client/base_client.rb, line 82
def login!
  result = run(
    'login', :post, 'application/json', 'application/json',
    '', nil, :basic
  )
  @session_token = result.data['token']
end