Class: Stratagem::Client

Inherits:
Object show all
Defined in:
lib/stratagem/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(authentication) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/stratagem/client.rb', line 7

def initialize(authentication)
  @authentication = authentication
end

Instance Method Details

#send(snapshot) ⇒ Object



11
12
13
14
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
# File 'lib/stratagem/client.rb', line 11

def send(snapshot)
  Stratagem.logger.phase('exporting')
  Stratagem.logger.debug "Sending report to server"

  url = URI.parse("#{@authentication.base_url}/snapshots")
  req = Stratagem.ssl? ? Net::HTTPS::Post.new(url.path) : Net::HTTP::Post.new(url.path)
  model = snapshot.model.export.to_json
  puts "Size: #{model.size}"
  compressed = Zlib::Deflate.deflate(model, Zlib::BEST_COMPRESSION)
  base64 = Base64.encode64(compressed)
  puts "Compressed size: #{compressed.size}"
  puts "Compressed size base 64: #{base64.size}"
  
  req.set_form_data({
    'auth_token' => @authentication.credentials[:token],
    'project_id' => @authentication.credentials[:project],
    'timestamp' => snapshot.timestamp.to_i,
    'model' => model
  }, ';')
  client = Stratagem.ssl? ? Net::HTTPS.new(url.host, url.port) : Net::HTTP.new(url.host, url.port)
  res = client.start {|http| 
    http.read_timeout= 360
    http.request(req) 
  }
  puts "response:"
  case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    puts "Visit #{@authentication.base_url} for your security posture."
    # OK
  else
    res.error!
  end
end