module Conserva

Constants

ClientError

Check data

ConvertError

base class for conserva exceptions

DownloadError

File was not correct download

FINISHED
GemUninitialized

Check application

InternalServerError

500 Internal Server Error

InvalidRequest

406 Not Acceptable

PermissionDenied
RestClient Exceptions

403 Forbidden

ServerError

Try later

TaskLocked

423 Locked

WrongParameters

422 Unprocessable Entity

WrongResource

404 Not Found

Public Class Methods

alive?() click to toggle source
# File lib/conserva.rb, line 75
def alive?
  if valid_settings
    ping_conserva = Net::Ping::HTTP.new("http://#{@@address}/api/v1/convert_combinations")
    ping_conserva.ping?
  end
end
create_task(input_file, input_extension, result_extension) click to toggle source
# File lib/conserva.rb, line 16
def create_task(input_file, input_extension, result_extension)
  response = RestClient.post "http://#{@@address}/api/v1/task",
                             input_extension: input_extension,
                             output_extension: result_extension,
                             api_key: @@api_key,
                             file: input_file
  JSON.parse(response)['id'] || (raise ServerErrorException)

rescue RestClient::ExceptionWithResponse, RestClient::RequestFailed => exception
  rescue_rest_client_exception exception
end
current_settings() click to toggle source
# File lib/conserva.rb, line 67
def current_settings
  if valid_settings
    {conserva_address: @@address,
     proxy: (defined? @@proxy) ? @@proxy : nil,
     api_key: @@api_key}
  end
end
download_file(task_id, options = {}) click to toggle source
# File lib/conserva.rb, line 40
def download_file(task_id, options = {})
  default_options = {check_sum: true}
  options.reverse_merge! default_options

  downloaded_file = RestClient.get "http://#{@@address}/api/v1/task/#{task_id}/download",
                                   {params: {api_key: @@api_key}}
  raise DownloadError if options[:check_sum] && (Digest::SHA256.hexdigest(downloaded_file) != task_info(task_id)[:result_file_sha256])
  downloaded_file
rescue RestClient::ExceptionWithResponse, RestClient::RequestFailed => exception
  rescue_rest_client_exception exception
end
initialize(conserva_address, api_key, options = {}) click to toggle source
# File lib/conserva.rb, line 8
def initialize(conserva_address, api_key, options = {})
  @@address = conserva_address
  @@api_key = api_key
  default_options = {proxy: nil}
  options.reverse_merge! default_options
  RestClient.proxy = options[:proxy]
end
remove_task(task_id) click to toggle source
# File lib/conserva.rb, line 52
def remove_task(task_id)
  RestClient.delete "http://#{@@address}/api/v1/task/#{task_id}",
                    {params: {api_key: @@api_key}}
rescue RestClient::ExceptionWithResponse, RestClient::RequestFailed => exception
  rescue_rest_client_exception exception
end
rescue_rest_client_exception(exception) click to toggle source
# File lib/conserva.rb, line 82
def rescue_rest_client_exception(exception)
  case exception
    when RestClient::UnprocessableEntity
      raise WrongParameters
    when RestClient::Forbidden
      raise PermissionDenied
    when RestClient::ResourceNotFound
      raise WrongResource
    when RestClient::NotAcceptable
      raise InvalidRequest
    when RestClient::InternalServerError
      raise InternalServerError
    when RestClient::Locked
      raise TaskLocked
    else
      raise exception
  end
end
task_info(task_id) click to toggle source
# File lib/conserva.rb, line 28
def task_info(task_id)
  response = RestClient.get "http://#{@@address}/api/v1/task/#{task_id}",
                            {params: {api_key: @@api_key}}
  JSON.parse(response).symbolize_keys
rescue RestClient::ExceptionWithResponse, RestClient::RequestFailed => exception
  rescue_rest_client_exception exception
end
task_ready?(task_id) click to toggle source
# File lib/conserva.rb, line 36
def task_ready?(task_id)
  task_info(task_id)[:state] == FINISHED
end
valid_file_convertations() click to toggle source

return valid combinations as two dimensional array: [[from,to], [from,to], …]

# File lib/conserva.rb, line 60
def valid_file_convertations
  result_string = RestClient.get "http://#{@@address}/api/v1/convert_combinations"
  JSON.parse(result_string)
rescue RestClient::ExceptionWithResponse, RestClient::RequestFailed => exception
  rescue_rest_client_exception exception
end

Private Class Methods

valid_settings() click to toggle source
# File lib/conserva.rb, line 102
def valid_settings
  if defined?(@@address) && defined?(@@api_key)
    true
  else
    raise GemUninitialized
  end
end