class Request

Public Class Methods

delete(path) click to toggle source
# File lib/busbar_cli/helpers/request.rb, line 24
def delete(path)
  request = Net::HTTP::Delete.new(uri_for(path))
  execute(request)
end
get(path) click to toggle source
# File lib/busbar_cli/helpers/request.rb, line 5
def get(path)
  request = Net::HTTP::Get.new(uri_for(path))
  execute(request)
end
post(path, body) click to toggle source
# File lib/busbar_cli/helpers/request.rb, line 10
def post(path, body)
  request      = Net::HTTP::Post.new(uri_for(path))
  request.body = body.to_json
  request['Content-Type'] = 'application/json'
  execute(request)
end
put(path, body) click to toggle source
# File lib/busbar_cli/helpers/request.rb, line 17
def put(path, body)
  request      = Net::HTTP::Put.new(uri_for(path))
  request.body = body.to_json
  request['Content-Type'] = 'application/json'
  execute(request)
end

Private Class Methods

api_url() click to toggle source
# File lib/busbar_cli/helpers/request.rb, line 35
def api_url
  BUSBAR_API_URL
end
execute(request) click to toggle source
# File lib/busbar_cli/helpers/request.rb, line 39
def execute(request)
  response = nil

  Net::HTTP.start(request.uri.host, request.uri.port) do |http|
    response = http.request(request)
  end

  case response.code
  when '500'
    exit_with_error_warn(request)
  when '503'
    exit_with_503_warn(request)
  when '504'
    exit_with_504_warn(request)
  end

  response
rescue SocketError
  puts "No connection could be established with the Busbar server (#{api_url}).\n" \
       'You may need to connect to a VPN to access it.'
  exit 0
end
exit_with_503_warn(request) click to toggle source
# File lib/busbar_cli/helpers/request.rb, line 71
def exit_with_503_warn(request)
  puts "(#{api_url}) service is unavailable.\n" \
        "URI: #{request.uri}\n" \
        'Response code: 503'
  exit 0
end
exit_with_504_warn(request) click to toggle source
# File lib/busbar_cli/helpers/request.rb, line 78
def exit_with_504_warn(request)
  puts "(#{api_url}) gateway Time-out.\n" \
        "URI: #{request.uri}\n" \
        'Response code: 504'
  exit 0
end
exit_with_error_warn(request) click to toggle source
# File lib/busbar_cli/helpers/request.rb, line 62
def exit_with_error_warn(request)
  puts "Internal error on Busbar server (#{api_url}).\n" \
       "URI: #{request.uri}\n" \
       "Body: #{request.body}\n" \
       'Response code: 500'

  exit 0
end
uri_for(path) click to toggle source
# File lib/busbar_cli/helpers/request.rb, line 31
def uri_for(path)
  URI("#{api_url}#{path.sub(/^\\+/, '')}")
end