class FileboundClient::Connection

Encapsulates low level logic to talk to Filebound server

Attributes

token[R]

The token returned from the Filebound server on successfully logging in @return [Guid] the security token used in all requests made to the Filebound server

Public Class Methods

build_connection(config) click to toggle source

Creates a new connection using the supplied configuration @param [Configuration] config the Configuration to use for the connection @return [Connection] the newly created and initialized Connection

# File lib/filebound_client/connection.rb, line 17
def self.build_connection(config)
  new(config)
end

Private Class Methods

new(config) click to toggle source

Initialize a new connection with the supplied configuration @param [Configuration] config the Configuration to use for the connection @return [Connection] the newly created and initialized Connection

# File lib/filebound_client/connection.rb, line 24
def initialize(config)
  configure do |c|
    c.host = config[:host]
    c.api_base_uri = config[:api_base_uri]
    c.username = config[:username]
    c.password = config[:password]
    # rubocop:disable Metrics/LineLength
    c.ntlm_auth = { user: config[:ntlm_user], password: config[:ntlm_password], domain: config[:ntlm_domain] } if config[:use_ntlm]
    # rubocop:enable Metrics/LineLength
    HTTPI.adapter = :net_http
  end
end

Public Instance Methods

api_base_uri() click to toggle source

The base path to the API on the Filebound server @return [String] the base path to the API @example

"/api"
# File lib/filebound_client/connection.rb, line 51
def api_base_uri
  configuration.api_base_uri
end
auth_params() click to toggle source

The authentication query parameters required by all requests to the API @return [Hash] the query parameters hash

# File lib/filebound_client/connection.rb, line 97
def auth_params
  { query: { guid: token } }
end
delete(url, params) click to toggle source

Sends a DELETE request to the supplied resource using the supplied params hash @param [String] url the url that represents the resource @param [Hash] params the params Hash that will be sent in the request (keys: query, headers, body) @return [Net::HTTPResponse] the response from the DELETE request

# File lib/filebound_client/connection.rb, line 134
def delete(url, params)
  request = HTTPI::Request.new(resource_url(url, query_params(params[:query])))
  execute_request(:delete, request, params)
end
get(url, params) click to toggle source

Sends a GET request to the supplied resource using the supplied params hash @param [String] url the url that represents the resource @param [Hash] params the params Hash that will be sent in the request (keys: query, headers, body) @return [Net::HTTPResponse] the response from the GET request

# File lib/filebound_client/connection.rb, line 105
def get(url, params)
  request = HTTPI::Request.new(resource_url(url, query_params(params[:query])))
  execute_request(:get, request, params)
end
host() click to toggle source

The Filebound server hostname and protocol @return [String] the Filebound server URL @example

"http://localhost"
# File lib/filebound_client/connection.rb, line 43
def host
  configuration.host
end
login() click to toggle source

Sends a POST request to the Filebound API's login endpoint to request a new security token @return [true, false] returns true if the login was successful and the token was set

# File lib/filebound_client/connection.rb, line 141
def login
  response = post('/login', body: { username: configuration.username, password: configuration.password },
                            headers: { 'Content-Type' => 'application/json' })
  if response.code == 200
    @token = JSON.parse(response.body, symbolize_names: true, quirks_mode: true)
    true
  else
    false
  end
end
ntlm_domain() click to toggle source

The NTLM domain to use for NTLM Authentication @return [String] the NTLM domain @example

"ntlm_domain"
# File lib/filebound_client/connection.rb, line 91
def ntlm_domain
  configuration.ntlm_auth[:domain] if configuration.ntlm_auth
end
ntlm_password() click to toggle source

The NTLM password to use for NTLM Authentication @return [String] the NTLM password @example

"ntlm_password"
# File lib/filebound_client/connection.rb, line 83
def ntlm_password
  configuration.ntlm_auth[:password] if configuration.ntlm_auth
end
ntlm_user() click to toggle source

The NTLM username to use for NTLM Authentication @return [String] the NTLM username @example

"ntlm_user"
# File lib/filebound_client/connection.rb, line 75
def ntlm_user
  configuration.ntlm_auth[:user] if configuration.ntlm_auth
end
password() click to toggle source

The password to log on to the Filebound server with @return [String] the password @example

"password"
# File lib/filebound_client/connection.rb, line 67
def password
  configuration.password
end
post(url, params) click to toggle source

Sends a POST request to the supplied resource using the supplied params hash @param [String] url the url that represents the resource @param [Hash] params the params Hash that will be sent in the request (keys: query, headers, body) @return [Net::HTTPResponse] the response from the POST request

# File lib/filebound_client/connection.rb, line 124
def post(url, params)
  request = HTTPI::Request.new(resource_url(url, query_params(params[:query])))
  request.body = params[:body].to_json
  execute_request(:post, request, params)
end
put(url, params) click to toggle source

Sends a PUT request to the supplied resource using the supplied params hash @param [String] url the url that represents the resource @param [Hash] params the params Hash that will be sent in the request (keys: query, headers, body) @return [Net::HTTPResponse] the response from the PUT request

# File lib/filebound_client/connection.rb, line 114
def put(url, params)
  request = HTTPI::Request.new(resource_url(url, query_params(params[:query])))
  request.body = params[:body].to_json
  execute_request(:put, request, params)
end
username() click to toggle source

The username to log on to the Filebound server with @return [String] the username @example

"username"
# File lib/filebound_client/connection.rb, line 59
def username
  configuration.username
end

Private Instance Methods

execute_request(method, request, params) click to toggle source
# File lib/filebound_client/connection.rb, line 177
def execute_request(method, request, params)
  request = set_headers(request, params[:headers])
  request.auth.ntlm(ntlm_user, ntlm_password, ntlm_domain) if configuration.ntlm_auth
  HTTPI.request(method, request)
end
query_params(params) click to toggle source
# File lib/filebound_client/connection.rb, line 160
def query_params(params)
  if params
    auth_params[:query].merge!(params)
  else
    auth_params[:query]
  end
end
resource_url(url, query) click to toggle source
# File lib/filebound_client/connection.rb, line 154
def resource_url(url, query)
  return "http://#{host}#{api_base_uri}/#{url.reverse.chomp('/').reverse}" unless query
  query_string = query.map { |k, v| "#{k}=#{v}" }.join('&')
  "http://#{host}#{api_base_uri}/#{url.reverse.chomp('/').reverse}?#{query_string}"
end
set_headers(request, headers) click to toggle source
# File lib/filebound_client/connection.rb, line 168
def set_headers(request, headers)
  if headers.respond_to?(:to_hash)
    headers.each do |k, v|
      request.headers[k.to_s] = v.to_s
    end
  end
  request
end