class SimpleJenkins::Adapter

Public Class Methods

new(url, username: nil, password: nil) click to toggle source
# 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

Public Instance Methods

build_job(job, params = {}) click to toggle source
# 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_jobs() click to toggle source
# 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_views() click to toggle source
# 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

Private Instance Methods

extract_attrs(model) click to toggle source
# File lib/simple_jenkins/adapter.rb, line 73
def extract_attrs(model)
  model
    .attribute_set
    .map { |attr| get_attr_name(attr) }
end
get_attr_name(attr) click to toggle source
# File lib/simple_jenkins/adapter.rb, line 79
def get_attr_name(attr)
  if attr.primitive.name.include?("SimpleJenkins::")
    "#{attr.name}[#{extract_attrs(attr.primitive).join(",")}]"
  elsif attr.is_a?(Virtus::Attribute::Collection) && attr.type.member_type != Axiom::Types::Object
    "#{attr.name}[#{extract_attrs(attr.type.member_type).join(",")}]"
  else
    attr.name
  end
end
headers() click to toggle source
# File lib/simple_jenkins/adapter.rb, line 65
def headers
  return {} unless @auth

  {
    "Authorization" => "Basic #{Base64.encode64(@auth).chomp}"
  }
end