class Ruhue::Client

Attributes

hue[R]

@return [Ruhue]

username[R]

@return [String]

Public Class Methods

new(hue, username) click to toggle source

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.

@param [Ruhue] hue @param [String] username

# 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
valid_username?(name) click to toggle source
# File lib/ruhue/client.rb, line 3
def valid_username?(name)
  name =~ /\A[0-9a-zA-Z]{10,40}\z/
end

Public Instance Methods

get(path) click to toggle source
# File lib/ruhue/client.rb, line 44
def get(path)
  hue.get(url(path))
end
post(path, data) click to toggle source
# File lib/ruhue/client.rb, line 48
def post(path, data)
  hue.post(url(path), data)
end
put(path, data) click to toggle source
# File lib/ruhue/client.rb, line 52
def put(path, data)
  hue.put(url(path), data)
end
register(device_type) click to toggle source

Register a given username with the Hue hub.

@param [String] device_type used as device name @return [Ruhue::Client] if successful @raise [APIError] if failed

# 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?() click to toggle source

@return [Boolean] true if username is registered.

# File lib/ruhue/client.rb, line 40
def registered?
  not get("/").error?
end

Protected Instance Methods

url(path) click to toggle source
# File lib/ruhue/client.rb, line 58
def url(path)
  "/api/#{username}/#{path.sub(/\A\//, "")}"
end