Class: Ruhue
- Inherits:
-
Object
- Object
- Ruhue
- Defined in:
- lib/ruhue.rb,
lib/ruhue/version.rb
Defined Under Namespace
Constant Summary collapse
- TimeoutError =
Class.new(TimeoutError)
- APIError =
Class.new(StandardError)
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#host ⇒ String
readonly
Hue host.
Class Method Summary collapse
-
.discover(timeout = 5) ⇒ Hub
Search for a Hue hub, with configurable timeout.
Instance Method Summary collapse
-
#delete(path) ⇒ Ruhue::Response
DELETE a resource.
-
#description ⇒ Nokogiri::XML
Hue device description.
-
#get(path) ⇒ Ruhue::Response
GET a path of the Hue.
-
#initialize(host) ⇒ Ruhue
constructor
A new instance of Ruhue.
-
#post(path, data) ⇒ Ruhue::Response
POST a payload to the Hue.
-
#put(path, data) ⇒ Ruhue::Response
PUT a payload to the Hue.
-
#url(path) ⇒ String
Full url.
Constructor Details
#initialize(host) ⇒ Ruhue
Returns a new instance of Ruhue.
47 48 49 |
# File 'lib/ruhue.rb', line 47 def initialize(host) @host = host.to_str end |
Instance Attribute Details
#host ⇒ String (readonly)
Returns 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.
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 , (_, _, hue_ip, _) = socket.recvfrom(1024) # TODO: improve this. How do we know it’s a Hue hub? return new(hue_ip) if =~ /description\.xml/ end end end |
Instance Method Details
#delete(path) ⇒ Ruhue::Response
DELETE a resource.
90 91 92 |
# File 'lib/ruhue.rb', line 90 def delete(path) request(:delete, path) end |
#description ⇒ Nokogiri::XML
Returns 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.
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.
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.
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.
56 57 58 |
# File 'lib/ruhue.rb', line 56 def url(path) "http://#{host}/#{path.to_s.sub(/\A\//, "")}" end |