Module: TranslationService

Defined in:
lib/translation_service.rb,
lib/translation_service/version.rb,
lib/translation_service/configuration.rb

Defined Under Namespace

Classes: Configuration, Error

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.add(key, text, url) ⇒ Object

Returns the value of attribute add.



14
15
16
# File 'lib/translation_service.rb', line 14

def add
  @add
end

.configObject



22
23
24
# File 'lib/translation_service.rb', line 22

def self.config
  @config = Configuration.new
end

.update_allObject

Keep for legacy



31
32
33
# File 'lib/translation_service.rb', line 31

def update_all
  @update_all
end

Class Method Details

.add_addressObject



92
93
94
# File 'lib/translation_service.rb', line 92

def self.add_address
  URI(@config.address + @config.key + '/add-string.json')
end

.configure {|config| ... } ⇒ Object

Yields:



26
27
28
# File 'lib/translation_service.rb', line 26

def self.configure
  yield(config)
end

.create_file(data, lang) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/translation_service.rb', line 106

def self.create_file(data, lang)
  dir = @config.location
  file = File.join(Dir.pwd, dir, lang + '.yml')
  #file = File.join(Rails.root, dir, lang + '.yml')
  File.open(file, 'w') do |f|
    f.write data.to_yaml
  end
end

.generate_token(address) ⇒ Object



100
101
102
103
104
# File 'lib/translation_service.rb', line 100

def self.generate_token(address)
  sha1 = Digest::SHA1.new
  digest = timestamp.to_s + address.to_s + @config.password
  sha1.hexdigest digest
end

.localesObject



115
116
117
118
# File 'lib/translation_service.rb', line 115

def self.locales
  locales = @config.available_locales
  locales.map!{ |l| l.to_s == 'vi' ? 'vi-VN' : l.to_s }
end

.pull(language) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/translation_service.rb', line 65

def self.pull(language)
  req = Net::HTTP::Post.new(pull_address.path)
  req.set_form_data(
    timestamp: timestamp,
    security_token: generate_token(pull_address),
    master_revision: @config.master_revision,
    locales: language
  )

  send_request(pull_address, req)
end

.pull_addressObject



88
89
90
# File 'lib/translation_service.rb', line 88

def self.pull_address
  URI(@config.address + @config.key + '/translations.json')
end

.pull_allObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/translation_service.rb', line 35

def self.pull_all
  locales.each do |lang|
    puts "\e[42m" + 'downloading ' + lang + "\e[0m"
    data = unsparse(pull(lang)[lang]['all'])

    # Convert vi-VN to vi since they rather not add it
    lang = 'vi' if lang == 'vi-VN'

    new_data = {}
    new_data[lang] = data
    create_file(new_data, lang)
  end
end

.send_request(address, req) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/translation_service.rb', line 77

def self.send_request(address, req)
  http = Net::HTTP.new(address.host, address.port)
  http.use_ssl = true

  begin
    JSON.parse(http.request(req).body)
  rescue => e
    Logger.new(STDERR).error("Localize failed: #{e}")
  end
end

.timestampObject



96
97
98
# File 'lib/translation_service.rb', line 96

def self.timestamp
  @timestamp ||= Time.now.to_i
end

.unsparse(hsh) ⇒ Object

Taken from Unsparsify gem. Error response now includes broken key.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/translation_service.rb', line 121

def self.unsparse(hsh)
  hsh.each_with_object({}) do |(k, v), memo|
    current = memo
    key = k.split('.')
    up_next = partial = key.shift
    until key.size.zero?
      up_next = key.shift
      up_next = up_next.to_i if up_next =~ /\A[0-9]+\Z/
      current = (current[partial] ||= (up_next.is_a?(Integer) ? [] : {}))
      case up_next
      when Integer then raise 'Problem with key ' + k.to_s unless current.is_a? Array
      else              raise 'Problem with key ' + k.to_s unless current.is_a? Hash
      end
      partial = up_next
    end
    current[up_next] = v
  end
end

Instance Method Details

#initialize(config = Config.new) ⇒ Object



18
19
20
# File 'lib/translation_service.rb', line 18

def initialize(config = Config.new)
  @config = config
end