class Tr3llo::RemoteServer

Constants

EXPECTED_CODES

Attributes

endpoint_url[R]

Public Class Methods

new(endpoint_url) click to toggle source
# File lib/3llo/remote_server.rb, line 24
def initialize(endpoint_url)
  @endpoint_url = endpoint_url
end

Public Instance Methods

delete(req_path, req_headers, payload, expected_codes = EXPECTED_CODES) click to toggle source
# File lib/3llo/remote_server.rb, line 63
def delete(req_path, req_headers, payload, expected_codes = EXPECTED_CODES)
  req_uri = build_request_uri(req_path)

  req_headers = {
    "Accept" => "application/json",
    "Content-Type" => "application/json"
  }.merge(req_headers)

  request = Net::HTTP::Delete.new(req_uri, req_headers)
  request.body = JSON.dump(payload)

  dispatch(request, expected_codes)
end
get(req_path, req_headers, expected_codes = EXPECTED_CODES) click to toggle source
# File lib/3llo/remote_server.rb, line 28
def get(req_path, req_headers, expected_codes = EXPECTED_CODES)
  req_uri = build_request_uri(req_path)
  req_headers = {"accept" => "application/json"}.merge(req_headers)

  dispatch(Net::HTTP::Get.new(req_uri, req_headers), expected_codes)
end
post(req_path, req_headers, payload, expected_codes = EXPECTED_CODES) click to toggle source
# File lib/3llo/remote_server.rb, line 35
def post(req_path, req_headers, payload, expected_codes = EXPECTED_CODES)
  req_uri = build_request_uri(req_path)

  req_headers = {
    "accept" => "application/json",
    "content-type" => "application/json"
  }.merge(req_headers)

  request = Net::HTTP::Post.new(req_uri, req_headers)
  request.body = JSON.dump(payload)

  dispatch(request, expected_codes)
end
put(req_path, req_headers, payload, expected_codes = EXPECTED_CODES) click to toggle source
# File lib/3llo/remote_server.rb, line 49
def put(req_path, req_headers, payload, expected_codes = EXPECTED_CODES)
  req_uri = build_request_uri(req_path)

  req_headers = {
    "Accept" => "application/json",
    "Content-Type" => "application/json"
  }.merge(req_headers)

  request = Net::HTTP::Put.new(req_uri, req_headers)
  request.body = JSON.dump(payload)

  dispatch(request, expected_codes)
end

Private Instance Methods

build_request_uri(req_path) click to toggle source
# File lib/3llo/remote_server.rb, line 79
def build_request_uri(req_path)
  URI.parse(endpoint_url + req_path)
rescue URI::Error
  raise InvalidArgumentError.new("invalid command arguments")
end
dispatch(request, expected_status_codes) click to toggle source
# File lib/3llo/remote_server.rb, line 85
def dispatch(request, expected_status_codes)
  req_uri = request.uri

  Net::HTTP.start(req_uri.host, req_uri.port, use_ssl: req_uri.scheme == "https") do |http|
    response = http.request(request)

    if expected_status_codes.include?(response.code)
      JSON.parse(response.body)
    else
      raise RequestError.new(response)
    end
  end
end