Module: Conserva

Defined in:
lib/conserva.rb,
lib/conserva/exceptions.rb

Constant Summary collapse

FINISHED =
'finished'
ConvertError =

base class for conserva exceptions

Class.new(StandardError)
GemUninitialized =

Check application

Class.new(ConvertError)
ClientError =

Check data

Class.new(ConvertError)
ServerError =

Try later

Class.new(ConvertError)
PermissionDenied =

RestClient Exceptions 403 Forbidden

Class.new(ClientError)
WrongResource =

404 Not Found

Class.new(ClientError)
InvalidRequest =

406 Not Acceptable

Class.new(ClientError)
WrongParameters =

422 Unprocessable Entity

Class.new(ClientError)
TaskLocked =

423 Locked

Class.new(ServerError)
InternalServerError =

500 Internal Server Error

Class.new(ServerError)
DownloadError =

File was not correct download

Class.new(ServerError)

Class Method Summary collapse

Class Method Details

.alive?Boolean



75
76
77
78
79
80
# 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) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# 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_settingsObject



67
68
69
70
71
72
73
# 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 = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# 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 = {}) ⇒ Object



8
9
10
11
12
13
14
# 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) ⇒ Object



52
53
54
55
56
57
# 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) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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) ⇒ Object



28
29
30
31
32
33
34
# 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) ⇒ Boolean



36
37
38
# File 'lib/conserva.rb', line 36

def task_ready?(task_id)
  task_info(task_id)[:state] == FINISHED
end

.valid_file_convertationsObject

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



60
61
62
63
64
65
# 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