Class: VlcProxy::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/vlc_proxy/connection.rb

Overview

When HTTP enabled on VLC, a LUA web server runs in background listening

on the specific port on localhost by default. VLC requires setting a
password for basic auth as well

Instance Method Summary collapse

Constructor Details

#initialize(hostname, password, port = 8080, scheme = 'http') ⇒ Connection

Returns a new instance of Connection.



9
10
11
12
13
14
15
# File 'lib/vlc_proxy/connection.rb', line 9

def initialize(hostname, password, port = 8080, scheme = 'http')
  @hostname = hostname
  @password = password
  @port = port
  @scheme = scheme
  @logger = VlcProxy.config.logger
end

Instance Method Details

#connected?Boolean

Test if the connection works with the connection parameters

Returns true if VLC is running and returns a response on /status
Returns false if there are any connection errors

Returns:

  • (Boolean)


20
21
22
23
24
25
26
# File 'lib/vlc_proxy/connection.rb', line 20

def connected?
  response = execute('status')
  valid_response?(response)
rescue VlcProxy::AccessDeniedError, Errno::ECONNREFUSED => e
  @logger.error(e.message)
  false
end

#execute(action, command = '', parameters = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/vlc_proxy/connection.rb', line 28

def execute(action, command = '', parameters = {})
  uri = build_uri(action, command, parameters)
  request = Net::HTTP::Get.new(uri)
  request.basic_auth('', @password)

  Net::HTTP.start(uri.hostname, uri.port) do |http|
    @logger.debug('Starting HTTP request')
    http.request(request)
  end
end