smartling 0.5.1

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.
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright 2012 Smartling, Inc.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
14
+
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ [Smartling Translation API](http://docs.smartling.com)
2
+ =================
3
+
4
+ This repository contains the Ruby SDK for accessing the Smartling Translation API.
5
+
6
+ The Smartling Translation API lets developers to internationalize their website or app by automating the translation and integration of their site content.
7
+ Developers can upload resource files and download the translated files in a language of their choosing. There are options to allow for professional translation, community translation and machine translation.
8
+
9
+ For a full description of the Smartling Translation API, please read the docs at: http://docs.smartling.com
10
+
11
+
12
+ Quick start
13
+ -----------
14
+
15
+ Install latest gem package from RubyGems
16
+
17
+ `gem install smartling`
18
+
19
+ Follow the [sample code](https://github.com/Smartling/api-sdk-ruby/tree/master/samples) to upload and manage your files.
20
+
21
+
22
+ Bug tracker
23
+ -----------
24
+
25
+ Have a bug? Please create an issue here on GitHub!
26
+
27
+ https://github.com/Smartling/api-sdk-ruby/issues
28
+
29
+
30
+ Hacking
31
+ -------
32
+
33
+ To get source code, clone the repo:
34
+
35
+ `git clone git@github.com:Smartling/api-sdk-ruby.git`
36
+
37
+ To contribute, fork it and follow [general GitHub guidelines](http://help.github.com/fork-a-repo/) with pull request.
38
+
39
+
40
+ Authors
41
+ -------
42
+
43
+ * [Pavel Ivashkov](https://github.com/paiv)
44
+
45
+
46
+ Copyright and license
47
+ ---------------------
48
+
49
+ Copyright 2012 Smartling, Inc.
50
+
51
+ Licensed under the Apache License, Version 2.0 (the "License");
52
+ you may not use this file except in compliance with the License.
53
+ You may obtain a copy of the License at
54
+
55
+ http://www.apache.org/licenses/LICENSE-2.0
56
+
57
+ Unless required by applicable law or agreed to in writing, software
58
+ distributed under the License is distributed on an "AS IS" BASIS,
59
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
60
+ See the License for the specific language governing permissions and
61
+ limitations under the License.
62
+
@@ -0,0 +1,108 @@
1
+ # Copyright 2012 Smartling, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'rest-client'
16
+ require 'multi_json'
17
+ require 'smartling/uri'
18
+
19
+ module Smartling
20
+
21
+ module Endpoints
22
+ V1 = 'https://api.smartling.com/v1/'
23
+ SANDBOX_V1 = 'https://sandbox-api.smartling.com/v1/'
24
+ CURRENT = V1
25
+ SANDBOX = SANDBOX_V1
26
+ end
27
+
28
+ class Api
29
+ attr_accessor :apiKey, :projectId, :baseUrl
30
+
31
+ def initialize(args = {})
32
+ @apiKey = args[:apiKey]
33
+ @projectId = args[:projectId]
34
+ @baseUrl = args[:baseUrl] || Endpoints::CURRENT
35
+ end
36
+
37
+ def self.sandbox(args = {})
38
+ new(args.merge(:baseUrl => Endpoints::SANDBOX))
39
+ end
40
+
41
+ def uri(path, params1 = nil, params2 = nil)
42
+ uri = Uri.new(@baseUrl, path)
43
+ params = { :apiKey => @apiKey, :projectId => @projectId }
44
+ params.merge!(params1) if params1
45
+ params.merge!(params2) if params2
46
+ uri.params = params
47
+ uri.require(:apiKey, :projectId)
48
+ return uri
49
+ end
50
+
51
+ def check_response(res)
52
+ return if res.code == 200
53
+ raise format_api_error(res.body)
54
+ end
55
+
56
+ def process(res)
57
+ check_response(res)
58
+ body = MultiJson.decode(res.body)
59
+ body = body['response']
60
+ raise format_api_error(res.body) unless body && body['code'] == 'SUCCESS'
61
+ return body['data']
62
+ end
63
+
64
+ def format_api_error(res)
65
+ begin
66
+ body = MultiJson.decode(res.body)
67
+ rescue
68
+ end
69
+ body = body['response'] if body
70
+ code = body['code'] if body
71
+ msg = body['messages'] if body
72
+ msg = msg.join(' -- ') if msg.is_a?(Array)
73
+ return "API error: #{code} #{msg}" if code
74
+ return res.description
75
+ end
76
+
77
+ def get(uri)
78
+ RestClient.get(uri) {|res, _, _|
79
+ process(res)
80
+ }
81
+ end
82
+ def get_raw(uri)
83
+ RestClient.get(uri) {|res, _, _|
84
+ check_response(res)
85
+ res.body
86
+ }
87
+ end
88
+ def post(uri, params = nil)
89
+ RestClient.post(uri, params) {|res, _, _|
90
+ process(res)
91
+ }
92
+ end
93
+ def delete(uri)
94
+ RestClient.delete(uri) {|res, _, _|
95
+ process(res)
96
+ }
97
+ end
98
+
99
+ def log=(v)
100
+ RestClient.log = v
101
+ end
102
+ def proxy=(v)
103
+ RestClient.proxy = v
104
+ end
105
+ end
106
+
107
+ end
108
+
@@ -0,0 +1,69 @@
1
+ # Copyright 2012 Smartling, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'smartling/api'
16
+
17
+ module Smartling
18
+
19
+ module Services
20
+ FILE_LIST = 'file/list'
21
+ FILE_STATUS = 'file/status'
22
+ FILE_GET = 'file/get'
23
+ FILE_UPLOAD = 'file/upload'
24
+ FILE_RENAME = 'file/rename'
25
+ FILE_DELETE = 'file/delete'
26
+ end
27
+
28
+ class File < Api
29
+ alias :api_delete :delete
30
+
31
+ def list(params = nil)
32
+ uri = uri(Services::FILE_LIST, params)
33
+ return get(uri.to_s)
34
+ end
35
+
36
+ def status(name, params)
37
+ keys = { :fileUri => name }
38
+ uri = uri(Services::FILE_STATUS, keys, params).require(:fileUri, :locale)
39
+ return get(uri.to_s)
40
+ end
41
+
42
+ def download(name, params = nil)
43
+ keys = { :fileUri => name }
44
+ uri = uri(Services::FILE_GET, keys, params).require(:fileUri)
45
+ return get_raw(uri.to_s)
46
+ end
47
+
48
+ def upload(file, name, type, params = nil)
49
+ keys = { :fileUri => name, :fileType => type }
50
+ uri = uri(Services::FILE_UPLOAD, keys, params).require(:fileUri, :fileType)
51
+ file = ::File.open(file, 'rb') if file.is_a?(String)
52
+ return post(uri.to_s, :file => file)
53
+ end
54
+
55
+ def rename(name, newname, params = nil)
56
+ keys = { :fileUri => name, :newFileUri => newname }
57
+ uri = uri(Services::FILE_RENAME, keys, params).require(:fileUri, :newFileUri)
58
+ return post(uri.to_s)
59
+ end
60
+
61
+ def delete(name, params = nil)
62
+ keys = { :fileUri => name }
63
+ uri = uri(Services::FILE_DELETE, keys, params).require(:fileUri)
64
+ return api_delete(uri.to_s)
65
+ end
66
+ end
67
+
68
+ end
69
+
@@ -0,0 +1,74 @@
1
+ # Copyright 2012 Smartling, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Smartling
16
+ class Uri
17
+ attr_accessor :base, :path, :params, :required
18
+
19
+ def initialize(base, path = nil)
20
+ @base = base
21
+ @path = path
22
+ @required = []
23
+ end
24
+
25
+ def require(*args)
26
+ @required += args
27
+ return self
28
+ end
29
+
30
+ def to_uri
31
+ params = @params || {}
32
+ required = @required || []
33
+ params.delete_if {|k,v| v.nil? || v.to_s.size <= 0 }
34
+ missing = required - params.keys
35
+ raise ArgumentError, "Missing parameters: " + missing.inspect if missing.size > 0
36
+
37
+ uri = URI.parse(@base)
38
+ uri.merge!(@path) if @path
39
+ if params.size > 0
40
+ uri.query = format_query(params)
41
+ end
42
+ return uri
43
+ end
44
+
45
+ def to_s
46
+ to_uri.to_s
47
+ end
48
+
49
+ def format_query(params)
50
+ # TODO: UTF-8 encode keys and values
51
+ # URI.encode_www_form(params)
52
+ params.map {|k,v|
53
+ if v.respond_to?(:to_ary)
54
+ v.to_ary.map {|w|
55
+ k.to_s + '=' + format_value(w)
56
+ }.join('&')
57
+ else
58
+ k.to_s + '=' + format_value(v)
59
+ end
60
+ }.join('&')
61
+ end
62
+
63
+ def format_value(v)
64
+ v.is_a?(Time) ? format_time(v) :
65
+ URI.escape(v.to_s)
66
+ end
67
+
68
+ def format_time(t)
69
+ t.utc.strftime('%Y-%m-%dT%H:%M:%S')
70
+ end
71
+
72
+ end
73
+ end
74
+
@@ -0,0 +1,19 @@
1
+ # Copyright 2012 Smartling, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Smartling
16
+ # This follows Semantic Versioning http://semver.org/
17
+ VERSION = '0.5.1'
18
+ end
19
+
data/lib/smartling.rb ADDED
@@ -0,0 +1,17 @@
1
+ # Copyright 2012 Smartling, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'smartling/version'
16
+ require 'smartling/fileapi'
17
+
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/ruby
2
+ # Copyright 2012 Smartling, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'rubygems'
17
+ require 'smartling'
18
+
19
+ API_KEY = 'YOUR_APIKEY'
20
+ PROJECT_ID = 'YOUR_PROJECTID'
21
+
22
+ puts 'Smartling Ruby client ' + Smartling::VERSION
23
+
24
+
25
+ # Initialize client to sandbox
26
+ sl = Smartling::File.sandbox(:apiKey => API_KEY, :projectId => PROJECT_ID)
27
+
28
+ # Initialize client for production File API
29
+ #sl = Smartling::File.new(:apiKey => API_KEY, :projectId => PROJECT_ID)
30
+
31
+ # Upload YAML file
32
+ res = sl.upload('source_file.yaml', 'name.yaml', 'YAML')
33
+ p res
34
+
35
+ # Get list of uploaded files
36
+ res = sl.list()
37
+ p res
38
+
39
+ name = 'path/file_name.yaml'
40
+ lang = 'es-ES'
41
+
42
+ # Request translation status of the file
43
+ res = sl.status(name, :locale => lang)
44
+ p res
45
+
46
+ # Download translated file in specified language
47
+ data = sl.download(name, :locale => lang)
48
+ puts data
49
+
50
+ # Rename file
51
+ newname = 'path/newname.yaml'
52
+ sl.rename(name, newname)
53
+
54
+ # Delete file
55
+ sl.delete(name)
56
+
57
+ # Extended parameters
58
+
59
+ # Upload with callback URL
60
+ res = sl.upload('source_file.yaml', 'name.yaml', 'YAML', :callbackUrl => 'http://yourdomain/someservice')
61
+ p res
62
+
63
+ # Upload with approved flag
64
+ res = sl.upload('source_file.yaml', 'name.yaml', 'YAML', :approved => true)
65
+ p res
66
+
67
+ # Filter list by mask
68
+ res = sl.list(:uriMask => '%.yaml')
69
+ p res
70
+
71
+ # Filter list by file type
72
+ res = sl.list(:fileTypes => ['yaml', 'ios'])
73
+ p res
74
+
75
+ # Order list by attribute
76
+ res = sl.list(:orderBy => ['fileUri', 'wordCount_desc'])
77
+ p res
78
+
79
+ # Page by page list
80
+ page, size = 2, 10
81
+ res = sl.list(:offset => (page - 1) * size, :limit => size)
82
+ p res
83
+
84
+ # Filter list by upload date
85
+ res = sl.list(:lastUploadedAfter => Time.utc(2012, 04, 05))
86
+ p res
87
+
88
+ # Filter list by upload date range
89
+ res = sl.list(:lastUploadedAfter => Time.utc(2012, 04, 01), :lastUploadedBefore => Time.utc(2012, 05, 01))
90
+ p res
91
+
92
+ # Filter by translation status
93
+ res = sl.list(:conditions => ['haveAllTranslated', 'haveAtLeastOneUnapproved'])
94
+ p res
95
+
96
+ # Combine multiple filter parameters in a single query
97
+ res = sl.list(:fileTypes => 'yaml', :orderBy => 'fileUri', :offset => 20, :limit => 10)
98
+ p res
99
+
data/tests/api_test.rb ADDED
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/ruby
2
+ # Copyright 2012 Smartling, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ $:.unshift File.expand_path('../', __FILE__)
17
+ require 'test_helper'
18
+
19
+ module SmartlingTests
20
+ class SmartlingApiTest < Test::Unit::TestCase
21
+
22
+ def stub_response(code, body)
23
+ status = Net::HTTPResponse.new('', code, '')
24
+ RestClient::Response.create(body, status, {})
25
+ end
26
+
27
+ def test_endpoints
28
+ sl = Smartling::Api.new()
29
+ assert_equal(Smartling::Endpoints::CURRENT, sl.baseUrl)
30
+
31
+ sl = Smartling::Api.new(:baseUrl => Smartling::Endpoints::V1)
32
+ assert_equal(Smartling::Endpoints::V1, sl.baseUrl)
33
+
34
+ sl = Smartling::Api.new(:baseUrl => Smartling::Endpoints::SANDBOX)
35
+ assert_equal(Smartling::Endpoints::SANDBOX, sl.baseUrl)
36
+
37
+ sl = Smartling::Api.sandbox()
38
+ assert_equal(Smartling::Endpoints::SANDBOX, sl.baseUrl)
39
+
40
+ sl = Smartling::Api.new(:baseUrl => 'custom')
41
+ assert_equal('custom', sl.baseUrl)
42
+ end
43
+
44
+ def test_response_format
45
+ sl = Smartling::Api.new()
46
+
47
+ res = stub_response(200, '{"response":{"code":"SUCCESS", "data":"foo"}}')
48
+ assert_equal('foo', sl.process(res))
49
+
50
+ res = stub_response(200, '{"response":{"code":"SUCCESS", "data":null}}')
51
+ res = sl.process(res)
52
+ assert(res.nil?)
53
+
54
+ end
55
+
56
+ def test_error_response
57
+ sl = Smartling::Api.new()
58
+
59
+ res = stub_response(200, '{"response":{"code":"SUCCESS", "data":"foo"}}')
60
+ assert_equal('foo', sl.process(res))
61
+
62
+ res = stub_response(200, '{"response":{"code":"ERROR", "messages":[]}}')
63
+ assert_raise RuntimeError do sl.process(res) end
64
+
65
+ res = stub_response(500, '{"response":{"code":"ERROR", "messages":[]}}')
66
+ assert_raise RuntimeError do sl.process(res) end
67
+
68
+ res = stub_response(500, '{"response":{"code":"ERROR"}}')
69
+ assert_raise RuntimeError do sl.process(res) end
70
+
71
+ res = stub_response(500, '{"response":null}')
72
+ assert_raise RuntimeError do sl.process(res) end
73
+
74
+ res = stub_response(500, '{}')
75
+ assert_raise RuntimeError do sl.process(res) end
76
+
77
+ res = stub_response(200, '{}')
78
+ assert_raise RuntimeError do sl.process(res) end
79
+ end
80
+
81
+ end
82
+ end
83
+
@@ -0,0 +1,6 @@
1
+ loglevel: 2 # DEBUG = 0, WARN = 2
2
+ server:
3
+ baseUrl: https://sandbox-api.smartling.com/v1/
4
+ apiKey: key
5
+ projectId: id
6
+
@@ -0,0 +1,175 @@
1
+ #!/usr/bin/ruby
2
+ # Copyright 2012 Smartling, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ $:.unshift File.expand_path('../', __FILE__)
17
+ require 'test_helper'
18
+ require 'iconv'
19
+
20
+ module SmartlingTests
21
+ class SmartlingFileApiTest < Test::Unit::TestCase
22
+
23
+ TEST_FILE_YAML = 'tests/upload.yaml'
24
+ TEST_FILE_UTF16 = 'tests/utf16.yaml'
25
+
26
+ def setup
27
+ @config = SmartlingTests.server_config
28
+ @log = SmartlingTests.logger
29
+ end
30
+
31
+ def yaml_file(encoding = nil)
32
+ data = <<-EOL
33
+ hello: world
34
+ we have: cookies
35
+ EOL
36
+ data = Iconv.conv(encoding, 'US-ASCII', data) if encoding
37
+ f = Tempfile.new('smartling_tests')
38
+ f.write(data)
39
+ f.flush
40
+ f.pos = 0
41
+ return f
42
+ end
43
+
44
+ def test_1_list
45
+ @log.debug '<- FileAPI:list'
46
+ sl = Smartling::File.new(@config)
47
+ res = nil
48
+ assert_nothing_raised do res = sl.list end
49
+ @log.debug res.inspect
50
+ end
51
+
52
+ def test_2_upload
53
+ @log.debug '<- FileAPI:upload'
54
+ sl = Smartling::File.new(@config)
55
+
56
+ res = nil
57
+ assert_nothing_raised do
58
+ res = sl.upload(yaml_file, TEST_FILE_YAML, 'YAML')
59
+ end
60
+ @log.debug res.inspect
61
+ end
62
+
63
+ def test_3_status
64
+ @log.debug '<- FileAPI:status'
65
+ sl = Smartling::File.new(@config)
66
+
67
+ res = nil
68
+ assert_nothing_raised do
69
+ res = sl.status(TEST_FILE_YAML, :locale => 'en-US')
70
+ end
71
+ @log.debug res.inspect
72
+ end
73
+
74
+ def test_4_download_en
75
+ @log.debug '-> FileAPI:download EN'
76
+ sl = Smartling::File.new(@config)
77
+
78
+ res = nil
79
+ assert_nothing_raised do
80
+ res = sl.download(TEST_FILE_YAML, :locale => 'en-US')
81
+ end
82
+ @log.debug res.inspect
83
+ end
84
+
85
+ def test_5_download_ru
86
+ @log.debug '-> FileAPI:download RU'
87
+ sl = Smartling::File.new(@config)
88
+
89
+ res = nil
90
+ assert_nothing_raised do
91
+ res = sl.download(TEST_FILE_YAML, :locale => 'ru-RU')
92
+ end
93
+ @log.debug res.inspect
94
+ end
95
+
96
+ def test_6_utf16
97
+ @log.debug '<- FileAPI UTF-16 upload'
98
+ sl = Smartling::File.new(@config)
99
+
100
+ file = yaml_file('UTF-16')
101
+ data = file.read
102
+ file.pos = 0
103
+
104
+ res = nil
105
+ assert_nothing_raised do
106
+ res = sl.upload(file, TEST_FILE_UTF16, 'YAML')
107
+ end
108
+ @log.debug res.inspect
109
+
110
+ @log.debug '-> FileAPI UTF-16 download EN'
111
+ assert_nothing_raised do
112
+ res = sl.download(TEST_FILE_UTF16, :locale => 'en-US')
113
+ end
114
+ @log.debug res.inspect
115
+
116
+ assert_equal(YAML.load(data), YAML.load(res))
117
+
118
+ @log.debug '-> FileAPI UTF-16 download RU'
119
+ assert_nothing_raised do
120
+ res = sl.download(TEST_FILE_UTF16, :locale => 'ru-RU')
121
+ end
122
+ @log.debug res.inspect
123
+ end
124
+
125
+ def test_7_filter
126
+ @log.debug '-> FileAPI full filter test'
127
+ sl = Smartling::File.new(@config)
128
+ res = nil
129
+ assert_nothing_raised do
130
+ res = sl.list(:locale => 'ru-RU', :uriMask => '%.yaml', :fileTypes => [:ios, :yaml],
131
+ :lastUploadedAfter => Time.now - 3600, :lastUploadedBefore => Time.now + 24*3600,
132
+ :offset => 0, :limit => 2)
133
+ end
134
+ @log.debug res.inspect
135
+ # FIXME: when sandbox adds support for filter parameters
136
+ assert(res['fileCount'] <= 2) unless sl.baseUrl == Smartling::Endpoints::SANDBOX
137
+ end
138
+
139
+ def test_8_rename
140
+ @log.debug '-> FileAPI rename file'
141
+ sl = Smartling::File.new(@config)
142
+ res = nil
143
+ assert_nothing_raised do
144
+ res = sl.rename(TEST_FILE_YAML, 'foo/foo/foo')
145
+ res = sl.rename('foo/foo/foo', TEST_FILE_YAML)
146
+ end
147
+ @log.debug res.inspect
148
+ end
149
+
150
+ def test_9_callbackurl
151
+ @log.debug '-> FileAPI upload with callback URL'
152
+ sl = Smartling::File.new(@config)
153
+ res = nil
154
+ assert_nothing_raised do
155
+ res = sl.upload(yaml_file, TEST_FILE_YAML, 'YAML', :callbackUrl => 'http://google.com/?q=hello')
156
+ end
157
+ @log.debug res.inspect
158
+ end
159
+
160
+ def test_99_delete
161
+ # NOTE: might break the tests following this and uploading with the same name
162
+ # until server actually deletes the file.
163
+ @log.debug '<- FileAPI:delete'
164
+ sl = Smartling::File.new(@config)
165
+
166
+ res = nil
167
+ assert_nothing_raised do
168
+ res = sl.delete(TEST_FILE_YAML)
169
+ end
170
+ @log.debug res.inspect
171
+ end
172
+
173
+ end
174
+ end
175
+
@@ -0,0 +1,60 @@
1
+ # Copyright 2012 Smartling, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ $:.unshift File.expand_path('../../lib', __FILE__)
16
+ require 'rubygems'
17
+ require 'logger'
18
+ require 'smartling'
19
+ require 'test/unit'
20
+ require 'yaml'
21
+
22
+ class Hash
23
+ def keysym!
24
+ keys.each {|key|
25
+ self[(key.to_sym rescue key) || key] = delete(key)
26
+ }
27
+ self
28
+ end
29
+ end
30
+
31
+ module SmartlingTests
32
+ CONFIG = 'tests/config'
33
+
34
+ class << self
35
+
36
+ def config
37
+ return @config if @config
38
+ return unless File.exists?(CONFIG)
39
+ h = YAML.load_file(CONFIG)
40
+ @config = h.keysym!
41
+ end
42
+
43
+ def server_config
44
+ cfg = config() or raise 'Missing config file for server tests'
45
+ h = cfg[:server]
46
+ h.keysym!
47
+ end
48
+
49
+ def logger
50
+ l = Logger.new($stderr)
51
+ l.level = config[:loglevel]
52
+ l.formatter = proc {|sev, dt, prg, msg|
53
+ "#{sev}: #{msg}\n"
54
+ }
55
+ return l
56
+ end
57
+
58
+ end
59
+ end
60
+
data/tests/uri_test.rb ADDED
@@ -0,0 +1,92 @@
1
+ #!/usr/bin/ruby
2
+ # Copyright 2012 Smartling, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ $:.unshift File.expand_path('../', __FILE__)
17
+ require 'test_helper'
18
+
19
+ module SmartlingTests
20
+ class SmartlingUriTest < Test::Unit::TestCase
21
+
22
+ def test_base
23
+ uri = Smartling::Uri.new('http://hello.wo/')
24
+ assert_equal('http://hello.wo/', uri.to_s)
25
+
26
+ uri = Smartling::Uri.new('http://hello.wo/', 'foo/bar')
27
+ assert_equal('http://hello.wo/foo/bar', uri.to_s)
28
+ end
29
+
30
+ def test_params
31
+ uri = Smartling::Uri.new('http://hello.wo/')
32
+ uri.params = {:foo => 'x'}
33
+ assert_equal('http://hello.wo/?foo=x', uri.to_s)
34
+
35
+ uri = Smartling::Uri.new('http://hello.wo/')
36
+ uri.params = {:foo => 18}
37
+ assert_equal('http://hello.wo/?foo=18', uri.to_s)
38
+
39
+ uri = Smartling::Uri.new('http://hello.wo/')
40
+ uri.params = {:foo => true}
41
+ assert_equal('http://hello.wo/?foo=true', uri.to_s)
42
+
43
+ uri = Smartling::Uri.new('http://hello.wo/')
44
+ uri.params = {:foo => Time.utc(2012, 04, 05, 11, 19, 59)}
45
+ assert_equal('http://hello.wo/?foo=2012-04-05T11:19:59', uri.to_s)
46
+
47
+ uri = Smartling::Uri.new('http://hello.wo/')
48
+ uri.params = {:foo => Time.utc(2012, 04, 05, 11, 49, 17).localtime}
49
+ assert_equal('http://hello.wo/?foo=2012-04-05T11:49:17', uri.to_s)
50
+
51
+ uri = Smartling::Uri.new('http://hello.wo/')
52
+ uri.params = {:foo => ['hello']}
53
+ assert_equal('http://hello.wo/?foo=hello', uri.to_s)
54
+
55
+ uri = Smartling::Uri.new('http://hello.wo/')
56
+ uri.params = {:foo => ['hello', 'world']}
57
+ assert_equal('http://hello.wo/?foo=hello&foo=world', uri.to_s)
58
+
59
+ uri = Smartling::Uri.new('http://hello.wo/')
60
+ uri.params = {:foo => ['hello', 'world', 'of', Time.utc(2012)]}
61
+ assert_equal('http://hello.wo/?foo=hello&foo=world&foo=of&foo=2012-01-01T00:00:00', uri.to_s)
62
+ end
63
+
64
+ def test_require
65
+ uri = Smartling::Uri.new('http://hello.wo/')
66
+ uri.require(:foo, :bar)
67
+ assert_raise ArgumentError do uri.to_s end
68
+
69
+ uri = Smartling::Uri.new('http://hello.wo/')
70
+ uri.params = {:foo => 'x', :bar => 'y'}
71
+ uri.require(:foo, :bar)
72
+ assert_nothing_raised do uri.to_s end
73
+
74
+ uri = Smartling::Uri.new('http://hello.wo/')
75
+ uri.params = {:foo => 'x', :baz => 'z'}
76
+ uri.require(:foo, :bar)
77
+ assert_raise ArgumentError do uri.to_s end
78
+
79
+ uri = Smartling::Uri.new('http://hello.wo/')
80
+ uri.params = {:bar => 'y', :baz => 'z', :foo => 'x'}
81
+ uri.require(:foo, :baz)
82
+ assert_nothing_raised do uri.to_s end
83
+
84
+ uri = Smartling::Uri.new('http://hello.wo/')
85
+ uri.require()
86
+ assert_nothing_raised do uri.to_s end
87
+ assert_equal('http://hello.wo/', uri.to_s)
88
+ end
89
+
90
+ end
91
+ end
92
+
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smartling
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pavel Ivashkov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: multi_json
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.0'
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ none: false
28
+ prerelease: false
29
+ type: :runtime
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: '1.6'
37
+ none: false
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '1.6'
43
+ none: false
44
+ prerelease: false
45
+ type: :runtime
46
+ description: A Ruby library to utilize Smartling services
47
+ email:
48
+ - hi@smartling.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - README.md
54
+ - LICENSE
55
+ - lib/smartling.rb
56
+ - lib/smartling/api.rb
57
+ - lib/smartling/fileapi.rb
58
+ - lib/smartling/uri.rb
59
+ - lib/smartling/version.rb
60
+ - samples/file_api.rb
61
+ - tests/api_test.rb
62
+ - tests/config.sample
63
+ - tests/srv_fileapi_test.rb
64
+ - tests/test_helper.rb
65
+ - tests/uri_test.rb
66
+ homepage: https://docs.smartling.com
67
+ licenses:
68
+ - LICENSE
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.6
78
+ none: false
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: !binary |-
84
+ MA==
85
+ none: false
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.24
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Smartling SDK
92
+ test_files:
93
+ - tests/api_test.rb
94
+ - tests/config.sample
95
+ - tests/srv_fileapi_test.rb
96
+ - tests/test_helper.rb
97
+ - tests/uri_test.rb