module Translatomatic::Provider

Provides methods to access and create instances of interfaces to translation APIs.

Public Class Methods

available(options = {}) click to toggle source

Find all configured providers @param options [Hash<String,String>] Provider options @return [Array<#translate>] A list of provider instances

# File lib/translatomatic/provider.rb, line 51
def available(options = {})
  available = types.collect { |klass| create_provider(klass, options) }
  available.compact
end
find(name) click to toggle source

@return [Class] The provider class corresponding to the given name

# File lib/translatomatic/provider.rb, line 9
def find(name)
  load_providers
  name && !name.empty? ? const_get(name) : nil
end
get_error(name) click to toggle source

Get errors for the specified provider @param name [String] Provider name

# File lib/translatomatic/provider.rb, line 58
def get_error(name)
  @provider_errors[name]
end
names() click to toggle source

@return [List<String>] A list of all providers

# File lib/translatomatic/provider.rb, line 44
def names
  types.collect { |i| i.name.demodulize }
end
resolve(list, options = {}) click to toggle source

Resolve the given list of provider names to a list of providers. If the list is empty, return all providers that are configured. @param list [Array<String>] Provider names or providers @param options [Hash<String,String>] Provider options @return [Array<Translatomatic::Provider::Base>] Providers

# File lib/translatomatic/provider.rb, line 19
def resolve(list, options = {})
  list = [list].flatten.compact.collect do |provider|
    if provider.respond_to?(:translate)
      provider
    else
      klass = find(provider)
      provider = create_provider(klass, options)
    end
    provider
  end

  # if we didn't resolve to any providers, find all available
  # providers that work with the given options.
  list.empty? ? available(options) : list
end
types() click to toggle source

@return [List<Class>] A list of all provider classes

# File lib/translatomatic/provider.rb, line 36
def types
  load_providers
  constants.collect { |c| const_get(c) }.select do |klass|
    klass.is_a?(Class) && klass != Translatomatic::Provider::Base
  end
end

Private Class Methods

create_provider(klass, options = {}) click to toggle source
# File lib/translatomatic/provider.rb, line 68
def create_provider(klass, options = {})
  return nil unless klass
  klass.new(options)
rescue StandardError => e
  name = klass.name.demodulize
  log.debug(t('provider.unavailable', name: name))
  provider_error(name, e)
  nil
end
load_providers() click to toggle source
# File lib/translatomatic/provider.rb, line 87
def load_providers
  return if loaded_providers?
  Dir[File.join(__dir__, 'provider/*.rb')].sort.each do |file|
    begin
      require file
    rescue StandardError => e
      name = File.basename(file, '.rb').classify
      provider_error(name, e)
    end
  end
  @loaded_providers = true
end
loaded_providers?() click to toggle source
# File lib/translatomatic/provider.rb, line 78
def loaded_providers?
  @loaded_providers
end
provider_error(name, e) click to toggle source
# File lib/translatomatic/provider.rb, line 82
def provider_error(name, e)
  @provider_errors ||= {}
  @provider_errors[name] = e
end
test_provider(provider) click to toggle source
# File lib/translatomatic/provider.rb, line 64
def test_provider(provider)
  provider.languages
end