Class: WssAgent::Client

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

Overview

Client class

Constant Summary collapse

POLICY_TYPES =
{
  basic: 'CHECK_POLICIES',
  compliance: 'CHECK_POLICY_COMPLIANCE'
}.freeze
UPDATE_TYPE =
'UPDATE'.freeze
REQUEST_TIMEOUT =
120

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wss_agent/client.rb', line 14

def initialize
  @connection ||= Faraday.new(connection_options) do |h|
    h.port = Configure.port
    h.headers[:content_type] = 'application/x-www-form-urlencoded'
    h.request :url_encoded
    h.adapter :excon
  end
  Excon.defaults[:ciphers] = 'DEFAULT' if defined?(JRuby)

  @connection
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/wss_agent/client.rb', line 5

def connection
  @connection
end

Instance Method Details

#check_policies(gem_list, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/wss_agent/client.rb', line 55

def check_policies(gem_list, options = {})
  request_options =
    if Configure['force_check_all_dependencies'] || options['force']
      { type: POLICY_TYPES[:compliance], forceCheckAllDependencies: true }
    else
      { type: POLICY_TYPES[:basic], forceCheckAllDependencies: false }
    end

  ResponsePolicies.new(request(gem_list, request_options))
end

#diff(gem_list) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/wss_agent/client.rb', line 26

def diff(gem_list)
  diff_data = {
    'coordinates' => Configure.coordinates,
    'dependencies' => gem_list
  }
  if Configure['project_token']
    diff_data['projectToken'] = Configure['project_token']
  end
  MultiJson.dump([diff_data])
end

#payload(gem_list, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wss_agent/client.rb', line 37

def payload(gem_list, options = {})
  req_options = {
    agent: Configure['agent'],
    agentVersion: Configure['agent_version'],
    token: Configure.token,
    product: Configure['product'].to_s,
    productVersion: Configure['product_version'].to_s,
    timeStamp: Time.now.to_i,
    diff: diff(gem_list)
  }
  req_options[:userKey] = Configure.user_key if Configure.user_key?
  req_options.merge(options)
end

#request(gem_list, options = {}) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/wss_agent/client.rb', line 66

def request(gem_list, options = {})
  WssAgent.logger.debug "request params: #{payload(gem_list, options)}"

  connection.post(Configure.api_path, payload(gem_list, options))
rescue Faraday::Error::ClientError => ex
  ex
end

#update(gem_list) ⇒ Object



51
52
53
# File 'lib/wss_agent/client.rb', line 51

def update(gem_list)
  ResponseInventory.new(request(gem_list, type: UPDATE_TYPE))
end