class Gergich::API

Public Class Methods

get(url, options = {}) click to toggle source
# File lib/gergich.rb, line 286
def get(url, options = {})
  perform(:get, url, options)
end
post(url, body, options = {}) click to toggle source
# File lib/gergich.rb, line 290
def post(url, body, options = {})
  perform(:post, url, options.merge(body: body))
end
put(url, body, options = {}) click to toggle source
# File lib/gergich.rb, line 294
def put(url, body, options = {})
  perform(:put, url, options.merge(body: body))
end

Private Class Methods

auth_config() click to toggle source
# File lib/gergich.rb, line 329
def auth_config
  if ENV["GERGICH_DIGEST_AUTH"]
    {
      digest_auth: {
        username: GERGICH_USER,
        password: ENV.fetch("GERGICH_KEY")
      }
    }
  else
    {
      basic_auth: {
        username: GERGICH_USER,
        password: ENV.fetch("GERGICH_KEY")
      }
    }
  end
end
base_uri() click to toggle source
# File lib/gergich.rb, line 322
def base_uri
  @base_uri ||= \
    ENV["GERRIT_BASE_URL"] ||
    (ENV.key?("GERRIT_HOST") && "https://#{ENV['GERRIT_HOST']}") ||
    raise(GergichError, "need to set GERRIT_BASE_URL or GERRIT_HOST")
end
perform(method, url, options) click to toggle source
# File lib/gergich.rb, line 300
def perform(method, url, options)
  # delay requiring httparty until here, to make local command line runs as fast as possible
  require "httparty"
  options = prepare_options(options)
  ret = HTTParty.send(method, url, options).body
  return ret if options[:raw]

  ret = ret.sub(/\A\)\]\}'\n/, "")
  if ret && ret =~ /\A("|\[|\{)/
    JSON.parse("[#{ret}]")[0] # array hack so we can parse a string literal
  elsif ret =~ /Not found: (?<change_id>.*)/i
    raise("Cannot find Change-Id: #{Regexp.last_match[:change_id]} at #{url}.\n"\
          "This is most likely due to this"\
          " Change-Id already being used"\
          " by an ABANDONED change.\n"\
          "To fix, `git commit --amend`,"\
          " remove the Change-Id line, and push again.")
  else
    raise("Non-JSON response: #{ret}")
  end
end
prepare_options(options) click to toggle source
# File lib/gergich.rb, line 347
def prepare_options(options)
  options = {
    base_uri: "#{base_uri}/a"
  }.merge(auth_config).merge(options)
  if options[:body]
    options[:headers] ||= {}
    options[:headers]["Content-Type"] ||= "application/json"
  end
  options
end