Class: Ruhue::Client
- Inherits:
-
Object
- Object
- Ruhue::Client
- Defined in:
- lib/ruhue/client.rb
Instance Attribute Summary collapse
- #hue ⇒ Ruhue readonly
- #username ⇒ String readonly
Class Method Summary collapse
Instance Method Summary collapse
- #get(path) ⇒ Object
-
#initialize(hue, username) ⇒ Client
constructor
Create a Hue client.
- #post(path, data) ⇒ Object
- #put(path, data) ⇒ Object
-
#register(device_type) ⇒ Ruhue::Client
Register a given username with the Hue hub.
-
#registered? ⇒ Boolean
True if username is registered.
Constructor Details
#initialize(hue, username) ⇒ Client
Create a Hue client. You’ll need a Hue hub username for this, created via a POST to the /api endpoint. See README for more information.
14 15 16 17 18 19 20 21 |
# File 'lib/ruhue/client.rb', line 14 def initialize(hue, username) unless self.class.valid_username?(username) raise ArgumentError, "invalid username, must be length 10-40, only numbers and letters" end @hue = hue @username = username end |
Instance Attribute Details
#username ⇒ String (readonly)
27 28 29 |
# File 'lib/ruhue/client.rb', line 27 def username @username end |
Class Method Details
.valid_username?(name) ⇒ Boolean
3 4 5 |
# File 'lib/ruhue/client.rb', line 3 def valid_username?(name) name =~ /\A[0-9a-zA-Z]{10,40}\z/ end |
Instance Method Details
#get(path) ⇒ Object
44 45 46 |
# File 'lib/ruhue/client.rb', line 44 def get(path) hue.get(url(path)) end |
#post(path, data) ⇒ Object
48 49 50 |
# File 'lib/ruhue/client.rb', line 48 def post(path, data) hue.post(url(path), data) end |
#put(path, data) ⇒ Object
52 53 54 |
# File 'lib/ruhue/client.rb', line 52 def put(path, data) hue.put(url(path), data) end |
#register(device_type) ⇒ Ruhue::Client
Register a given username with the Hue hub.
34 35 36 37 |
# File 'lib/ruhue/client.rb', line 34 def register(device_type) response = hue.post("/api", username: username, devicetype: device_type) tap { raise Ruhue::APIError, response..join(", ") if response.error? } end |
#registered? ⇒ Boolean
Returns true if username is registered.
40 41 42 |
# File 'lib/ruhue/client.rb', line 40 def registered? not get("/").error? end |