class Ruhue

Constants

APIError
TimeoutError
VERSION

@see semver.org/

Attributes

host[R]

@return [String] hue host

Public Class Methods

discover(timeout = 5) click to toggle source

Search for a Hue hub, with configurable timeout.

This sends out a broadcast packet with UDP, and waits for a response from the Hue hub.

@param [Integer] timeout seconds until giving up @raise [Ruhue::TimeoutError] in case timeout is reached @return [Hub]

# File lib/ruhue.rb, line 23
def discover(timeout = 5)
  socket  = UDPSocket.new(Socket::AF_INET)
  payload = []
  payload << "M-SEARCH * HTTP/1.1"
  payload << "HOST: 239.255.255.250:1900"
  payload << "MAN: ssdp:discover"
  payload << "MX: 10"
  payload << "ST: ssdp:all"
  socket.send(payload.join("\n"), 0, "239.255.255.250", 1900)

  Timeout.timeout(timeout, Ruhue::TimeoutError) do
    loop do
      message, (_, _, hue_ip, _) = socket.recvfrom(1024)
      # TODO: improve this. How do we know it’s a Hue hub?
      return new(hue_ip) if message =~ /description\.xml/
    end
  end
end
new(host) click to toggle source

@example

hue = Ruhue.new("192.168.0.21")

@param [String] host address to the Hue hub.

# File lib/ruhue.rb, line 47
def initialize(host)
  @host = host.to_str
end

Public Instance Methods

delete(path) click to toggle source

DELETE a resource.

@param [String] path @return [Ruhue::Response]

# File lib/ruhue.rb, line 90
def delete(path)
  request(:delete, path)
end
description() click to toggle source

@return [Nokogiri::XML] Hue device description

# File lib/ruhue.rb, line 95
def description
  Nokogiri::XML(get("/description.xml").body)
end
get(path) click to toggle source

GET a path of the Hue.

@param [String] path @return [Ruhue::Response]

# File lib/ruhue.rb, line 64
def get(path)
  request(:get, path)
end
post(path, data) click to toggle source

POST a payload to the Hue.

@param [String] path @param data json-serializable @return [Ruhue::Response]

# File lib/ruhue.rb, line 73
def post(path, data)
  request(:post, path, JSON.dump(data))
end
put(path, data) click to toggle source

PUT a payload to the Hue.

@param [String] path @param data json-serializable @return [Ruhue::Response]

# File lib/ruhue.rb, line 82
def put(path, data)
  request(:put, path, JSON.dump(data))
end
url(path) click to toggle source

@param [String] path @return [String] full url

# File lib/ruhue.rb, line 56
def url(path)
  "http://#{host}/#{path.to_s.sub(/\A\//, "")}"
end

Protected Instance Methods

request(method, path, *args) click to toggle source
# File lib/ruhue.rb, line 101
def request(method, path, *args)
  response = HTTPI.send(method, url(path), *args)
  Ruhue::Response.new(response)
end