ghost_reader 1.2.1 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +24 -4
- data/lib/ghost_reader/backend.rb +2 -11
- data/lib/ghost_reader/engine.rb +4 -0
- data/lib/ghost_reader/util.rb +44 -0
- data/lib/ghost_reader/version.rb +1 -1
- data/lib/ghost_reader.rb +1 -1
- data/lib/tasks/ghost_reader.rake +61 -56
- data/spec/ghost_reader/backend_spec.rb +4 -4
- metadata +6 -4
data/README.md
CHANGED
@@ -2,15 +2,17 @@ GhostReader
|
|
2
2
|
===========
|
3
3
|
|
4
4
|
GhostReader is an alternative I18n backend which makes use of the
|
5
|
-
GhostWriter service.
|
5
|
+
GhostWriter service (https://github.com/branch14/ghost_writer).
|
6
6
|
|
7
7
|
## Usage
|
8
8
|
|
9
9
|
### Gemfile
|
10
10
|
|
11
|
-
gem 'ghost_reader'
|
11
|
+
gem 'ghost_reader'
|
12
12
|
|
13
|
-
###
|
13
|
+
### For development & staging
|
14
|
+
|
15
|
+
#### config/initializers/ghost_reader.rb
|
14
16
|
|
15
17
|
unless Rails.env.test?
|
16
18
|
config = {
|
@@ -26,7 +28,7 @@ GhostWriter service.
|
|
26
28
|
I18n.backend = GhostReader::Backend.new(config)
|
27
29
|
end
|
28
30
|
|
29
|
-
|
31
|
+
#### Configuration
|
30
32
|
|
31
33
|
* 'report interval' is the delay in seconds between subsequent POST
|
32
34
|
requests (reporting requests), which report missing translations.
|
@@ -43,6 +45,24 @@ GhostWriter service.
|
|
43
45
|
- 'uri' is the complete uri with the folling keys, which will be
|
44
46
|
replaced: :protocol, :host, :api_key
|
45
47
|
|
48
|
+
### For Live
|
49
|
+
|
50
|
+
In a live system you typically don't want to depend on another system,
|
51
|
+
so we'll just go with a static export of translations.
|
52
|
+
|
53
|
+
# specify where to get the static file from
|
54
|
+
echo 'static: https://<ghostservice>/system/static/<apikey>.yml' >> config/ghost_reader.yml
|
55
|
+
|
56
|
+
# in a cron job do this
|
57
|
+
rake ghost_reader:poll
|
58
|
+
|
59
|
+
The rake task will rewrite config/locales/ghost_reader.yml if it
|
60
|
+
changed and touch tmp/restart.txt to make passenger restart the
|
61
|
+
application.
|
62
|
+
|
63
|
+
It's a good idea to delete all other translations files on this
|
64
|
+
system.
|
65
|
+
|
46
66
|
## TODO
|
47
67
|
|
48
68
|
* implement counting, for statistics
|
data/lib/ghost_reader/backend.rb
CHANGED
@@ -9,9 +9,6 @@ module GhostReader
|
|
9
9
|
|
10
10
|
attr_accessor :config, :missings
|
11
11
|
|
12
|
-
# exposed for testing & debugging
|
13
|
-
attr_reader :retriever, :reporter
|
14
|
-
|
15
12
|
# for options see code of default_config
|
16
13
|
def initialize(conf={})
|
17
14
|
self.config = OpenStruct.new(default_config.merge(conf))
|
@@ -20,12 +17,6 @@ module GhostReader
|
|
20
17
|
config.logger.level = config.log_level || Logger::WARN
|
21
18
|
config.service[:logger] ||= config.logger
|
22
19
|
config.client ||= Client.new(config.service)
|
23
|
-
# unless config.no_auto_spawn
|
24
|
-
# log "GhostReader spawning agents."
|
25
|
-
# spawn_retriever
|
26
|
-
# spawn_reporter
|
27
|
-
# log "GhostReader spawned its agents."
|
28
|
-
# end
|
29
20
|
@report_ts = @retrieval_ts = Time.now
|
30
21
|
log "Initialized GhostReader backend.", :info
|
31
22
|
end
|
@@ -41,10 +32,10 @@ module GhostReader
|
|
41
32
|
raise 'no fallback given' if config.fallback.nil?
|
42
33
|
log "lookup: #{locale} #{key} #{scope.inspect} #{options.inspect}"
|
43
34
|
|
44
|
-
result = config.fallback.
|
35
|
+
result = config.fallback.lookup(locale, key, scope, options)
|
45
36
|
log "fallback result: #{result.inspect}"
|
46
37
|
rescue Exception => ex
|
47
|
-
log "fallback.
|
38
|
+
log "fallback.lookup raised exception: #{ex}"
|
48
39
|
ensure # make sure everything is tracked
|
49
40
|
# TODO results which are hashes need to be tracked disaggregated
|
50
41
|
track({ key => { locale.to_s => { 'default' => result } } }) unless result.is_a?(Hash)
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'excon'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module GhostReader
|
5
|
+
module Util
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def poll(headers={})
|
9
|
+
p config_file
|
10
|
+
return unless File.exist?(config_file)
|
11
|
+
headers['If-Modified-Since'] = File.mtime(translation_file) if File.exist?(translation_file)
|
12
|
+
headers.reverse_merge! :method => :get
|
13
|
+
p headers
|
14
|
+
p config
|
15
|
+
excon = Excon.new(config['static'])
|
16
|
+
response = excon.request(headers)
|
17
|
+
p response
|
18
|
+
if response.status == 200
|
19
|
+
translation_file response.body
|
20
|
+
restart!
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def translation_file(content=nil)
|
25
|
+
path = File.join(Rails.root, 'config', 'locales', 'ghost_reader.yml')
|
26
|
+
return path if content.nil?
|
27
|
+
File.open(path, 'w') { |f| f.puts content }
|
28
|
+
end
|
29
|
+
|
30
|
+
def config_file
|
31
|
+
File.join(Rails.root, 'config', 'ghost_reader.yml')
|
32
|
+
end
|
33
|
+
|
34
|
+
def restart!
|
35
|
+
FileUtils.touch(File.join(Rails.root, 'tmp', 'restart.txt'))
|
36
|
+
end
|
37
|
+
|
38
|
+
def config
|
39
|
+
YAML.load(File.read(config_file))
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/ghost_reader/version.rb
CHANGED
data/lib/ghost_reader.rb
CHANGED
data/lib/tasks/ghost_reader.rake
CHANGED
@@ -1,62 +1,67 @@
|
|
1
1
|
namespace :ghost_reader do
|
2
2
|
|
3
|
-
desc "
|
4
|
-
task :
|
5
|
-
|
6
|
-
raise "ERROR: Ghostwriter is not configured as I18n.backend"
|
7
|
-
end
|
8
|
-
|
9
|
-
begin
|
10
|
-
puts "Loading data from Ghostwriter and delete old translations"
|
11
|
-
yaml_data = I18n.backend.load_yaml_from_ghostwriter
|
12
|
-
rescue Exception => e
|
13
|
-
abort e.message
|
14
|
-
end
|
15
|
-
|
16
|
-
yaml_data.each_pair do |key,value|
|
17
|
-
outfile = Rails.root.join("config", "locales",
|
18
|
-
"#{key.to_s}.yml")
|
19
|
-
begin
|
20
|
-
puts "Deleting old translations: #{outfile}"
|
21
|
-
File.delete(outfile) if File.exists?(outfile)
|
22
|
-
rescue Exception => e
|
23
|
-
abort "Couldn't delete file: #{e.message}"
|
24
|
-
end
|
25
|
-
|
26
|
-
begin
|
27
|
-
puts "Writing new translations: #{outfile}"
|
28
|
-
File.open(outfile, "w") do |yaml_file|
|
29
|
-
yaml_file.write({key => value}.to_yaml)
|
30
|
-
end
|
31
|
-
rescue Exception => e
|
32
|
-
abort "Couldn't write file: #{e.message}"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
desc "Push all locally configured translations to ghost-writer"
|
38
|
-
task :push => :environment do
|
39
|
-
unless I18n.backend.respond_to? :push_all_backend_data
|
40
|
-
raise "ERROR: Ghostwriter is not configured as I18n.backend"
|
41
|
-
end
|
42
|
-
puts "Pushing data to Ghostwriter"
|
43
|
-
begin
|
44
|
-
I18n.backend.push_all_backend_data
|
45
|
-
rescue Exception => e
|
46
|
-
abort e.message
|
47
|
-
end
|
3
|
+
desc "Poll for changes in translations for production"
|
4
|
+
task :poll => :environment do
|
5
|
+
GhostReader::Util.poll
|
48
6
|
end
|
49
7
|
|
50
|
-
desc "
|
51
|
-
task :
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
8
|
+
# desc "Fetch newest translations from ghost-writer and overwrite the local translations"
|
9
|
+
# task :fetch => :environment do
|
10
|
+
# unless I18n.backend.respond_to? :load_yaml_from_ghostwriter
|
11
|
+
# raise "ERROR: Ghostwriter is not configured as I18n.backend"
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# begin
|
15
|
+
# puts "Loading data from Ghostwriter and delete old translations"
|
16
|
+
# yaml_data = I18n.backend.load_yaml_from_ghostwriter
|
17
|
+
# rescue Exception => e
|
18
|
+
# abort e.message
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# yaml_data.each_pair do |key,value|
|
22
|
+
# outfile = Rails.root.join("config", "locales",
|
23
|
+
# "#{key.to_s}.yml")
|
24
|
+
# begin
|
25
|
+
# puts "Deleting old translations: #{outfile}"
|
26
|
+
# File.delete(outfile) if File.exists?(outfile)
|
27
|
+
# rescue Exception => e
|
28
|
+
# abort "Couldn't delete file: #{e.message}"
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# begin
|
32
|
+
# puts "Writing new translations: #{outfile}"
|
33
|
+
# File.open(outfile, "w") do |yaml_file|
|
34
|
+
# yaml_file.write({key => value}.to_yaml)
|
35
|
+
# end
|
36
|
+
# rescue Exception => e
|
37
|
+
# abort "Couldn't write file: #{e.message}"
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# desc "Push all locally configured translations to ghost-writer"
|
43
|
+
# task :push => :environment do
|
44
|
+
# unless I18n.backend.respond_to? :push_all_backend_data
|
45
|
+
# raise "ERROR: Ghostwriter is not configured as I18n.backend"
|
46
|
+
# end
|
47
|
+
# puts "Pushing data to Ghostwriter"
|
48
|
+
# begin
|
49
|
+
# I18n.backend.push_all_backend_data
|
50
|
+
# rescue Exception => e
|
51
|
+
# abort e.message
|
52
|
+
# end
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# desc "Install default initializer for ghost reader"
|
56
|
+
# task :install => :environment do
|
57
|
+
#
|
58
|
+
# # TODO: this should work only when the ghost_reader is introduced as gem
|
59
|
+
# infile = 'templates/ghost_reader.rb'
|
60
|
+
# outdir = Rails.root.join("config", "initializers")
|
61
|
+
#
|
62
|
+
# puts "Installing ghost_reader.rb initializer..."
|
63
|
+
# FileUtils.copy_file(infile, outdir)
|
64
|
+
# puts "Done."
|
65
|
+
# end
|
61
66
|
|
62
67
|
end
|
@@ -25,7 +25,7 @@ describe GhostReader::Backend do
|
|
25
25
|
|
26
26
|
let(:fallback) do
|
27
27
|
mock("FallbackBackend").tap do |fallback|
|
28
|
-
fallback.stub!(:
|
28
|
+
fallback.stub!(:lookup).and_return(translation)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -37,7 +37,7 @@ describe GhostReader::Backend do
|
|
37
37
|
|
38
38
|
it 'should use the given fallback' do
|
39
39
|
backend.config.fallback.should be(fallback)
|
40
|
-
fallback.should_receive(:
|
40
|
+
fallback.should_receive(:lookup)
|
41
41
|
backend.translate(:en, 'this.is.a.test').should eq(translation)
|
42
42
|
end
|
43
43
|
|
@@ -48,7 +48,7 @@ describe GhostReader::Backend do
|
|
48
48
|
end
|
49
49
|
|
50
50
|
it 'should use memoization' do
|
51
|
-
fallback.should_receive(:
|
51
|
+
fallback.should_receive(:lookup).exactly(1)
|
52
52
|
2.times { backend.translate(:en, 'this.is.a.test').should eq(translation) }
|
53
53
|
end
|
54
54
|
|
@@ -116,7 +116,7 @@ describe GhostReader::Backend do
|
|
116
116
|
context 'GhostReader set up with raising fallback' do
|
117
117
|
let(:fallback) do
|
118
118
|
mock("FallbackBackend").tap do |fallback|
|
119
|
-
fallback.stub!(:
|
119
|
+
fallback.stub!(:lookup) do
|
120
120
|
raise 'missing translation'
|
121
121
|
end
|
122
122
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ghost_reader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 1.2.
|
9
|
+
- 2
|
10
|
+
version: 1.2.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Phil Hofmann
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-01-
|
18
|
+
date: 2012-01-23 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -154,6 +154,8 @@ files:
|
|
154
154
|
- lib/ghost_reader.rb
|
155
155
|
- lib/ghost_reader/backend.rb
|
156
156
|
- lib/ghost_reader/client.rb
|
157
|
+
- lib/ghost_reader/engine.rb
|
158
|
+
- lib/ghost_reader/util.rb
|
157
159
|
- lib/ghost_reader/version.rb
|
158
160
|
- lib/tasks/ghost_reader.rake
|
159
161
|
- lib/tasks/templates/ghost_reader.rb
|