class Verbalizeit::Client

Attributes

languages[R]

Public Class Methods

new(api_key, environement) click to toggle source
# File lib/verbalizeit/client.rb, line 8
def initialize(api_key, environement)
  @api_key = api_key
  @environment = environement
  @languages = fetch_languages
end

Public Instance Methods

create_task(source_language, target_language, operation, options = {}) click to toggle source
# File lib/verbalizeit/client.rb, line 14
def create_task(source_language, target_language, operation, options = {})
  body = {
    source_language: source_language,
    target_language: target_language,
    operation: operation,
  }
  body[:file] = options[:file] if options[:file]
  body[:media_resource_url] = options[:media_resource_url] if options[:media_resource_url]
  body[:start] = "#{options[:start]}" if options[:start]
  body[:rush_order] = "#{options[:rush_order]}" if options[:rush_order]
  body[:postback_url] = options[:postback_url] if options[:postback_url]
  body[:status_url] = options[:status_url] if options[:status_url]
  body[:special_instructions] = options[:special_instructions] if options[:special_instructions]

  response = Typhoeus.post(tasks_url, body: body, headers: authorization_header)

  if response.code == 200
    Task.from(parse_body(response.body), self)
  elsif response.code == 400
    raise Error::BadRequest, format_errors(response.body)
  elsif response.code == 401
    raise Error::Unauthorized
  end
end
get_task(id) click to toggle source
# File lib/verbalizeit/client.rb, line 54
def get_task(id)
  response = Typhoeus.get(get_task_url(id), headers: authorization_header)
  validate_response(response.code)
  Task.from(parse_body(response.body), self)
end
list_tasks(options = {}) click to toggle source
# File lib/verbalizeit/client.rb, line 39
def list_tasks(options = {})
  params = {
    start: options[:start],
    limit: options[:limit] || 10,
    status: options[:status]
  }
  response = Typhoeus.get(tasks_url, params: params, headers: authorization_header)

  if response.code == 200
    list_tasks_success(response.body)
  else
    raise Error::NotImplemented
  end
end
start_task(id) click to toggle source
# File lib/verbalizeit/client.rb, line 60
def start_task(id)
  response = Typhoeus.post(start_task_url(id), headers: authorization_header)
  validate_response(response.code)
  true
end
task_completed_file(id) click to toggle source
# File lib/verbalizeit/client.rb, line 66
def task_completed_file(id)
  response = Typhoeus.get(task_completed_file_url(id), headers: authorization_header)
  validate_response(response.code)

  # original string => "attachment; filename=\"sample.srt\""
  filename = response.headers["Content-Disposition"].rpartition("filename=").last
  struct = Struct.new(:filename, :content)
  struct.new(filename, response.body)
end

Private Instance Methods

authorization_header() click to toggle source
# File lib/verbalizeit/client.rb, line 141
def authorization_header
  {"x-api-key" => @api_key}
end
base_url() click to toggle source
# File lib/verbalizeit/client.rb, line 128
def base_url
  case @environment
    when :staging
      "https://stagingapi.verbalizeit.com/v2/"
    when :production
      "https://api.verbalizeit.com/v2/"
    when :local
      "http://localhost.verbalizeit.com:3000/v2/"
    else
      raise Error::UnknownEnvironment, "you may specify :staging or :production"
  end
end
fetch_languages() click to toggle source
# File lib/verbalizeit/client.rb, line 97
def fetch_languages
  response = Typhoeus.get(languages_url, headers: authorization_header)

  if response.code == 200
    parsed_body = JSON.parse(response.body)
    parsed_body.map { |language| Language.new(language) }
  elsif response.code == 401
    raise Error::Unauthorized
  end
end
format_errors(body) click to toggle source
# File lib/verbalizeit/client.rb, line 145
def format_errors(body)
  parse_body(body).map { |_, errors| errors.map { |type, error| error } }.flatten.join(". ")
end
get_task_url(id) click to toggle source
# File lib/verbalizeit/client.rb, line 116
def get_task_url(id)
  tasks_url << "/#{id}"
end
languages_url() click to toggle source
# File lib/verbalizeit/client.rb, line 124
def languages_url
  base_url << "languages"
end
list_tasks_success(body) click to toggle source
# File lib/verbalizeit/client.rb, line 86
def list_tasks_success(body)
  parsed_body = parse_body(body)

  {
    total: parsed_body["total"],
    start: parsed_body["start"],
    limit: parsed_body["limit"],
    tasks: parsed_body["tasks"].map { |task| Task.from(task, self) }
  }
end
parse_body(body) click to toggle source
# File lib/verbalizeit/client.rb, line 149
def parse_body(body)
  JSON.parse(body)
end
start_task_url(id) click to toggle source
# File lib/verbalizeit/client.rb, line 112
def start_task_url(id)
  get_task_url(id) << "/start"
end
task_completed_file_url(id) click to toggle source
# File lib/verbalizeit/client.rb, line 108
def task_completed_file_url(id)
  get_task_url(id) << "/completed_file"
end
tasks_url() click to toggle source
# File lib/verbalizeit/client.rb, line 120
def tasks_url
  base_url << "tasks"
end
validate_response(code) click to toggle source
# File lib/verbalizeit/client.rb, line 78
def validate_response(code)
  if code == 404
    raise Error::NotFound
  elsif code == 403
    raise Error::Forbidden
  end
end