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 = '1234567890') ⇒ Client

Returns a new instance of Client


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

def initialize(username = '1234567890')
  unless USERNAME_RANGE.include?(username.length)
    raise InvalidUsername, "Usernames must be between #{USERNAME_RANGE.first} and #{USERNAME_RANGE.last}."
  end

  @username = username

  begin
    validate_user
  rescue Hue::UnauthorizedUser
    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


49
50
51
# File 'lib/hue/client.rb', line 49

def add_lights
  bridge.add_lights
end

#bridgeObject

Raises:


23
24
25
26
27
28
# File 'lib/hue/client.rb', line 23

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


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

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


62
63
64
65
66
67
# File 'lib/hue/client.rb', line 62

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


58
59
60
# File 'lib/hue/client.rb', line 58

def groups
  bridge.groups
end

#light(id) ⇒ Object


53
54
55
56
# File 'lib/hue/client.rb', line 53

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

#lightsObject


45
46
47
# File 'lib/hue/client.rb', line 45

def lights
  bridge.lights
end

#scene(id) ⇒ Object


73
74
75
76
# File 'lib/hue/client.rb', line 73

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

#scenesObject


69
70
71
# File 'lib/hue/client.rb', line 69

def scenes
  bridge.scenes
end