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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c93181c73e8d51d0547614c850cd4f1902397a5
4
- data.tar.gz: 8388206c002b497ee3210f6cc07860374c0aaeee
3
+ metadata.gz: b305529c8b1fefe33c3d8ba0fd445566a6f7667a
4
+ data.tar.gz: 1fe8c90ddcd2d26edc1b7e70894085cf3d8e6470
5
5
  SHA512:
6
- metadata.gz: e6f3cb0e93280f4fba083a262f54996e464712ca57caf597a22a46b361315768b0f6f32e20d2a827c64a832e1170de167738985dece40d88317b9aecb150c02a
7
- data.tar.gz: 977bfea69f9e79248688fab4b9b48d62d803c8931cac356a32e6471c0bf55cf46b7e8a39ac3780066bb688255e39eec1f20fbb5e4501d0b60e8f6f21330d02d1
6
+ metadata.gz: 1f8535261a61e58a53fa9bd5e6101ec13b80819adb79a58b89eb4c916fb0d58fa7555b5e359222b6999542b608ad4d638a38fa4a9a731b28993e0163ad842aeb
7
+ data.tar.gz: 6c7af851ad4066fa6701704ba3ee06e36b3678d195e30acccbdbbb8cc933fef989c5488752b1fc3e7dd7ea82df660a531e1cee92402c6e495ed602a089be5c8f
@@ -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? ? {} : execute_request(path, params, opts)
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
- Tml.logger.trace_api_call(path, params, opts) do
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
@@ -33,7 +33,7 @@
33
33
  require 'faraday'
34
34
 
35
35
  class Tml::Application < Tml::Base
36
- attributes :host, :id, :key, :secret, :access_token, :name, :description, :threshold, :default_locale, :default_level, :tools
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
- cache_host, cache_port = Tml.config.cache[:host].split(':') if Tml.config.cache[:host]
40
- cache_host ||= 'localhost'
41
- cache_port ||= 6379
39
+ config = Tml.config.cache
42
40
 
43
- @cache = ::Redis.new(host: cache_host, port: cache_port)
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
- logfile_path = File.expand_path(Tml.config.logger[:path])
40
- logfile_dir = logfile_path.split("/")[0..-2].join("/")
41
- FileUtils.mkdir_p(logfile_dir) unless File.exist?(logfile_dir)
42
- logfile = File.open(logfile_path, 'a')
43
- logfile.sync = true
44
- Tml::Logger.new(logfile)
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
- key = opts[:key] || Tml.config.application[:key]
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, :key => key, :secret => secret, :access_token => Tml::Session.access_token).fetch
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.decode_and_verify_params(opts[:cookies][cookie_name], secret))
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] || '/tml/core'
95
+ self.current_source = opts[:source] || 'index'
93
96
  self.current_component = opts[:component]
94
- self.current_locale = opts[:locale] || self.cookie_params[:locale] || Tml.config.default_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.language(self.current_locale)
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
@@ -89,6 +89,7 @@ class Tml::Source < Tml::Base
89
89
 
90
90
  self
91
91
  rescue Tml::Exception => ex
92
+ self.translations = {}
92
93
  self
93
94
  end
94
95
 
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.sign_and_encode_params(params, secret)
95
- URI::encode(Base64.encode64(params.to_json))
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
@@ -30,5 +30,5 @@
30
30
  #++
31
31
 
32
32
  module Tml
33
- VERSION = '4.3.4'
33
+ VERSION = '4.3.6'
34
34
  end
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
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-03 00:00:00.000000000 Z
11
+ date: 2015-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday