Class: Jenkins2API::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/client.rb

Overview

Basically the only public class to call Jenkins2 API nedpoints

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Client

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

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
# 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

Instance Method Details

#api_request(method, path, response_type = :json, **opts) {|req| ... } ⇒ Object

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

Yields:

  • (req)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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

#artifactObject

Artifact related endpoints. Creates new Jenkins2API::Endpoint::Artifact instance



46
47
48
# File 'lib/client.rb', line 46

def artifact
  @artifact ||= Endpoint::Artifact.new(self)
end

#buildObject

Build related endpoints. Creates new Jenkins2API::Endpoint::Build instance



40
41
42
# File 'lib/client.rb', line 40

def build
  @build ||= Endpoint::Build.new(self)
end

#configurationObject

Configuration related endpoints. Creates new Jenkins2API::Endpoint::Configuration instance



52
53
54
# File 'lib/client.rb', line 52

def configuration
  @configuration ||= Endpoint::Configuration.new(self)
end

#handle_response(response, response_type) ⇒ Object

Handles response based on response_type



103
104
105
106
107
108
109
110
111
112
113
# 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

#jobObject

Job related endpoints. Creates new Jenkins2API::Endpoint::Job instance



34
35
36
# File 'lib/client.rb', line 34

def job
  @job ||= Endpoint::Job.new(self)
end

#new_request(method, path, response_type, **opts) ⇒ Object

Creates a new request for the API call with default and required values



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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

#nodeObject

Node/Computer related endpoints. Creates new Jenkins2API::Endpoint::Node instance



58
59
60
# File 'lib/client.rb', line 58

def node
  @node ||= Endpoint::Node.new(self)
end