tml 5.4.8 → 5.5.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66299fedc0ad670424b3c099d816feca815f25cc
4
- data.tar.gz: a3511b6492de5957d70cf7abdc70f44b7b87491e
3
+ metadata.gz: b7bc2c8b32d5455a156eebf41a65c19b6a7cbfd9
4
+ data.tar.gz: f9ee0897ad9781e22a18dc2ba9aaf3386808e9ce
5
5
  SHA512:
6
- metadata.gz: 31bad9591d26cdeb58bf2ecb8c0720ba728f966c6f29b743665476e3ce807163bbe875262dcf9647145d9fc74b7e182099accdc1b658d67073e28592431583ad
7
- data.tar.gz: 521afb4459fa5d718f957a94f3839634f6894e00b754248b8c09592388e2fd04c0d25fc5e8738072e4fc600c81163776e00020215f5d85ce1043fb14db7511a9
6
+ metadata.gz: 47764416ebb9968bd83076755164c8189c803f9bc88d3b491ba27363b954cf1186d7b0a608e9c62b63a89d4510a9d0cd579d65d50bafef2bdf8a6df614f0e502
7
+ data.tar.gz: 334d36b8e44bde4865cf36a0eb10e7b58a2f2850990ef0ba090ac215329413d19c1369bf6efeb42f04121278f797f695ea65faeb5022f280153aba649900dde4
@@ -69,9 +69,14 @@ class Tml::Api::Client < Tml::Base
69
69
  not data['error'].nil?
70
70
  end
71
71
 
72
+ # API Host
73
+ def host
74
+ application.host
75
+ end
76
+
72
77
  # API connection
73
78
  def connection
74
- @connection ||= Faraday.new(:url => application.host) do |faraday|
79
+ @connection ||= Faraday.new(:url => host) do |faraday|
75
80
  faraday.request(:url_encoded) # form-encode POST params
76
81
  # faraday.response :logger # log requests to STDOUT
77
82
  faraday.adapter(Faraday.default_adapter) # make requests with Net::HTTP
@@ -187,6 +192,7 @@ class Tml::Api::Client < Tml::Base
187
192
  def api(path, params = {}, opts = {})
188
193
  # inline mode should always use API calls
189
194
  if live_api_request?
195
+ params = params.merge(:access_token => access_token, :app_id => application.key)
190
196
  return process_response(execute_request(path, params, opts), opts)
191
197
  end
192
198
 
@@ -249,10 +255,8 @@ class Tml::Api::Client < Tml::Base
249
255
 
250
256
  path = prepare_api_path(path)
251
257
 
252
- params = params.merge(:access_token => access_token, :app_id => application.key)
253
-
254
258
  @compressed = false
255
- trace_api_call(path, params, opts.merge(:host => application.host)) do
259
+ trace_api_call(path, params, opts.merge(:host => host)) do
256
260
  begin
257
261
  if opts[:method] == :post
258
262
  response = connection.post(path, params)
@@ -38,7 +38,7 @@ class Tml::Application < Tml::Base
38
38
  CDN_HOST = 'https://cdn.translationexchange.com'
39
39
  # CDN_HOST = 'https://trex-snapshots.s3-us-west-1.amazonaws.com'
40
40
 
41
- attributes :host, :cdn_host, :id, :key, :access_token, :name, :description, :threshold, :default_locale, :default_level
41
+ attributes :host, :cdn_host, :id, :key, :access_token, :name, :description, :threshold, :default_locale, :default_level
42
42
  has_many :features, :languages, :languages_by_locale, :sources, :tokens, :css, :shortcuts, :translations, :extensions
43
43
  has_many :ignored_keys
44
44
 
@@ -56,10 +56,12 @@ class Tml::Application < Tml::Base
56
56
  access_token
57
57
  end
58
58
 
59
+ # API host
59
60
  def host
60
61
  super || API_HOST
61
62
  end
62
63
 
64
+ # CDN host
63
65
  def cdn_host
64
66
  super || CDN_HOST
65
67
  end
data/lib/tml/config.rb CHANGED
@@ -75,7 +75,7 @@ module Tml
75
75
  # The class can be extended with a different implementation, as long as the interface is supported
76
76
  class Config
77
77
  # Configuration Attributes
78
- attr_accessor :enabled, :locale, :default_level, :format, :application, :context_rules, :logger, :cache, :default_tokens, :localization
78
+ attr_accessor :enabled, :locale, :default_level, :format, :application, :postoffice, :context_rules, :logger, :cache, :default_tokens, :localization
79
79
  attr_accessor :auto_init, :source_separator
80
80
 
81
81
  # Used by Rails and Sinatra extensions
@@ -119,6 +119,7 @@ module Tml
119
119
  @current_user_method = :current_user
120
120
 
121
121
  @application = nil
122
+ @postoffice = nil
122
123
 
123
124
  @translator_options = {
124
125
  debug: false,
@@ -0,0 +1,99 @@
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
+ module Tml
34
+
35
+ class << self
36
+ attr_accessor :postoffice
37
+ end
38
+
39
+ def self.postoffice
40
+ @postoffice ||= begin
41
+ po_config = Tml.config.postoffice || {}
42
+ Tml::Postoffice.new(
43
+ key: po_config[:key],
44
+ token: po_config[:token],
45
+ host: po_config[:host]
46
+ )
47
+ end
48
+ end
49
+
50
+ class Postoffice < Tml::Base
51
+
52
+ PO_HOST = 'https://postoffice.translationexchange.com'
53
+ API_VERSION = 'v1'
54
+
55
+ attributes :host, :key, :access_token
56
+
57
+ # API host
58
+ def host
59
+ super || PO_HOST
60
+ end
61
+
62
+ # API token
63
+ def token
64
+ access_token
65
+ end
66
+
67
+ # Postoffice API client
68
+ def deliver(to, template, tokens = {}, options = {})
69
+ if key.blank?
70
+ Tml.logger.error("Failed to deliver #{template} to #{to} - PostOffice has not been configured")
71
+ return
72
+ end
73
+
74
+ Tml.logger.debug("Delivering #{template} to #{to}")
75
+ params = {
76
+ client_id: key,
77
+ template: template,
78
+ tokens: tokens,
79
+ to: to,
80
+ via: options[:via],
81
+ from: options[:from],
82
+ locale: options[:locale],
83
+ first_name: options[:first_name],
84
+ last_name: options[:last_name],
85
+ name: options[:name]
86
+ }
87
+ params = Tml::Utils.remove_nils(params)
88
+ api_client.execute_request("#{host}/api/#{API_VERSION}/deliver", params, {:method => :post})
89
+ end
90
+
91
+ private
92
+
93
+ # Create API client
94
+ def api_client
95
+ @api_client ||= Tml.config.api_client[:class].new(application: self)
96
+ end
97
+
98
+ end
99
+ end
data/lib/tml/session.rb CHANGED
@@ -43,22 +43,29 @@ module Tml
43
43
  def init(opts = {})
44
44
  return if Tml.config.disabled?
45
45
 
46
- key = opts[:key] || Tml.config.application[:key]
47
- host = opts[:host] || Tml.config.application[:host]
48
- cdn_host = opts[:cdn_host] || Tml.config.application[:cdn_host]
49
- token = opts[:access_token] || opts[:token] || Tml.config.application[:token]
46
+ # Tml.logger.debug(opts.inspect)
50
47
 
51
48
  Tml.cache.reset_version
52
49
  Tml.cache.namespace = opts[:namespace]
53
50
 
51
+ init_application(opts)
52
+
53
+ self
54
+ end
55
+
56
+ def init_application(opts = {})
54
57
  self.current_user = opts[:user]
55
58
  self.current_source = opts[:source] || 'index'
56
59
  self.current_locale = opts[:locale]
57
60
  self.current_translator = opts[:translator]
58
61
 
59
- # Tml.logger.debug(opts.inspect)
60
-
61
- self.application = Tml::Application.new(:key => key, :access_token => token, :host => host, :cdn_host => cdn_host).fetch
62
+ app_config = Tml.config.application || {}
63
+ self.application = Tml::Application.new(
64
+ :key => opts[:key] || app_config[:key],
65
+ :access_token => opts[:access_token] || opts[:token] || app_config[:token],
66
+ :host => opts[:host] || app_config[:host],
67
+ :cdn_host => opts[:cdn_host] || app_config[:cdn_host]
68
+ ).fetch
62
69
 
63
70
  if self.current_translator
64
71
  self.current_translator.application = self.application
@@ -66,8 +73,6 @@ module Tml
66
73
 
67
74
  self.current_locale = preferred_locale(opts[:locale])
68
75
  self.current_language = self.application.current_language(self.current_locale)
69
-
70
- self
71
76
  end
72
77
 
73
78
  def preferred_locale(locales)
data/lib/tml/utils.rb CHANGED
@@ -75,6 +75,12 @@ module Tml
75
75
  hash[key.to_s] || hash[key.to_sym]
76
76
  end
77
77
 
78
+ def self.remove_nils(hash)
79
+ return unless hash
80
+ proc = Proc.new { |k, v| v.kind_of?(Hash) ? (v.delete_if(&proc); nil) : v.blank? }
81
+ hash.delete_if(&proc)
82
+ end
83
+
78
84
  def self.load_json(file_path, env = nil)
79
85
  json = JSON.parse(File.read(file_path))
80
86
  return json if env.nil?
data/lib/tml/version.rb CHANGED
@@ -31,7 +31,7 @@
31
31
  #++
32
32
 
33
33
  module Tml
34
- VERSION = '5.4.8'
34
+ VERSION = '5.5.0'
35
35
 
36
36
  def self.full_version
37
37
  "tml-ruby v#{Tml::VERSION} (Faraday v#{Faraday::VERSION})"
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.4.8
4
+ version: 5.5.0
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-05-15 00:00:00.000000000 Z
11
+ date: 2016-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -76,6 +76,7 @@ files:
76
76
  - lib/tml/language_context_rule.rb
77
77
  - lib/tml/languages/en.json
78
78
  - lib/tml/logger.rb
79
+ - lib/tml/postoffice.rb
79
80
  - lib/tml/rules_engine/evaluator.rb
80
81
  - lib/tml/rules_engine/parser.rb
81
82
  - lib/tml/session.rb