class Toptranslation::Resource::Project

Attributes

created_at[R]
identifier[R]
locales[R]
name[RW]
source_locale[R]

Public Class Methods

new(connection, options = {}) click to toggle source
# File lib/toptranslation/resource/project.rb, line 6
def initialize(connection, options = {})
  @connection = connection
  @options = options

  update_from_response(options)
end

Public Instance Methods

documents(options = {}) click to toggle source
# File lib/toptranslation/resource/project.rb, line 31
def documents(options = {})
  options.merge!(project_identifier: @identifier)
  ProjectDocumentList.new(@connection, options)
end
save() click to toggle source
# File lib/toptranslation/resource/project.rb, line 40
def save
  response = @identifier ? update_remote_project : create_remote_project
  update_and_return_from_response(response)
end
strings() click to toggle source
# File lib/toptranslation/resource/project.rb, line 36
def strings
  StringList.new(@connection, project_identifier: @identifier)
end
upload_document(filepath, locale_code, options = {}, &block) click to toggle source
# File lib/toptranslation/resource/project.rb, line 13
def upload_document(filepath, locale_code, options = {}, &block)
  upload = Upload.new(@connection).upload(filepath, &block)

  attr_hash = {
    document_store_id: upload.document_store_id,
    document_token: upload.document_token,
    locale_code: locale_code,
    sha1: upload.sha1
  }

  attr_hash[:path] = options[:path] if options[:path]
  attr_hash[:name] = options[:name] if options[:name]

  response = @connection.post("/projects/#{@identifier}/documents", attr_hash)

  Document.new(@connection, response)
end

Private Instance Methods

create_remote_project() click to toggle source
# File lib/toptranslation/resource/project.rb, line 51
def create_remote_project
  @connection.post('/projects', remote_hash)
end
remote_hash() click to toggle source
# File lib/toptranslation/resource/project.rb, line 73
def remote_hash
  hash = {}
  hash[:name] = @name if @name
  hash
end
update_and_return_from_response(response) click to toggle source
# File lib/toptranslation/resource/project.rb, line 55
def update_and_return_from_response(response)
  if response
    update_from_response(response)
    self
  end
end
update_from_response(response) click to toggle source
# File lib/toptranslation/resource/project.rb, line 62
def update_from_response(response)
  @identifier = response['identifier'] if response['identifier']
  @created_at = DateTime.parse(response['created_at']) if response['created_at']
  @locales = response['locales'].inject([]) do |accu, locale_data|
    locale = Locale.new(locale_data)
    @source_locale = locale if locale_data['is_source_locale'] # Set source locale of the project
    accu << locale
  end
  @name = response['name'] if response['name']
end
update_remote_project() click to toggle source
# File lib/toptranslation/resource/project.rb, line 47
def update_remote_project
  @connection.patch("/projects/#{@identifier}", remote_hash)
end