class MT::DataAPI::Client::EndpointManager

Retrieve and find endpoints

Constants

DEFAULT_API_VERSION
LIST_ENDPOINTS_HASH

Attributes

endpoints[RW]

Public Class Methods

new(opts) click to toggle source
# File lib/mt/data_api/client/endpoint_manager.rb, line 20
def initialize(opts)
  raise parameter_should_be_hash unless opts.is_a? Hash
  initialize_parameters(opts.symbolize_keys)
  raise invalid_parameter unless @base_url && @client_id
  Endpoint.api_url = api_url
  Endpoint.client_id = @client_id
end

Public Instance Methods

find_endpoint(id) click to toggle source
# File lib/mt/data_api/client/endpoint_manager.rb, line 28
def find_endpoint(id)
  hash = find_endpoint_hash(id)
  hash ? Endpoint.new(hash) : nil
end

Private Instance Methods

api_url() click to toggle source
# File lib/mt/data_api/client/endpoint_manager.rb, line 58
def api_url
  url = @base_url
  url += '/' unless url[-1] == '/'
  url + 'v' + @api_version.to_s
end
find_endpoint_hash(id) click to toggle source
# File lib/mt/data_api/client/endpoint_manager.rb, line 50
def find_endpoint_hash(id)
  @endpoints ||= retrieve_endpoints
  endpoints = @endpoints.select do |ep|
    ep['id'].to_s == id.to_s && @api_version.to_i >= ep['version'].to_i
  end
  endpoints.first
end
initialize_parameters(opts) click to toggle source
# File lib/mt/data_api/client/endpoint_manager.rb, line 35
def initialize_parameters(opts)
  @base_url = opts[:base_url]
  @client_id = opts[:client_id]
  @api_version = opts[:api_version] || DEFAULT_API_VERSION
  @endpoints = opts[:endpoints] if opts.key? :endpoints
end
invalid_parameter() click to toggle source
# File lib/mt/data_api/client/endpoint_manager.rb, line 46
def invalid_parameter
  ArgumentError.new 'parameter "base_url" and "client_id" are required'
end
parameter_should_be_hash() click to toggle source
# File lib/mt/data_api/client/endpoint_manager.rb, line 42
def parameter_should_be_hash
  ArgumentError.new 'parameter should be hash'
end
retrieve_endpoints() click to toggle source
# File lib/mt/data_api/client/endpoint_manager.rb, line 64
def retrieve_endpoints
  response = Endpoint.new(LIST_ENDPOINTS_HASH).call
  raise response['error'] if response.key? 'error'
  endpoints = response['items']
  endpoints.sort { |a, b| b['version'].to_i <=> a['version'].to_i }
end