tml 5.7.5 → 5.7.9

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
  SHA256:
3
- metadata.gz: f22472b3451faaa34531f1a366b002b3c0fcb6836a6511f249428bd696b3f913
4
- data.tar.gz: d0b282b91dc07cc90aa1542daa363d643aecb1ecef8ee50e982c02a819a96844
3
+ metadata.gz: 9026636ec030714b79ad8afe340acdcbfb29eeaa3339507649dbc6f52ec69a7c
4
+ data.tar.gz: 9ef3bdeb5f1037475cebbf3a8347d65ce46b851f63208393fccb045b008a870d
5
5
  SHA512:
6
- metadata.gz: 5a7ac88acae16bf896ec80bd9b05e45843fb0975f6ba3ed8d6d7f525916930ab7efbf28cc0541413bdab9d230bff15a44357debbc32efee00cce6149b5be3050
7
- data.tar.gz: 4a5880962f6c461cafcb7c9f88343632db6eba4c870c048763e2b8277f6f8a7af3b62e83801bb6760f22bb8ccd13cc947304b7dca9b06716ff326c4e54aa0ce4
6
+ metadata.gz: d808a5cbdb34c2c7e7c0383f319458749ed85cadf3102896ed1d449ac4d6b3093b664f0ecda8413ebb643614a2fe2c7088fccde9dd5284e5d369ceef0b19d547
7
+ data.tar.gz: 86823e1dd30d4ca934231ccd42a7fda2490c3e61f01d63acbab1f29716a35cd29395df2859b64fc5812cb7892f853eb6a9ddfed7a480ef89d54c1ff82599ac20
data/README.md CHANGED
@@ -8,6 +8,7 @@ TML Library For Ruby
8
8
  [![Coverage Status](https://coveralls.io/repos/translationexchange/tml-ruby/badge.png?branch=master)](https://coveralls.io/r/translationexchange/tml-ruby?branch=master)
9
9
  [![Dependency Status](https://www.versioneye.com/user/projects/54c1457a6c00352081000416/badge.svg?style=flat)](https://www.versioneye.com/user/projects/54c1457a6c00352081000416)
10
10
  [![Gem Version](https://badge.fury.io/rb/tml.svg)](http://badge.fury.io/rb/tml)
11
+ [![Open Source Helpers](https://www.codetriage.com/translationexchange/tml-ruby/badges/users.svg)](https://www.codetriage.com/translationexchange/tml-ruby)
11
12
 
12
13
  TML library for Ruby is a set of classes that provide translation functionality for any Ruby based application.
13
14
  The library uses Translation Markup Language that allows you to encode complex language structures in simple, yet powerful forms.
@@ -69,14 +69,23 @@ class Tml::Api::Client < Tml::Base
69
69
  not data['error'].nil?
70
70
  end
71
71
 
72
+ def api_uri
73
+ @api_uri ||= URI::parse(application.host)
74
+ end
75
+
72
76
  # API Host
73
- def host
74
- application.host
77
+ def api_host
78
+ @api_host ||= begin
79
+ uri = api_uri.dup
80
+ uri.path = ''
81
+ uri.query = nil
82
+ uri.to_s
83
+ end
75
84
  end
76
85
 
77
86
  # API connection
78
87
  def connection
79
- @connection ||= Faraday.new(:url => host) do |faraday|
88
+ @connection ||= Faraday.new(:url => api_host) do |faraday|
80
89
  faraday.request(:url_encoded) # form-encode POST params
81
90
  # faraday.response :logger # log requests to STDOUT
82
91
  faraday.adapter(Faraday.default_adapter) # make requests with Net::HTTP
@@ -110,14 +119,47 @@ class Tml::Api::Client < Tml::Base
110
119
  Tml.logger.info("Cache Version: #{Tml.cache.version}")
111
120
  end
112
121
 
122
+ def cdn_host
123
+ @cdn_host ||= URI.join(application.cdn_host, '/').to_s
124
+ end
125
+
126
+ def cdn_uri
127
+ @cdn_host ||= URI::parse(application.cdn_host)
128
+ end
129
+
130
+ def get_cdn_path(key, opts = {})
131
+ base_path = URI(application.cdn_host).path
132
+ # Remove usage of String#last for clients who do not use ActiveSupport
133
+ base_path += '/' unless base_path.size > 0 && base_path[-1..-1] == '/'
134
+
135
+ adjusted_path = "#{base_path}#{application.key}/"
136
+
137
+ if key == 'version'
138
+ adjusted_path += "#{key}.json"
139
+ else
140
+ adjusted_path += "#{Tml.cache.version.to_s}/#{key}.json#{opts[:uncompressed] ? '' : '.gz'}"
141
+ end
142
+
143
+ adjusted_path
144
+ end
145
+
113
146
  # cdn_connection
114
147
  def cdn_connection
115
- @cdn_connection ||= Faraday.new(:url => application.cdn_host) do |faraday|
148
+ @cdn_connection ||= Faraday.new(:url => cdn_host) do |faraday|
116
149
  faraday.request(:url_encoded) # form-encode POST params
117
150
  faraday.adapter(Faraday.default_adapter) # make requests with Net::HTTP
118
151
  end
119
152
  end
120
153
 
154
+ def decompress_data(compressed_data)
155
+ data = Zlib::GzipReader.new(StringIO.new(compressed_data.to_s)).read
156
+ Tml.logger.debug("Compressed: #{compressed_data.length} Uncompressed: #{data.length}")
157
+ data
158
+ rescue => ex
159
+ Tml.logger.error("Failed to decompress data: #{ex.message[0..255]}")
160
+ compressed_data
161
+ end
162
+
121
163
  # get from the CDN
122
164
  def get_from_cdn(key, params = {}, opts = {})
123
165
  if Tml.cache.version.invalid? and key != 'version'
@@ -125,20 +167,14 @@ class Tml::Api::Client < Tml::Base
125
167
  end
126
168
 
127
169
  response = nil
128
- cdn_path = "/#{application.key}"
129
-
130
- if key == 'version'
131
- cdn_path += "/#{key}.json"
132
- else
133
- cdn_path += "/#{Tml.cache.version.to_s}/#{key}.json#{opts[:uncompressed] ? '' : '.gz'}"
134
- end
170
+ cdn_path = get_cdn_path(key, opts)
135
171
 
136
- trace_api_call(cdn_path, params, opts.merge(:host => application.cdn_host)) do
172
+ trace_api_call(cdn_path, params, opts.merge(:host => cdn_host)) do
137
173
  begin
138
174
  response = cdn_connection.get do |request|
139
175
  prepare_request(request, cdn_path, params, opts)
140
176
  end
141
- rescue Exception => ex
177
+ rescue => ex
142
178
  Tml.logger.error("Failed to execute request: #{ex.message[0..255]}")
143
179
  return nil
144
180
  end
@@ -146,19 +182,17 @@ class Tml::Api::Client < Tml::Base
146
182
  return if response.status >= 500 and response.status < 600
147
183
  return if response.body.nil? or response.body == '' or response.body.match(/xml/)
148
184
 
149
- compressed_data = response.body
150
- return if compressed_data.nil? or compressed_data == ''
151
-
152
- data = compressed_data
185
+ data = response.body
186
+ return if data.nil? or data == ''
153
187
 
154
188
  unless opts[:uncompressed]
155
- data = Zlib::GzipReader.new(StringIO.new(compressed_data.to_s)).read
156
- Tml.logger.debug("Compressed: #{compressed_data.length} Uncompressed: #{data.length}")
189
+ data = decompress_data(data)
157
190
  end
158
191
 
159
192
  begin
160
193
  data = JSON.parse(data)
161
194
  rescue => ex
195
+ Tml.logger.error("Failed to parse response: #{ex.message[0..255]}")
162
196
  return nil
163
197
  end
164
198
 
@@ -233,15 +267,19 @@ class Tml::Api::Client < Tml::Base
233
267
  end
234
268
  end
235
269
 
270
+ def join_uri(*string)
271
+ string.join('/').gsub(/\/+/, '/')
272
+ end
273
+
236
274
  # prepares API path
237
275
  def prepare_api_path(path)
238
276
  return path if path.match(/^https?:\/\//)
239
277
  clean_path = trim_prepending_slash(path)
240
278
 
241
279
  if clean_path.index('v1') == 0 || clean_path.index('v2') == 0
242
- "/#{clean_path}"
280
+ join_uri(api_uri.path, clean_path)
243
281
  else
244
- "#{API_PATH}/#{clean_path}"
282
+ join_uri(api_uri.path, API_PATH, clean_path)
245
283
  end
246
284
  end
247
285
 
@@ -272,7 +310,7 @@ class Tml::Api::Client < Tml::Base
272
310
 
273
311
  opts[:method] ||= :get
274
312
 
275
- trace_api_call(path, params, opts.merge(:host => host)) do
313
+ trace_api_call(path, params, opts.merge(:host => api_host)) do
276
314
  begin
277
315
  if opts[:method] == :post
278
316
  response = connection.post(path, params)
@@ -300,14 +338,14 @@ class Tml::Api::Client < Tml::Base
300
338
  raise Tml::Exception.new("Error: #{response.body}")
301
339
  end
302
340
 
341
+ data = response.body
342
+
303
343
  if opts[:method] == :get && !opts[:uncompressed]
304
- compressed_data = response.body
305
- return if compressed_data.nil? or compressed_data == ''
344
+ return if data.nil? or data == ''
306
345
 
307
- data = Zlib::GzipReader.new(StringIO.new(compressed_data.to_s)).read
308
- Tml.logger.debug("Compressed: #{compressed_data.length} Uncompressed: #{data.length}")
309
- else
310
- data = response.body
346
+ unless opts[:uncompressed]
347
+ data = decompress_data(data)
348
+ end
311
349
  end
312
350
 
313
351
  return data if opts[:raw]
data/lib/tml/version.rb CHANGED
@@ -31,7 +31,7 @@
31
31
  #++
32
32
 
33
33
  module Tml
34
- VERSION = '5.7.5'
34
+ VERSION = '5.7.9'
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.7.5
4
+ version: 5.7.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Berkovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-28 00:00:00.000000000 Z
11
+ date: 2018-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday