class HelperModule::SimpleRequestHelper

Attributes

_debug[RW]
host[R]
path[R]
port[R]

Public Class Methods

new(host, port, path) click to toggle source
# File lib/helpers/simple_http_helper.rb, line 12
def initialize(host, port, path)
  @host, @port, @path = host, port, path
end

Public Instance Methods

_create_request_sender(host, port) click to toggle source
# File lib/helpers/simple_http_helper.rb, line 26
def _create_request_sender(host, port)
  raise NotImplementedError
end
_request_prefix() click to toggle source
# File lib/helpers/simple_http_helper.rb, line 22
def _request_prefix
  raise NotImplementedError
end
delete(*args, &block) click to toggle source
# File lib/helpers/simple_http_helper.rb, line 71
def delete(*args, &block)
  return _send_request(DeleteContent, *args, &block)
end
get(*args, &block) click to toggle source
# File lib/helpers/simple_http_helper.rb, line 63
def get(*args, &block)
  return _send_request(GetContent, *args, &block)
end
loginsight_get(*args, &block) click to toggle source
# File lib/helpers/simple_http_helper.rb, line 75
def loginsight_get(*args, &block)
  return _send_request(LoginsightGetContent, *args, &block)
end
patch(*args, &block) click to toggle source
# File lib/helpers/simple_http_helper.rb, line 67
def patch(*args, &block)
  return _send_request(PatchContent, *args, &block)
end
post(*args, &block) click to toggle source
# File lib/helpers/simple_http_helper.rb, line 59
def post(*args, &block)
  return _send_request(PostContent, *args, &block)
end
set_auth(username, password) click to toggle source
# File lib/helpers/simple_http_helper.rb, line 16
def set_auth(username, password)
  @auth = OpenStruct.new
  @auth.username = username
  @auth.password = password
end

Private Instance Methods

_get_base_uri(*args) click to toggle source
# File lib/helpers/simple_http_helper.rb, line 30
def _get_base_uri(*args)

  port_str = @port && ":#{@port}"
  path_str = @path
  args_str = args.join('/')
  args_str = "/" + args_str if not args_str.empty?

  URI(URI.escape("#{_request_prefix}://#{@host}#{port_str}/#{path_str}#{args_str}"))
end
_send_request(content_klass, *args, &block) click to toggle source
# File lib/helpers/simple_http_helper.rb, line 40
def _send_request(content_klass, *args, &block)
  uri = _get_base_uri(*args)
  content = content_klass.new(uri)

  block.call(content) if block_given?

  if @_debug
    puts content.get_request.uri
  end

  response = _create_request_sender(@host, @port).start do |http|
    request = content.get_request
    request.basic_auth(@auth.username, @auth.password) if @auth
    http.request(request)
  end

  return SimpleHttpResult.new(response)
end