Class: SimpleJenkins::Adapter

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

Instance Method Summary collapse

Constructor Details

#initialize(url, username: nil, password: nil) ⇒ Adapter

Returns a new instance of Adapter.



6
7
8
9
# File 'lib/simple_jenkins/adapter.rb', line 6

def initialize(url, username: nil, password: nil)
  @auth = "#{username}:#{password}" if username && password
  @jenkins_url = url
end

Instance Method Details

#build_job(job, params = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simple_jenkins/adapter.rb', line 11

def build_job(job, params = {})
  if params.empty?
    path = URI::encode("#{@jenkins_url}/job/#{job.name}/build")
  else
    path = URI::encode("#{@jenkins_url}/job/#{job.name}/buildWithParameters")
  end

  result = RestClient.post(path, params, headers)

  return Response.new(
    code: result.code,
    message: result
  )

rescue RestClient::Exception => e
  return Response.new(
    code: e.response.code,
    message: e.response
  )
end

#fetch_jobsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/simple_jenkins/adapter.rb', line 32

def fetch_jobs
  attrs = extract_attrs(Job)

  path = "#{@jenkins_url}/api/json?tree=jobs[#{attrs.join(",")}]"

  api_response = RestClient.get(path, headers)

  JSON
    .parse(api_response.body)
    .fetch("jobs", {})
    .map { |job_hash| Job.new(job_hash) }

rescue RestClient::Exception => e
  raise ApiException, e.response
end

#fetch_viewsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/simple_jenkins/adapter.rb', line 48

def fetch_views
  attrs = extract_attrs(View)

  path = "#{@jenkins_url}/api/json?tree=views[#{attrs.join(",")}]"
  api_response = RestClient.get(path, headers)

  JSON
    .parse(api_response.body)
    .fetch("views", {})
    .map { |view_hash| View.new(view_hash) }

rescue RestClient::Exception => e
  raise ApiException, e.message
end