Class: Ruhue

Inherits:
Object
  • Object
show all
Defined in:
lib/ruhue.rb,
lib/ruhue/version.rb

Defined Under Namespace

Classes: Client, Response

Constant Summary collapse

TimeoutError =
Class.new(TimeoutError)
APIError =
Class.new(StandardError)
VERSION =

See Also:

"0.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Ruhue

Returns a new instance of Ruhue.

Examples:

hue = Ruhue.new("192.168.0.21")

Parameters:

  • host (String)

    address to the Hue hub.



47
48
49
# File 'lib/ruhue.rb', line 47

def initialize(host)
  @host = host.to_str
end

Instance Attribute Details

#hostString (readonly)

Returns hue host.

Returns:

  • (String)

    hue host



52
53
54
# File 'lib/ruhue.rb', line 52

def host
  @host
end

Class Method Details

.discover(timeout = 5) ⇒ Hub

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.

Parameters:

  • timeout (Integer) (defaults to: 5)

    seconds until giving up

Returns:

  • (Hub)

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

Instance Method Details

#delete(path) ⇒ Ruhue::Response

DELETE a resource.

Parameters:

  • path (String)

Returns:



90
91
92
# File 'lib/ruhue.rb', line 90

def delete(path)
  request(:delete, path)
end

#descriptionNokogiri::XML

Returns Hue device description.

Returns:

  • (Nokogiri::XML)

    Hue device description



95
96
97
# File 'lib/ruhue.rb', line 95

def description
  Nokogiri::XML(get("/description.xml").body)
end

#get(path) ⇒ Ruhue::Response

GET a path of the Hue.

Parameters:

  • path (String)

Returns:



64
65
66
# File 'lib/ruhue.rb', line 64

def get(path)
  request(:get, path)
end

#post(path, data) ⇒ Ruhue::Response

POST a payload to the Hue.

Parameters:

  • path (String)
  • data

    json-serializable

Returns:



73
74
75
# File 'lib/ruhue.rb', line 73

def post(path, data)
  request(:post, path, JSON.dump(data))
end

#put(path, data) ⇒ Ruhue::Response

PUT a payload to the Hue.

Parameters:

  • path (String)
  • data

    json-serializable

Returns:



82
83
84
# File 'lib/ruhue.rb', line 82

def put(path, data)
  request(:put, path, JSON.dump(data))
end

#url(path) ⇒ String

Returns full url.

Parameters:

  • path (String)

Returns:

  • (String)

    full url



56
57
58
# File 'lib/ruhue.rb', line 56

def url(path)
  "http://#{host}/#{path.to_s.sub(/\A\//, "")}"
end