tml 5.2.7 → 5.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/tml/session.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # encoding: UTF-8
2
2
  #--
3
- # Copyright (c) 2016 Translation Exchange, Inc
3
+ # Copyright (c) 2016 Translation Exchange, Inc
4
4
  #
5
5
  # _______ _ _ _ ______ _
6
6
  # |__ __| | | | | (_) | ____| | |
@@ -33,7 +33,7 @@
33
33
  module Tml
34
34
 
35
35
  def self.session
36
- Thread.current[:session] ||= Tml::Session.new
36
+ Thread.current[:tml_context] ||= Tml::Session.new
37
37
  end
38
38
 
39
39
  class Session
@@ -44,8 +44,9 @@ module Tml
44
44
  return if Tml.config.disabled?
45
45
 
46
46
  key = opts[:key] || Tml.config.application[:key]
47
- token = opts[:token] || Tml.config.application[:token]
48
47
  host = opts[:host] || Tml.config.application[:host]
48
+ # token = opts[:token] || Tml.config.application[:token]
49
+ token = opts[:access_token]
49
50
 
50
51
  Tml.cache.reset_version
51
52
 
@@ -54,6 +55,8 @@ module Tml
54
55
  self.current_locale = opts[:locale]
55
56
  self.current_translator = opts[:translator]
56
57
 
58
+ # Tml.logger.debug(opts.inspect)
59
+
57
60
  self.application = Tml::Application.new(:key => key, :access_token => token, :host => host).fetch
58
61
 
59
62
  if self.current_translator
data/lib/tml/source.rb CHANGED
@@ -98,7 +98,7 @@ class Tml::Source < Tml::Base
98
98
 
99
99
  data = self.application.api_client.get(
100
100
  "sources/#{self.key}/translations",
101
- {:locale => locale, :all => true, :ignored => true},
101
+ {:locale => locale, :all => true, :ignored => true, :app_id => self.application.key},
102
102
  {:cache_key => Tml::Source.cache_key(locale, self.source), :raw_json => true}
103
103
  ) || {}
104
104
 
data/lib/tml/utils.rb CHANGED
@@ -62,6 +62,11 @@ module Tml
62
62
  (0..16).to_a.map{|a| rand(16).to_s(16)}.join
63
63
  end
64
64
 
65
+ def self.interval_timestamp(interval)
66
+ t = Time.now.to_i
67
+ t - (t % interval.to_i)
68
+ end
69
+
65
70
  def self.cookie_name(app_key)
66
71
  "trex_#{app_key}"
67
72
  end
@@ -130,5 +135,30 @@ module Tml
130
135
  []
131
136
  end
132
137
 
138
+ def self.ungzip(tarfile)
139
+ z = Zlib::GzipReader.new(tarfile)
140
+ unzipped = StringIO.new(z.read)
141
+ z.close
142
+ unzipped
143
+ end
144
+
145
+ def self.untar(io, destination)
146
+ Gem::Package::TarReader.new io do |tar|
147
+ tar.each do |tarfile|
148
+ destination_file = File.join destination, tarfile.full_name
149
+
150
+ if tarfile.directory?
151
+ FileUtils.mkdir_p destination_file
152
+ else
153
+ destination_directory = File.dirname(destination_file)
154
+ FileUtils.mkdir_p destination_directory unless File.directory?(destination_directory)
155
+ File.open destination_file, "wb" do |f|
156
+ f.print tarfile.read
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
162
+
133
163
  end
134
164
  end
data/lib/tml/version.rb CHANGED
@@ -31,7 +31,7 @@
31
31
  #++
32
32
 
33
33
  module Tml
34
- VERSION = '5.2.7'
34
+ VERSION = '5.4.1'
35
35
 
36
36
  def self.full_version
37
37
  "tml-ruby v#{Tml::VERSION} (Faraday v#{Faraday::VERSION})"
data/lib/tml.rb CHANGED
@@ -40,7 +40,7 @@ module Tml
40
40
  module Generators end
41
41
  end
42
42
 
43
- %w(tml/base.rb tml tml/api tml/rules_engine tml/tokens tml/tokenizers tml/decorators tml/cache_adapters tml/cache tml/cache/generators tml/ext tml/modules tml/generators).each do |f|
43
+ %w(tml/base.rb tml tml/api tml/rules_engine tml/tokens tml/tokenizers tml/decorators tml/cache_adapters tml/cache tml/ext).each do |f|
44
44
  if f.index('.rb')
45
45
  require(File.expand_path(File.join(File.dirname(__FILE__), f)))
46
46
  next
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tml
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.7
4
+ version: 5.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Berkovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-03 00:00:00.000000000 Z
11
+ date: 2016-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -50,13 +50,13 @@ files:
50
50
  - Rakefile
51
51
  - lib/tml.rb
52
52
  - lib/tml/api/client.rb
53
- - lib/tml/api/post_office.rb
54
53
  - lib/tml/application.rb
55
54
  - lib/tml/base.rb
56
55
  - lib/tml/cache.rb
57
56
  - lib/tml/cache_adapters/file.rb
58
57
  - lib/tml/cache_adapters/memcache.rb
59
58
  - lib/tml/cache_adapters/redis.rb
59
+ - lib/tml/cache_version.rb
60
60
  - lib/tml/config.rb
61
61
  - lib/tml/decorators/base.rb
62
62
  - lib/tml/decorators/default.rb
@@ -69,8 +69,6 @@ files:
69
69
  - lib/tml/ext/hash.rb
70
70
  - lib/tml/ext/string.rb
71
71
  - lib/tml/ext/time.rb
72
- - lib/tml/generators/base.rb
73
- - lib/tml/generators/file.rb
74
72
  - lib/tml/language.rb
75
73
  - lib/tml/language_case.rb
76
74
  - lib/tml/language_case_rule.rb
@@ -1,71 +0,0 @@
1
- # encoding: UTF-8
2
- #--
3
- # Copyright (c) 2016 Translation Exchange, Inc
4
- #
5
- # _______ _ _ _ ______ _
6
- # |__ __| | | | | (_) | ____| | |
7
- # | |_ __ __ _ _ __ ___| | __ _| |_ _ ___ _ __ | |__ __ _____| |__ __ _ _ __ __ _ ___
8
- # | | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \| __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
9
- # | | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ > < (__| | | | (_| | | | | (_| | __/
10
- # |_|_| \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
11
- # __/ |
12
- # |___/
13
- # Permission is hereby granted, free of charge, to any person obtaining
14
- # a copy of this software and associated documentation files (the
15
- # "Software"), to deal in the Software without restriction, including
16
- # without limitation the rights to use, copy, modify, merge, publish,
17
- # distribute, sublicense, and/or sell copies of the Software, and to
18
- # permit persons to whom the Software is furnished to do so, subject to
19
- # the following conditions:
20
- #
21
- # The above copyright notice and this permission notice shall be
22
- # included in all copies or substantial portions of the Software.
23
- #
24
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
- #++
32
-
33
- class Tml::Api::PostOffice < Tml::Api::Client
34
- POST_OFFICE_HOST = 'https://postoffice.translationexchange.com'
35
- POST_OFFICE_API_VERSION = 'v1'
36
-
37
- attributes :application
38
-
39
- def templates
40
- api('templates', {
41
- :access_token => access_token
42
- })
43
- end
44
-
45
- def channels
46
- api('channels', {
47
- :access_token => access_token
48
- })
49
- end
50
-
51
- def deliver(template, to, tokens = {}, options = {})
52
- api("templates/#{template}/deliver", {
53
- :access_token => access_token,
54
- :tokens => tokens,
55
- :to => to,
56
- :via => options[:via],
57
- :from => options[:from],
58
- :locale => options[:locale],
59
- :realtime => options[:realtime]
60
- }, options.merge(:method => :post))
61
- end
62
-
63
- def host
64
- application.tools['postoffice'] || POST_OFFICE_HOST
65
- end
66
-
67
- def prepare_api_path(path)
68
- "/api/#{POST_OFFICE_API_VERSION}#{path.first == '/' ? '' : '/'}#{path}"
69
- end
70
-
71
- end
@@ -1,122 +0,0 @@
1
- #--
2
- # Copyright (c) 2016 Translation Exchange Inc. http://translationexchange.com
3
- #
4
- # _______ _ _ _ ______ _
5
- # |__ __| | | | | (_) | ____| | |
6
- # | |_ __ __ _ _ __ ___| | __ _| |_ _ ___ _ __ | |__ __ _____| |__ __ _ _ __ __ _ ___
7
- # | | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \| __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
8
- # | | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ > < (__| | | | (_| | | | | (_| | __/
9
- # |_|_| \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
10
- # __/ |
11
- # |___/
12
- # Permission is hereby granted, free of charge, to any person obtaining
13
- # a copy of this software and associated documentation files (the
14
- # "Software"), to deal in the Software without restriction, including
15
- # without limitation the rights to use, copy, modify, merge, publish,
16
- # distribute, sublicense, and/or sell copies of the Software, and to
17
- # permit persons to whom the Software is furnished to do so, subject to
18
- # the following conditions:
19
- #
20
- # The above copyright notice and this permission notice shall be
21
- # included in all copies or substantial portions of the Software.
22
- #
23
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
- #++
31
-
32
- class Tml::Generators::Base
33
-
34
- attr_accessor :started_at, :finished_at
35
-
36
- def log(msg)
37
- msg = "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}: #{msg}"
38
- puts msg
39
- Tml.logger.debug(msg)
40
- end
41
-
42
- def cache_path
43
- @cache_path ||= begin
44
- path = Tml.config.cache[:path]
45
- path ||= 'config/tml'
46
- FileUtils.mkdir_p(path)
47
- FileUtils.chmod(0777, path)
48
- path
49
- end
50
- end
51
-
52
- def current_path
53
- "#{cache_path}/current"
54
- end
55
-
56
- def cache_version
57
- @cache_version ||= api_client.get_cache_version
58
- end
59
-
60
- def cache(key, data)
61
- raise Tml::Exception.new('Must be implemented by the subclass')
62
- end
63
-
64
- def execute
65
- raise Tml::Exception.new('Must be implemented by the subclass')
66
- end
67
-
68
- def run
69
- prepare
70
- execute
71
- finalize
72
- end
73
-
74
- def prepare
75
- @started_at = Time.now
76
- Tml.session.init
77
- end
78
-
79
- def api_client
80
- Tml.session.application.api_client
81
- end
82
-
83
- def application
84
- @application ||= api_client.get('projects/current/definition', {})
85
- end
86
-
87
- def languages
88
- application['languages']
89
- end
90
-
91
- def finalize
92
- @finished_at = Time.now
93
- log("Cache generation took #{@finished_at - @started_at} mls.")
94
- log('Done.')
95
- end
96
-
97
- def ungzip(tarfile)
98
- z = Zlib::GzipReader.new(tarfile)
99
- unzipped = StringIO.new(z.read)
100
- z.close
101
- unzipped
102
- end
103
-
104
- def untar(io, destination)
105
- Gem::Package::TarReader.new io do |tar|
106
- tar.each do |tarfile|
107
- destination_file = File.join destination, tarfile.full_name
108
-
109
- if tarfile.directory?
110
- FileUtils.mkdir_p destination_file
111
- else
112
- destination_directory = File.dirname(destination_file)
113
- FileUtils.mkdir_p destination_directory unless File.directory?(destination_directory)
114
- File.open destination_file, "wb" do |f|
115
- f.print tarfile.read
116
- end
117
- end
118
- end
119
- end
120
- end
121
-
122
- end
@@ -1,72 +0,0 @@
1
- #--
2
- # Copyright (c) 2016 Translation Exchange Inc. http://translationexchange.com
3
- #
4
- # _______ _ _ _ ______ _
5
- # |__ __| | | | | (_) | ____| | |
6
- # | |_ __ __ _ _ __ ___| | __ _| |_ _ ___ _ __ | |__ __ _____| |__ __ _ _ __ __ _ ___
7
- # | | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \| __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
8
- # | | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ > < (__| | | | (_| | | | | (_| | __/
9
- # |_|_| \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
10
- # __/ |
11
- # |___/
12
- # Permission is hereby granted, free of charge, to any person obtaining
13
- # a copy of this software and associated documentation files (the
14
- # "Software"), to deal in the Software without restriction, including
15
- # without limitation the rights to use, copy, modify, merge, publish,
16
- # distribute, sublicense, and/or sell copies of the Software, and to
17
- # permit persons to whom the Software is furnished to do so, subject to
18
- # the following conditions:
19
- #
20
- # The above copyright notice and this permission notice shall be
21
- # included in all copies or substantial portions of the Software.
22
- #
23
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
- #++
31
-
32
- require 'rubygems'
33
- require 'rubygems/package'
34
- require 'open-uri'
35
- require 'zlib'
36
- require 'fileutils'
37
-
38
- class Tml::Generators::File < Tml::Generators::Base
39
-
40
- def execute
41
- if cache_version == '0'
42
- log('No releases have been generated yet. Please visit your Dashboard and publish translations.')
43
- else
44
- log("Current cache version: #{cache_version}")
45
-
46
- archive_name = "#{cache_version}.tar.gz"
47
- path = "#{cache_path}/#{archive_name}"
48
- url = "#{api_client.application.cdn_host}/#{Tml.config.application[:key]}/#{archive_name}"
49
-
50
- log("Downloading cache file: #{url}")
51
- open(path, 'wb') do |file|
52
- file << open(url).read
53
- end
54
-
55
- log('Extracting cache file...')
56
- version_path = "#{cache_path}/#{cache_version}"
57
- untar(ungzip(File.new(path)), version_path)
58
- log("Cache has been stored in #{version_path}")
59
-
60
- File.unlink(path)
61
-
62
- begin
63
- FileUtils.rm(current_path) if File.exist?(current_path)
64
- FileUtils.ln_s(cache_version, current_path)
65
- log('The new cache path has been marked as current')
66
- rescue
67
- log('Could not generate current symlink to the cach path. Please indicate the version manually in the Tml initializer.')
68
- end
69
- end
70
- end
71
-
72
- end