class Jenkins2API::Client
Basically the only public class to call Jenkins2 API nedpoints
Public Class Methods
Save Jenkins credentials and server path
Options:¶ ↑
server
-
Server path (e.g.: jenkins.example.com/)
username
-
Username for Jenkins
password
-
Password or API Token for Jenkins
Throws an ArgumentError
if username is specified but password is empty
# File lib/client.rb, line 22 def initialize(**options) @server = options[:server] || 'http://127.0.0.1/' @username = options[:username] @password = options[:password] return if @username && @password raise ArgumentError, 'If username is provided, password is required' end
Public Instance Methods
Creates and calls an API endpoint.
Params:¶ ↑
method
-
:post
or:get
path
-
Path to the Jenkins resource (e.g.:
/job/my-job/
) response_type
-
:json
,:xml
or:raw
opts
-
sym options to pass to the endpoint. Applicable only if
:post
# File lib/client.rb, line 69 def api_request(method, path, response_type = :json, **opts) req = new_request(method, path, response_type, opts) req.basic_auth @username, @password yield req if block_given? req.content_type ||= 'application/x-www-form-urlencoded' response = Net::HTTP.start( req.uri.hostname, req.uri.port, use_ssl: req.uri.scheme == 'https' ) do |http| http.request(req) end handle_response(response, response_type) end
Artifact related endpoints. Creates new Jenkins2API::Endpoint::Artifact
instance
# File lib/client.rb, line 46 def artifact @artifact ||= Endpoint::Artifact.new(self) end
Build related endpoints. Creates new Jenkins2API::Endpoint::Build
instance
# File lib/client.rb, line 40 def build @build ||= Endpoint::Build.new(self) end
Configuration related endpoints. Creates new Jenkins2API::Endpoint::Configuration
instance
# File lib/client.rb, line 52 def configuration @configuration ||= Endpoint::Configuration.new(self) end
Handles response based on response_type
# File lib/client.rb, line 103 def handle_response(response, response_type) return response['location'] if response.is_a?(Net::HTTPRedirection) response.value unless response.is_a?(Net::HTTPSuccess) if response_type == :json JSON.parse(response.body) else response.body end end
Job related endpoints. Creates new Jenkins2API::Endpoint::Job
instance
# File lib/client.rb, line 34 def job @job ||= Endpoint::Job.new(self) end
Creates a new request for the API call with default and required values
# File lib/client.rb, line 87 def new_request(method, path, response_type, **opts) response_type = :json unless %i[json raw xml].include?(response_type) parts = [@server, URI.escape(path)] parts << 'api/json' if response_type == :json parts << 'api/xml' if response_type == :xml uri = URI(File.join(parts)) uri.query = URI.encode_www_form(opts) case method when :get then Net::HTTP::Get when :post then Net::HTTP::Post end.new(uri) end
Node/Computer related endpoints. Creates new Jenkins2API::Endpoint::Node
instance
# File lib/client.rb, line 58 def node @node ||= Endpoint::Node.new(self) end