Module: Splunker::Request

Included in:
Auth::SplunkAuth
Defined in:
lib/splunker/request.rb

Overview

The Request module, to be used by classes that will make requests to the Splunk API.

Note that any class that mixes in the Request module will also mix in Splunker::Connection automatically (see Splunker::Request.included)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



81
82
83
# File 'lib/splunker/request.rb', line 81

def self.included(base)
  base.send(:include, Splunker::Connection)
end

Instance Method Details

#assemble_path(path_str) ⇒ Object



77
78
79
# File 'lib/splunker/request.rb', line 77

def assemble_path(path_str)
  path_str.gsub(/\/+/,"/")
end

#authenticateObject

Authenticates the user (not the request) identified by :username and :password in the configuration.



14
# File 'lib/splunker/request.rb', line 14

def authenticate; end

#authenticate_connection(conn) ⇒ Object

Attaches credentials to the supplied Faraday connection object.



22
23
24
# File 'lib/splunker/request.rb', line 22

def authenticate_connection(conn)
  conn
end

#authenticated?Boolean

true if #authenticate does not need to be called.

Returns:

  • (Boolean)


17
18
19
# File 'lib/splunker/request.rb', line 17

def authenticated?
  true
end

#get(resource, parameters = {}) ⇒ Object

HTTP GET helper method. Parameters:

  • resource => The resource under configuration to make the

    request
    
  • parameters => GET parameters to supply with the request.

Returns Nokogiri:XML:Document object, parsed from the response body. Raises an exception from Splunker::Errors in accordance with the status code.



33
34
35
# File 'lib/splunker/request.rb', line 33

def get(resource, parameters={})
  request(:get, resource, parameters)
end

#post(resource, body = {}) ⇒ Object



37
38
39
# File 'lib/splunker/request.rb', line 37

def post(resource, body={})
  request(:post, resource, nil, body)
end

#request(method, resource, parameters = {}, body = {}) ⇒ Object

HTTP request helper method. Parameters:

  • method => A symbol representing the HTTP method (:get, :post, :put, :delete)

  • resource => The resource under configuration to make the

    request
    
  • parameters => GET parameters to supply with the request.

  • body => POST/PUT data

Returns Nokogiri:XML:Document object, parsed from the response body. Raises an exception from Splunker::Errors in accordance with the status code.



54
55
56
57
58
59
# File 'lib/splunker/request.rb', line 54

def request(method, resource, parameters={}, body={})
  authenticate unless authenticated?
  authenticate_connection(self.connection)
  final_resource = resource_builder(resource, parameters)
  self.connection.send(method, final_resource, body).body
end

#resource_builder(resource, parameters = {}) ⇒ Object

Returns a string representing the final resource path



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/splunker/request.rb', line 62

def resource_builder(resource, parameters={})
  u = Addressable::URI.new
  u.path = assemble_path("/servicesNS/#{configuration[:username]}/#{configuration[:app]}/#{resource}")
  unless parameters.nil? || parameters.empty?
    # Let's remap and cast everything to a string.
    # Addressable doesn't handle values like true well.
    final_parameters = {}
    parameters.map do |key, value|
      final_parameters[key] = "#{value}"
    end
    u.query_values = final_parameters 
  end
  u.to_s
end