class FileboundClient::Client

This encapsulates all client/server communications

Attributes

connection[R]

The connection representing the currently logged on session with the Filebound API @return [FileboundClient::Connection] the security token used in all requests made to the Filebound server

Public Class Methods

connect(config) click to toggle source

Creates, initialize and logs into the Filebound API @param [Hash] config a hash of configuration values @return [FileboundClient::Client] an instance of FileboundClient::Client

# File lib/filebound_client.rb, line 27
def self.connect(config)
  connection = FileboundClient::Connection.build_connection(config)
  raise FileboundClientException.new('Failed to login', 401) unless connection.login
  new(connection)
end

Private Class Methods

new(connection) click to toggle source

Initializes the client with the supplied Connection @param [Connection] connection the logged in Connection @return [FileboundClient::Client] an instance of FileboundClient::Client

# File lib/filebound_client.rb, line 36
def initialize(connection)
  @connection = connection
end

Public Instance Methods

delete(url, query_params = nil) click to toggle source

Executes a DELETE request on the current Filebound client session @param [String] url the resource url to request @param [Hash] query_params the optional query parameters to pass to the DELETE request @return [true, false] true if resource was deleted successfully

# File lib/filebound_client.rb, line 80
def delete(url, query_params = nil)
  perform('delete', url, query: query_params)
  true
end
get(url, query_params = nil) click to toggle source

Executes a GET request on the current Filebound client session expecting JSON in the body of the response @param [String] url the resource url to request @param [Hash] query_params the optional query parameters to pass to the GET request @return [Hash] the JSON parsed hash of the response body

# File lib/filebound_client.rb, line 44
def get(url, query_params = nil)
  JSON.parse(perform('get', url, query: query_params), symbolize_names: true, quirks_mode: true)
end
get_binary(url, query_params = nil) click to toggle source

Executes a GET request on the current Filebound client session expecting binary in the body of the response @param [String] url the resource url to request @param [Hash] query_params the optional query parameters to pass to the GET request @return [String] Base64 encoded binary string

# File lib/filebound_client.rb, line 52
def get_binary(url, query_params = nil)
  perform('get', url, query: query_params)
end
post(url, query_params = nil, body = nil) click to toggle source

Executes a POST request on the current Filebound client session expecting JSON in the body of the request/response @param [String] url the resource url to request @param [Hash] query_params the optional query parameters to pass to the POST request @param [Hash] body the hash that will be converted to JSON when inserted in the body of the request @return [Hash] the JSON parsed hash of the response body

# File lib/filebound_client.rb, line 71
def post(url, query_params = nil, body = nil)
  params = { headers: { 'Content-Type' => 'application/json' }, query: query_params, body: body }
  JSON.parse(perform('post', url, params), symbolize_names: true, quirks_mode: true)
end
put(url, query_params = nil, body = nil) click to toggle source

Executes a PUT request on the current Filebound client session expecting JSON in the body of the request/response @param [String] url the resource url to request @param [Hash] query_params the optional query parameters to pass to the PUT request @param [Hash] body the hash that will be converted to JSON when inserted in the body of the request @return [Hash] the JSON parsed hash of the response body

# File lib/filebound_client.rb, line 61
def put(url, query_params = nil, body = nil)
  params = { headers: { 'Content-Type' => 'application/json' }, query: query_params, body: body }
  JSON.parse(perform('put', url, params), symbolize_names: true, quirks_mode: true)
end

Private Instance Methods

perform(action, url, params) click to toggle source
# File lib/filebound_client.rb, line 93
    def perform(action, url, params)
      response = connection.send(action, url, params)
      if response.error?
        message = <<~ERROR
          #{action.upcase} request failed: #{response.code}
          Headers: #{response.headers}
          Body: #{response.body}
        ERROR
        raise FileboundClientException.new(message, response.code)
      end

      if response.body.nil? || response.body.to_s == ''
        '{}'
      else
        response.body
      end
    end