class ARest

Public Class Methods

new(base_url, **options) click to toggle source

Input:

  • base_url - the base URL for REST API

Options:

# File lib/arest.rb, line 40
def initialize(base_url, **options)
  @base_url = base_url
  @headers = options[:headers]
  @auth_user = options[:username]
  @auth_password = options[:password]
  @auth_token = options[:token]
end

Public Instance Methods

delete(path, **options) click to toggle source

Perform a HTTP DELETE request. Requires a path to current operation If given, headers and http authentication can be override

Input

  • path - relative to URI given in constructor

Options Hash

  • headers - overrides the header given in constructor

  • username, password - overrides the http authentication

  • token - overrides authorization token

  • form_data - hash with HTML form data

# File lib/arest.rb, line 100
def delete(path, **options)
  execute :delete, path, options
end
get(path, **options) click to toggle source

Perform a HTTP GET request. Requires a path to current operation If given, headers and http authentication can be override

Input

  • path - relative to URI given in constructor

Options Hash

  • headers - overrides the header given in constructor

  • username, password - overrides the http authentication

  • token - overrides authorization token

  • form_data - hash with HTML form data

# File lib/arest.rb, line 58
def get(path, **options)
  execute :get, path, options
end
inspect() click to toggle source
# File lib/arest.rb, line 104
def inspect
  "<ARest #{@base_url}#{if @headers then ', with headers' else '' end}#{if @auth_user && @auth_password then ', authenticating as '+@auth_user else '' end}>"
end
post(path, **options) click to toggle source

Perform a HTTP POST request. Requires a path to current operation If given, headers and http authentication can be override

Input

  • path - relative to URI given in constructor

Options Hash

  • headers - overrides the header given in constructor

  • username, password - overrides the http authentication

  • token - overrides authorization token

  • form_data - hash with HTML form data

# File lib/arest.rb, line 72
def post(path, **options)
  execute :post, path, options
end
put(path, **options) click to toggle source

Perform a HTTP PUT request. Requires a path to current operation If given, headers and http authentication can be override

Input

  • path - relative to URI given in constructor

Options Hash

  • headers - overrides the header given in constructor

  • username, password - overrides the http authentication

  • token - overrides authorization token

  • form_data - hash with HTML form data

# File lib/arest.rb, line 86
def put(path, **options)
  execute :put, path, options
end

Private Instance Methods

execute(method, path, **options) click to toggle source

Perform a HTTP request. Requires method name a path to current operation. If given, headers and http authentication can be override

Input

  • method - HTTP method, :get, :post, :delete, :header, :update

  • path - relative to URI given in constructor

Options Hash

  • headers - overrides the header given in constructor

  • username, password - overrides the http authentication

  • token - overrides authorization token

  • form_data - hash with HTML form data

# File lib/arest.rb, line 120
def execute(method, path, **options)
  uri = URI("#{@base_url}/#{path}")

  case method.to_sym
  when :get
    req = Net::HTTP::Get.new(uri)
  when :post
    req = Net::HTTP::Post.new(uri)
  when :put
    req = Net::HTTP::Put.new(uri)
  when :delete
    req = Net::HTTP::Delete.new(uri)
  else
    raise ARestException, "Unknown method: #{method}"
  end

  req.form_data = options[:form_data] if options[:form_data]

  headers = options[:headers] || @headers 
  headers.each { |k,v| req[k] = v } if headers

  auth_user = options[:auth_user] || @auth_user
  auth_password = options[:auth_password] || @auth_password
  req.basic_auth auth_user, auth_password if auth_user && auth_password

  token = options[:token] || @auth_token
  if token
    req["Authorization"] = "Token token=#{token}"
  end

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(req)
  end
  res
end