Class: Crowdin::API

Inherits:
Object
  • Object
show all
Defined in:
lib/crowdin-api/methods.rb,
lib/crowdin-api.rb,
lib/crowdin-api/errors.rb,
lib/crowdin-api/version.rb

Overview

A wrapper and interface to the Crowdin api. Please visit the Crowdin developers site for a full explaination of what each of the Crowdin api methods expect and perform.

crowdin.com/page/api

Defined Under Namespace

Modules: Errors

Constant Summary collapse

VERSION =
"0.5.0"

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Create a new API object using the given parameters.


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/crowdin-api.rb', line 38

def initialize(options = {})
  @api_key     = options.delete(:api_key)
  @project_id  = options.delete(:project_id)
  @account_key = options.delete(:account_key)
  @base_url    = options.delete(:base_url) || 'https://api.crowdin.com'

  @log = nil

  options = {
    :headers                => {},
    :params                 => {},
    :timeout                => nil,
    :key                    => @api_key,
    :'account-key'          => @account_key,
    :json                   => true
  }.merge(options)

  options[:headers] = {
    'Accept'                => 'application/json',
    'User-Agent'            => "crowdin-rb/#{Crowdin::API::VERSION}",
    'X-Ruby-Version'        => RUBY_VERSION,
    'X-Ruby-Platform'       => RUBY_PLATFORM
  }.merge(options[:headers])

  options[:params] = {
    :key                    => @api_key,
    :'account-key'          => @account_key,
    :json                   => true
  }.merge(options[:params])

  RestClient.proxy = ENV['http_proxy'] if ENV['http_proxy']
  @connection = RestClient::Resource.new(@base_url, options)
end

Class Attribute Details

.logObject

Default logger for all Crowdin::API instances

Crowdin::API.log = Logger.new($stderr)

28
29
30
# File 'lib/crowdin-api.rb', line 28

def log
  @log
end

Instance Method Details

#add_directory(name, params = {}) ⇒ Object

Add directory to Crowdin project.

Parameters

name - directory name (with path if nested directory should be created). (required)

Optional:

  • :is_branch - create new branch. Valid values - 0, 1. Only when create root directory.

  • :branch - a branch name.

Request

POST


201
202
203
204
205
206
207
208
209
# File 'lib/crowdin-api/methods.rb', line 201

def add_directory(name, params = {})
  params[:name] = name

  request(
    :method => :post,
    :path   => "/api/project/#{@project_id}/add-directory",
    :query  => params,
  )
end

#add_file(files, params = {}) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/crowdin-api/methods.rb', line 33

def add_file(files, params = {})
  params[:files] = Hash[files.map { |f| [
    f[:dest]               || raise(ArgumentError, "'`:dest`' is required"),
    ::File.open(f[:source] || raise(ArgumentError, "'`:source` is required'"))
  ] }]

  params[:titles] = Hash[files.map { |f| [f[:dest], f[:title]] }]
  params[:titles].delete_if { |_, v| v.nil? }

  params[:export_patterns] = Hash[files.map { |f| [f[:dest], f[:export_pattern]] }]
  params[:export_patterns].delete_if { |_, v| v.nil? }

  params.delete_if { |_, v| v.respond_to?(:empty?) ? !!v.empty? : !v }

  request(
    :method => :post,
    :path   => "/api/project/#{@project_id}/add-file",
    :query  => params,
  )
end

#change_directory(name, params = {}) ⇒ Object


256
257
258
259
260
261
262
263
264
# File 'lib/crowdin-api/methods.rb', line 256

def change_directory(name, params = {})
  params[:name] = name

  request(
    :method => :post,
    :path   => "/api/project/#{@project_id}/change-directory",
    :query  => params,
  )
end

#create_project(params = {}) ⇒ Object


376
377
378
379
380
381
382
# File 'lib/crowdin-api/methods.rb', line 376

def create_project(params = {})
  request(
    :method => :post,
    :path   => "/api/account/create-project",
    :query  => params,
  )
end

#delete_directory(name, params = {}) ⇒ Object

#delete_file(file) ⇒ Object


273
274
275
276
277
278
279
# File 'lib/crowdin-api/methods.rb', line 273

def delete_file(file)
  request(
    :method => :post,
    :path   => "/api/project/#{@project_id}/delete-file",
    :query  => { :file => file },
  )
end

#delete_projectObject


404
405
406
407
408
409
# File 'lib/crowdin-api/methods.rb', line 404

def delete_project
  request(
    :method => :post,
    :path   => "/api/project/#{@project_id}/delete-project",
  )
end

#download_glossary(params = {}) ⇒ Object


287
288
289
290
291
292
293
# File 'lib/crowdin-api/methods.rb', line 287

def download_glossary(params = {})
  request(
    :method => :get,
    :path   => "/api/project/#{@project_id}/download-glossary",
    :output => params[:output],
  )
end

#download_tm(params = {}) ⇒ Object


301
302
303
304
305
306
307
# File 'lib/crowdin-api/methods.rb', line 301

def download_tm(params = {})
  request(
    :method => :get,
    :path   => "/api/project/#{@project_id}/download-tm",
    :output => params[:output],
  )
end

#download_translation(language = 'all', params = {}) ⇒ Object


145
146
147
148
149
150
151
152
# File 'lib/crowdin-api/methods.rb', line 145

def download_translation(language = 'all', params = {})
  request(
    :method  => :get,
    :path    => "/api/project/#{@project_id}/download/#{language}.zip",
    :output  => params.delete(:output),
    :query   => params,
  )
end

#edit_project(params = {}) ⇒ Object


390
391
392
393
394
395
396
# File 'lib/crowdin-api/methods.rb', line 390

def edit_project(params = {})
  request(
    :method => :post,
    :path   => "/api/project/#{@project_id}/edit-project",
    :query  => params,
  )
end

#export_translations(params = {}) ⇒ Object


321
322
323
324
325
326
327
# File 'lib/crowdin-api/methods.rb', line 321

def export_translations(params = {})
  request(
    :method => :get,
    :path   => "/api/project/#{@project_id}/export",
    :query  => params,
  )
end

#get_projects(login) ⇒ Object


418
419
420
421
422
423
424
# File 'lib/crowdin-api/methods.rb', line 418

def get_projects()
  request(
    :method => :get,
    :path   => "/api/account/get-projects",
    :query  => { :login =>  },
  )
end

#logObject

The current logger. If no logger has been set Crowdin::API.log is used.


116
117
118
# File 'lib/crowdin-api.rb', line 116

def log
  @log || Crowdin::API.log
end

#log=(logger) ⇒ Object

Sets the logger used by this instance of Crowdin::API


122
123
124
# File 'lib/crowdin-api.rb', line 122

def log= logger
  @log = logger
end

#project_infoObject


362
363
364
365
366
367
# File 'lib/crowdin-api/methods.rb', line 362

def project_info
  request(
    :method => :post,
    :path   => "/api/project/#{@project_id}/info",
  )
end

#request(params, &block) ⇒ Object


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/crowdin-api.rb', line 72

def request(params, &block)
  # Returns a query hash with non nil values.
  params[:query].reject! { |_, value| value.nil? } if params[:query]

  case params[:method]
  when :post
    query = @connection.options.merge(params[:query] || {})
    @connection[params[:path]].post(query) { |response, _, _|
      @response = response
    }
  when :get
    query = @connection.options[:params].merge(params[:query] || {})
    @connection[params[:path]].get(:params => query) { |response, _, _|
      @response = response
    }
  end

  log.debug("args: #{@response.request.args}") if log

  if @response.headers[:content_disposition]
    filename = params[:output] || @response.headers[:content_disposition][/attachment; filename="(.+?)"/, 1]
    body = @response.body
    file = open(filename, 'wb')
    file.write(body)
    file.close
    return true
  else
    doc = JSON.load(@response.body)
    log.debug("body: #{doc}") if log

    if doc.kind_of?(Hash) && doc['success'] == false
      code    = doc['error']['code']
      message = doc['error']['message']
      error   = Crowdin::API::Errors::Error.new(code, message)
      raise(error)
    else
      return doc
    end
  end

end

#supported_languagesObject


336
337
338
339
340
341
# File 'lib/crowdin-api/methods.rb', line 336

def supported_languages
  request(
    :method => :get,
    :path   => "/api/project/#{@project_id}/supported-languages",
  )
end

#translations_statusObject


349
350
351
352
353
354
# File 'lib/crowdin-api/methods.rb', line 349

def translations_status
  request(
    :method => :post,
    :path   => "/api/project/#{@project_id}/status",
  )
end

#update_file(files, params = {}) ⇒ Object


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/crowdin-api/methods.rb', line 72

def update_file(files, params = {})
  params[:files] = Hash[files.map { |f|
    dest = f[:dest] || raise(ArgumentError, "'`:dest` is required'")
    source = ::File.open(f[:source] || raise(ArgumentError, "'`:source` is required'"))
    source.define_singleton_method(:original_filename) do
      dest
    end
    [dest, source]
  }]

  params[:titles] = Hash[files.map { |f| [f[:dest], f[:title]] }]
  params[:titles].delete_if { |_, v| v.nil? }

  params[:export_patterns] = Hash[files.map { |f| [f[:dest], f[:export_pattern]] }]
  params[:export_patterns].delete_if { |_, v| v.nil? }

  params.delete_if { |_, v| v.respond_to?(:empty?) ? !!v.empty? : !v }

  request(
    :method => :post,
    :path   => "/api/project/#{@project_id}/update-file",
    :query  => params,
  )
end

#upload_glossary(file) ⇒ Object


160
161
162
163
164
165
166
167
168
169
# File 'lib/crowdin-api/methods.rb', line 160

def upload_glossary(file)
  # raise "#{path} file does not exist" unless ::File.exist?(path)
  file = ::File.open(file, 'rb')

  request(
    :method => :post,
    :path   => "/api/project/#{@project_id}/upload-glossary",
    :query  => { :file => file },
  )
end

#upload_tm(file) ⇒ Object


177
178
179
180
181
182
183
184
185
# File 'lib/crowdin-api/methods.rb', line 177

def upload_tm(file)
  file = ::File.open(file, 'rb')

  request(
    :method => :post,
    :path   => "/api/project/#{@project_id}/upload-tm",
    :query  => { :file => file },
  )
end