class HybridAnalysis::Clients::Client
Constants
- BASE_URL
- DEFAULT_UA
- HOST
- VERSION
Attributes
key[R]
Public Class Methods
new(key)
click to toggle source
# File lib/hybridanalysis/clients/client.rb, line 18 def initialize(key) @key = key end
Private Instance Methods
_delete(path, params = {}, &block)
click to toggle source
# File lib/hybridanalysis/clients/client.rb, line 94 def _delete(path, params = {}, &block) delete = Net::HTTP::Delete.new(url_for(path)) delete.body = JSON.generate(params) if params request(delete, &block) end
_get(path, params = {}, &block)
click to toggle source
# File lib/hybridanalysis/clients/client.rb, line 68 def _get(path, params = {}, &block) uri = url_for(path) uri.query = URI.encode_www_form(params) get = Net::HTTP::Get.new(uri) request(get, &block) end
_post(path, params = {}, &block)
click to toggle source
# File lib/hybridanalysis/clients/client.rb, line 76 def _post(path, params = {}, &block) post = Net::HTTP::Post.new(url_for(path)) post.set_form params request(post, &block) end
_post_with_file(path, file:, filename:, params: {}, &block)
click to toggle source
# File lib/hybridanalysis/clients/client.rb, line 83 def _post_with_file(path, file:, filename:, params: {}, &block) post = Net::HTTP::Post.new(url_for(path)) data = [ ["file", file, { filename: filename }], ] data += stringfy_keys(params) post.set_form(data, "multipart/form-data") request(post, &block) end
https_options()
click to toggle source
# File lib/hybridanalysis/clients/client.rb, line 28 def https_options if proxy = ENV["HTTPS_PROXY"] || ENV["https_proxy"] uri = URI(proxy) { proxy_address: uri.hostname, proxy_port: uri.port, proxy_from_env: false, use_ssl: true } else { use_ssl: true } end end
path?(path)
click to toggle source
# File lib/hybridanalysis/clients/client.rb, line 107 def path?(path) File.exist? path end
request(req) { |json| ... }
click to toggle source
# File lib/hybridanalysis/clients/client.rb, line 42 def request(req) Net::HTTP.start(HOST, 443, https_options) do |http| req["api-key"] = key req["user-agent"] = DEFAULT_UA response = http.request(req) code = response.code.to_i body = response.body json = JSON.parse(body) if response["Content-Type"].to_s.include?("application/json") body = unzip(body) if response["Content-Type"].to_s.include?("application/gzip") case code when 200, 201, 202 if json yield json else yield body end else message = json&.dig("message") || body raise Error, "Unsupported response code returned: #{code} (#{message})" end end end
stringfy_keys(hash)
click to toggle source
# File lib/hybridanalysis/clients/client.rb, line 101 def stringfy_keys(hash) hash.map do |k, v| [k.to_s, v.to_s] end end
unzip(data)
click to toggle source
# File lib/hybridanalysis/clients/client.rb, line 111 def unzip(data) sio = StringIO.new(data) gz = Zlib::GzipReader.new(sio) gz.read end
url_for(path)
click to toggle source
# File lib/hybridanalysis/clients/client.rb, line 24 def url_for(path) URI(BASE_URL + path) end