Class: Bravtroller::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bravtroller/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(bravia_address) ⇒ Client

Returns a new instance of Client.



6
7
8
# File 'lib/bravtroller/client.rb', line 6

def initialize(bravia_address)
  @bravia_address = bravia_address
end

Instance Method Details

#hw_addrObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bravtroller/client.rb', line 10

def hw_addr
  arp = `which arp`.strip

  raise RuntimeError.new "Couldn't find arp binary" if arp.empty?

  result = `#{arp} #{@bravia_address}`
  hw_addr_match = result.match(/((?:[a-f0-9]{1,2}:?){6})/i)
  if hw_addr_match.nil?
    nil
  else
    # On my mac, octets with the most significant nibble = 0 show as "0" instead of "00"
    hw_addr_match
        .captures
        .first
        .split(':')
        .map { |x| "#{x.length == 1 ? '0' : ''}#{x}" }
        .join(':')
  end
end

#post_request(path, params = {}, headers = {}) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/bravtroller/client.rb', line 30

def post_request(path, params = {}, headers = {})
  json = JSON.generate(params)
  uri = URI("http://#{@bravia_address}#{path}")

  Net::HTTP.start(uri.host, uri.port) do |http|
    return http.post(path, json, headers)
  end
end