lifx_http 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c679e37e1d17aa75273acf54fd4b6d17d88482f1
4
+ data.tar.gz: 341a096e166e266d0f2cc26f0fcea649cd17876a
5
+ SHA512:
6
+ metadata.gz: 0d2964cea069f21eafa221fb0517b20b8ebf23382d969cd2244cc1cd711e12be5a26de18bbb76f78f50bde39afcc65da7bf998d05c36abf58fc57aa7cc11c9de
7
+ data.tar.gz: 96f83f715583322f3d6f54009e324a0a5a0212da5f9ac646e2474805e1fe08085a3955a2877bc25fefa272a8e6ee3c493ee7289f946fbf973c1dd9d0fe9da0e4
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ lifx_toys-*.gem
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lifx_http.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Lucas Parry
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ # Lifx HTTP gem for ruby
2
+
3
+ ## Development
4
+
5
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
6
+
7
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
8
+
9
+ ## Contributing
10
+
11
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lparry/lifx_http.
12
+
13
+
14
+ ## License
15
+
16
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
17
+
@@ -0,0 +1,35 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require "lifx_http"
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "test"
7
+ t.libs << "lib"
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ end
10
+
11
+ task :default => :test
12
+
13
+ desc "display all light info"
14
+ task :info do
15
+ puts all_lights.get_info
16
+ end
17
+
18
+ desc "Turn all lights off"
19
+ task :off do
20
+ puts all_lights.set_power_state 'off'
21
+ end
22
+
23
+ desc "Turn all lights on"
24
+ task :on do
25
+ puts all_lights.set_power_state 'on'
26
+ end
27
+
28
+ desc "Toggle all lights"
29
+ task :toggle do
30
+ puts all_lights.toggle
31
+ end
32
+
33
+ def all_lights
34
+ LifxHttp::Api.with_default_selector('all')
35
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lifx_http"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "pry"
14
+ Pry.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require "lifx_http/version"
2
+ require "lifx_http/api"
3
+ require "lifx_http/lifx_light"
4
+
5
+ module LifxHttp
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,106 @@
1
+ require 'httparty'
2
+ require_relative 'api/with_default_selector'
3
+
4
+ module LifxHttp
5
+ module Api
6
+ class << self
7
+
8
+ def with_default_selector(selector)
9
+ WithDefaultSelector.new(selector)
10
+ end
11
+
12
+ def set_color(selector, color, options = { duration: 2.0,
13
+ power_on: true})
14
+ HTTParty.put(color_url(selector),
15
+ headers: authorization_headers,
16
+ query: options.merge({
17
+ color: color
18
+ })).tap do |response|
19
+ if response.success?
20
+ puts "info: set #{selector} to #{color}" if ENV["DEBUG"]
21
+ puts response.to_s if ENV["DEBUG"]
22
+ nil
23
+ else
24
+ puts "warning: light status - #{response}"
25
+ -1
26
+ end
27
+ end
28
+ end
29
+
30
+ def get_info(selector)
31
+ HTTParty.get(info_url(selector),
32
+ headers: authorization_headers)
33
+ end
34
+
35
+ def set_power_state(selector, state, options = {duration: 2})
36
+ HTTParty.put(power_url(selector),
37
+ headers: authorization_headers,
38
+ query: options.merge({
39
+ state: state
40
+ }))
41
+ end
42
+
43
+ def toggle_power_state(selector)
44
+ HTTParty.post(toggle_power_url(selector),
45
+ headers: authorization_headers)
46
+ end
47
+
48
+ def breathe(selector, color, options = {
49
+ period: 1.0,
50
+ cycles: 1.0,
51
+ persist: false,
52
+ power_on: true,
53
+ peak: 0.5 })
54
+ HTTParty.post(breathe_url(selector),
55
+ headers: authorization_headers,
56
+ query: options.merge({
57
+ color: color
58
+ }))
59
+ end
60
+
61
+ def pulse(selector, color, options = {
62
+ # from_color: current_bulb_color,
63
+ period: 1.0,
64
+ cycles: 1.0,
65
+ persist: false,
66
+ power_on: true,
67
+ peak: 1.0 })
68
+ HTTParty.post(pulse_url(selector),
69
+ headers: authorization_headers,
70
+ query: options.merge({
71
+ color: color
72
+ }))
73
+ end
74
+
75
+ private
76
+
77
+ def info_url(selector)
78
+ "https://api.lifx.com/v1beta1/lights/#{selector}"
79
+ end
80
+
81
+ def color_url(selector)
82
+ "#{info_url(selector)}/color"
83
+ end
84
+
85
+ def power_url(selector)
86
+ "#{info_url(selector)}/power"
87
+ end
88
+
89
+ def toggle_power_url(selector)
90
+ "#{info_url(selector)}/toggle"
91
+ end
92
+
93
+ def breathe_url(selector)
94
+ "#{info_url(selector)}/effects/breathe"
95
+ end
96
+
97
+ def pulse_url(selector)
98
+ "#{info_url(selector)}/effects/pulse"
99
+ end
100
+
101
+ def authorization_headers
102
+ {"Authorization" => "Bearer #{ENV["LIFX_TOKEN"]}"}
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,30 @@
1
+ require_relative '../api'
2
+
3
+ module LifxHttp
4
+ module Api
5
+
6
+ class WithDefaultSelector
7
+
8
+ attr_reader :selector
9
+
10
+ def initialize(selector)
11
+ @selector = selector
12
+ end
13
+
14
+ private
15
+
16
+ def method_missing(method_name, *args)
17
+ if ::LifxHttp::Api.respond_to? method_name
18
+ ::LifxHttp::Api.public_send(method_name, selector, *args)
19
+ else
20
+ raise
21
+ end
22
+ end
23
+
24
+ def make_request(method_name, *args)
25
+ Api.public_send(method_name, selector, *args)
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,42 @@
1
+ require 'forwardable'
2
+ require_relative 'api'
3
+
4
+ module LifxHttp
5
+ class LifxLight
6
+
7
+ extend Forwardable
8
+ def_delegators :@data, :label, :connected, :power, :color, :brightness, :product_name, :last_seen, :seconds_since_seen
9
+ def_delegators :@http_api, :set_color
10
+
11
+ attr_reader :light_id
12
+
13
+ def self.get_lights
14
+ @lights ||= Api.with_default_selector("all").get_info.map do |data|
15
+ LifxLight.new(data)
16
+ end
17
+ end
18
+
19
+ def initialize(data)
20
+ @light_id = data["id"]
21
+ @data = OpenStruct.new(data)
22
+ @updated_at = Time.now
23
+ @http_api = Api.with_default_selector(selector)
24
+ end
25
+
26
+ def update_info
27
+ @data ||= OpenStruct.new(@http_api.get_info)
28
+ @updated_at = Time.now
29
+ end
30
+
31
+ def inspect
32
+ "#<#{self.class.name} light_id=#{light_id.inspect} label=#{label.inspect} connected=#{connected.inspect} power=#{power.inspect} color=#{color.inspect} brightness=#{brightness.inspect} product_name=#{product_name.inspect} last_seen=#{last_seen.inspect} seconds_since_seen=#{seconds_since_seen.inspect} @updated_at=#{@updated_at}>"
33
+ end
34
+
35
+ private
36
+
37
+ def selector
38
+ "id:#{light_id}"
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module LifxHttp
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lifx_http/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lifx_http"
8
+ spec.version = LifxHttp::VERSION
9
+ spec.authors = ["Lucas Parry"]
10
+ spec.email = ["lparry@gmail.com"]
11
+
12
+ spec.summary = %q{Lucas's LIFX HTTP API gem}
13
+ spec.description = %q{ruby wrapper for the LIFX cloud api}
14
+ spec.homepage = "https://github.com/lparry/lifx_http"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.7"
25
+ spec.add_development_dependency "pry", "~> 0.10"
26
+ spec.add_dependency "json", "~> 1.8"
27
+ spec.add_dependency "httparty", "~> 0.13"
28
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lifx_http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Lucas Parry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.8'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httparty
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.13'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.13'
97
+ description: ruby wrapper for the LIFX cloud api
98
+ email:
99
+ - lparry@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - lib/lifx_http.rb
113
+ - lib/lifx_http/api.rb
114
+ - lib/lifx_http/api/with_default_selector.rb
115
+ - lib/lifx_http/lifx_light.rb
116
+ - lib/lifx_http/version.rb
117
+ - lifx_http.gemspec
118
+ homepage: https://github.com/lparry/lifx_http
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.2.2
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Lucas's LIFX HTTP API gem
142
+ test_files: []