crowdin-api 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in crowdin-api.gemspec
4
+ gem 'rest-client'
data/Gemfile.lock ADDED
@@ -0,0 +1,12 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ mime-types (1.19)
5
+ rest-client (1.6.7)
6
+ mime-types (>= 1.16)
7
+
8
+ PLATFORMS
9
+ ruby
10
+
11
+ DEPENDENCIES
12
+ rest-client
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Anton Maminov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # Crowdin::API
2
+
3
+ The Crowdin Ruby Client is used to interact with the Crowdin API from Ruby.
4
+ For more about the Crowdin API see <http://crowdin.net/page/api>.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```
11
+ gem 'crowdin-api'
12
+ ```
13
+
14
+ And then execute:
15
+ ```
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```
21
+ $ gem install crowdin-api
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ Start by creating a connection to Crowdin with your credentials:
27
+
28
+ ```ruby
29
+ require 'crowdin-api'
30
+
31
+ crowdin = Crowdin::API.new(:api_key => API_KEY, :project_id => PROJECT_ID, :account_key => ACCOUNT_KEY)
32
+ ```
33
+
34
+ Now you can make requests to the api.
35
+
36
+ ### Add File
37
+
38
+ Documentation: <http://crowdin.net/page/api/add-file>.
39
+
40
+ First parameter is array of files that should be added to Crowdin project.
41
+ Every file is hash:
42
+ * `:dest` - file name with path in Crowdin project (_required_)
43
+ * `:source` - uploaded file (_required_)
44
+ * `:title` - title for uploaded file (_optional_)
45
+ * `:export_pattern` - string that defines name of resulted file (_optional_)
46
+
47
+
48
+ ```ruby
49
+ crowdin.add_file(
50
+ files = [
51
+ { :dest => '/directory/array.xml', :source => 'array.xml', :export_pattern => '/values-%two_letter_code%/%original_file_name%' },
52
+ { :dest => 'strings.xml', :source => 'strings.xml', :title => 'Texts in Application' }
53
+ ], :type => 'android')
54
+ ```
55
+
56
+ ### Update File
57
+
58
+ Documentation <http://crowdin.net/page/api/update-file>
59
+
60
+ First parameter is array of files that should be updated in Crowdin project.
61
+ Every file is hash:
62
+ * `:dest` - file name with path in Crowdin project (_required_)
63
+ * `:source` - uploaded file (_required_)
64
+
65
+ ```ruby
66
+ crowdin.update_file(
67
+ files = [
68
+ { :dest => '/directory/array.xml', :source => 'array.xml' },
69
+ { :dest => 'strings.xml', :source => 'strings.xml' }
70
+ ])
71
+
72
+ ```
73
+
74
+ ### Delete File
75
+
76
+ Documentation <http://crowdin.net/page/api/delete-file>
77
+
78
+ ```ruby
79
+ crowdin.delete_file('strings.xml')
80
+ ```
81
+
82
+ ### Upload Translations
83
+
84
+ ### Translation Status
85
+
86
+ ### Project Info
87
+
88
+ ### Download Translations
89
+
90
+ Documentation: <http://crowdin.net/page/api/download>
91
+
92
+ First parameter is the language of translation you need or download `all` of them at once.
93
+
94
+ ```ruby
95
+ crowdin.download_translation('ru', :output => '/path/to/download/ru_RU.zip')
96
+ ```
97
+
98
+ ### Export Translations
99
+
100
+ ### Account Projects
101
+
102
+ ### Create Project
103
+
104
+ ### Edit Project
105
+
106
+ ### Delete Project
107
+
108
+ ### Create Directory
109
+
110
+ ### Delete Directory
111
+
112
+ ### Download Glossary
113
+
114
+ ### Upload Glossary
115
+
116
+ ### Download TM
117
+
118
+ ### Upload TM
119
+
120
+ ###Supported Languages
121
+
122
+
123
+ ## Contributing
124
+
125
+ 1. Fork it
126
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
127
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
128
+ 4. Push to the branch (`git push origin my-new-feature`)
129
+ 5. Create new Pull Request
130
+
131
+ ## License
132
+
133
+ This library is distributed under the MIT license. Please see the LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/crowdin-api/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Anton Maminov"]
6
+ gem.email = ["anton.linux@gmail.com"]
7
+ gem.description = %q{The Crowdin Ruby Client is used to interact with the Crowdin API from Ruby}
8
+ gem.summary = %q{The Crowdin Ruby Client is used to interact with the Crowdin API from Ruby}
9
+ gem.homepage = "https://github.com/mamantoha/crowdin-api"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "crowdin-api"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Crowdin::API::VERSION
17
+
18
+ gem.add_dependency('rest-client')
19
+ end
@@ -0,0 +1,83 @@
1
+ require 'logger'
2
+ require 'rexml/document'
3
+ require 'rest-client'
4
+
5
+ require "crowdin-api/errors"
6
+ require "crowdin-api/methods"
7
+ require "crowdin-api/version"
8
+
9
+ #log = Logger.new(STDOUT)
10
+ #RestClient.log = log
11
+ #log.level = Logger::DEBUG
12
+
13
+ module Crowdin
14
+ class API
15
+
16
+ def initialize(options = {})
17
+ @api_key = options.delete(:api_key)
18
+ @project_id = options.delete(:project_id)
19
+ @account_key = options.delete(:account_key)
20
+ @base_url = options.delete(:base_url) || 'http://api.crowdin.net'
21
+
22
+ options = {
23
+ :headers => {},
24
+ :params => {},
25
+ :key => @api_key,
26
+ :'account-key' => @account_key,
27
+ }.merge(options)
28
+
29
+ options[:headers] = {
30
+ 'Accept' => 'application/xml',
31
+ 'User-Agent' => "crowdin-rb/#{Crowdin::API::VERSION}",
32
+ 'X-Ruby-Version' => RUBY_VERSION,
33
+ 'X-Ruby-Platform' => RUBY_PLATFORM
34
+ }.merge(options[:headers])
35
+
36
+ options[:params] = {
37
+ :key => @api_key,
38
+ :'account-key' => @account_key,
39
+ }.merge(options[:params])
40
+
41
+ @connection = RestClient::Resource.new(@base_url, options)
42
+ end
43
+
44
+ def request(params, &block)
45
+ case params[:method]
46
+ when :post
47
+ query = @connection.options.merge(params[:query] || {})
48
+ @connection[params[:path]].post(query) { |response, _, _|
49
+ @response = response
50
+ }
51
+ when :get
52
+ query = @connection.options[:params].merge(params[:query] || {})
53
+ @connection[params[:path]].get(:params => query) { |response, _, _|
54
+ @response = response
55
+ }
56
+ end
57
+
58
+ if @response.headers[:content_disposition]
59
+ filename = params[:output] || @response.headers[:content_disposition][/attachment; filename="(.+?)"/, 1]
60
+ body = @response.body
61
+ file = open(filename, 'wb')
62
+ file.write(body)
63
+ file.close
64
+ return true
65
+ else
66
+ doc = REXML::Document.new @response.body
67
+ if doc.elements['error']
68
+ code = doc.elements['error'].elements['code'].text
69
+ message = doc.elements['error'].elements['message'].text
70
+ error = Crowdin::API::Errors::Error.new(code, message)
71
+ raise(error)
72
+ else
73
+ # leave body as is
74
+ return @response
75
+ end
76
+ end
77
+
78
+ end
79
+
80
+ private
81
+
82
+ end
83
+ end
@@ -0,0 +1,23 @@
1
+ module Crowdin
2
+ class API
3
+
4
+ module Errors
5
+ class Error < StandardError
6
+ attr_reader :error_code
7
+ attr_reader :error_message
8
+ attr_reader :message
9
+
10
+ def initialize(error_code, error_message)
11
+ @error_code = error_code.to_i
12
+ @error_message = error_message
13
+ @message = "#{error_code}: #{error_message}"
14
+ end
15
+
16
+ def to_s
17
+ @message
18
+ end
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,332 @@
1
+ module Crowdin
2
+
3
+ class API
4
+
5
+ # Add new file to Crowdin project.
6
+ #
7
+ # == Parameters
8
+ #
9
+ # files - Array of files that should be added to Crowdin project.
10
+ # file is a Hash {:dest, :source, :title, :export_pattern}
11
+ # * :dest - file name with path in Crowdin project (required)
12
+ # * :source - path for uploaded file (required)
13
+ # * :title - title in Crowdin UI (optional)
14
+ # * :export_pattern - Resulted file name (optional)
15
+ #
16
+ # == Request
17
+ #
18
+ # POST http://api.crowdin.net/api/project/{project-identifier}/add-file?key={project-key}
19
+ #
20
+ def add_file(files, params = {})
21
+ params[:files] = Hash[files.map{ |f| [
22
+ f[:dest] || raise(ArgumentError, "'`:dest`' is required"),
23
+ ::File.open(f[:source] || raise(ArgumentError, "'`:source` is required'"))
24
+ ] }]
25
+
26
+ params[:titles] = Hash[files.map{ |f| [f[:dest], f[:title]] }]
27
+ params[:titles].delete_if{ |k, v| v.nil? }
28
+
29
+ params[:export_patterns] = Hash[files.map{ |f| [f[:dest], f[:export_pattern]] }]
30
+ params[:export_patterns].delete_if{ |k, v| v.nil? }
31
+
32
+ params.delete_if{ |k, v| v.empty? }
33
+
34
+ request(
35
+ :method => :post,
36
+ :path => "/api/project/#{@project_id}/add-file",
37
+ :query => params,
38
+ )
39
+ end
40
+
41
+ # Upload fresh version of your localization file to Crowdin.
42
+ #
43
+ # == Parameters
44
+ #
45
+ # files - Array of files that should be added to Crowdin project.
46
+ # file is a Hash {:dest, :source}
47
+ # * :dest - file name with path in Crowdin project (required)
48
+ # * :source - path for uploaded file (required)
49
+ #
50
+ # == Request
51
+ #
52
+ # POST http://api.crowdin.net/api/project/{project-identifier}/update-file?key={project-key}
53
+ #
54
+ def update_file(files)
55
+ files = Hash[files.map{ |f| [
56
+ f[:dest] || raise(ArgumentError, "'`:dest` is required'"),
57
+ ::File.open(f[:source] || raise(ArgumentError, "'`:source` is required'") )
58
+ ] }]
59
+
60
+ request(
61
+ :method => :post,
62
+ :path => "/api/project/#{@project_id}/update-file",
63
+ :query => { :files => files }
64
+ )
65
+ end
66
+
67
+ # Upload existing translations to your Crowdin project.
68
+ #
69
+ # == Parameters
70
+ #
71
+ # files - Array of files that should be added to Crowdin project.
72
+ # file is a Hash {:dest, :source}
73
+ # * :dest - file name with path in Crowdin project (required)
74
+ # * :source - path for uploaded file (required)
75
+ #
76
+ # Optional:
77
+ # * :import_duplicates (default: false)
78
+ # * :import_eq_suggestions (default: false)
79
+ # * :auto_approve_imported (default: false)
80
+ #
81
+ # == Request
82
+ #
83
+ # POST http://api.crowdin.net/api/project/{project-identifier}/upload-translation?key={project-key}
84
+ #
85
+ def upload_translation(files, language, params = {})
86
+ params[:files] = Hash[files.map{ |f| [
87
+ f[:dest] || raise(ArgumentError, "`:dest` is required"),
88
+ ::File.open(f[:source] || raise(ArgumentError, "`:source` is required"))
89
+ ] }]
90
+
91
+ params[:language] = language
92
+
93
+ request(
94
+ :method => :post,
95
+ :path => "/api/project/#{@project_id}/upload-translation",
96
+ :query => params,
97
+ )
98
+ end
99
+
100
+ # Download ZIP file with translations. You can choose the language of translation you need or download all of them at once.
101
+ # Note: If you would like to download the most recent translations you may want to use export API method before downloading.
102
+ #
103
+ # == Request
104
+ #
105
+ # GET http://api.crowdin.net/api/project/{project-identifier}/download/{package}.zip?key={project-key}
106
+ #
107
+ def download_translation(language = 'all', params = {})
108
+ request(
109
+ :method => :get,
110
+ :path => "/api/project/#{@project_id}/download/#{language}.zip",
111
+ :output => params[:output],
112
+ )
113
+ end
114
+
115
+ # Upload your glossarries for Crowdin Project in TBX file format.
116
+ #
117
+ # == Request
118
+ #
119
+ # POST http://api.crowdin.net/api/project/{project-identifier}/upload-glossary?key={project-key}
120
+ #
121
+ def upload_glossary(file)
122
+ # raise "#{path} file does not exist" unless ::File.exist?(path)
123
+ file = ::File.open(file, 'rb')
124
+
125
+ request(
126
+ :method => :post,
127
+ :path => "/api/project/#{@project_id}/upload-glossary",
128
+ :query => { :file => file },
129
+ )
130
+ end
131
+
132
+ # Upload your Translation Memory for Crowdin Project in TMX file format.
133
+ #
134
+ # == Request
135
+ #
136
+ # POST http://api.crowdin.net/api/project/{project-identifier}/upload-tm?key={project-key}
137
+ #
138
+ def upload_tm(file)
139
+ file = ::File.open(file, 'rb')
140
+
141
+ request(
142
+ :method => :post,
143
+ :path => "/api/project/#{@project_id}/upload-tm",
144
+ :query => { :file => file },
145
+ )
146
+ end
147
+
148
+ # Add directory to Crowdin project.
149
+ #
150
+ # == Request
151
+ #
152
+ # POST http://api.crowdin.net/api/project/{project-identifier}/add-directory?key={project-key}
153
+ #
154
+ def add_directory(name)
155
+ request(
156
+ :method => :post,
157
+ :path => "/api/project/#{@project_id}/add-directory.json",
158
+ :query => { :name => name },
159
+ )
160
+ end
161
+
162
+ # Delete Crowdin project directory. All nested files and directories will be deleted too.
163
+ #
164
+ # == Request
165
+ #
166
+ # POST http://api.crowdin.net/api/project/{project-identifier}/delete-directory?key={project-key}
167
+ #
168
+ def delete_directory(name)
169
+ request(
170
+ :method => :post,
171
+ :path => "/api/project/#{@project_id}/delete-directory",
172
+ :query => { :name => name },
173
+ )
174
+ end
175
+
176
+
177
+ # Delete file from Crowdin project. All the translations will be lost without ability to restore them.
178
+ #
179
+ # == Request
180
+ #
181
+ # POST http://api.crowdin.net/api/project/{project-identifier}/delete-file?key={project-key}
182
+ #
183
+ def delete_file(file)
184
+ request(
185
+ :method => :post,
186
+ :path => "/api/project/#{@project_id}/delete-file",
187
+ :query => { :file => file },
188
+ )
189
+ end
190
+
191
+ # Download Crowdin project glossaries as TBX file.
192
+ #
193
+ # == Request
194
+ #
195
+ # GET http://api.crowdin.net/api/project/{project-identifier}/download-glossary?key={project-key}
196
+ #
197
+ def download_glossary(params = {})
198
+ request(
199
+ :method => :get,
200
+ :path => "/api/project/#{@project_id}/download-glossary",
201
+ :output => params[:output],
202
+ )
203
+ end
204
+
205
+ # Download Crowdin project Translation Memory as TMX file.
206
+ #
207
+ # == Request
208
+ #
209
+ # GET http://api.crowdin.net/api/project/{project-identifier}/download-tm?key={project-key}
210
+ #
211
+ def download_tm(params = {})
212
+ request(
213
+ :method => :get,
214
+ :path => "/api/project/#{@project_id}/download-tm",
215
+ :output => params[:output],
216
+ )
217
+ end
218
+
219
+ # Build ZIP archive with the latest translations. Please note that this method can be invoked only every 30 minutes. Also API call will be ignored if there were no any changes in project since last export.
220
+ #
221
+ # == Request
222
+ #
223
+ # POST http://api.crowdin.net/api/project/{project-identifier}/export?key={project-key}
224
+ #
225
+ def export_translations
226
+ request(
227
+ :method => :post,
228
+ :path => "/api/project/#{@project_id}/export",
229
+ )
230
+ end
231
+
232
+
233
+ # Get supported languages list with Crowdin codes mapped to locale name and standarded codes.
234
+ #
235
+ # == Request
236
+ #
237
+ # GET http://api.crowdin.net/api/supported-languages
238
+ #
239
+ def supported_languages(format = :xml)
240
+ request(
241
+ :method => :get,
242
+ :path => "/api/supported-languages",
243
+ :query => { format => true },
244
+ )
245
+ end
246
+
247
+ # Track your Crowdin project translation progress by language. Default response format is XML.
248
+ #
249
+ # == Request
250
+ #
251
+ # POST http://api.crowdin.net/api/project/{project-identifier}/status?key={project-key}
252
+ #
253
+ def translations_status(format = :xml)
254
+ request(
255
+ :method => :post,
256
+ :path => "/api/project/#{@project_id}/status",
257
+ :query => { format => true },
258
+ )
259
+ end
260
+
261
+ # Get Crowdin Project details.
262
+ #
263
+ # == Request
264
+ #
265
+ # POST http://api.crowdin.net/api/project/{project-identifier}/info?key={project-key}
266
+ #
267
+ def project_info(format = :json)
268
+ request(
269
+ :method => :post,
270
+ :path => "/api/project/#{@project_id}/info",
271
+ :query => { format => true },
272
+ )
273
+ end
274
+
275
+ # Create new Crowdin project.
276
+ # Important: The API method requires Account API Key. This key can not be found on your profile pages, please contact us to obtain API key for your account.
277
+ #
278
+ # == Request
279
+ #
280
+ # POST http://api.crowdin.net/api/account/create-project?account-key={account-key}
281
+ #
282
+ def create_project(params = {})
283
+ request(
284
+ :method => :post,
285
+ :path => "/api/account/create-project",
286
+ :query => params,
287
+ )
288
+ end
289
+
290
+ # Edit Crowdin project.
291
+ #
292
+ # == Request
293
+ #
294
+ # POST http://api.crowdin.net/api/project/{project-identifier}/edit-project?key={key}
295
+ #
296
+ def edit_project(params = {})
297
+ request(
298
+ :method => :post,
299
+ :path => "/api/project/#{@project_id}/edit-project",
300
+ :query => params,
301
+ )
302
+ end
303
+
304
+ # Delete Crowdin project with all translations.
305
+ #
306
+ # == Request
307
+ #
308
+ # POST http://api.crowdin.net/api/project/{project-identifier}/delete-project?key={project-key}
309
+ #
310
+ def delete_project
311
+ request(
312
+ :method => :post,
313
+ :path => "/api/project/#{@project_id}/delete-project",
314
+ )
315
+ end
316
+
317
+ # Get Crowdin Project details.
318
+ #
319
+ # == Request
320
+ #
321
+ # GET http://api.crowdin.net/api/account/get-projects?key={account-key}
322
+ #
323
+ def get_projects(login)
324
+ request(
325
+ :method => :get,
326
+ :path => "/api/account/get-projects",
327
+ :query => { :login => login },
328
+ )
329
+ end
330
+
331
+ end
332
+ end
@@ -0,0 +1,5 @@
1
+ module Crowdin
2
+ class API
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crowdin-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anton Maminov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: The Crowdin Ruby Client is used to interact with the Crowdin API from
31
+ Ruby
32
+ email:
33
+ - anton.linux@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - crowdin-api.gemspec
44
+ - lib/crowdin-api.rb
45
+ - lib/crowdin-api/errors.rb
46
+ - lib/crowdin-api/methods.rb
47
+ - lib/crowdin-api/version.rb
48
+ homepage: https://github.com/mamantoha/crowdin-api
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.24
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: The Crowdin Ruby Client is used to interact with the Crowdin API from Ruby
72
+ test_files: []