aigu 0.3.1 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
1
+ module Aigu
2
+ class Mergeer
3
+ def initialize(opts = {})
4
+ @output_file = opts[:'output-file']
5
+ @input_directory = opts[:'input-directory']
6
+ end
7
+
8
+ def process!
9
+ puts "Generating flat Accent JSON file `#{@output_file}` merging json files in `#{@input_directory}` directory"
10
+
11
+ puts '---'
12
+
13
+ build_output
14
+ write_json_file
15
+
16
+ puts '---'
17
+ puts 'Done'
18
+ end
19
+
20
+ protected
21
+
22
+ def build_output
23
+ @output = {}
24
+
25
+ pattern = File.join(@input_directory, '*.json')
26
+ Dir[pattern].each do |filepath|
27
+ puts "Processing #{filepath}"
28
+
29
+ file_name = filepath.rpartition(File::SEPARATOR).last.split('.', 2).first
30
+ prefix = file_name == 'default' ? '' : "@#{file_name.upcase}@__"
31
+
32
+ json = JSON.parse(File.read(filepath))
33
+ json.each_pair do |key, value|
34
+ @output[prefix + key] = value
35
+ end
36
+ end
37
+
38
+ @output
39
+ end
40
+
41
+ def write_json_file
42
+ file_path = @output_file
43
+ puts "Generating #{file_path}"
44
+ FileUtils.mkdir_p(File.dirname(file_path))
45
+
46
+ File.open(file_path, 'w+') do |file|
47
+ file << JSON.pretty_generate(JSON.parse(@output.to_json))
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,44 @@
1
+ module Aigu
2
+ class Puller
3
+ PULL_PATH = '/public_api/latest_revision?language=%s&render_format=json&render_filename=aigu.json'
4
+
5
+ def initialize(opts = {})
6
+ @output_file = opts[:'output-file']
7
+ @accent_api_key = opts[:'accent-api-key']
8
+ @accent_url = opts[:'accent-url']
9
+ @locale = opts[:'locale']
10
+ end
11
+
12
+ def process!
13
+ puts "Pulling JSON file `#{@output_file}` from Accent"
14
+
15
+ puts '---'
16
+
17
+ pull
18
+
19
+ puts '---'
20
+ puts 'Done'
21
+ end
22
+
23
+ protected
24
+
25
+ def pull
26
+ uri = URI(@accent_url + format(PULL_PATH, @locale))
27
+
28
+ http = Net::HTTP.new(uri.host, uri.port)
29
+ uri.scheme == 'https' && http.use_ssl = true
30
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
31
+ request = Net::HTTP::Get.new(uri)
32
+ request.add_field('Authorization', @accent_api_key)
33
+
34
+ response = http.request(request)
35
+ puts 'Response code: ' + response.code
36
+
37
+ puts "Generating #{@output_file}"
38
+
39
+ File.open(@output_file, 'w+') do |file|
40
+ file << response.body
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,44 @@
1
+ module Aigu
2
+ class Pusher
3
+ PUSH_PATH = '/public_api/revisions'
4
+
5
+ def initialize(opts = {})
6
+ @input_file = opts[:'input-file']
7
+ @accent_api_key = opts[:'accent-api-key']
8
+ @accent_url = opts[:'accent-url']
9
+ end
10
+
11
+ def process!
12
+ puts "Sending JSON file `#{@input_file}` to Accent"
13
+
14
+ puts '---'
15
+
16
+ send
17
+
18
+ puts '---'
19
+ puts 'Done'
20
+ end
21
+
22
+ protected
23
+
24
+ def send
25
+ payload = File.read(@input_file)
26
+ file_name = @input_file.rpartition(File::SEPARATOR).last
27
+
28
+ uri = URI(@accent_url + PUSH_PATH)
29
+
30
+ http = Net::HTTP.new(uri.host, uri.port)
31
+ uri.scheme == 'https' && http.use_ssl = true
32
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
33
+ request = Net::HTTP::Post.new(uri)
34
+ request.add_field('Authorization', @accent_api_key)
35
+ request.add_field('Content-Type', 'application/json')
36
+
37
+ request.body = JSON.pretty_generate(JSON.parse({ revision: { file: payload, filename: file_name } }.to_json))
38
+
39
+ response = http.request(request)
40
+
41
+ puts 'Response: ' + response.code
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,55 @@
1
+ module Aigu
2
+ class Unmergeer
3
+ UNMERGE_REGEX = /^@(?<file_name>\w+)@__(?<key>.+)$/
4
+
5
+ def initialize(opts = {})
6
+ @output_directory = opts[:'output-directory']
7
+ @input_file = opts[:'input-file']
8
+ end
9
+
10
+ def process!
11
+ puts "Unmerging Accent json file `#{@input_file}` to `#{@output_directory}` directory"
12
+
13
+ puts '---'
14
+
15
+ parse_json
16
+ write_json_files
17
+
18
+ puts '---'
19
+ puts 'Done'
20
+ end
21
+
22
+ protected
23
+
24
+ def parse_json
25
+ @object = {}
26
+
27
+ json = JSON.parse(File.read(@input_file))
28
+
29
+ json.each_pair do |key, value|
30
+ match_data = key.match(UNMERGE_REGEX)
31
+ if match_data
32
+ @object[match_data[:file_name]] ||= {}
33
+ @object[match_data[:file_name]][match_data[:key]] = value
34
+ else
35
+ @object['DEFAULT'] ||= {}
36
+ @object['DEFAULT'][key] = value
37
+ end
38
+ end
39
+
40
+ @object
41
+ end
42
+
43
+ def write_json_files
44
+ @object.each_pair do |file_name, json|
45
+ file_path = File.join(@output_directory, "#{file_name.downcase}.json")
46
+ puts "Generating #{file_path}"
47
+ FileUtils.mkdir_p(File.dirname(file_path))
48
+
49
+ File.open(file_path, 'w+') do |file|
50
+ file << JSON.pretty_generate(JSON.parse(json.to_json))
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,3 +1,3 @@
1
1
  module Aigu
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4'
3
3
  end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Aigu::AndroidExporter do
4
+ describe :parse_xml do
5
+ let(:exporter) { Aigu::AndroidExporter.new }
6
+ let(:parse_xml) { exporter.send(:parse_xml, content) }
7
+
8
+ let(:content) do
9
+ <<-EOS.unindent
10
+ <?xml version="1.0" encoding="utf-8"?>
11
+ <resources>
12
+ <string name="string1">value1</string>
13
+ <string name="string2">value2</string>
14
+ <string name="string3">value3 with special characters: &amp; &lt; &gt; &apos; &quot;</string>
15
+
16
+ <string-array name="pairing_step_three_custom_nickname_array">
17
+ <item>Custom</item>
18
+ <item>Living Room</item>
19
+ <item>Kitchen</item>
20
+ <item>Bedroom</item>
21
+ </string-array>
22
+ </resources>
23
+ EOS
24
+ end
25
+
26
+ let(:expected_content) do
27
+ {
28
+ 'string1' => 'value1',
29
+ 'string2' => 'value2',
30
+ 'string3' => 'value3 with special characters: & < > \' "',
31
+ 'pairing_step_three_custom_nickname_array__ARRAY_ITEM__#0' => 'Custom',
32
+ 'pairing_step_three_custom_nickname_array__ARRAY_ITEM__#1' => 'Living Room',
33
+ 'pairing_step_three_custom_nickname_array__ARRAY_ITEM__#2' => 'Kitchen',
34
+ 'pairing_step_three_custom_nickname_array__ARRAY_ITEM__#3' => 'Bedroom'
35
+ }
36
+ end
37
+
38
+ it { expect(parse_xml).to eql expected_content }
39
+ end
40
+ end
@@ -0,0 +1,131 @@
1
+ require 'spec_helper'
2
+
3
+ describe Aigu::AndroidImporter do
4
+ describe :expand_content_values do
5
+ let(:importer) { Aigu::AndroidImporter.new }
6
+ let(:expanded_content) { importer.send(:expand_content_values, content) }
7
+
8
+ let(:content) do
9
+ {
10
+ 'string1' => 'value1',
11
+ 'string2' => 'value2',
12
+ 'string3' => 'value3 with special characters: & < > \' "',
13
+ 'pairing_step_three_custom_nickname_array__ARRAY_ITEM__#0' => 'Custom',
14
+ 'pairing_step_three_custom_nickname_array__ARRAY_ITEM__#1' => 'Living Room',
15
+ 'pairing_step_three_custom_nickname_array__ARRAY_ITEM__#2' => 'Kitchen',
16
+ 'pairing_step_three_custom_nickname_array__ARRAY_ITEM__#3' => 'Bedroom'
17
+ }
18
+ end
19
+
20
+ let(:expected_content) do
21
+ {
22
+ 'string1' => 'value1',
23
+ 'string2' => 'value2',
24
+ 'string3' => 'value3 with special characters: & < > \' "',
25
+ 'pairing_step_three_custom_nickname_array' => [
26
+ 'Custom',
27
+ 'Living Room',
28
+ 'Kitchen',
29
+ 'Bedroom'
30
+ ]
31
+ }
32
+ end
33
+
34
+ it { expect(expanded_content).to eql expected_content }
35
+ end
36
+
37
+ describe :split_res_types do
38
+ let(:importer) { Aigu::AndroidImporter.new }
39
+ let(:splited_res_types) { importer.send(:split_res_types, content) }
40
+
41
+ let(:content) do
42
+ {
43
+ 'string1' => 'value1',
44
+ 'string2' => 'value2',
45
+ 'string3' => 'value3 with special characters: & < > \' "',
46
+ 'pairing_step_three_custom_nickname_array' => [
47
+ 'Custom',
48
+ 'Living Room',
49
+ 'Kitchen',
50
+ 'Bedroom'
51
+ ],
52
+ 'string1_t1__@TYPE_type1' => 'value1',
53
+ 'string2_t1__@TYPE_type1' => 'value2',
54
+ 'string3_t1__@TYPE_type1' => 'value3 with special characters: & < > \' "',
55
+ 'pairing_step_three_custom_nickname_array_t1__@TYPE_type1' => [
56
+ 'Custom',
57
+ 'Living Room',
58
+ 'Kitchen',
59
+ 'Bedroom'
60
+ ]
61
+ }
62
+ end
63
+
64
+ let(:expected_content) do
65
+ {
66
+ '' => {
67
+ 'string1' => 'value1',
68
+ 'string2' => 'value2',
69
+ 'string3' => 'value3 with special characters: & < > \' "',
70
+ 'pairing_step_three_custom_nickname_array' => [
71
+ 'Custom',
72
+ 'Living Room',
73
+ 'Kitchen',
74
+ 'Bedroom'
75
+ ]
76
+ },
77
+ 'type1' => {
78
+ 'string1_t1' => 'value1',
79
+ 'string2_t1' => 'value2',
80
+ 'string3_t1' => 'value3 with special characters: & < > \' "',
81
+ 'pairing_step_three_custom_nickname_array_t1' => [
82
+ 'Custom',
83
+ 'Living Room',
84
+ 'Kitchen',
85
+ 'Bedroom'
86
+ ]
87
+ }
88
+ }
89
+ end
90
+
91
+ it { expect(splited_res_types).to eql expected_content }
92
+ end
93
+
94
+ describe :res_to_xml do
95
+ let(:importer) { Aigu::AndroidImporter.new }
96
+ let(:res_xml) { importer.send(:res_to_xml, content) }
97
+
98
+ let(:content) do
99
+ {
100
+ 'string1' => 'value1',
101
+ 'string2' => 'value2',
102
+ 'string3' => 'value3 with special characters: & < > \' "',
103
+ 'pairing_step_three_custom_nickname_array' => [
104
+ 'Custom',
105
+ 'Living Room',
106
+ 'Kitchen',
107
+ 'Bedroom'
108
+ ]
109
+ }
110
+ end
111
+
112
+ let(:expected_content) do
113
+ <<-EOS.unindent
114
+ <?xml version="1.0" encoding="utf-8"?>
115
+ <resources>
116
+ <string name="string1">value1</string>
117
+ <string name="string2">value2</string>
118
+ <string name="string3">value3 with special characters: &amp; &lt; &gt; \' "</string>
119
+ <string-array name="pairing_step_three_custom_nickname_array">
120
+ <item>Custom</item>
121
+ <item>Living Room</item>
122
+ <item>Kitchen</item>
123
+ <item>Bedroom</item>
124
+ </string-array>
125
+ </resources>
126
+ EOS
127
+ end
128
+
129
+ it { expect(res_xml).to eql expected_content }
130
+ end
131
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Aigu::CoreExporter do
4
+ describe :parse_java_file do
5
+ let(:exporter) { Aigu::CoreExporter.new }
6
+ let(:parse_java_file_en) { exporter.send(:parse_java_file, content) }
7
+ let(:parse_java_file_fr) { exporter.send(:parse_java_file, content, 'fr') }
8
+
9
+ let(:content) do
10
+ <<-EOS.unindent
11
+ package ca.bell.fiberemote.core;
12
+
13
+ public enum CoreLocalizedStrings {
14
+
15
+ /* !!LOCALIZED STRINGS!! --> */
16
+ STRING_1("Value of string #1 en", "Value of string #1 fr"),
17
+ STRING_2 ("Value of string #2 en", "Value of string #2 fr"),
18
+ STRING_3("Value with quotes \\" and \\nlinefeeds en", "Value with quotes \\" and \\nlinefeeds fr"),
19
+ STRING_4("Value with parentheses () en", "Value with parentheses () fr")
20
+ /* <-- !!LOCALIZED STRINGS!! */
21
+ ;
22
+
23
+ private String textEn;
24
+
25
+ CoreLocalizedStrings(String textEn) {
26
+ this.textEn = textEn;
27
+ }
28
+ }
29
+ EOS
30
+ end
31
+
32
+ let(:expected_content_en) do
33
+ {
34
+ 'STRING_1' => 'Value of string #1 en',
35
+ 'STRING_2' => 'Value of string #2 en',
36
+ 'STRING_3' => 'Value with quotes \" and \nlinefeeds en',
37
+ 'STRING_4' => 'Value with parentheses () en'
38
+ }
39
+ end
40
+
41
+ let(:expected_content_fr) do
42
+ {
43
+ 'STRING_1' => 'Value of string #1 fr',
44
+ 'STRING_2' => 'Value of string #2 fr',
45
+ 'STRING_3' => 'Value with quotes \" and \nlinefeeds fr',
46
+ 'STRING_4' => 'Value with parentheses () fr'
47
+ }
48
+ end
49
+
50
+ it { expect(parse_java_file_en).to eql expected_content_en }
51
+ it { expect(parse_java_file_fr).to eql expected_content_fr }
52
+ end
53
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ describe Aigu::CoreImporter do
4
+ describe :parse_java_file do
5
+ let(:importer) { Aigu::CoreImporter.new }
6
+ let(:update_in_memory_en) { importer.send(:update_in_memory, java_file_content, content_en, 'en') }
7
+ let(:update_in_memory_fr) { importer.send(:update_in_memory, java_file_content, content_fr, 'fr') }
8
+
9
+ let(:java_file_content) do
10
+ <<-EOS.unindent
11
+ package ca.bell.fiberemote.core;
12
+
13
+ public enum CoreLocalizedStrings {
14
+
15
+ /* !!LOCALIZED STRINGS!! --> */
16
+ STRING_1("Value of string #1 en", "Value of string #1 fr"),
17
+ STRING_2 ("Value of string #2 en","Value of string #2 fr"),
18
+ /* Comment */
19
+ STRING_3("Value with quotes \\" and \\nlinefeeds en", "Value with quotes \\" and \\nlinefeeds fr"),
20
+ STRING_4("Value with parentheses () en", "Value with parentheses () fr")
21
+ /* <-- !!LOCALIZED STRINGS!! */
22
+ ;
23
+
24
+ private String textEn;
25
+
26
+ CoreLocalizedStrings(String textEn) {
27
+ this.textEn = textEn;
28
+ }
29
+ }
30
+ EOS
31
+ end
32
+
33
+ let(:content_en) do
34
+ {
35
+ 'STRING_1' => 'New value of string #1 en',
36
+ 'STRING_2' => 'New value of string #2 en',
37
+ 'STRING_3' => 'New value with quotes \" and \nlinefeeds en',
38
+ 'STRING_4' => 'New value with parentheses () en'
39
+ }
40
+ end
41
+
42
+ let(:content_fr) do
43
+ {
44
+ 'STRING_1' => 'New value of string #1 fr',
45
+ 'STRING_2' => 'New value of string #2 fr',
46
+ 'STRING_3' => 'New value with quotes \" and \nlinefeeds fr',
47
+ 'STRING_4' => 'New value with parentheses () fr'
48
+ }
49
+ end
50
+
51
+ let(:expected_content_en) do
52
+ <<-EOS.unindent
53
+ package ca.bell.fiberemote.core;
54
+
55
+ public enum CoreLocalizedStrings {
56
+
57
+ /* !!LOCALIZED STRINGS!! --> */
58
+ STRING_1("New value of string #1 en", "Value of string #1 fr"),
59
+ STRING_2("New value of string #2 en", "Value of string #2 fr"),
60
+ /* Comment */
61
+ STRING_3("New value with quotes \\" and \\nlinefeeds en", "Value with quotes \\" and \\nlinefeeds fr"),
62
+ STRING_4("New value with parentheses () en", "Value with parentheses () fr")
63
+ /* <-- !!LOCALIZED STRINGS!! */
64
+ ;
65
+
66
+ private String textEn;
67
+
68
+ CoreLocalizedStrings(String textEn) {
69
+ this.textEn = textEn;
70
+ }
71
+ }
72
+ EOS
73
+ end
74
+
75
+ let(:expected_content_fr) do
76
+ <<-EOS.unindent
77
+ package ca.bell.fiberemote.core;
78
+
79
+ public enum CoreLocalizedStrings {
80
+
81
+ /* !!LOCALIZED STRINGS!! --> */
82
+ STRING_1("Value of string #1 en", "New value of string #1 fr"),
83
+ STRING_2("Value of string #2 en", "New value of string #2 fr"),
84
+ /* Comment */
85
+ STRING_3("Value with quotes \\" and \\nlinefeeds en", "New value with quotes \\" and \\nlinefeeds fr"),
86
+ STRING_4("Value with parentheses () en", "New value with parentheses () fr")
87
+ /* <-- !!LOCALIZED STRINGS!! */
88
+ ;
89
+
90
+ private String textEn;
91
+
92
+ CoreLocalizedStrings(String textEn) {
93
+ this.textEn = textEn;
94
+ }
95
+ }
96
+ EOS
97
+ end
98
+
99
+ it { expect(update_in_memory_en).to eql expected_content_en }
100
+ it { expect(update_in_memory_fr).to eql expected_content_fr }
101
+ end
102
+ end