Module: Nexpose::AJAX
- Defined in:
- lib/nexpose/ajax.rb
Overview
Accessor to the Nexpose AJAX API. These core methods should allow direct access to underlying controllers in order to test functionality that is not currently exposed through the XML API.
Defined Under Namespace
Modules: CONTENT_TYPE
Class Method Summary collapse
-
._headers(nsc, request) ⇒ Object
Attach necessary header fields.
-
._https(nsc) ⇒ Object
Use the Nexpose::Connection to establish a correct HTTPS object.
- ._request(nsc, request) ⇒ Object
-
.delete(nsc, uri, content_type = CONTENT_TYPE::XML) ⇒ Object
DELETE call to a Nexpose controller.
-
.form_post(nsc, uri, parameters, content_type = CONTENT_TYPE::FORM) ⇒ Hash
POST call to a Nexpose controller that uses a form-post model.
-
.get(nsc, uri, content_type = CONTENT_TYPE::XML) ⇒ String|REXML::Document|Hash
GET call to a Nexpose controller.
-
.parametrize_uri(uri, parameters) ⇒ Hash
Append the query parameters to given URI.
-
.patch(nsc, uri, payload = nil, content_type = CONTENT_TYPE::XML) ⇒ String
PATCH call to a Nexpose controller.
-
.post(nsc, uri, payload = nil, content_type = CONTENT_TYPE::XML) ⇒ String|REXML::Document|Hash
POST call to a Nexpose controller.
-
.put(nsc, uri, payload = nil, content_type = CONTENT_TYPE::XML) ⇒ String
PUT call to a Nexpose controller.
Class Method Details
._headers(nsc, request) ⇒ Object
Attach necessary header fields.
128 129 130 131 |
# File 'lib/nexpose/ajax.rb', line 128 def _headers(nsc, request) request.add_field('nexposeCCSessionID', nsc.session_id) request.add_field('Cookie', "nexposeCCSessionID=#{nsc.session_id}") end |
._https(nsc) ⇒ Object
Use the Nexpose::Connection to establish a correct HTTPS object.
120 121 122 123 124 125 |
# File 'lib/nexpose/ajax.rb', line 120 def _https(nsc) http = Net::HTTP.new(nsc.host, nsc.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE http end |
._request(nsc, request) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/nexpose/ajax.rb', line 133 def _request(nsc, request) http = _https(nsc) _headers(nsc, request) # Return response body if request is successful. Brittle. response = http.request(request) case response when Net::HTTPOK response.body when Net::HTTPCreated response.body when Net::HTTPUnauthorized raise Nexpose::PermissionError.new(response) else req_type = request.class.name.split('::').last.upcase raise Nexpose::APIError.new(response, "#{req_type} request to #{request.path} failed. #{request.body}") end end |
.delete(nsc, uri, content_type = CONTENT_TYPE::XML) ⇒ Object
DELETE call to a Nexpose controller.
99 100 101 102 103 |
# File 'lib/nexpose/ajax.rb', line 99 def delete(nsc, uri, content_type = CONTENT_TYPE::XML) delete = Net::HTTP::Delete.new(uri) delete.set_content_type(content_type) _request(nsc, delete) end |
.form_post(nsc, uri, parameters, content_type = CONTENT_TYPE::FORM) ⇒ Hash
POST call to a Nexpose controller that uses a form-post model. This is here to support legacy use of POST in old controllers.
87 88 89 90 91 92 |
# File 'lib/nexpose/ajax.rb', line 87 def form_post(nsc, uri, parameters, content_type = CONTENT_TYPE::FORM) post = Net::HTTP::Post.new(uri) post.set_content_type(content_type) post.set_form_data(parameters) _request(nsc, post) end |
.get(nsc, uri, content_type = CONTENT_TYPE::XML) ⇒ String|REXML::Document|Hash
GET call to a Nexpose controller.
26 27 28 29 30 |
# File 'lib/nexpose/ajax.rb', line 26 def get(nsc, uri, content_type = CONTENT_TYPE::XML) get = Net::HTTP::Get.new(uri) get.set_content_type(content_type) _request(nsc, get) end |
.parametrize_uri(uri, parameters) ⇒ Hash
Append the query parameters to given URI.
112 113 114 |
# File 'lib/nexpose/ajax.rb', line 112 def parametrize_uri(uri, parameters) uri = uri.concat(('?').concat(parameters.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&'))) if parameters end |
.patch(nsc, uri, payload = nil, content_type = CONTENT_TYPE::XML) ⇒ String
PATCH call to a Nexpose controller.
70 71 72 73 74 75 |
# File 'lib/nexpose/ajax.rb', line 70 def patch(nsc, uri, payload = nil, content_type = CONTENT_TYPE::XML) patch = Net::HTTP::Patch.new(uri) patch.set_content_type(content_type) patch.body = payload.to_s if payload _request(nsc, patch) end |
.post(nsc, uri, payload = nil, content_type = CONTENT_TYPE::XML) ⇒ String|REXML::Document|Hash
POST call to a Nexpose controller.
55 56 57 58 59 60 |
# File 'lib/nexpose/ajax.rb', line 55 def post(nsc, uri, payload = nil, content_type = CONTENT_TYPE::XML) post = Net::HTTP::Post.new(uri) post.set_content_type(content_type) post.body = payload.to_s if payload _request(nsc, post) end |
.put(nsc, uri, payload = nil, content_type = CONTENT_TYPE::XML) ⇒ String
PUT call to a Nexpose controller.
40 41 42 43 44 45 |
# File 'lib/nexpose/ajax.rb', line 40 def put(nsc, uri, payload = nil, content_type = CONTENT_TYPE::XML) put = Net::HTTP::Put.new(uri) put.set_content_type(content_type) put.body = payload.to_s if payload _request(nsc, put) end |