Sha256: 55a07bf2f0db44cb5cea27fee8d76ba044574c364c5d068d588c244b89d1f23d

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module WebTranslateIt
  class Configuration
    require 'yaml'
    require 'fileutils'
    attr_accessor :api_key, :files, :ignore_locales, :logger
    
    def initialize
      file = File.join(RAILS_ROOT, 'config', 'translation.yml')
      configuration       = YAML.load_file(file)
      self.logger         = logger
      self.api_key        = configuration['api_key']
      self.files          = []
      self.ignore_locales = configuration['ignore_locales'].to_a.map{ |locale| locale.to_s }
      configuration['files'].each do |file_id, file_path|
        self.files.push(TranslationFile.new(file_id, file_path, api_key))
      end
    end
    
    def locales
      WebTranslateIt::Util.http_connection do |http|
        request  = Net::HTTP::Get.new(api_url)
        response = http.request(request)
        if response.code.to_i >= 400 and response.code.to_i < 500
          puts "----------------------------------------------------------------------"
          puts "You API key seems to be misconfigured. It is currently “self.api_key”."
          puts "Change it in RAILS_ROOT/configuration/translation.yml."
        else
          response.body.split
        end
      end
    end
    
    def api_url
      "/api/projects/#{api_key}/locales"
    end
    
    def logger
      if defined?(Rails.logger)
        Rails.logger
      elsif defined?(RAILS_DEFAULT_LOGGER)
        RAILS_DEFAULT_LOGGER
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
web_translate_it-1.4.4 lib/web_translate_it/configuration.rb