class Toptranslation::Resource::Order

Attributes

comment[RW]
coupon_code[RW]
created_at[R]
creator_hash[R]
desired_delivery_date[RW]
estimated_delivery_date[R]
identifier[R]
name[RW]
order_number[R]
ordered_at[R]
progress[R]
quote_required[RW]
reference[RW]
requested_at[R]
service_level[RW]
state[R]

Public Class Methods

new(connection, options = {}) click to toggle source
# File lib/toptranslation/resource/order.rb, line 8
def initialize(connection, options = {})
  @connection = connection
  @options = options
  @name = @options[:name]
  @reference = @options[:reference]
  @comment = @options[:comment]
  @coupon_code = @options[:coupon_code]
  @service_level = @options[:service_level]
  @desired_delivery_date = @options[:desired_delivery_date]
  @quote_required = @options[:quote_required]
  @delivery_date = nil
  @identifier = nil

  update_from_response(options)
end

Public Instance Methods

add_document(document_store_id, document_token, source_locale_code, target_locale_codes) click to toggle source
# File lib/toptranslation/resource/order.rb, line 24
def add_document(document_store_id, document_token, source_locale_code, target_locale_codes)
  response = @connection.post("/orders/#{@identifier}/documents",
                              document_store_id: document_store_id,
                              document_token: document_token,
                              source_locale_code: source_locale_code,
                              target_locale_codes: target_locale_codes,
                              version: 2)

  Document.new(@connection, response)
end
creator() click to toggle source
# File lib/toptranslation/resource/order.rb, line 53
def creator
  @creator ||= User.new(@connection, @options['creator'])
end
documents() click to toggle source
# File lib/toptranslation/resource/order.rb, line 41
def documents
  @connection.get("/orders/#{@identifier}/documents", version: 2)
end
quotes() click to toggle source
# File lib/toptranslation/resource/order.rb, line 45
def quotes
  @quotes ||= @options['quotes'].map { |quote| Quote.new(@connection, quote) }
end
request() click to toggle source
# File lib/toptranslation/resource/order.rb, line 62
def request
  response = @connection.patch("/orders/#{@identifier}/request", version: 2)
  update_and_return_from_response(response)
end
save() click to toggle source
# File lib/toptranslation/resource/order.rb, line 57
def save
  response = @identifier ? update_remote_order : create_remote_order
  update_and_return_from_response(response)
end
translations() click to toggle source
# File lib/toptranslation/resource/order.rb, line 49
def translations
  TranslationList.new(@connection, order_identifier: @identifier)
end
upload_document(filepath, source_locale_code, target_locale_codes) click to toggle source
# File lib/toptranslation/resource/order.rb, line 35
def upload_document(filepath, source_locale_code, target_locale_codes)
  upload = Upload.new(@connection).upload(filepath)

  add_document(upload.document_store_id, upload.document_token, source_locale_code, target_locale_codes)
end

Private Instance Methods

create_remote_order() click to toggle source
# File lib/toptranslation/resource/order.rb, line 73
def create_remote_order
  @connection.post('/orders', remote_hash.merge(version: 2))
end
remote_hash() click to toggle source
# File lib/toptranslation/resource/order.rb, line 99
def remote_hash
  hash = {}
  hash[:reference] = @reference if @reference
  hash[:comment] = @comment if @comment
  hash[:coupon_code] = @coupon_code
  hash[:delivery_date] = @delivery_date
  hash[:name] = @name if @name
  hash[:service_level] = @service_level if @service_level
  hash[:desired_delivery_date] = @desired_delivery_date if @desired_delivery_date
  hash[:quote_required] = @quote_required if @quote_required

  hash
end
update_and_return_from_response(response) click to toggle source
# File lib/toptranslation/resource/order.rb, line 77
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/order.rb, line 84
def update_from_response(response)
  @identifier = response['identifier'] if response['identifier']
  @state = response['state'] if response['state']
  @created_at = Time.parse(response['created_at']) if response['created_at']
  @ordered_at = Time.parse(response['ordered_at']) if response['ordered_at']
  @estimated_delivery_date = Time.parse(response['estimated_delivery_date']) if response['estimated_delivery_date']
  @name = response['name'] if response['name']
  @reference = response['reference'] if response['reference']
  @comment = response['comment'] if response['comment']
  @progress = response['progress_in_percent'] if response['progress_in_percent']
  @order_number = response['order_number'] if response['order_number']
  @service_level = response['service_level'] if response['service_level']
  @desired_delivery_date = response['desired_delivery_date'] if response['desired_delivery_date']
end
update_remote_order() click to toggle source
# File lib/toptranslation/resource/order.rb, line 69
def update_remote_order
  @connection.patch("/orders/#{@identifier}", remote_hash.merge(version: 2))
end