zendesk_apps_tools 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  module ZendeskAppsTools
2
2
  autoload :Command, 'zendesk_apps_tools/command'
3
3
  autoload :Translate, 'zendesk_apps_tools/translate'
4
+ autoload :LocaleIdentifier, 'zendesk_apps_tools/locale_identifier'
4
5
  end
5
6
 
@@ -0,0 +1,22 @@
1
+ require 'faraday'
2
+
3
+ module ZendeskAppsTools
4
+ module Common
5
+ def api_request(url, user, password, request = Faraday.new)
6
+ request.basic_auth(user, password)
7
+ request.get(url)
8
+ end
9
+
10
+ def get_value_from_stdin(prompt, valid_regex, error_msg)
11
+ while input = ask(prompt)
12
+ unless input =~ valid_regex
13
+ say(error_msg, :red)
14
+ else
15
+ break
16
+ end
17
+ end
18
+
19
+ return input
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ module ZendeskAppsTools
2
+ class LocaleIdentifier
3
+ attr_reader :language_id
4
+
5
+ # Convert :"en-US-x-12" to 'en'
6
+ def initialize(code)
7
+ @language_id = code.sub(/-.*/, '')
8
+ end
9
+ end
10
+ end
@@ -1,9 +1,15 @@
1
1
  require 'thor'
2
+ require 'json'
3
+ require 'zendesk_apps_tools/common'
4
+ require 'zendesk_apps_tools/locale_identifier'
2
5
 
3
6
  module ZendeskAppsTools
4
7
  class Translate < Thor
5
8
  include Thor::Shell
6
9
  include Thor::Actions
10
+ include ZendeskAppsTools::Common
11
+
12
+ LOCALE_ENDPOINT = "https://support.zendesk.com/api/v2/locales.json"
7
13
 
8
14
  desc 'create', 'Create Zendesk translation file from en.json'
9
15
  def create
@@ -19,22 +25,66 @@ module ZendeskAppsTools
19
25
  write_yaml(app_name, package)
20
26
  end
21
27
 
28
+ desc 'update', 'Update translation files from Zendesk'
29
+ def update(request_builder = Faraday.new)
30
+ app_package = get_value_from_stdin("What is the package name for this app? (without app_)", /^[a-z_]+$/, "Invalid package name, try again:")
31
+ user = get_value_from_stdin("What is your support.zendesk.com username?", /^.+@.+\..+$/, "Invalid email, try again:")
32
+ token = get_value_from_stdin("What is your support.zendesk.com API token?", /^\w*$/, "Invalid API token, try again:")
33
+
34
+ user = "#{user}/token"
35
+ key_prefix = "txt.apps.#{app_package}."
36
+
37
+ say("Fetching translations...")
38
+ locale_response = api_request(LOCALE_ENDPOINT, user, token, request_builder)
39
+
40
+ if locale_response.status == 200
41
+ locales = JSON.parse(locale_response.body)["locales"]
42
+
43
+ locales.each do |locale|
44
+ locale_url = "#{locale["url"]}?include=translations&packages=app_#{app_package}"
45
+ locale_response = api_request(locale_url, user, token, request_builder).body
46
+ translations = JSON.parse(locale_response)['locale']['translations']
47
+
48
+ locale_name = ZendeskAppsTools::LocaleIdentifier.new(locale['locale']).language_id
49
+ write_json(locale_name, nest_translations_hash(translations, key_prefix))
50
+ end
51
+ say("Translations updated", :green)
52
+
53
+ elsif locale_response.status == 401
54
+ say("Authentication failed", :red)
55
+ end
56
+ end
57
+
22
58
  def self.source_root
23
59
  File.expand_path(File.join(File.dirname(__FILE__), "../.."))
24
60
  end
25
61
 
26
62
  no_commands do
27
63
 
28
- def get_value_from_stdin(prompt, valid_regex, error_msg)
29
- while input = ask(prompt)
30
- unless input =~ valid_regex
31
- say(error_msg, :red)
32
- else
33
- break
64
+ def write_json(locale_name, translations_hash)
65
+ create_file("translations/#{locale_name}.json", JSON.pretty_generate(translations_hash))
66
+ end
67
+
68
+ def nest_translations_hash(translations_hash, key_prefix)
69
+ result = {}
70
+
71
+ translations_hash.each do |full_key, value|
72
+ parts = full_key.gsub(key_prefix, '').split('.')
73
+ parts_count = parts.size - 1
74
+ context = result
75
+
76
+ parts.each_with_index do |part, i|
77
+
78
+ if parts_count == i
79
+ context[part] = value
80
+ else
81
+ context = context[part] ||= {}
82
+ end
83
+
34
84
  end
35
85
  end
36
86
 
37
- return input
87
+ result
38
88
  end
39
89
 
40
90
  def write_yaml(app_name, package)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zendesk_apps_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-06-14 00:00:00.000000000 Z
15
+ date: 2013-06-19 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: thor
@@ -62,6 +62,22 @@ dependencies:
62
62
  - - ~>
63
63
  - !ruby/object:Gem::Version
64
64
  version: 1.3.4
65
+ - !ruby/object:Gem::Dependency
66
+ name: faraday
67
+ requirement: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ version: 0.8.7
73
+ type: :runtime
74
+ prerelease: false
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ~>
79
+ - !ruby/object:Gem::Version
80
+ version: 0.8.7
65
81
  - !ruby/object:Gem::Dependency
66
82
  name: zendesk_apps_support
67
83
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +152,8 @@ extra_rdoc_files: []
136
152
  files:
137
153
  - bin/zat
138
154
  - lib/zendesk_apps_tools/command.rb
155
+ - lib/zendesk_apps_tools/common.rb
156
+ - lib/zendesk_apps_tools/locale_identifier.rb
139
157
  - lib/zendesk_apps_tools/server.rb
140
158
  - lib/zendesk_apps_tools/translate.rb
141
159
  - lib/zendesk_apps_tools.rb