localeapp 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.autotest +4 -0
- data/.gitignore +7 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +2 -0
- data/README.textile +147 -0
- data/Rakefile +11 -0
- data/bin/localeapp +61 -0
- data/cucumber.yml +8 -0
- data/features/localeapp_binary.feature +116 -0
- data/features/step_definitions/cli_steps.rb +27 -0
- data/features/support/env.rb +21 -0
- data/features/support/hooks.rb +3 -0
- data/init.rb +1 -0
- data/lib/locale_app/api_call.rb +9 -0
- data/lib/locale_app/api_caller.rb +77 -0
- data/lib/locale_app/cli/install.rb +41 -0
- data/lib/locale_app/cli/pull.rb +34 -0
- data/lib/locale_app/cli/push.rb +49 -0
- data/lib/locale_app/cli/update.rb +19 -0
- data/lib/locale_app/configuration.rb +93 -0
- data/lib/locale_app/exception_handler.rb +21 -0
- data/lib/locale_app/key_checker.rb +43 -0
- data/lib/locale_app/missing_translations.rb +36 -0
- data/lib/locale_app/poller.rb +61 -0
- data/lib/locale_app/rails/2_3_translation_helper_monkeypatch.rb +36 -0
- data/lib/locale_app/rails/controller.rb +34 -0
- data/lib/locale_app/rails/flatten.rb +113 -0
- data/lib/locale_app/rails.rb +53 -0
- data/lib/locale_app/routes.rb +80 -0
- data/lib/locale_app/sender.rb +49 -0
- data/lib/locale_app/tasks/locale_app.rake +20 -0
- data/lib/locale_app/updater.rb +63 -0
- data/lib/locale_app/version.rb +3 -0
- data/lib/locale_app.rb +98 -0
- data/lib/localeapp.rb +1 -0
- data/localeapp.gemspec +35 -0
- data/run_ci +5 -0
- data/spec/fixtures/en.yml +6 -0
- data/spec/fixtures/es.yml +6 -0
- data/spec/locale_app/api_call_spec.rb +15 -0
- data/spec/locale_app/api_caller_spec.rb +157 -0
- data/spec/locale_app/cli/install_spec.rb +42 -0
- data/spec/locale_app/cli/pull_spec.rb +45 -0
- data/spec/locale_app/cli/push_spec.rb +30 -0
- data/spec/locale_app/cli/update_spec.rb +18 -0
- data/spec/locale_app/configuration_spec.rb +119 -0
- data/spec/locale_app/exception_handler_spec.rb +21 -0
- data/spec/locale_app/key_checker_spec.rb +19 -0
- data/spec/locale_app/missing_translations_spec.rb +28 -0
- data/spec/locale_app/poller_spec.rb +61 -0
- data/spec/locale_app/rails/controller_spec.rb +117 -0
- data/spec/locale_app/routes_spec.rb +134 -0
- data/spec/locale_app/sender_spec.rb +49 -0
- data/spec/locale_app/updater_spec.rb +89 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/support/locale_app_integration_data.rb +33 -0
- data/spec/support/locale_app_synchronization_data.rb +21 -0
- metadata +300 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LocaleApp::KeyChecker, "#check(key)" do
|
4
|
+
it "returns false and an empty hash if the response from locale app is a 404" do
|
5
|
+
FakeWeb.register_uri(:get, 'http://api.localeapp.com/v1/projects/TEST_KEY.json', :body => "", :status => ['404', 'Not Found'])
|
6
|
+
with_configuration do
|
7
|
+
@checker = LocaleApp::KeyChecker.new
|
8
|
+
end
|
9
|
+
@checker.check('TEST_KEY').should == [false, {}]
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns true and and the parsed json hash if the response from locale app is a 200" do
|
13
|
+
FakeWeb.register_uri(:get, 'http://api.localeapp.com/v1/projects/TEST_KEY.json', :body => valid_project_data.to_json, :status => ['200', 'OK'])
|
14
|
+
with_configuration do
|
15
|
+
@checker = LocaleApp::KeyChecker.new
|
16
|
+
end
|
17
|
+
@checker.check('TEST_KEY').should == [true, valid_project_data]
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'locale_app/missing_translations'
|
3
|
+
|
4
|
+
describe LocaleApp::MissingTranslations, "#add(locale, key, options = {})" do
|
5
|
+
it "stores the missing translation data" do
|
6
|
+
translations = LocaleApp::MissingTranslations.new
|
7
|
+
translations.add(:en, 'foo', { :baz => 'bam' })
|
8
|
+
translations[:en].should include('foo')
|
9
|
+
translations[:en]['foo'].options.should == { :baz => 'bam' }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe LocaleApp::MissingTranslations, "#to_send" do
|
14
|
+
it "returns an array of missing translation data that needs to be sent to localeapp.com" do
|
15
|
+
translations = LocaleApp::MissingTranslations.new
|
16
|
+
translations.add(:en, 'foo', { :baz => 'bam' })
|
17
|
+
translations.add(:es, 'bar')
|
18
|
+
|
19
|
+
to_send = translations.to_send
|
20
|
+
to_send.size.should == 2
|
21
|
+
to_send[0][:key].should == 'foo'
|
22
|
+
to_send[0][:locale].should == :en
|
23
|
+
to_send[0][:options].should == { :baz => 'bam' }
|
24
|
+
to_send[1][:key].should == 'bar'
|
25
|
+
to_send[1][:locale].should == :es
|
26
|
+
to_send[1][:options].should == {}
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LocaleApp::Poller do
|
4
|
+
before do
|
5
|
+
@updated_at = Time.now.to_i
|
6
|
+
with_configuration(:synchronization_data_file => LocaleAppSynchronizationData::setup(nil, @updated_at), :api_key => 'TEST_KEY') do
|
7
|
+
@poller = LocaleApp::Poller.new
|
8
|
+
end
|
9
|
+
@hash = { 'translations' => {}, 'deleted' => [], 'locales' => [] }
|
10
|
+
end
|
11
|
+
|
12
|
+
after do
|
13
|
+
LocaleAppSynchronizationData::destroy
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#needs_reloading?" do
|
17
|
+
it "returns true when updated_at has been changed in the synchronization file" do
|
18
|
+
@poller.write_synchronization_data!(@poller.polled_at, 12345)
|
19
|
+
@poller.needs_reloading?.should be_true
|
20
|
+
end
|
21
|
+
|
22
|
+
it "returns false when updated_at is the same as in the synchronization file" do
|
23
|
+
@poller.needs_reloading?.should be_false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#write_synchronization_data!(polled_at, updated_at)" do
|
28
|
+
it "updates polled_at in the synchronization file" do
|
29
|
+
polled_at = lambda { @poller.synchronization_data[:polled_at] }
|
30
|
+
expect { @poller.write_synchronization_data!(01234, 56789) }.to change(polled_at, :call).to(01234)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "updates updated_at in the synchronization file" do
|
34
|
+
updated_at = lambda { @poller.synchronization_data[:updated_at] }
|
35
|
+
expect { @poller.write_synchronization_data!(01234, 56789) }.to change(updated_at, :call).to(56789)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#poll!" do
|
40
|
+
it "returns false if get returns 304 Not Modified" do
|
41
|
+
FakeWeb.register_uri(:get, "http://api.localeapp.com/v1/projects/TEST_KEY/translations.json?updated_at=#{@updated_at}", :body => '', :status => ['304', 'Not Modified'])
|
42
|
+
@poller.poll!.should == false
|
43
|
+
end
|
44
|
+
|
45
|
+
it "returns false if get returns a 50x response" do
|
46
|
+
FakeWeb.register_uri(:get, "http://api.localeapp.com/v1/projects/TEST_KEY/translations.json?updated_at=#{@updated_at}", :body => '', :status => ['500', 'Internal Server Error'])
|
47
|
+
@poller.poll!.should == false
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns false if get returns 200 OK" do
|
51
|
+
FakeWeb.register_uri(:get, "http://api.localeapp.com/v1/projects/TEST_KEY/translations.json?updated_at=#{@updated_at}", :body => @hash.to_json, :status => ['200', 'OK'], :date => Time.now.httpdate)
|
52
|
+
@poller.poll!.should == true
|
53
|
+
end
|
54
|
+
|
55
|
+
it "passes the data through to the Updater" do
|
56
|
+
FakeWeb.register_uri(:get, "http://api.localeapp.com/v1/projects/TEST_KEY/translations.json?updated_at=#{@updated_at}", :body => @hash.to_json, :status => ['200', 'OK'], :date => Time.now.httpdate)
|
57
|
+
LocaleApp.updater.should_receive(:update).with(@hash)
|
58
|
+
@poller.poll!
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestController
|
4
|
+
def self.before_filter(*options)
|
5
|
+
end
|
6
|
+
def self.after_filter(*options)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'locale_app/rails/controller'
|
11
|
+
|
12
|
+
describe LocaleApp::Rails::Controller, '#handle_translation_updates' do
|
13
|
+
before do
|
14
|
+
TestController.send(:include, LocaleApp::Rails::Controller)
|
15
|
+
with_configuration(:synchronization_data_file => LocaleAppSynchronizationData::setup) do
|
16
|
+
@controller = TestController.new
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
after do
|
21
|
+
LocaleAppSynchronizationData::destroy
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when polling is enabled" do
|
25
|
+
before do
|
26
|
+
LocaleApp.configuration.environment_name = 'development' # reloading enabled
|
27
|
+
LocaleApp.configuration.disabled_reloading_environments << 'development'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "calls poller.poll! when the synchronization file's polled_at has changed" do
|
31
|
+
LocaleApp.poller.write_synchronization_data!(01234, 56789)
|
32
|
+
LocaleApp.poller.should_receive(:poll!)
|
33
|
+
@controller.handle_translation_updates
|
34
|
+
end
|
35
|
+
|
36
|
+
it "doesn't call poller.poll! when the synchronization file's polled_at is the same" do
|
37
|
+
LocaleApp.poller.should_not_receive(:poll!)
|
38
|
+
@controller.handle_translation_updates
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when polling is disabled" do
|
43
|
+
before do
|
44
|
+
LocaleApp.configuration.environment_name = 'production' # reloading disabled
|
45
|
+
LocaleApp.configuration.disabled_reloading_environments << 'production'
|
46
|
+
end
|
47
|
+
|
48
|
+
it "doesn't poller.poll! when the synchronization file's polled_at has changed" do
|
49
|
+
LocaleApp.poller.write_synchronization_data!(01234, 56789)
|
50
|
+
LocaleApp.poller.should_not_receive(:poll!)
|
51
|
+
@controller.handle_translation_updates
|
52
|
+
end
|
53
|
+
|
54
|
+
it "doesn't poller.poll! when the synchronization file's polled_at is the same" do
|
55
|
+
LocaleApp.poller.should_not_receive(:poll!)
|
56
|
+
@controller.handle_translation_updates
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "when reloading is enabled" do
|
61
|
+
before do
|
62
|
+
LocaleApp.configuration.environment_name = 'development' # reloading enabled
|
63
|
+
LocaleApp.configuration.disabled_polling_environments << 'development'
|
64
|
+
end
|
65
|
+
|
66
|
+
it "calls I18n.reload! when the synchronization file's updated_at has changed" do
|
67
|
+
LocaleApp.poller.write_synchronization_data!(01234, 56789)
|
68
|
+
I18n.should_receive(:reload!)
|
69
|
+
@controller.handle_translation_updates
|
70
|
+
end
|
71
|
+
|
72
|
+
it "doesn't call I18n.relaod! when the synchronization file's updated_at is the same" do
|
73
|
+
I18n.should_not_receive(:reload!)
|
74
|
+
@controller.handle_translation_updates
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context "when reloading is disabled" do
|
79
|
+
before do
|
80
|
+
LocaleApp.configuration.environment_name = 'production' # reloading disabled
|
81
|
+
LocaleApp.configuration.disabled_polling_environments << 'production'
|
82
|
+
end
|
83
|
+
|
84
|
+
it "doesn't call I18n.reload! when the synchronization file's updated_at has changed" do
|
85
|
+
LocaleApp.poller.write_synchronization_data!(01234, 56789)
|
86
|
+
I18n.should_not_receive(:reload!)
|
87
|
+
@controller.handle_translation_updates
|
88
|
+
end
|
89
|
+
|
90
|
+
it "doesn't call I18n.relaod! when the synchronization file's updated_at is the same" do
|
91
|
+
I18n.should_not_receive(:reload!)
|
92
|
+
@controller.handle_translation_updates
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe LocaleApp::Rails::Controller, '#send_missing_translations' do
|
98
|
+
before(:each) do
|
99
|
+
LocaleApp.configure do |config|
|
100
|
+
config.api_key = 'abcdef'
|
101
|
+
end
|
102
|
+
TestController.send(:include, LocaleApp::Rails::Controller)
|
103
|
+
@controller = TestController.new
|
104
|
+
end
|
105
|
+
|
106
|
+
it "does nothing when sending is disabled" do
|
107
|
+
LocaleApp.configuration.environment_name = 'test'
|
108
|
+
LocaleApp.sender.should_not_receive(:post_missing_translations)
|
109
|
+
@controller.send_missing_translations
|
110
|
+
end
|
111
|
+
|
112
|
+
it "proceeds when configuration is enabled" do
|
113
|
+
LocaleApp.configuration.environment_name = 'development'
|
114
|
+
LocaleApp.sender.should_receive(:post_missing_translations)
|
115
|
+
@controller.send_missing_translations
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestRoutes
|
4
|
+
include LocaleApp::Routes
|
5
|
+
end
|
6
|
+
|
7
|
+
describe LocaleApp::Routes do
|
8
|
+
before(:each) do
|
9
|
+
@routes = TestRoutes.new
|
10
|
+
@config = {:host => 'test.host', :api_key => 'API_KEY'}
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#project_endpoint(options = {})" do
|
14
|
+
it "returns :get and the project url for the options" do
|
15
|
+
with_configuration(@config) do
|
16
|
+
options = { :foo => :bar }
|
17
|
+
@routes.should_receive(:project_url).with(options).and_return('url')
|
18
|
+
@routes.project_endpoint(options).should == [:get, 'url']
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#project_url' do
|
24
|
+
it "is constructed from the configuration host and port and defaults to json" do
|
25
|
+
with_configuration(@config.merge(:port => 1234)) do
|
26
|
+
@routes.project_url.should == "http://test.host:1234/v1/projects/API_KEY.json"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "includes http auth if in configuration" do
|
31
|
+
with_configuration(@config.merge(:port => 1234, :http_auth_username => 'foo', :http_auth_password => 'bar')) do
|
32
|
+
@routes.project_url.should == "http://foo:bar@test.host:1234/v1/projects/API_KEY.json"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it "can be changed to another content type" do
|
37
|
+
with_configuration(@config) do
|
38
|
+
@routes.project_url(:format => :yml).should == 'http://test.host/v1/projects/API_KEY.yml'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#translations_url" do
|
44
|
+
it "it extends the project_url and defaults to json" do
|
45
|
+
with_configuration(@config) do
|
46
|
+
@routes.translations_url.should == "http://test.host/v1/projects/API_KEY/translations.json"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
it "adds query parameters on to the url" do
|
51
|
+
with_configuration(@config) do
|
52
|
+
url = @routes.translations_url(:query => {:updated_at => '2011-04-19', :foo => :bar})
|
53
|
+
url.should match(/\?.*updated_at=2011-04-19/)
|
54
|
+
url.should match(/\?.*foo=bar/)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it "can be changed to another content type" do
|
59
|
+
with_configuration(@config) do
|
60
|
+
@routes.translations_url(:format => :yml).should == 'http://test.host/v1/projects/API_KEY/translations.yml'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#translations_endpoint(options = {})" do
|
66
|
+
it "returns :get and the translations url for the options" do
|
67
|
+
with_configuration(@config) do
|
68
|
+
options = { :foo => :bar }
|
69
|
+
@routes.should_receive(:translations_url).with(options).and_return('url')
|
70
|
+
@routes.translations_endpoint(options).should == [:get, 'url']
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "#create_translation_endpoint(options = {})" do
|
76
|
+
it "returns :post and the translation url for the options" do
|
77
|
+
with_configuration(@config) do
|
78
|
+
options = { :foo => :bar }
|
79
|
+
@routes.should_receive(:translations_url).with(options).and_return('url')
|
80
|
+
@routes.create_translation_endpoint(options).should == [:post, 'url']
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "#missing_translations_endpoint(options = {})" do
|
86
|
+
it "returns :post and the missing_translations url for the options" do
|
87
|
+
with_configuration(@config) do
|
88
|
+
options = { :foo => :bar }
|
89
|
+
@routes.should_receive(:missing_translations_url).with(options).and_return('url')
|
90
|
+
@routes.missing_translations_endpoint(options).should == [:post, 'url']
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "#missing_translations_url" do
|
96
|
+
it "it extends the project_url and defaults to json" do
|
97
|
+
with_configuration(@config) do
|
98
|
+
@routes.missing_translations_url.should == "http://test.host/v1/projects/API_KEY/translations/missing.json"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
it "adds query parameters on to the url" do
|
103
|
+
with_configuration(@config) do
|
104
|
+
url = @routes.missing_translations_url(:query => {:updated_at => '2011-04-19', :foo => :bar})
|
105
|
+
url.should match(/\?.*updated_at=2011-04-19/)
|
106
|
+
url.should match(/\?.*foo=bar/)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
it "can be changed to another content type" do
|
111
|
+
with_configuration(@config) do
|
112
|
+
@routes.missing_translations_url(:format => :yml).should == 'http://test.host/v1/projects/API_KEY/translations/missing.yml'
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe "#import_url" do
|
118
|
+
it "appends 'import to the project url" do
|
119
|
+
with_configuration(@config) do
|
120
|
+
@routes.import_url.should == 'http://test.host/v1/projects/API_KEY/import/'
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe "#import_endpoint(options = {})" do
|
126
|
+
it "returns :post and the import url for the options" do
|
127
|
+
with_configuration(@config) do
|
128
|
+
options = { :foo => :bar }
|
129
|
+
@routes.should_receive(:import_url).with(options).and_return('url')
|
130
|
+
@routes.import_endpoint(options).should == [:post, 'url']
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LocaleApp::Sender, "#post_translation(locale, key, options, value = nil)" do
|
4
|
+
before(:each) do
|
5
|
+
with_configuration(:api_key => "TEST_KEY") do
|
6
|
+
@sender = LocaleApp::Sender.new
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it "posts the missing translation data to the backend" do
|
11
|
+
data = {
|
12
|
+
:translation => {
|
13
|
+
:key => "test.key",
|
14
|
+
:locale => "en",
|
15
|
+
:substitutions => ['foo', 'bar'],
|
16
|
+
:description => "test content"
|
17
|
+
}
|
18
|
+
}
|
19
|
+
# have to stub RestClient here as FakeWeb doesn't support looking at the post body yet
|
20
|
+
RestClient.should_receive(:post).with(@sender.translations_url, data.to_json, :content_type => :json).and_return(double('response', :code => 200))
|
21
|
+
@sender.post_translation('en', 'test.key', { 'foo' => 'foo', 'bar' => 'bar' }, 'test content')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe LocaleApp::Sender, "#post_missing_translations" do
|
26
|
+
before(:each) do
|
27
|
+
with_configuration(:api_key => 'TEST_KEY') do
|
28
|
+
@sender = LocaleApp::Sender.new
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "sends the missing translation data to the API" do
|
33
|
+
missing_to_send = [
|
34
|
+
{ :key => "test.key", :locale => "en" },
|
35
|
+
{ :key => "test.key2", :locale => "en" }
|
36
|
+
]
|
37
|
+
LocaleApp.missing_translations.should_receive(:to_send).and_return(missing_to_send)
|
38
|
+
data = { :translations => missing_to_send }
|
39
|
+
# have to stub RestClient here as FakeWeb doesn't support looking at the post body yet
|
40
|
+
RestClient.should_receive(:post).with(@sender.missing_translations_url, data.to_json, :content_type => :json).and_return(double('response', :code => 200))
|
41
|
+
@sender.post_missing_translations
|
42
|
+
end
|
43
|
+
|
44
|
+
it "does nothing if there are no missing translations to send" do
|
45
|
+
LocaleApp.missing_translations.should_receive(:to_send).and_return([])
|
46
|
+
RestClient.should_not_receive(:post)
|
47
|
+
@sender.post_missing_translations
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LocaleApp::Updater, ".update(data)" do
|
4
|
+
before(:each) do
|
5
|
+
@yml_dir = Dir.mktmpdir
|
6
|
+
Dir.glob(File.join(File.dirname(__FILE__), '..', 'fixtures', '*.yml')).each { |f| FileUtils.cp f, @yml_dir }
|
7
|
+
with_configuration(:translation_data_directory => @yml_dir) do
|
8
|
+
@updater = LocaleApp::Updater.new
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
after(:each) do
|
13
|
+
FileUtils.rm_rf @yml_dir
|
14
|
+
end
|
15
|
+
|
16
|
+
def do_update(data)
|
17
|
+
@updater.update(data)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "adds, updates and deletes keys in the yml files" do
|
21
|
+
do_update({
|
22
|
+
'translations' => {
|
23
|
+
'en' => {
|
24
|
+
'foo' => { 'monkey' => 'hello', 'night' => 'night' }
|
25
|
+
},
|
26
|
+
'es' => {
|
27
|
+
'foo' => { 'monkey' => 'hola', 'night' => 'noche' }
|
28
|
+
}
|
29
|
+
},
|
30
|
+
'deleted' => [
|
31
|
+
'foo.delete_me',
|
32
|
+
'bar.delete_me_too',
|
33
|
+
'hah.imnotreallyhere'
|
34
|
+
],
|
35
|
+
'locales' => %w{en es}
|
36
|
+
})
|
37
|
+
File.read(File.join(@yml_dir, 'en.yml')).should == <<-EN
|
38
|
+
en:
|
39
|
+
foo:
|
40
|
+
monkey: hello
|
41
|
+
night: night
|
42
|
+
EN
|
43
|
+
File.read(File.join(@yml_dir, 'es.yml')).should == <<-ES
|
44
|
+
es:
|
45
|
+
foo:
|
46
|
+
monkey: hola
|
47
|
+
night: noche
|
48
|
+
ES
|
49
|
+
end
|
50
|
+
|
51
|
+
it "deletes keys in the yml files when updates are empty" do
|
52
|
+
do_update({
|
53
|
+
'translations' => {},
|
54
|
+
'deleted' => [
|
55
|
+
'foo.delete_me',
|
56
|
+
'bar.delete_me_too',
|
57
|
+
'hah.imnotreallyhere'
|
58
|
+
],
|
59
|
+
'locales' => %w{es}
|
60
|
+
})
|
61
|
+
File.read(File.join(@yml_dir, 'es.yml')).should == <<-ES
|
62
|
+
es:
|
63
|
+
foo:
|
64
|
+
monkey: Mono
|
65
|
+
ES
|
66
|
+
end
|
67
|
+
|
68
|
+
it "creates a new yml file if an unknown locale is passed" do
|
69
|
+
do_update({
|
70
|
+
'translations' => {
|
71
|
+
'ja' => { 'foo' => 'bar'}
|
72
|
+
},
|
73
|
+
'locales' => ['ja']
|
74
|
+
})
|
75
|
+
File.read(File.join(@yml_dir, 'ja.yml')).should == <<-JA
|
76
|
+
ja:
|
77
|
+
foo: bar
|
78
|
+
JA
|
79
|
+
end
|
80
|
+
|
81
|
+
it "doesn't create a new yml file if an unknown locale is passed but it has no translations" do
|
82
|
+
do_update({
|
83
|
+
'translations' => {},
|
84
|
+
'deletes' => ['foo.delete_me'],
|
85
|
+
'locales' => ['ja']
|
86
|
+
})
|
87
|
+
File.exist?(File.join(@yml_dir, 'ja.yml')).should be_false
|
88
|
+
end
|
89
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
require 'locale_app'
|
3
|
+
require 'fakeweb'
|
4
|
+
require 'support/locale_app_integration_data'
|
5
|
+
require 'support/locale_app_synchronization_data'
|
6
|
+
require 'logger'
|
7
|
+
|
8
|
+
def with_configuration(options = {})
|
9
|
+
LocaleApp.configuration = nil
|
10
|
+
LocaleApp.configure do |configuration|
|
11
|
+
options.each do |option, value|
|
12
|
+
configuration.send("#{option}=", value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
yield
|
16
|
+
end
|
17
|
+
|
18
|
+
RSpec.configure do |config|
|
19
|
+
config.include(LocaleAppIntegrationData)
|
20
|
+
config.include(LocaleAppSynchronizationData)
|
21
|
+
config.before(:each) do
|
22
|
+
FakeWeb.allow_net_connect = false
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# This module is the source for all data that simulates what the real app would return.
|
2
|
+
# It's included in the specs and cucumber tests, so if our format changes we
|
3
|
+
# should only have to change here
|
4
|
+
module LocaleAppIntegrationData
|
5
|
+
def valid_project_data
|
6
|
+
{
|
7
|
+
'name' => "Test Project",
|
8
|
+
'default_locale' => {
|
9
|
+
'name' => "English",
|
10
|
+
'code' => "en"
|
11
|
+
}
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid_translation_data
|
16
|
+
{
|
17
|
+
'translations' => {
|
18
|
+
'en' => {
|
19
|
+
'foo' => { 'monkey' => 'hello', 'night' => 'night' }
|
20
|
+
},
|
21
|
+
'es' => {
|
22
|
+
'foo' => { 'monkey' => 'hola', 'night' => 'noche' }
|
23
|
+
},
|
24
|
+
},
|
25
|
+
'deleted' => [
|
26
|
+
'foo.delete_me',
|
27
|
+
'bar.delete_me_too',
|
28
|
+
'hah.imnotreallyhere'
|
29
|
+
],
|
30
|
+
'locales' => %w{en es}
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module LocaleAppSynchronizationData
|
2
|
+
def self.file(dir)
|
3
|
+
File.join(dir, 'test_sync.yml')
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.setup(polled_at=nil, updated_at=nil)
|
7
|
+
polled_at ||= Time.now.to_i
|
8
|
+
updated_at ||= Time.now.to_i
|
9
|
+
|
10
|
+
@dir = Dir.mktmpdir
|
11
|
+
@file = file(@dir)
|
12
|
+
File.open(@file, 'w+') do |f|
|
13
|
+
f.write({ :polled_at => polled_at, :updated_at => updated_at }.to_yaml)
|
14
|
+
end
|
15
|
+
@file
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.destroy
|
19
|
+
FileUtils.rm_rf @dir
|
20
|
+
end
|
21
|
+
end
|