Class: Ruhue::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • hue (Ruhue)
  • username (String)


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

#hueRuhue (readonly)

Returns:



24
25
26
# File 'lib/ruhue/client.rb', line 24

def hue
  @hue
end

#usernameString (readonly)

Returns:

  • (String)


27
28
29
# File 'lib/ruhue/client.rb', line 27

def username
  @username
end

Class Method Details

.valid_username?(name) ⇒ Boolean

Returns:

  • (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.

Parameters:

  • device_type (String)

    used as device name

Returns:

Raises:



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.error_messages.join(", ") if response.error? }
end

#registered?Boolean

Returns true if username is registered.

Returns:

  • (Boolean)

    true if username is registered.



40
41
42
# File 'lib/ruhue/client.rb', line 40

def registered?
  not get("/").error?
end