ruby_hue 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ # 0.1.0
2
+
3
+ * First official version
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # RubyHue
2
2
 
3
- TODO: Write a gem description
3
+ RubyHue is a library to interact with Hue lights.
4
+
5
+ http://meethue.com/
4
6
 
5
7
  ## Installation
6
8
 
@@ -18,7 +20,54 @@ Or install it yourself as:
18
20
 
19
21
  ## Usage
20
22
 
21
- TODO: Write usage instructions here
23
+ RubyHue starts mostly at the Bridge class.
24
+
25
+ ```ruby
26
+ bridge = RubyHue::Bridge.new("192.168.1.100", "yourbridgeusername")
27
+ ```
28
+
29
+ To get all lights for the bridge:
30
+
31
+ ```ruby
32
+ bridge = RubyHue::Bridge.new("192.168.1.100", "yourbridgeusername")
33
+ lights = bridge.lights
34
+
35
+ lights.state.on = true
36
+ lights.state.save # This will turn on all lights on the bridge
37
+ ```
38
+
39
+ ### Light Usage
40
+
41
+ RubyHue comes with a Light class that you initialize with an ID and Bridge instance. Most interactions will happen with an instance of this class.
42
+
43
+ ```ruby
44
+ bridge = RubyHue::Bridge.new("192.168.1.100", "yourbridgeusername")
45
+ light = RubyHue::Light.new("1", bridge)
46
+ ```
47
+
48
+ Now you're ready to ask for the current state of the light (on, brightness, hue, etc...)
49
+
50
+ ```ruby
51
+ light = RubyHue::Light.new("1", bridge)
52
+ light.state.brightness
53
+ # => 50
54
+
55
+ light.state.brightness = 255
56
+ light.state.save
57
+ #=> [{"success"=>{"/lights/1/state/bri"=>255}}, ....
58
+ ```
59
+
60
+ Here's the list of states you can change:
61
+
62
+ * `on` (true, false)
63
+ * `brightness` (0..255)
64
+ * `hue` (0..65535)
65
+ * `saturation` (0..255)
66
+ * `xy` See http://developers.meethue.com/coreconcepts.html#color_gets_more_complicated
67
+ * `color_temperature`
68
+ * `alert` ("none", "select", "lselect")
69
+ * `effect` ("none", "colorloop")
70
+
22
71
 
23
72
  ## Contributing
24
73
 
@@ -1,13 +1,13 @@
1
1
  require "ruby_hue/version"
2
- require "ruby_hue/client"
3
- require "ruby_hue/configuration"
4
- require "ruby_hue/model"
5
- require "ruby_hue/light"
6
- require "ruby_hue/light/state"
7
- require "ruby_hue/bridge"
8
2
 
9
3
  module RubyHue
10
- def self.configuration
11
- @configuration ||= Configuration.new
12
- end
4
+ autoload :Client, "ruby_hue/client"
5
+ autoload :Bridge, "ruby_hue/bridge"
6
+ autoload :Model, "ruby_hue/model"
7
+ autoload :Light, "ruby_hue/light"
8
+ autoload :LightsCollection, "ruby_hue/lights_collection"
9
+ autoload :LightsCollectionState, "ruby_hue/lights_collection_state"
13
10
  end
11
+
12
+ require "ruby_hue/light/basic_state"
13
+ require "ruby_hue/light/state"
@@ -1,6 +1,7 @@
1
1
  module RubyHue
2
2
  class Bridge
3
- attr_reader :ip_address, :username
3
+ attr_reader :ip_address
4
+ attr_accessor :username
4
5
 
5
6
  class << self
6
7
  def all
@@ -20,9 +21,7 @@ module RubyHue
20
21
  end
21
22
 
22
23
  def lights
23
- Client.get_and_parse(resource_url_for("lights")).map do |id, light|
24
- RubyHue::Light.new(id, self)
25
- end
24
+ @lights ||= LightsCollection.new(self)
26
25
  end
27
26
  end
28
27
  end
@@ -0,0 +1,45 @@
1
+ module RubyHue
2
+ class Light
3
+ module BasicState
4
+ MAPPING = {
5
+ on: :on,
6
+ bri: :brightness,
7
+ hue: :hue,
8
+ sat: :saturation,
9
+ xy: :xy,
10
+ ct: :color_temperature,
11
+ alert: :alert,
12
+ effect: :effect,
13
+ colormode: :color_mode,
14
+ reachable: :reachable
15
+ }
16
+
17
+ IMMUTABLE_KEYS = %w(colormode reachable)
18
+
19
+ attr_writer :state
20
+
21
+ def updateable_state
22
+ state.reject do |key, value|
23
+ IMMUTABLE_KEYS.include? key
24
+ end
25
+ end
26
+
27
+ def state
28
+ @state ||= {}
29
+ end
30
+
31
+ MAPPING.each do |hue_key, method_name|
32
+ hue_key = hue_key.to_s
33
+ define_method method_name do
34
+ state[hue_key]
35
+ end
36
+
37
+ unless IMMUTABLE_KEYS.include?(hue_key)
38
+ define_method "#{method_name}=".to_sym do |new_value|
39
+ state[hue_key] = new_value
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -3,22 +3,7 @@ require "json"
3
3
  module RubyHue
4
4
  class Light
5
5
  class State
6
- MAPPING = {
7
- on: :on,
8
- bri: :brightness,
9
- hue: :hue,
10
- sat: :saturation,
11
- xy: :xy,
12
- ct: :color_temperature,
13
- alert: :alert,
14
- effect: :effect,
15
- colormode: :color_mode,
16
- reachable: :reachable
17
- }
18
-
19
- IMMUTABLE_KEYS = %w(colormode reachable)
20
-
21
- attr_writer :state
6
+ include BasicState
22
7
  attr_reader :light
23
8
 
24
9
  def initialize(light, state)
@@ -26,10 +11,6 @@ module RubyHue
26
11
  @state = state
27
12
  end
28
13
 
29
- def state
30
- @state ||= {}
31
- end
32
-
33
14
  def save
34
15
  url = bridge.resource_url_for("lights/#{light.id}/state")
35
16
  body = JSON.generate updateable_state
@@ -39,25 +20,6 @@ module RubyHue
39
20
  def bridge
40
21
  light.bridge
41
22
  end
42
-
43
- def updateable_state
44
- @state.reject do |key, value|
45
- IMMUTABLE_KEYS.include? key
46
- end
47
- end
48
-
49
- MAPPING.each do |hue_key, method_name|
50
- hue_key = hue_key.to_s
51
- define_method method_name do
52
- @state[hue_key]
53
- end
54
-
55
- unless IMMUTABLE_KEYS.include?(hue_key)
56
- define_method "#{method_name}=".to_sym do |new_value|
57
- @state[hue_key] = new_value
58
- end
59
- end
60
- end
61
23
  end
62
24
  end
63
25
  end
@@ -0,0 +1,27 @@
1
+ module RubyHue
2
+ class LightsCollection
3
+ include Enumerable
4
+
5
+ attr_reader :bridge
6
+
7
+ def initialize(bridge)
8
+ @bridge = bridge
9
+ end
10
+
11
+ def lights
12
+ Client.get_and_parse(bridge.resource_url_for("lights")).map do |id, light|
13
+ RubyHue::Light.new(id, self)
14
+ end
15
+ end
16
+
17
+ def state
18
+ @state ||= LightsCollectionState.new(self)
19
+ end
20
+
21
+ def each(&block)
22
+ lights.each do |light|
23
+ yield light
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ module RubyHue
2
+ class LightsCollectionState
3
+ include Light::BasicState
4
+
5
+ attr_reader :collection
6
+
7
+ def initialize(collection)
8
+ @collection = collection
9
+ end
10
+
11
+ def bridge
12
+ collection.bridge
13
+ end
14
+
15
+ def save
16
+ collection.each do |light|
17
+ url = bridge.resource_url_for("lights/#{light.id}/state")
18
+ body = JSON.generate updateable_state
19
+ Client.put_and_parse(url, body: body)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module RubyHue
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -35,13 +35,8 @@ describe RubyHue::Bridge do
35
35
  end
36
36
 
37
37
  describe "#lights" do
38
- before do
39
- stub_request(:get, subject.resource_url_for("lights")).to_return(body: fixture("lights.json"))
40
- end
41
-
42
- it "returns a collection of light objects" do
43
- RubyHue::Light.should_receive(:new).with(kind_of(String), kind_of(RubyHue::Bridge)).exactly(3).times
44
- bridge.lights
38
+ it "returns a light collection" do
39
+ expect(bridge.lights).to be_kind_of RubyHue::LightsCollection
45
40
  end
46
41
  end
47
42
  end
@@ -10,6 +10,8 @@ describe RubyHue::Light::State do
10
10
  to_return(body: fixture("light_1.json"))
11
11
  end
12
12
 
13
+ it_behaves_like "basic state"
14
+
13
15
  describe "#initialize" do
14
16
  let(:instance) { described_class.new(light, state_hash) }
15
17
 
@@ -28,23 +30,6 @@ describe RubyHue::Light::State do
28
30
  end
29
31
  end
30
32
 
31
- describe "getter methods" do
32
- let(:method_mapping) { method_mapping = described_class::MAPPING }
33
-
34
- it "responds to all keys available for a state" do
35
- method_mapping.each do |hue_key, ruby_hue_method|
36
- expect(subject.state).to have_key hue_key.to_s
37
- expect(subject).to respond_to ruby_hue_method
38
- end
39
- end
40
-
41
- it "returns the value for a state" do
42
- method_mapping.each do |hue_key, ruby_hue_method|
43
- expect(subject.send(ruby_hue_method)).to eq(subject.state[hue_key.to_s])
44
- end
45
- end
46
- end
47
-
48
33
  describe "#save" do
49
34
  let!(:state_request_stub) do
50
35
  stub_request(:put, bridge.resource_url_for("lights/#{light.id}/state")).to_return(body: "{}")
@@ -55,12 +40,4 @@ describe RubyHue::Light::State do
55
40
  expect(state_request_stub).to have_been_requested
56
41
  end
57
42
  end
58
-
59
- describe "#updateable_state" do
60
- it "does not include immutable values" do
61
- described_class::IMMUTABLE_KEYS.each do |key|
62
- expect(subject.updateable_state.keys).not_to include key
63
- end
64
- end
65
- end
66
43
  end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe RubyHue::LightsCollection do
4
+ let(:bridge) { RubyHue::Bridge.new("127.0.0.1", "username") }
5
+ subject { described_class.new(bridge) }
6
+
7
+ describe "#initialize" do
8
+ it "accepts a bridge" do
9
+ expect { described_class.new(bridge) }.to_not raise_error
10
+ end
11
+ end
12
+
13
+ describe "#lights" do
14
+ before do
15
+ stub_request(:get, bridge.resource_url_for("lights")).to_return(body: fixture("lights.json"))
16
+ end
17
+
18
+ it "returns 3 light objects" do
19
+ expect(subject).to have(3).lights
20
+ end
21
+ end
22
+
23
+ describe "#state" do
24
+ it "returns a collection state object" do
25
+ expect(subject.state).to be_kind_of RubyHue::LightsCollectionState
26
+ end
27
+ end
28
+
29
+ describe "#each" do
30
+ before { subject.stub(lights: [RubyHue::Light.new(1, bridge)]) }
31
+
32
+ it "yields a light object" do
33
+ expect {|b| subject.each(&b) }.to yield_with_args(kind_of(RubyHue::Light))
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ describe RubyHue::LightsCollectionState do
4
+ let(:bridge) { RubyHue::Bridge.new("127.0.0.1", "username") }
5
+ let(:light_collection) { RubyHue::LightsCollection.new(bridge) }
6
+ subject { described_class.new(light_collection) }
7
+
8
+ it_behaves_like "basic state"
9
+
10
+ describe "#save" do
11
+ let(:light) { double("light", id: 1) }
12
+ let!(:state_request_stub) do
13
+ stub_request(:put, bridge.resource_url_for("lights/#{light.id}/state")).to_return(body: "{}")
14
+ end
15
+
16
+ it "changes the state for all of the lights" do
17
+ light_collection.stub(lights: [light])
18
+ subject.on = true
19
+ subject.save
20
+ expect(state_request_stub).to have_been_requested
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ shared_examples_for "basic state" do
2
+ describe "getter methods" do
3
+ let(:method_mapping) { method_mapping = RubyHue::Light::BasicState::MAPPING }
4
+
5
+ it "responds to all keys available for a state" do
6
+ method_mapping.each do |hue_key, ruby_hue_method|
7
+ expect(subject).to respond_to ruby_hue_method
8
+ end
9
+ end
10
+
11
+ it "returns the value for a state" do
12
+ method_mapping.each do |hue_key, ruby_hue_method|
13
+ expect(subject.send(ruby_hue_method)).to eq(subject.state[hue_key.to_s])
14
+ end
15
+ end
16
+ end
17
+
18
+ describe "#updateable_state" do
19
+ it "does not include immutable values" do
20
+ RubyHue::Light::BasicState::IMMUTABLE_KEYS.each do |key|
21
+ expect(subject.updateable_state.keys).not_to include key
22
+ end
23
+ end
24
+ end
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_hue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-31 00:00:00.000000000 Z
12
+ date: 2013-05-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -132,6 +132,7 @@ extra_rdoc_files: []
132
132
  files:
133
133
  - .gitignore
134
134
  - .rspec
135
+ - CHANGELOG.md
135
136
  - Gemfile
136
137
  - Guardfile
137
138
  - LICENSE.txt
@@ -140,9 +141,11 @@ files:
140
141
  - lib/ruby_hue.rb
141
142
  - lib/ruby_hue/bridge.rb
142
143
  - lib/ruby_hue/client.rb
143
- - lib/ruby_hue/configuration.rb
144
144
  - lib/ruby_hue/light.rb
145
+ - lib/ruby_hue/light/basic_state.rb
145
146
  - lib/ruby_hue/light/state.rb
147
+ - lib/ruby_hue/lights_collection.rb
148
+ - lib/ruby_hue/lights_collection_state.rb
146
149
  - lib/ruby_hue/model.rb
147
150
  - lib/ruby_hue/version.rb
148
151
  - ruby_hue.gemspec
@@ -152,12 +155,13 @@ files:
152
155
  - spec/fixtures/responses/lights.json
153
156
  - spec/ruby_hue/bridge_spec.rb
154
157
  - spec/ruby_hue/client_spec.rb
155
- - spec/ruby_hue/configuration_spec.rb
156
158
  - spec/ruby_hue/light/state_spec.rb
157
159
  - spec/ruby_hue/light_spec.rb
160
+ - spec/ruby_hue/lights_collection_spec.rb
161
+ - spec/ruby_hue/lights_collection_state_spec.rb
158
162
  - spec/ruby_hue/model_spec.rb
159
- - spec/ruby_hue/ruby_hue_spec.rb
160
163
  - spec/spec_helper.rb
164
+ - spec/support/behavior/state_examples.rb
161
165
  - spec/support/complete_hue_model.rb
162
166
  - spec/support/fixture_helpers.rb
163
167
  - spec/support/light_bridge_helpers.rb
@@ -192,12 +196,13 @@ test_files:
192
196
  - spec/fixtures/responses/lights.json
193
197
  - spec/ruby_hue/bridge_spec.rb
194
198
  - spec/ruby_hue/client_spec.rb
195
- - spec/ruby_hue/configuration_spec.rb
196
199
  - spec/ruby_hue/light/state_spec.rb
197
200
  - spec/ruby_hue/light_spec.rb
201
+ - spec/ruby_hue/lights_collection_spec.rb
202
+ - spec/ruby_hue/lights_collection_state_spec.rb
198
203
  - spec/ruby_hue/model_spec.rb
199
- - spec/ruby_hue/ruby_hue_spec.rb
200
204
  - spec/spec_helper.rb
205
+ - spec/support/behavior/state_examples.rb
201
206
  - spec/support/complete_hue_model.rb
202
207
  - spec/support/fixture_helpers.rb
203
208
  - spec/support/light_bridge_helpers.rb
@@ -1,5 +0,0 @@
1
- module RubyHue
2
- class Configuration
3
- attr_accessor :bridge_ip, :api_username
4
- end
5
- end
@@ -1,13 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe RubyHue::Configuration do
4
- it "has a bridge_ip" do
5
- expect(subject).to respond_to :bridge_ip
6
- expect(subject).to respond_to :bridge_ip=
7
- end
8
-
9
- it "has a api_username" do
10
- expect(subject).to respond_to :api_username
11
- expect(subject).to respond_to :api_username=
12
- end
13
- end
@@ -1,9 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe RubyHue do
4
- describe ".configure" do
5
- it "returns a configuration object" do
6
- expect(RubyHue.configuration).to be_kind_of RubyHue::Configuration
7
- end
8
- end
9
- end