Class: FileboundClient::Connection

Inherits:
Object
  • Object
show all
Includes:
Config
Defined in:
lib/filebound_client/connection.rb

Overview

Encapsulates low level logic to talk to Filebound server

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config

#configuration, #configure

Constructor Details

#initialize(config) ⇒ Connection

Initialize a new connection with the supplied configuration



24
25
26
27
28
29
30
31
32
33
34
35
# 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

Instance Attribute Details

#tokenGuid (readonly)

The token returned from the Filebound server on successfully logging in



12
13
14
# File 'lib/filebound_client/connection.rb', line 12

def token
  @token
end

Class Method Details

.build_connection(config) ⇒ Connection

Creates a new connection using the supplied configuration



17
18
19
# File 'lib/filebound_client/connection.rb', line 17

def self.build_connection(config)
  new(config)
end

Instance Method Details

#api_base_uriString

The base path to the API on the Filebound server

Examples:

"/api"


51
52
53
# File 'lib/filebound_client/connection.rb', line 51

def api_base_uri
  configuration.api_base_uri
end

#auth_paramsHash

The authentication query parameters required by all requests to the API



97
98
99
# File 'lib/filebound_client/connection.rb', line 97

def auth_params
  { query: { guid: token } }
end

#delete(url, params) ⇒ Net::HTTPResponse

Sends a DELETE request to the supplied resource using the supplied params hash



134
135
136
137
# 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) ⇒ Net::HTTPResponse

Sends a GET request to the supplied resource using the supplied params hash



105
106
107
108
# 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

#hostString

The Filebound server hostname and protocol

Examples:

"http://localhost"


43
44
45
# File 'lib/filebound_client/connection.rb', line 43

def host
  configuration.host
end

#logintrue, false

Sends a POST request to the Filebound API’s login endpoint to request a new security token



141
142
143
144
145
146
147
148
149
150
# File 'lib/filebound_client/connection.rb', line 141

def 
  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_domainString

The NTLM domain to use for NTLM Authentication

Examples:

"ntlm_domain"


91
92
93
# File 'lib/filebound_client/connection.rb', line 91

def ntlm_domain
  configuration.ntlm_auth[:domain] if configuration.ntlm_auth
end

#ntlm_passwordString

The NTLM password to use for NTLM Authentication

Examples:

"ntlm_password"


83
84
85
# File 'lib/filebound_client/connection.rb', line 83

def ntlm_password
  configuration.ntlm_auth[:password] if configuration.ntlm_auth
end

#ntlm_userString

The NTLM username to use for NTLM Authentication

Examples:

"ntlm_user"


75
76
77
# File 'lib/filebound_client/connection.rb', line 75

def ntlm_user
  configuration.ntlm_auth[:user] if configuration.ntlm_auth
end

#passwordString

The password to log on to the Filebound server with

Examples:

"password"


67
68
69
# File 'lib/filebound_client/connection.rb', line 67

def password
  configuration.password
end

#post(url, params) ⇒ Net::HTTPResponse

Sends a POST request to the supplied resource using the supplied params hash



124
125
126
127
128
# 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) ⇒ Net::HTTPResponse

Sends a PUT request to the supplied resource using the supplied params hash



114
115
116
117
118
# 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

#usernameString

The username to log on to the Filebound server with

Examples:

"username"


59
60
61
# File 'lib/filebound_client/connection.rb', line 59

def username
  configuration.username
end