Class: I18n::OneSky::ClientBase

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/i18n/one_sky/client_base.rb

Overview

A base class to deal with the OneSky apis

Its subclasses;

SimpleClient
ActiveRecordClient

encapsulate specifics of uploading/downloading for the I18n backends;

I18n::Backend::Simple
I18n::Backend::ActiveRecord

Direct Known Subclasses

ActiveRecordClient, SimpleClient

Constant Summary collapse

SKIP_KEYS_REGEXP =

Remove all Rails i18n keys as of December 2nd 2011.

https://github.com/svenfuchs/rails-i18n/blob/a4fb3d3dbb1a05a2adc82355c934e81eea67e3a1/rails/locale/en-GB.yml
/^(date|time|support|number|datetime|helpers|errors|activerecord)#{Regexp.escape(I18n.default_separator)}/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ClientBase

When you initialize a client inside a Rails project, it will take the OneSky configuration variables supplied when you called rails generate one_sky:init. Outside of Rails, credentials are expected to come from environment variables: ONESKY_API_KEY, ONESKY_API_SECRET, ONESKY_PROJECT. You can override these defaults by providing a hash of options:

  • api_key

  • api_secret

  • project



63
64
65
66
67
68
69
# File 'lib/i18n/one_sky/client_base.rb', line 63

def initialize(options = {})
  verify_options!(options)

  @client   = ::OneSky::Client.new(options[:api_key], options[:api_secret])
  @project  = @client.project(options[:project])
  @platform = @project.platform(options[:platform_id])
end

Instance Attribute Details

#clientObject (readonly)

The base OneSky project. Gives you low-level access to the API gem.



18
19
20
# File 'lib/i18n/one_sky/client_base.rb', line 18

def client
  @client
end

#platformObject (readonly)

The base OneSky project. Gives you low-level access to the API gem.



18
19
20
# File 'lib/i18n/one_sky/client_base.rb', line 18

def platform
  @platform
end

#projectObject (readonly)

The base OneSky project. Gives you low-level access to the API gem.



18
19
20
# File 'lib/i18n/one_sky/client_base.rb', line 18

def project
  @project
end

Class Method Details

.config_exists?(config_path) ⇒ Boolean

check if the path exists

Returns:

  • (Boolean)


53
54
55
# File 'lib/i18n/one_sky/client_base.rb', line 53

def self.config_exists?(config_path)
  File.exist?(config_path)
end

.from_config(config_path) ⇒ Object

Load the client from the one_sky.yml file (installed by ‘rails generate one_sky:init_generator`)



21
22
23
24
25
26
27
# File 'lib/i18n/one_sky/client_base.rb', line 21

def self.from_config(config_path)
  if self.config_exists?(config_path)
    self.new(load_config(config_path))
  else
    self.from_env
  end
end

.from_envObject

Load the client from environment variables.



30
31
32
33
34
35
36
# File 'lib/i18n/one_sky/client_base.rb', line 30

def self.from_env
  self.new(
    :api_key     => ENV["ONESKY_API_KEY"],
    :api_secret  => ENV["ONESKY_API_SECRET"],
    :project     => ENV["ONESKY_PROJECT"],
    :platform_id => ENV["ONESKY_PLATFORM_ID"])
end

.load_config(config_path) ⇒ Object

Load the one_sky.yml file. first parsing it as ERB to enable dynamic config;

api_key:     <%= ENV["ONESKY_API_KEY"] %>
api_secret:  <%= ENV["ONESKY_API_SECRET"] %>
project:     <%= ENV["ONESKY_PROJECT"] %>
platform_id: <%= ENV["ONESKY_PLATFORM_ID"] %>


47
48
49
50
# File 'lib/i18n/one_sky/client_base.rb', line 47

def self.load_config(config_path)
  require 'erb'
  YAML::load(ERB.new(File.read(config_path)).result).symbolize_keys
end

Instance Method Details

#platform_base_localeObject

The default locale for the platform.



78
79
80
# File 'lib/i18n/one_sky/client_base.rb', line 78

def platform_base_locale
  platform_details["base_locale"]
end

#platform_detailsObject

Cached call to load the details for the platform.



96
97
98
# File 'lib/i18n/one_sky/client_base.rb', line 96

def platform_details
  @platform_details ||= @platform.details
end

#platform_locale_codesObject

An array of codes. eg. [“en_US”, “zh_CN”]



83
84
85
86
87
# File 'lib/i18n/one_sky/client_base.rb', line 83

def platform_locale_codes
  platform_locales.map do |hash|
    hash["locale"]
  end
end

#platform_localesObject

Cached call to load the locales for a platform.



90
91
92
# File 'lib/i18n/one_sky/client_base.rb', line 90

def platform_locales
  @platform.locales
end

#verify!Object

Check the configuration of our client.



72
73
74
75
# File 'lib/i18n/one_sky/client_base.rb', line 72

def verify!
  verify_platform!
  verify_default_locale!
end