Class: Sonar::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Certificate, Request, Search, User
Defined in:
lib/sonar/client.rb

Constant Summary

Constants included from Search

Search::QUERY_TYPES_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from User

#usage

Methods included from Search

#search

Methods included from Certificate

#get_certificate

Methods included from Request

#extract_params, #get, #post, #put, #request

Constructor Details

#initialize(options = {}) ⇒ Client

Create a new Sonar::Client object



24
25
26
27
28
29
# File 'lib/sonar/client.rb', line 24

def initialize(options = {})
  @api_url        = options[:api_url]       || Sonar.api_url       || "https://sonar.labs.rapid7.com"
  @api_version    = options[:api_version]   || Sonar.api_version   || "v2"
  @access_token   = options[:access_token]  || Sonar.access_token
  @email          = options[:email]         || Sonar.email
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token



18
19
20
# File 'lib/sonar/client.rb', line 18

def access_token
  @access_token
end

#api_urlObject

Returns the value of attribute api_url



18
19
20
# File 'lib/sonar/client.rb', line 18

def api_url
  @api_url
end

#api_versionObject

Returns the value of attribute api_version



18
19
20
# File 'lib/sonar/client.rb', line 18

def api_version
  @api_version
end

#emailObject

Returns the value of attribute email



18
19
20
# File 'lib/sonar/client.rb', line 18

def email
  @email
end

Instance Method Details

#connectionFaraday::Connection

Create a Faraday::Connection object

Returns:

  • (Faraday::Connection)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sonar/client.rb', line 35

def connection
  params = {}
  @conn = Faraday.new(url: api_url, params: params, headers: default_headers, ssl: { verify: true }) do |faraday|
    faraday.use FaradayMiddleware::Mashify
    faraday.use FaradayMiddleware::ParseJson, content_type: /\bjson$/
    faraday.use FaradayMiddleware::FollowRedirects
    faraday.adapter Faraday.default_adapter
  end
  @conn.headers['X-Sonar-Token'] = access_token
  @conn.headers['X-Sonar-Email'] = email
  @conn
end

#get_endpoint(type, params = {}) ⇒ Object

Generic GET of Sonar Objects



61
62
63
64
# File 'lib/sonar/client.rb', line 61

def get_endpoint(type, params = {})
  url = "/api/#{api_version}/#{type}"
  get(url, params)
end

#get_search_endpoint(type, params = {}) ⇒ Object

Generic GET of Sonar search Objects



50
51
52
53
54
55
56
57
# File 'lib/sonar/client.rb', line 50

def get_search_endpoint(type, params = {})
  url = "/api/#{api_version}/search/#{type}"
  if params[:limit]
    RequestIterator.new(url, connection, params)
  else
    get(url, params)
  end
end

#post_to_sonar(type, params = {}) ⇒ Object

Generic POST to Sonar



68
69
70
71
# File 'lib/sonar/client.rb', line 68

def post_to_sonar(type, params = {})
  url = "/api/#{api_version}/#{type}"
  post(url, params)
end