Class: Hue::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = nil) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hue/client.rb', line 9

def initialize(username = nil)
  username = find_username unless @username

  @username = username

  if @username
    begin
      validate_user
    rescue Hue::UnauthorizedUser
      register_user
    end
  else
    register_user
  end
end

Instance Attribute Details

#usernameObject (readonly)

Returns the value of attribute username.



7
8
9
# File 'lib/hue/client.rb', line 7

def username
  @username
end

Instance Method Details

#add_lightsObject



51
52
53
# File 'lib/hue/client.rb', line 51

def add_lights
  bridge.add_lights
end

#bridgeObject

Raises:



25
26
27
28
29
30
# File 'lib/hue/client.rb', line 25

def bridge
  # Pick the first one for now. In theory, they should all do the same thing.
  bridge = bridges.first
  raise NoBridgeFound unless bridge
  bridge
end

#bridgesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hue/client.rb', line 32

def bridges
  @bridges ||= begin
    bs = []
    easy = Curl::Easy.new
    easy.follow_location = true
    easy.max_redirects = 10
    easy.url = 'https://www.meethue.com/api/nupnp'
    easy.perform
    JSON(easy.body).each do |hash|
      bs << Bridge.new(self, hash)
    end
    bs
  end
end

#group(id = nil) ⇒ Object



64
65
66
67
68
69
# File 'lib/hue/client.rb', line 64

def group(id = nil)
  return Group.new(self, bridge) if id.nil?

  id = id.to_s
  groups.select { |g| g.id == id }.first
end

#groupsObject



60
61
62
# File 'lib/hue/client.rb', line 60

def groups
  bridge.groups
end

#light(id) ⇒ Object



55
56
57
58
# File 'lib/hue/client.rb', line 55

def light(id)
  id = id.to_s
  lights.select { |l| l.id == id }.first
end

#lightsObject



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

def lights
  bridge.lights
end

#scene(id) ⇒ Object



75
76
77
78
# File 'lib/hue/client.rb', line 75

def scene(id)
  id = id.to_s
  scenes.select { |s| s.id == id }.first
end

#scenesObject



71
72
73
# File 'lib/hue/client.rb', line 71

def scenes
  bridge.scenes
end