class Bisu::Source::OneSky

Public Class Methods

new(api_key, api_secret, project_id, file_name) click to toggle source
# File lib/bisu/source/one_sky.rb, line 7
def initialize(api_key, api_secret, project_id, file_name)
  @api_key    = api_key
  @api_secret = api_secret
  @project_id = project_id
  @file_name  = file_name
end

Public Instance Methods

to_i18() click to toggle source
# File lib/bisu/source/one_sky.rb, line 14
def to_i18
  Logger.info("Downloading dictionary from OneSky...")

  path = "https://platform.api.onesky.io/1/projects/#{@project_id}/translations/multilingual"
  file = get(path, source_file_name: @file_name, file_format: "I18NEXT_MULTILINGUAL_JSON")

  hash = JSON.parse(file)

  hash.each do |lang, v|
    hash[lang] = v["translation"]
    hash[lang].each do |key, text|
      hash[lang][key] = hash[lang][key].join("\\n") if hash[lang][key].is_a? Array
      hash[lang][key] = hash[lang][key].gsub("\n", "\\n") # fixes the 'stupid newline bug'
      hash[lang][key] = hash[lang][key].gsub("\\'", "'") # fixes the 'stupid single quote bug'
    end
  end

  Logger.info("OneSky response parsed successfully!")
  Logger.info("Found #{hash.count} languages.")

  hash
end

Private Instance Methods

authenticated_params(params) click to toggle source
# File lib/bisu/source/one_sky.rb, line 55
def authenticated_params(params)
  now = Time.now.to_i

  { api_key: @api_key,
    timestamp: now,
    dev_hash: Digest::MD5.hexdigest(now.to_s + @api_secret)
  }.merge(params)
end
get(url, params) click to toggle source
# File lib/bisu/source/one_sky.rb, line 39
def get(url, params)
  uri = URI(url)
  uri.query = URI.encode_www_form(authenticated_params(params))

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)

  raise "Bisu::Source::OneSky: Http Error #{JSON.parse(response.body)}" if response.code.to_i >= 400

  response.body
end