Class: Sonar::Client

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

Constant Summary

Constants included from Search

Search::IS_IP, Search::QUERY_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Registration

#register_metasploit

Methods included from User

#usage

Methods included from Search

#domain_search_type_names, #domain_search_types, #ip_search_type_names, #ip_search_types, #query_type_names, #search

Methods included from Request

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

Constructor Details

#initialize(options = {}) ⇒ Client

Create a new Sonar::Client object



27
28
29
30
31
32
# File 'lib/sonar/client.rb', line 27

def initialize(options = {})
  @api_url        = options.fetch(:api_url, default_api_url)
  @api_version    = options.fetch(:api_version, default_api_version )
  @access_token   = options.fetch(:access_token, default_access_token)
  @email          = options.fetch(:email, default_email)
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



21
22
23
# File 'lib/sonar/client.rb', line 21

def access_token
  @access_token
end

#api_urlObject

Returns the value of attribute api_url.



21
22
23
# File 'lib/sonar/client.rb', line 21

def api_url
  @api_url
end

#api_versionObject

Returns the value of attribute api_version.



21
22
23
# File 'lib/sonar/client.rb', line 21

def api_version
  @api_version
end

#emailObject

Returns the value of attribute email.



21
22
23
# File 'lib/sonar/client.rb', line 21

def email
  @email
end

Instance Method Details

#connectionFaraday::Connection

Create a Faraday::Connection object

Returns:

  • (Faraday::Connection)


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

def connection
  params = {}
  @conn = Faraday.new(url: api_url, params: params, headers: default_headers, ssl: { verify: true }) do |faraday|
    faraday.use Faraday::FollowRedirects::Middleware
    faraday.use Faraday::Rashify::Middleware
    faraday.request :json

    faraday.response :json
    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



66
67
68
69
# File 'lib/sonar/client.rb', line 66

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



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

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



73
74
75
76
# File 'lib/sonar/client.rb', line 73

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