zmeygo_sync 0.2 → 0.21

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/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ test/tmp
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in trclient.gemspec
4
4
  gem 'rest-client', :git => 'git://github.com/archiloque/rest-client.git'
5
+ gem 'webmock'
data/Rakefile CHANGED
@@ -1,2 +1,11 @@
1
- require 'bundler'
2
- Bundler::GemHelper.install_tasks
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.pattern = 'test/**/*_test.rb'
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test do
11
+ end
data/TODO CHANGED
@@ -1,6 +1,5 @@
1
1
  Write tests to test 3 basic things this gem will do
2
- 1. Hook into I18n.translate
3
- 2. Gather cache
4
- 3. Send cache (push method)
5
- 4. Receive locale data (pull method)
2
+ 1. Gather cache
3
+ 2. Send cache (push method)
4
+ 3. Receive locale data (pull method)
6
5
 
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
4
- require 'zmeygo/command'
5
- Zmeygo::Command.new(ARGV).daemonize
4
+ require 'zmeygo_sync/command'
5
+ ZmeygoSync::Command.new(ARGV).daemonize
@@ -1,24 +1,22 @@
1
1
  # encoding: UTF-8
2
+ require 'fileutils'
2
3
 
3
4
  module ZmeygoSync
4
- # Cache is responsable to gather missing keys from I18n.t/I18n.translate
5
- # calls and once in a while send them to server.
6
- # Server where keys will be sent is configured in config file Zmeygo.config['server']
5
+ # Cache is responsable to gather missing keys from I18n.t/I18n.translate calls
7
6
  class Cache
8
7
  def initialize
9
- @config = Zmeygo.config
8
+ @config = ZmeygoSync.config
10
9
  @missing_keys = {}
11
10
  @cache_file_path = "#{Rails.root}/tmp/zmeygo_sync_cache"
12
- end
13
-
14
- def get_cache_proj
15
- proj = validate_config!
16
- proj
11
+ dir = File.dirname(@cache_file_path)
12
+ unless File.exists?(dir)
13
+ FileUtils.mkdir_p(dir)
14
+ end
15
+ FileUtils.touch(@cache_file_path)
17
16
  end
18
17
 
19
18
  def get_cache
20
- proj = validate_config!
21
- @missing_keys[proj]
19
+ @missing_keys
22
20
  end
23
21
 
24
22
  def empty?
@@ -26,9 +24,8 @@ module ZmeygoSync
26
24
  end
27
25
 
28
26
  def add_to_cache(key)
29
- proj = validate_config!
30
- @missing_keys[proj] ||= {}
31
- @missing_keys[proj][key] = true
27
+ @missing_keys ||= {}
28
+ @missing_keys[key] = true
32
29
  File.open(@cache_file_path,'w') do |f|
33
30
  f.write(Marshal.dump(@missing_keys))
34
31
  end
@@ -49,20 +46,10 @@ module ZmeygoSync
49
46
  end
50
47
 
51
48
  def clear_cache
52
- proj = validate_config!
53
- @missing_keys[proj].clear
49
+ @missing_keys.clear
54
50
  File.truncate(@cache_file_path,0)
55
51
  end
56
52
 
57
- def validate_config!
58
- proj = @config['project']
59
- if proj.blank?
60
- raise "Please provide default project name in"
61
- end
62
- @missing_keys[proj] ||= {}
63
- proj
64
- end
65
-
66
53
  end # Cache
67
54
  end # Zmeygo
68
55
 
@@ -1,14 +1,14 @@
1
1
  # encoding: utf-8
2
2
  require 'ya2yaml'
3
3
 
4
- module ZmeygoSyncSync
4
+ module ZmeygoSync
5
5
  class Client
6
6
 
7
7
  attr_accessor :config, :options
8
8
 
9
9
  def initialize(options={})
10
10
  self.options = options
11
- self.config = ZmeygoSyncSync.config
11
+ self.config = ZmeygoSync.config
12
12
  end
13
13
 
14
14
  def push
@@ -16,7 +16,7 @@ module ZmeygoSyncSync
16
16
  puts "No cache" if options[:verbose]
17
17
  return
18
18
  end
19
- url = "http://ZmeygoSync.com/api/pushmissing?api_token=#{config['api_token']}"
19
+ url = "http://zmeygo.com/api/pushmissing?api_token=#{config['api_token']}"
20
20
  keys = []
21
21
  hash = ZmeygoSync.load_cache
22
22
  puts "cache=#{hash}" if options[:verbose]
@@ -38,7 +38,8 @@ module ZmeygoSyncSync
38
38
  exit
39
39
  end
40
40
  if response.code == 200
41
- reply = JSON.parse(response.body)
41
+ #reply = JSON.parse(response.body)
42
+ #do nothing on success
42
43
  elsif response.code == 401
43
44
  puts "Invalid api_token given."
44
45
  elsif response.code == 500
@@ -159,7 +160,7 @@ module ZmeygoSyncSync
159
160
  end
160
161
 
161
162
  def http_connect(path)
162
- url = "#{config['server'] || 'http://ZmeygoSync.com'}/api/#{path}?api_token=#{config['api_token']}"
163
+ url = "#{config['server'] || 'http://zmeygo.com'}/api/#{path}?api_token=#{config['api_token']}"
163
164
  begin
164
165
  puts "GET #{url}" if options[:verbose]
165
166
  response = RestClient.get(url)
@@ -2,7 +2,7 @@
2
2
  require 'daemons'
3
3
  require 'optparse'
4
4
 
5
- module ZmeygoSyncSync
5
+ module ZmeygoSync
6
6
  class Command
7
7
 
8
8
  attr_accessor :options, :client
@@ -11,7 +11,7 @@ module ZmeygoSyncSync
11
11
  @options = {
12
12
  :verbose => false,
13
13
  :pid_dir => "#{Rails.root}/tmp/pids",
14
- :sleep_delay => 10,
14
+ :sleep_delay => 60,
15
15
  :debug => false # when debug is false - it will daomonize, otherwise - it won't
16
16
  }
17
17
 
@@ -47,13 +47,21 @@ module ZmeygoSyncSync
47
47
  unless defined?(Rails)
48
48
  raise "This is not rails app???"
49
49
  end
50
+ step = 0
50
51
  loop do
51
52
  self.client.push
52
- self.client.pull
53
+ # once in 5 minutes download locales from server
54
+ if step % 10 == 0
55
+ self.client.pull
56
+ end
57
+ step += 1
58
+ if step > 1_000
59
+ step = 0
60
+ end
53
61
  sleep(@options[:sleep_delay])
54
62
  end
55
63
  rescue => e
56
- Rails.logger.fatal e
64
+ # Rails.logger.fatal e
57
65
  STDERR.puts e.message
58
66
  exit 1
59
67
  end
@@ -8,10 +8,10 @@ module ZmeygoSync
8
8
  attr_accessor :server, :api_token, :locale_dir, :project
9
9
 
10
10
  def initialize
11
- conf_file = File.join(Rails.root,'config','zmeygo.yml')
11
+ conf_file = File.join(Rails.root,'config','zmeygo_sync.yml')
12
12
  conf_all = YAML.load_file(File.expand_path(conf_file))
13
13
  conf = conf_all[Rails.env]
14
- conf['server'] = 'http://zmeygo.com'
14
+ # conf['server'] = 'http://zmeygo.com'
15
15
  self.api_token = conf['api_token']
16
16
  self.project = conf['project']
17
17
  end
@@ -1,8 +1,8 @@
1
1
  # encoding: utf-8
2
2
  #
3
- module ZmeygoSync
4
- class Constant
5
- DEFAULT_CONFIG_FILE = "#{Rails.root}/config/zmeygo.yml"
6
- CACHE_FILE = "#{Rails.root}/tmp/zmeygo_sync_cache"
7
- end
8
- end
3
+ # module ZmeygoSync
4
+ # class Constant
5
+ # DEFAULT_CONFIG_FILE = "#{Rails.root}/config/zmeygo.yml"
6
+ # CACHE_FILE = "#{Rails.root}/tmp/zmeygo_sync_cache"
7
+ # end
8
+ # end
@@ -1,20 +1,20 @@
1
1
  # encoding: utf-8
2
2
 
3
- module ZmeygoSync
4
- class I18nBackend
5
-
6
- attr_accessor :client
7
-
8
- def initialize(options={})
9
- @collection = {}
10
- @options = options
11
- Zmeygo.configure
12
- self.client = Zmeygo::Client.new(options)
13
- end
14
-
15
- def translate(locale, key, options = {})
16
- client.translate(nil,locale,key)
17
- end
18
- end
19
- end
3
+ # module ZmeygoSync
4
+ # class I18nBackend
5
+ #
6
+ # attr_accessor :client
7
+ #
8
+ # def initialize(options={})
9
+ # @collection = {}
10
+ # @options = options
11
+ # Zmeygo.configure
12
+ # self.client = Zmeygo::Client.new(options)
13
+ # end
14
+ #
15
+ # def translate(locale, key, options = {})
16
+ # client.translate(nil,locale,key)
17
+ # end
18
+ # end
19
+ # end
20
20
 
@@ -1,3 +1,3 @@
1
1
  module ZmeygoSync
2
- VERSION = "0.2"
2
+ VERSION = "0.21"
3
3
  end
@@ -1,31 +1,34 @@
1
1
  require File.join(File.dirname(__FILE__),'/test_helper')
2
- require File.join(File.dirname(__FILE__),'../lib/zmeygo')
2
+ require File.join(File.dirname(__FILE__),'../lib/zmeygo_sync')
3
3
 
4
4
  class ExtI18nTest < Test::Unit::TestCase
5
5
  # check if i18n.t method indeed populates Zmeygo.missing_keys
6
+
7
+ include ZmeygoSync
8
+
6
9
  def setup
10
+ common_setup
11
+ end
7
12
 
13
+ def teardown
14
+ common_teardown
8
15
  end
16
+
9
17
  def test_assert_i18n_translate
10
- Zmeygo.clear_cache
11
- assert_equal 0, Zmeygo.get_cache.size
18
+ assert_equal 0, ZmeygoSync.get_cache.size
12
19
  I18n.t :key1
13
20
  I18n.t :key2
14
- assert_equal 2, Zmeygo.get_cache.size
15
- assert Zmeygo.get_cache.include?(:key1)
16
- assert Zmeygo.get_cache.include?(:key2)
21
+ assert_equal 2, ZmeygoSync.get_cache.size
22
+ assert ZmeygoSync.get_cache.include?(:key1)
23
+ assert ZmeygoSync.get_cache.include?(:key2)
17
24
  end
18
25
 
19
- # c
20
26
  def test_if_i18n_correctly_populates_cache
21
- Zmeygo.clear_cache
22
- assert_equal 0, Zmeygo.get_cache.size
27
+ assert_equal 0, ZmeygoSync.get_cache.size
23
28
  I18n.t :some_key
24
29
  I18n.t :other_key
25
30
  I18n.t 'text.signin'
26
- cache = Zmeygo.get_cache.clone
27
- proj = Zmeygo.get_cache_proj
28
- assert_equal Zmeygo.config['default_project'],proj
31
+ cache = ZmeygoSync.get_cache.clone
29
32
  assert cache.include?(:some_key)
30
33
  assert cache.include?(:other_key)
31
34
  assert cache.include?('text.signin')
data/test/pull_test.rb ADDED
@@ -0,0 +1,28 @@
1
+ require File.join(File.dirname(__FILE__),'/test_helper')
2
+ require File.join(File.dirname(__FILE__),'../lib/zmeygo_sync')
3
+
4
+ # Test push functionality
5
+ class PushTest < Test::Unit::TestCase
6
+ # check if i18n.t method indeed populates Zmeygo.missing_keys
7
+
8
+ include ZmeygoSync
9
+
10
+ def setup
11
+ common_setup
12
+ end
13
+
14
+ def teardown
15
+ common_teardown
16
+ end
17
+
18
+ def test_if_pull_request_correct_link
19
+ response_body = {'message' => 'OK', 'data' => ''}.to_json
20
+ stub_request(:get, "http://zmeygo.com/api/available_locales/fake_proj").
21
+ with(:query => {:api_token => '1234'}).
22
+ to_return(:body => response_body, :status => 200 )
23
+ I18n.t :key1
24
+ command = Command.new(["start","-d"])
25
+ command.client.pull
26
+ end
27
+
28
+ end
data/test/push_test.rb ADDED
@@ -0,0 +1,27 @@
1
+ require File.join(File.dirname(__FILE__),'/test_helper')
2
+ require File.join(File.dirname(__FILE__),'../lib/zmeygo_sync')
3
+
4
+ # Test push functionality
5
+ class PushTest < Test::Unit::TestCase
6
+ # check if i18n.t method indeed populates Zmeygo.missing_keys
7
+
8
+ include ZmeygoSync
9
+
10
+ def setup
11
+ common_setup
12
+ end
13
+
14
+ def teardown
15
+ common_teardown
16
+ end
17
+
18
+ def test_if_push_request_correct_link
19
+ stub_request(:post, "http://zmeygo.com/api/pushmissing").
20
+ with(:query => {:api_token => '1234'}).
21
+ to_return(:body => "OK", :status => 200 )
22
+ I18n.t :key1
23
+ command = Command.new(["start","-d"])
24
+ command.client.push
25
+ end
26
+
27
+ end
data/test/test_helper.rb CHANGED
@@ -1 +1,22 @@
1
+ require 'mocha'
1
2
  require 'test/unit'
3
+ require 'webmock/test_unit'
4
+ require 'json'
5
+
6
+ class Rails
7
+ def self.root
8
+ File.dirname(__FILE__)
9
+ end
10
+ end
11
+
12
+ def common_setup
13
+ Configuration.stubs(:new)
14
+ Logger.stubs(:new)
15
+ ZmeygoSync.stubs(:config).returns({'project' => 'fake_proj','api_token'=>'1234'})
16
+ ZmeygoSync.configure
17
+ ZmeygoSync.clear_cache
18
+ end
19
+
20
+ def common_teardown
21
+ ZmeygoSync.clear_cache
22
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: zmeygo_sync
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: "0.2"
5
+ version: "0.21"
6
6
  platform: ruby
7
7
  authors:
8
8
  - Eugen Ciur
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-10-17 00:00:00 +03:00
13
+ date: 2011-10-18 00:00:00 +03:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -44,6 +44,8 @@ files:
44
44
  - lib/zmeygo_sync/i18n_backend.rb
45
45
  - lib/zmeygo_sync/version.rb
46
46
  - test/ext_i18n_test.rb
47
+ - test/pull_test.rb
48
+ - test/push_test.rb
47
49
  - test/test_helper.rb
48
50
  - zmeygo_sync.gemspec
49
51
  has_rdoc: true