Class: Hued::Hub

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, token) ⇒ Hub

Returns a new instance of Hub.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hued.rb', line 16

def initialize(ip, token)
  @ip    = ip
  @token = token

  raise "no IP specified" if @ip.nil?
  raise "no token specified" if @token.nil?

  @config = get_config()

  @lights    = get_lights()
  @scenes    = get_scenes()
  @schedules = get_schedules()
end

Instance Attribute Details

#lightsObject

Returns the value of attribute lights.



14
15
16
# File 'lib/hued.rb', line 14

def lights
  @lights
end

#scenesObject

Returns the value of attribute scenes.



14
15
16
# File 'lib/hued.rb', line 14

def scenes
  @scenes
end

#schedulesObject

Returns the value of attribute schedules.



14
15
16
# File 'lib/hued.rb', line 14

def schedules
  @schedules
end

Instance Method Details

#all_lights_offObject



84
85
86
87
88
# File 'lib/hued.rb', line 84

def all_lights_off
  url     = get_url('groups/0/action')
  payload = { :on => false }
  put_http(url, payload)
end

#all_lights_onObject



78
79
80
81
82
# File 'lib/hued.rb', line 78

def all_lights_on
  url     = get_url('groups/0/action')
  payload = { :on => true }
  put_http(url, payload)
end

#get_configObject



44
45
46
# File 'lib/hued.rb', line 44

def get_config
  JSON.parse(get_http(get_url('config')).body)
end

#get_http(url) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/hued.rb', line 95

def get_http(url)
  uri      = URI.parse(url)
  http     = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = false
  request  = Net::HTTP::Get.new(uri.request_uri)
  http.request(request)
end

#get_lights(input = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/hued.rb', line 48

def get_lights(input = nil)
  lights   = Array.new
  response = JSON.parse(get_http(get_url('lights')).body)
  response.each do |_i, hash|
    lights << OpenStruct.new(hash)
  end

  lights
end

#get_scenes(input = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/hued.rb', line 58

def get_scenes(input = nil)
  scenes = Array.new
  response = JSON.parse(get_http(get_url('scenes')).body)
  response.each do |_i, hash|
    scenes << OpenStruct.new(hash)
  end

  scenes
end

#get_schedules(input = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/hued.rb', line 68

def get_schedules(input = nil)
  schedules = Array.new
  response  = JSON.parse(get_http(get_url('schedules')).body)
  response.each do |_i, hash|
    schedules << OpenStruct.new(hash)
  end

  schedules
end

#get_url(method) ⇒ Object

helper functions



91
92
93
# File 'lib/hued.rb', line 91

def get_url(method)
  sprintf('http://%s/api/%s/%s', @ip, @token, method)
end

#inspectObject



30
31
32
33
34
35
36
37
38
# File 'lib/hued.rb', line 30

def inspect
  {
    :ip        => @ip,
    :token     => @token,
    :lights    => @lights.size,
    :scenes    => @scenes.size,
    :schedules => @schedules.size,
  }
end

#put_http(url, body) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/hued.rb', line 103

def put_http(url, body)
  uri     = URI.parse(url)
  http    = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Put.new(uri.request_uri)

  request.add_field('Content-Type', 'application.json')
  request.body = body.to_json
  http.request(request)
end

#to_sObject



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

def to_s
  inspect.to_s
end