Class: SmartlingApi::File

Inherits:
Object
  • Object
show all
Defined in:
lib/smartling_api/file.rb

Overview

Access to Smartling Files API

Instance Method Summary collapse

Constructor Details

#initialize(smartling: smartling_client, token: access_token, project_id: current_project_id) ⇒ File

Returns a new instance of File.



8
9
10
11
12
# File 'lib/smartling_api/file.rb', line 8

def initialize(smartling: smartling_client, token: access_token, project_id: current_project_id)
  @token      = token
  @project_id = project_id
  @smartling  = smartling
end

Instance Method Details

#delete(file_uri:) ⇒ Hash

Access to Smartling files api to delete a file

Examples:

List Files

SmartlingApi::File.new.delete(file_uri: '/translations/website') #=> { "code" => "SUCCESS" }

Parameters:

  • file_uri (String)

    File path within Smartling to delete

Returns:

  • (Hash)

    Details for file deletion

See Also:



76
77
78
# File 'lib/smartling_api/file.rb', line 76

def delete(file_uri:)
  smartling.post(url: "/files-api/v2/projects/#{project_id}/file/delete", token: token, body: { fileUri: file_uri })
end

#download_locale(locale_id:, file_uri:, **options) ⇒ String

Access to Smartling files api to download a file for a given locale id

Examples:

Download file

SmartlingApi::File.new.download_locale(locale_id: 'fr-Fr', file_uri: '/translation/website') #=> "translations"

Parameters:

  • locale_id (String)

    Locale id for the given file

  • file_uri (String)

    File path within smartling

  • options (Hash)

    Additional options for the given request.

Returns:

  • (String)

    Contents of the specified file

See Also:



39
40
41
42
43
# File 'lib/smartling_api/file.rb', line 39

def download_locale(locale_id:, file_uri:, **options)
  params = { fileUri: file_uri }.merge(options)

  smartling.download(url: "/files-api/v2/projects/#{project_id}/locales/#{locale_id}/file", token: token, params: params)
end

#get_translations(file_path:, file_uri:, locale_id:, **options) ⇒ Hash

Temporarily uploads a file, then returns a translated version for requested locales.

Examples:

Get Translations

SmartlingApi::File.new.get_translations(file: 'translations.pot', file_uri: '/translations/website.pot') #=> { "code" => "SUCCESS" }

Parameters:

  • file_path (String)

    File path of contents to upload

  • file_uri (String)

    File path within Smartling to base off

  • locale_id (String)

    Locale id for the given file

  • options (Hash)

    Additional options for the given request.

Returns:

  • (Hash)

    Details of tranlsations

See Also:



92
93
94
95
96
97
98
99
# File 'lib/smartling_api/file.rb', line 92

def get_translations(file_path:, file_uri:, locale_id:, **options)
  body = {
    file:     Faraday::UploadIO.new(file_path, 'text/plain'),
    fileUri:  file_uri,
  }.merge(options)

  smartling.upload(url: "/files-api/v2/projects/#{project_id}/locales/#{locale_id}/file/get-translations", token: token, body: body)
end

#list_files(**options) ⇒ Array

Access to Smartling files api to retrieve list of files for a given project

Examples:

List Files

SmartlingApi::File.new.list_files(uriMask: '.json') #=> [{"fileUri" => "[/translate/file.json]", ...}]

Parameters:

  • options (Hash)

    Additional options for the given request.

Returns:

  • (Array)

    A list of files matching the criteria

See Also:



23
24
25
26
# File 'lib/smartling_api/file.rb', line 23

def list_files(**options)
  files = smartling.get(url: "/files-api/v2/projects/#{project_id}/files/list", token: token, params: options)
  files.fetch("items")
end

#upload(file_path:, file_uri:, file_type:, **options) ⇒ Hash

Access to Smartling files api to upload a file

Examples:

Upload file

SmartlingApi::File.new.upload(file_path: 'website.pot', file_uri: '/translation/website', file_type: 'gettext') #=> { "code" => "SUCCESS" }

Parameters:

  • file_path (String)

    Location of file to upload

  • file_uri (String)

    File path within smartling

  • file_type (String)

    Type of file to upload

  • options (Hash)

    Additional options for the given request.

Returns:

  • (Hash)

    Details of upload

See Also:



57
58
59
60
61
62
63
64
65
# File 'lib/smartling_api/file.rb', line 57

def upload(file_path:, file_uri:, file_type:, **options)
  body = {
    file:     Faraday::UploadIO.new(file_path, 'text/plain'),
    fileUri:  file_uri,
    fileType: file_type
  }.merge(options)

  smartling.upload(url: "/files-api/v2/projects/#{project_id}/file", token: token, body: body)
end