tml 4.3.4 → 4.3.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tml/api/client.rb +32 -2
- data/lib/tml/application.rb +9 -1
- data/lib/tml/cache.rb +2 -2
- data/lib/tml/cache_adapters/redis.rb +11 -5
- data/lib/tml/logger.rb +14 -34
- data/lib/tml/session.rb +15 -9
- data/lib/tml/source.rb +1 -0
- data/lib/tml/utils.rb +9 -6
- data/lib/tml/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b305529c8b1fefe33c3d8ba0fd445566a6f7667a
|
4
|
+
data.tar.gz: 1fe8c90ddcd2d26edc1b7e70894085cf3d8e6470
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f8535261a61e58a53fa9bd5e6101ec13b80819adb79a58b89eb4c916fb0d58fa7555b5e359222b6999542b608ad4d638a38fa4a9a731b28993e0163ad842aeb
|
7
|
+
data.tar.gz: 6c7af851ad4066fa6701704ba3ee06e36b3678d195e30acccbdbbb8cc933fef989c5488752b1fc3e7dd7ea82df660a531e1cee92402c6e495ed602a089be5c8f
|
data/lib/tml/api/client.rb
CHANGED
@@ -97,7 +97,7 @@ class Tml::Api::Client < Tml::Base
|
|
97
97
|
|
98
98
|
if opts[:method] == :get and opts[:cache_key]
|
99
99
|
data = Tml.cache.fetch(opts[:cache_key]) do
|
100
|
-
Tml.cache.read_only? ?
|
100
|
+
Tml.cache.read_only? ? nil : execute_request(path, params, opts)
|
101
101
|
end
|
102
102
|
process_response(data, opts)
|
103
103
|
else
|
@@ -143,7 +143,7 @@ class Tml::Api::Client < Tml::Base
|
|
143
143
|
params = params.merge(:api_key => application.key)
|
144
144
|
end
|
145
145
|
|
146
|
-
|
146
|
+
trace_api_call(path, params, opts) do
|
147
147
|
begin
|
148
148
|
if opts[:method] == :post
|
149
149
|
response = connection.post(path, params)
|
@@ -187,6 +187,7 @@ class Tml::Api::Client < Tml::Base
|
|
187
187
|
end
|
188
188
|
|
189
189
|
def process_response(data, opts)
|
190
|
+
return nil if data.nil?
|
190
191
|
return data if opts['raw']
|
191
192
|
|
192
193
|
if data.is_a?(Hash) and data['results']
|
@@ -203,4 +204,33 @@ class Tml::Api::Client < Tml::Base
|
|
203
204
|
object_class(opts).new(data.merge(opts[:attributes] || {}))
|
204
205
|
end
|
205
206
|
|
207
|
+
def to_query(hash)
|
208
|
+
query = []
|
209
|
+
hash.each do |key, value|
|
210
|
+
query << "#{key}=#{value}"
|
211
|
+
end
|
212
|
+
query.join('&')
|
213
|
+
end
|
214
|
+
|
215
|
+
def trace_api_call(path, params, opts = {})
|
216
|
+
#[:client_secret, :access_token].each do |param|
|
217
|
+
# params = params.merge(param => "##filtered##") if params[param]
|
218
|
+
#end
|
219
|
+
|
220
|
+
if opts[:method] == :post
|
221
|
+
Tml.logger.debug("post: [#{path}] #{params.inspect}")
|
222
|
+
else
|
223
|
+
Tml.logger.debug("get: #{path}?#{to_query(params)}")
|
224
|
+
end
|
225
|
+
|
226
|
+
t0 = Time.now
|
227
|
+
if block_given?
|
228
|
+
ret = yield
|
229
|
+
end
|
230
|
+
t1 = Time.now
|
231
|
+
|
232
|
+
Tml.logger.debug("call took #{t1 - t0} seconds")
|
233
|
+
ret
|
234
|
+
end
|
235
|
+
|
206
236
|
end
|
data/lib/tml/application.rb
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
require 'faraday'
|
34
34
|
|
35
35
|
class Tml::Application < Tml::Base
|
36
|
-
attributes :host, :id, :key, :
|
36
|
+
attributes :host, :id, :key, :access_token, :name, :description, :threshold, :default_locale, :default_level, :tools
|
37
37
|
has_many :features, :languages, :featured_locales, :sources, :components, :tokens, :css, :shortcuts, :translations
|
38
38
|
|
39
39
|
def self.cache_key
|
@@ -81,6 +81,14 @@ class Tml::Application < Tml::Base
|
|
81
81
|
@languages_by_locale[locale] = Tml.config.default_language
|
82
82
|
end
|
83
83
|
|
84
|
+
def current_language(locale)
|
85
|
+
locale = locale.gsub('_', '-')
|
86
|
+
lang = language(locale)
|
87
|
+
lang ||= language(locale.split('-').first) if locale.index('-')
|
88
|
+
lang ||= Tml.config.default_language
|
89
|
+
lang
|
90
|
+
end
|
91
|
+
|
84
92
|
# Mostly used for testing
|
85
93
|
def add_language(new_language)
|
86
94
|
@languages_by_locale ||= {}
|
data/lib/tml/cache.rb
CHANGED
@@ -41,7 +41,7 @@ module Tml
|
|
41
41
|
def self.cache
|
42
42
|
@cache ||= begin
|
43
43
|
if Tml.config.cache_enabled?
|
44
|
-
klass = Tml::CacheAdapters.const_get(Tml.config.cache[:adapter].camelcase)
|
44
|
+
klass = Tml::CacheAdapters.const_get(Tml.config.cache[:adapter].to_s.camelcase)
|
45
45
|
klass.new
|
46
46
|
else
|
47
47
|
# blank implementation
|
@@ -77,7 +77,7 @@ module Tml
|
|
77
77
|
end
|
78
78
|
|
79
79
|
def upgrade_version
|
80
|
-
update_version(version + 1)
|
80
|
+
update_version((version || Tml.config.cache[:version] || 0).to_i + 1)
|
81
81
|
@version = nil
|
82
82
|
end
|
83
83
|
|
@@ -36,11 +36,18 @@ require 'redis' if defined?(::Redis)
|
|
36
36
|
class Tml::CacheAdapters::Redis < Tml::Cache
|
37
37
|
|
38
38
|
def initialize
|
39
|
-
|
40
|
-
cache_host ||= 'localhost'
|
41
|
-
cache_port ||= 6379
|
39
|
+
config = Tml.config.cache
|
42
40
|
|
43
|
-
|
41
|
+
config[:host] ||= 'localhost'
|
42
|
+
config[:port] ||= 6379
|
43
|
+
|
44
|
+
if config[:host].index(':')
|
45
|
+
parts = config[:host].split(':')
|
46
|
+
config[:host] = parts.first
|
47
|
+
config[:port] = parts.last
|
48
|
+
end
|
49
|
+
|
50
|
+
@cache = ::Redis.new(config)
|
44
51
|
end
|
45
52
|
|
46
53
|
def cache_name
|
@@ -74,7 +81,6 @@ class Tml::CacheAdapters::Redis < Tml::Cache
|
|
74
81
|
data
|
75
82
|
rescue Exception => ex
|
76
83
|
warn("Failed to retrieve data: #{ex.message}")
|
77
|
-
pp ex, ex.backtrace
|
78
84
|
return nil unless block_given?
|
79
85
|
yield
|
80
86
|
end
|
data/lib/tml/logger.rb
CHANGED
@@ -36,15 +36,23 @@ module Tml
|
|
36
36
|
|
37
37
|
def self.logger
|
38
38
|
@logger ||= begin
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
39
|
+
if Tml.config.logger[:type].to_s == 'rails'
|
40
|
+
Rails.logger
|
41
|
+
else
|
42
|
+
logfile_path = File.expand_path(Tml.config.logger[:path] || './log/tml.log')
|
43
|
+
logfile_dir = logfile_path.split("/")[0..-2].join("/")
|
44
|
+
FileUtils.mkdir_p(logfile_dir) unless File.exist?(logfile_dir)
|
45
|
+
logfile = File.open(logfile_path, 'a')
|
46
|
+
logfile.sync = true
|
47
|
+
Tml::Logger.new(logfile)
|
48
|
+
end
|
45
49
|
end
|
46
50
|
end
|
47
51
|
|
52
|
+
def self.logger=(logger)
|
53
|
+
@logger = logger
|
54
|
+
end
|
55
|
+
|
48
56
|
class Logger < ::Logger
|
49
57
|
|
50
58
|
def format_message(severity, timestamp, progname, msg)
|
@@ -62,34 +70,6 @@ module Tml
|
|
62
70
|
@stack ||= []
|
63
71
|
end
|
64
72
|
|
65
|
-
def trace_api_call(path, params, opts = {})
|
66
|
-
#[:client_secret, :access_token].each do |param|
|
67
|
-
# params = params.merge(param => "##filtered##") if params[param]
|
68
|
-
#end
|
69
|
-
if opts[:method] == :post
|
70
|
-
debug("post: [#{path}] #{params.inspect}")
|
71
|
-
else
|
72
|
-
debug("get: #{path}?#{to_query(params)}")
|
73
|
-
end
|
74
|
-
stack.push(caller)
|
75
|
-
t0 = Time.now
|
76
|
-
if block_given?
|
77
|
-
ret = yield
|
78
|
-
end
|
79
|
-
t1 = Time.now
|
80
|
-
stack.pop
|
81
|
-
debug("call took #{t1 - t0} seconds")
|
82
|
-
ret
|
83
|
-
end
|
84
|
-
|
85
|
-
def to_query(hash)
|
86
|
-
query = []
|
87
|
-
hash.each do |key, value|
|
88
|
-
query << "#{key}=#{value}"
|
89
|
-
end
|
90
|
-
query.join('&')
|
91
|
-
end
|
92
|
-
|
93
73
|
def trace(message)
|
94
74
|
debug(message)
|
95
75
|
stack.push(caller)
|
data/lib/tml/session.rb
CHANGED
@@ -49,12 +49,14 @@ module Tml
|
|
49
49
|
@access_token = token
|
50
50
|
end
|
51
51
|
|
52
|
+
def cookie_name
|
53
|
+
"trex_#{self.application.key}"
|
54
|
+
end
|
55
|
+
|
52
56
|
def init(opts = {})
|
53
57
|
return unless Tml.config.enabled? and Tml.config.application
|
54
58
|
|
55
|
-
|
56
|
-
secret = opts[:secret] || Tml.config.application[:secret]
|
57
|
-
host = opts[:host] || Tml.config.application[:host]
|
59
|
+
host = opts[:host] || Tml.config.application[:host]
|
58
60
|
|
59
61
|
Tml::Session.access_token ||= begin
|
60
62
|
self.access_token = opts[:token] || Tml.config.application[:token]
|
@@ -64,7 +66,7 @@ module Tml
|
|
64
66
|
Tml.cache.reset_version
|
65
67
|
|
66
68
|
self.application = Tml.memory.fetch(Tml::Application.cache_key) do
|
67
|
-
Tml::Application.new(:host => host, :
|
69
|
+
Tml::Application.new(:host => host, :access_token => Tml::Session.access_token).fetch
|
68
70
|
end
|
69
71
|
|
70
72
|
if Tml.cache.read_only?
|
@@ -74,10 +76,11 @@ module Tml
|
|
74
76
|
# Tml.logger.info(self.cookie_params.inspect)
|
75
77
|
|
76
78
|
self.cookie_params = begin
|
77
|
-
cookie_name = "trex_#{self.application.key}"
|
78
79
|
if opts[:cookies] and opts[:cookies][cookie_name]
|
79
80
|
begin
|
80
|
-
HashWithIndifferentAccess.new(Tml::Utils.
|
81
|
+
params = HashWithIndifferentAccess.new(Tml::Utils.decode(opts[:cookies][cookie_name]))
|
82
|
+
params[:locale] = opts[:locale] if opts[:change_locale]
|
83
|
+
params
|
81
84
|
rescue Exception => ex
|
82
85
|
Tml.logger.error("Failed to parse tml cookie: #{ex.message}")
|
83
86
|
{}
|
@@ -89,9 +92,9 @@ module Tml
|
|
89
92
|
|
90
93
|
self.tools_enabled = opts[:tools_enabled]
|
91
94
|
self.current_user = opts[:user]
|
92
|
-
self.current_source = opts[:source] || '
|
95
|
+
self.current_source = opts[:source] || 'index'
|
93
96
|
self.current_component = opts[:component]
|
94
|
-
self.current_locale =
|
97
|
+
self.current_locale = self.cookie_params[:locale] || opts[:locale] || Tml.config.default_locale
|
95
98
|
|
96
99
|
if self.cookie_params['translator']
|
97
100
|
self.current_translator = Tml::Translator.new(self.cookie_params['translator'])
|
@@ -107,7 +110,10 @@ module Tml
|
|
107
110
|
self.current_translator.application = self.application
|
108
111
|
end
|
109
112
|
|
110
|
-
self.current_language = self.application.
|
113
|
+
self.current_language = self.application.current_language(self.current_locale)
|
114
|
+
self.current_locale = self.current_language.locale
|
115
|
+
|
116
|
+
self
|
111
117
|
end
|
112
118
|
|
113
119
|
def tools_enabled?
|
data/lib/tml/source.rb
CHANGED
data/lib/tml/utils.rb
CHANGED
@@ -91,18 +91,21 @@ module Tml
|
|
91
91
|
yaml['defaults'].rmerge(yaml[env] || {})
|
92
92
|
end
|
93
93
|
|
94
|
-
def self.
|
95
|
-
URI::
|
96
|
-
end
|
97
|
-
|
98
|
-
def self.decode_and_verify_params(signed_request, secret)
|
99
|
-
payload = URI::decode(signed_request)
|
94
|
+
def self.decode(data)
|
95
|
+
payload = URI::decode(data)
|
100
96
|
payload = Base64.decode64(payload)
|
101
97
|
JSON.parse(payload)
|
102
98
|
rescue Exception => ex
|
103
99
|
{}
|
104
100
|
end
|
105
101
|
|
102
|
+
def self.encode(params)
|
103
|
+
payload = Base64.encode64(params.to_json)
|
104
|
+
URI::encode(payload)
|
105
|
+
rescue Exception => ex
|
106
|
+
''
|
107
|
+
end
|
108
|
+
|
106
109
|
def self.split_sentences(paragraph)
|
107
110
|
sentence_regex = /[^.!?\s][^.!?]*(?:[.!?](?![\'"]?\s|$)[^.!?]*)*[.!?]?[\'"]?(?=\s|$)/
|
108
111
|
paragraph.match(sentence_regex)
|
data/lib/tml/version.rb
CHANGED
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: 4.3.
|
4
|
+
version: 4.3.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Berkovich
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|