Class: Sandbox

Inherits:
Object
  • Object
show all
Defined in:
lib/deepviz/sandbox.rb

Constant Summary collapse

URL_UPLOAD_SAMPLE =
'https://api.deepviz.com/sandbox/submit'
URL_DOWNLOAD_REPORT =
'https://api.deepviz.com/general/report'
URL_DOWNLOAD_SAMPLE =
'https://api.deepviz.com/sandbox/sample'
URL_DOWNLOAD_BULK =
'https://api.deepviz.com/sandbox/sample/bulk/retrieve'
URL_REQUEST_BULK =
'https://api.deepviz.com/sandbox/sample/bulk/request'

Instance Method Summary collapse

Instance Method Details

#bulk_download_request(api_key, md5_list) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/deepviz/sandbox.rb', line 163

def bulk_download_request(api_key, md5_list)
  if api_key == nil or api_key == ''
    return Result.new(status=INPUT_ERROR, msg='API key cannot be null or empty String')
  end

  if !md5_list.kind_of?(Array)
    return Result.new(status=INPUT_ERROR, msg='MD5 list empty or invalid')
  end

  body = {
      :api_key => api_key,
      :hashes => md5_list
  }

  return do_post(body, URL_REQUEST_BULK)
end

#bulk_download_retrieve(api_key, id_request, path) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/deepviz/sandbox.rb', line 181

def bulk_download_retrieve(api_key, id_request, path)
  if api_key == nil or api_key == ''
    return Result.new(status=INPUT_ERROR, msg='API key cannot be null or empty String')
  end

  if id_request == nil or id_request == ''
    return Result.new(status=INPUT_ERROR, msg='Request ID cannot be null or empty String')
  end

  if path == nil or path == ''
    return Result.new(status=INPUT_ERROR, msg='Destination path cannot be null or empty String')
  else
    if File.exist?(path) and !File.directory?(path)
      return Result.new(status=INPUT_ERROR, msg='Invalid destination folder')
    end
  end

  body = {
      :api_key => api_key,
      :id_request => id_request.to_s
  }

  begin
    response = Unirest.post(URL_DOWNLOAD_BULK,
                            headers: { 'Accept' => 'application/json' },
                            parameters: body.to_json)
  rescue Exception
    return Result.new(status=NETWORK_ERROR, msg='%s - Error while connecting to Deepviz: %s' % [response.code, body['errmsg']])
  end

  if response.code == 200
    dest_path = File.absolute_path(File.join(path, 'bulk_request_%s.zip' % id_request.to_s))
    open(dest_path, 'wb') do |file|
      file.write(response.body)
    end

    return Result.new(status=SUCCESS, msg='Archive downloaded to "%s"' % dest_path)
  elsif response.code == 428
      return Result.new(status=PROCESSING, msg='%s - Your request is being processed. Please try again in a few minutes' % response.code)
  else
    if response.code >= 500
      return Result.new(status=SERVER_ERROR, msg='%s - Error while connecting to Deepviz: %s' % [response.code, response.body['errmsg']])
    else
      return Result.new(status=CLIENT_ERROR, msg='%s - Error while connecting to Deepviz: %s' % [response.code, response.body['errmsg']])
    end
  end
end

#download_sample(api_key, md5, path) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/deepviz/sandbox.rb', line 92

def download_sample(api_key, md5, path)
  if api_key == nil or api_key == ''
    return Result.new(status=INPUT_ERROR, msg='API key cannot be null or empty String')
  end

  if md5 == nil or md5 == ''
    return Result.new(status=INPUT_ERROR, msg='MD5 cannot be null or empty String')
  end

  if path == nil or path == ''
    return Result.new(status=INPUT_ERROR, msg='Destination path cannot be null or empty String')
  else
    if File.exist?(path) and !File.directory?(path)
      return Result.new(status=INPUT_ERROR, msg='Invalid destination folder')
    end
  end

  body = {
      :api_key => api_key,
      :md5 => md5
  }

  begin
    response = Unirest.post(URL_DOWNLOAD_SAMPLE,
                            headers:{ 'Accept' => 'application/json' },
                            parameters:body.to_json)
  rescue Exception
    return Result.new(status=NETWORK_ERROR, msg='%s - Error while connecting to Deepviz: %s' % [response.code, body['errmsg']])
  end

  if response.code == 200
    dest_path = File.absolute_path(File.join(path, md5))
    open(dest_path, 'wb') do |file|
      file.write(response.body)
    end

    return Result.new(status=SUCCESS, msg='Sample downloaded to "%s"' % dest_path)
  else
    if response.code >= 500
      return Result.new(status=SERVER_ERROR, msg='%s - Error while connecting to Deepviz: %s' % [response.code, response.body['errmsg']])
    else
      return Result.new(status=CLIENT_ERROR, msg='%s - Error while connecting to Deepviz: %s' % [response.code, response.body['errmsg']])
    end
  end
end

#sample_report(api_key, md5, filters = nil) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/deepviz/sandbox.rb', line 144

def sample_report(api_key, md5, filters=nil)
  if api_key == nil or api_key == ''
    return Result.new(status=INPUT_ERROR, msg='API key cannot be null or empty String')
  end

  if md5 == nil or md5 == ''
    return Result.new(status=INPUT_ERROR, msg='MD5 cannot be null or empty String')
  end

  if filters != nil
    body = {:api_key => api_key, :md5 => md5, :output_filters => filters}
  else
    body = {:api_key => api_key, :md5 => md5}
  end

  return do_post(body, URL_DOWNLOAD_REPORT)
end

#sample_result(api_key, md5) ⇒ Object



139
140
141
# File 'lib/deepviz/sandbox.rb', line 139

def sample_result(api_key, md5)
  return sample_report(api_key, md5, ['classification'])
end

#upload_folder(api_key, path) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/deepviz/sandbox.rb', line 56

def upload_folder(api_key, path)
  if api_key == nil or api_key == ''
    return Result.new(status=INPUT_ERROR, msg='API key cannot be null or empty String')
  end

  if path == nil or path == ''
    return Result.new(status=INPUT_ERROR, msg='Folder path cannot be null or empty String')
  else
    if !File.exist?(path)
      return Result.new(status=INPUT_ERROR, msg='Directory does not exists')
    else
      if !File.directory?(path)
        return Result.new(status=INPUT_ERROR, msg='Path is a file instead of a directory')
      end
    end
  end

  if Dir.entries(path).length <= 2
    return Result.new(status=INPUT_ERROR, msg='Empty folder')
  end

  Dir.foreach(path) { |x|
    if x != '.' and x != '..'
      file_path = File.join(path, x)
      result = upload_sample(api_key, file_path)
      if result.status != SUCCESS
        result.msg = '"Unable to upload file "%s"' % file_path
        return result
      end
    end
  }

  return Result.new(status=SUCCESS, msg='Every file in folder has been uploaded')
end

#upload_sample(api_key, path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/deepviz/sandbox.rb', line 15

def upload_sample(api_key, path)
  if api_key == nil or api_key == ''
    return Result.new(status=INPUT_ERROR, msg='API key cannot be null or empty String')
  end

  if path == nil or path == ''
    return Result.new(status=INPUT_ERROR, msg='File path cannot be null or empty String')
  else
    if !File.exist?(path)
      return Result.new(status=INPUT_ERROR, msg='File does not exists')
    else
      if File.directory?(path)
        return Result.new(status=INPUT_ERROR, msg='Path is a directory instead of a file')
      else
        if !File.readable?(path)
          return Result.new(status=INPUT_ERROR, msg='Cannot open file "%s"' % File.absolute_path(path))
        end
      end
    end
  end

  begin
    response = Unirest.post(URL_UPLOAD_SAMPLE,
                            headers:{ 'Content-Type' => 'application/json' },
                            parameters:{ :api_key => api_key, :source => 'ruby_deepviz', :file => File.new(path, 'rb') })
  rescue Exception
    return Result.new(status=NETWORK_ERROR, msg='%s - Error while connecting to Deepviz: %s' % [response.code, body['errmsg']])
  end

  if response.code == 200
    return Result.new(status=SUCCESS, msg=response.body['data'])
  else
    if response.code >= 500
      return Result.new(status=SERVER_ERROR, msg='%s - Error while connecting to Deepviz: %s' % [response.code, response.body['errmsg']])
    else
      return Result.new(status=CLIENT_ERROR, msg='%s - Error while connecting to Deepviz: %s' % [response.code, response.body['errmsg']])
    end
  end
end