class Gerrit

Public Instance Methods

change(change_id) click to toggle source
# File lib/gerrit.rb, line 46
def change(change_id)
  Change.new(self, change_id)
end
get(endpoint) click to toggle source
# File lib/gerrit.rb, line 7
def get endpoint
  request endpoint
end
post(endpoint, body = '') click to toggle source
# File lib/gerrit.rb, line 11
def post endpoint, body = ''
  request endpoint, method: 'post', body: body.to_s
end
put(endpoint, body = {}) click to toggle source
# File lib/gerrit.rb, line 15
def put endpoint, body = {}
  request endpoint, method: 'put', body: JSON.dump(body)
end

Private Instance Methods

request(endpoint, method: 'get', body: nil) click to toggle source
# File lib/gerrit.rb, line 52
def request endpoint, method: 'get', body: nil
  if method == 'get' && body.nil?
    @get_caches ||= {}
    @get_caches[endpoint] ||= send_request endpoint, method: method, body: body
  else
    send_request endpoint, method: method, body: body
  end
end
send_request(endpoint, method: 'get', body: nil) click to toggle source
# File lib/gerrit.rb, line 61
def send_request endpoint, method: 'get', body: nil
  req = HTTPI::Request.new File.join(base_url, username ? 'a' : '', endpoint)
  req.auth.digest username, password if username && password && !username.empty?
  req.auth.ssl.verify_mode = :none
  if body
    req.body = body
    req.headers['Content-Type'] = 'application/json;charset=UTF-8'
  end
  res = HTTPI.send method, req
  content_type = [*res.headers['Content-Type']].last.split(/[ ;]/).first
  case content_type
  when 'application/json'
    JSON.parse res.body.lines[1..-1].join
  when 'text/plain'
    case res.headers['X-FYI-Content-Encoding']
    when 'base64'
      Base64::decode64 res.body
    else
      res.body
    end
  else
    raise "Unknown content type: #{content_type}"
  end
end