class StashNotifier::HTTPClient

Constants

DEFAULTS
STASH_BUILD_STATUS_PATH

Attributes

build_result[RW]
git_commit[RW]
http_client[RW]
stash_pwd[RW]
stash_url[RW]
stash_user[RW]

Public Class Methods

new(args) click to toggle source
# File lib/stash_notifier/http_client.rb, line 31
def initialize(args)
  args = DEFAULTS.merge(args)

  @stash_url    = args[:stash_url]
  @stash_user   = args[:stash_user]
  @stash_pwd    = args[:stash_pwd]
  @git_commit   = args[:git_commit]
  @build_result = args[:build_result]
  @http_client  = args[:http_client]
end

Public Instance Methods

post_to_stash() click to toggle source
# File lib/stash_notifier/http_client.rb, line 51
def post_to_stash
  setup_http_client
  response = post_results
  check_errors(response)
  response
end
to_s() click to toggle source
# File lib/stash_notifier/http_client.rb, line 42
def to_s
  "StashNotifier connection:
  url: #{stash_url},
  git_commit: #{git_commit},
  user: #{stash_user},
  pwd: ***********,
  build_result: #{build_result}"
end

Private Instance Methods

check_errors(response) click to toggle source
# File lib/stash_notifier/http_client.rb, line 75
def check_errors(response)
  unless response.success?

    err_msg = get_stash_err(response)

    case response.status
    when 400
      raise StashNotifier::BadRequest, err_msg
    when 401
      raise StashNotifier::Unauthorized, err_msg
    else
      raise StashNotifier::Error, err_msg
    end
  end
end
get_stash_err(response) click to toggle source
# File lib/stash_notifier/http_client.rb, line 91
def get_stash_err(response)
  response_body = JSON.parse(response.body)
  response_body['errors'].first['message']
end
post_results() click to toggle source
# File lib/stash_notifier/http_client.rb, line 67
def post_results
  http_client.post do |req|
    req.url git_commit
    req.headers['Content-Type'] = 'application/json'
    req.body = build_result
  end
end
setup_http_client() click to toggle source
# File lib/stash_notifier/http_client.rb, line 60
def setup_http_client
  http_client.url_prefix = stash_url
  http_client.path_prefix = STASH_BUILD_STATUS_PATH
  http_client.basic_auth(stash_user, stash_pwd)
  http_client.ssl[:verify] = false
end