ambient 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +21 -0
- data/README +69 -0
- data/lib/ambient.rb +171 -0
- data/rakefile +74 -0
- data/test/ambient_test.rb +66 -0
- metadata +45 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2005 Tom Fakes
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/README
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
== Ambient: Control your Ambient Orb or Beacon
|
2
|
+
|
3
|
+
Ambient version 0.1.0
|
4
|
+
|
5
|
+
This package contains Ambient, a package that allows a Ruby program to
|
6
|
+
control an Ambient Orb using the Ambient Devices web based API.
|
7
|
+
|
8
|
+
== Download
|
9
|
+
|
10
|
+
The latest version of Ambient can be found at
|
11
|
+
|
12
|
+
* http://rubyforge.org/project/showfiles.php?group_id=753
|
13
|
+
|
14
|
+
== Installation
|
15
|
+
|
16
|
+
=== GEM Installation
|
17
|
+
|
18
|
+
Download and install Ambient with the following.
|
19
|
+
|
20
|
+
gem install --remote ambient
|
21
|
+
|
22
|
+
== Online Resources
|
23
|
+
|
24
|
+
* http://www.ambientdevices.com/
|
25
|
+
* http://www.ambientdevices.com/developer/OrbWDK.pdf
|
26
|
+
|
27
|
+
== Prerequisites
|
28
|
+
|
29
|
+
* You must own an Ambient Orb or Beacon
|
30
|
+
* You must be in the Ambient coverage area
|
31
|
+
* You must register your device with Ambient
|
32
|
+
* You must have a Premium account
|
33
|
+
* You must have your device 'tuned' to the developer channel (which involves
|
34
|
+
registering *again* to get access)
|
35
|
+
|
36
|
+
== Simple Example
|
37
|
+
|
38
|
+
Ambient::Orb.new(:id => 'ABC-DEF-GHI', :color => :red).update
|
39
|
+
|
40
|
+
|
41
|
+
== License
|
42
|
+
|
43
|
+
Ambient is available under an MIT-style license.
|
44
|
+
|
45
|
+
:include: MIT-LICENSE
|
46
|
+
|
47
|
+
== Support
|
48
|
+
|
49
|
+
The ambient homepage is http://ambient.rubyforge.org. You can find the Ambient
|
50
|
+
RubyForge page at http://rubyforge.org/projects/ambient.
|
51
|
+
|
52
|
+
Feel free to submit commits or feature requests.
|
53
|
+
|
54
|
+
For other information, feel free to contact me
|
55
|
+
mailto:tom@craz8.com.
|
56
|
+
|
57
|
+
= Other stuff
|
58
|
+
|
59
|
+
Author:: Tom Fakes <tom@craz8.com>
|
60
|
+
License:: Copyright 2005 by Tom Fakes.
|
61
|
+
Released under an MIT-style license. See the MIT-LICENSE file
|
62
|
+
included in the distribution.
|
63
|
+
|
64
|
+
== Warranty
|
65
|
+
|
66
|
+
This software is provided "as is" and without any express or
|
67
|
+
implied warranties, including, without limitation, the implied
|
68
|
+
warranties of merchantibility and fitness for a particular
|
69
|
+
purpose.
|
data/lib/ambient.rb
ADDED
@@ -0,0 +1,171 @@
|
|
1
|
+
|
2
|
+
require 'net/http'
|
3
|
+
require 'rexml/document'
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
module Ambient
|
7
|
+
|
8
|
+
#
|
9
|
+
# All Ambient::Orb methods will raise an exception of type AmbientError
|
10
|
+
# if they detect some badness.
|
11
|
+
#
|
12
|
+
class AmbientError < StandardError; end
|
13
|
+
|
14
|
+
#
|
15
|
+
# = Ambient::Orb
|
16
|
+
#
|
17
|
+
# The Ambient::Orb methods will check their parameters to avoid making expensive
|
18
|
+
# net calls that will fail due to values out of range. Any error cases will
|
19
|
+
# raise an AmbientError exception
|
20
|
+
#
|
21
|
+
# Example:
|
22
|
+
# Ambient::Orb.new(:id => 'ABC-DEF-GHI', :color => :red).update
|
23
|
+
#
|
24
|
+
# or:
|
25
|
+
# @my_orb = Ambient::Orb.new(:id => my_id)
|
26
|
+
# @my_orb.color = :blue
|
27
|
+
# @my_orb.update
|
28
|
+
# @my_orb.color = :green
|
29
|
+
# @my_orb.update
|
30
|
+
#
|
31
|
+
# The second example isn't as useful, since it can take up to 30 minutes for
|
32
|
+
# an Orb to actually be updated over the ether.
|
33
|
+
|
34
|
+
class Orb
|
35
|
+
|
36
|
+
@@animations = {
|
37
|
+
:none => 0,
|
38
|
+
:very_slow => 1,
|
39
|
+
:slow => 2,
|
40
|
+
:medium_slow => 3,
|
41
|
+
:medium => 4,
|
42
|
+
:medium_fast => 5,
|
43
|
+
:fast => 6,
|
44
|
+
:very_fast => 7,
|
45
|
+
:cresendo => 8,
|
46
|
+
:heartbeat => 9
|
47
|
+
}
|
48
|
+
|
49
|
+
# Colors available for the 'color' property
|
50
|
+
@@colors = {
|
51
|
+
:red => 0,
|
52
|
+
:light_red => 1,
|
53
|
+
:dark_orange => 2,
|
54
|
+
:orange => 3,
|
55
|
+
:light_orange => 4,
|
56
|
+
:dark_yellow => 5,
|
57
|
+
:yellow => 6,
|
58
|
+
:lime_green => 7,
|
59
|
+
:pale_green => 8,
|
60
|
+
:green => 12,
|
61
|
+
:pale_aqua => 15,
|
62
|
+
:aqua => 16,
|
63
|
+
:dark_aqua => 17,
|
64
|
+
:cyan => 18,
|
65
|
+
:dark_cyan => 19,
|
66
|
+
:light_blue => 20,
|
67
|
+
:sky_blue => 21,
|
68
|
+
:blue => 24,
|
69
|
+
:deep_blue => 25,
|
70
|
+
:very_deep_blue => 26,
|
71
|
+
:violet => 27,
|
72
|
+
:purple => 28,
|
73
|
+
:light_purple => 29,
|
74
|
+
:magenta => 30,
|
75
|
+
:deep_magenta => 33,
|
76
|
+
:very_deep_magenta => 35,
|
77
|
+
:white => 36
|
78
|
+
}
|
79
|
+
|
80
|
+
# Orb ID from the bottom of your Orb or Beacon
|
81
|
+
attr_accessor :id
|
82
|
+
|
83
|
+
# Color to set the Orb to display
|
84
|
+
attr_accessor :color
|
85
|
+
|
86
|
+
# Animation to set the Orb to perform
|
87
|
+
attr_accessor :animation
|
88
|
+
|
89
|
+
# The time and date at which the orb should be updated, set after the update() call
|
90
|
+
attr :update_at
|
91
|
+
|
92
|
+
AMBIENT_HOST = "myambient.com" #:nodoc:
|
93
|
+
AMBIENT_PORT = 8080 #:nodoc:
|
94
|
+
AMBIENT_URL = '/java/my_devices/submitdata.jsp' #:nodoc:
|
95
|
+
|
96
|
+
# Sets the color value for the Orb to be set to. This can either be
|
97
|
+
# a numeric value between zero and 36, or one of the color symbols,
|
98
|
+
# such as :red, :yellow, :blue
|
99
|
+
def color=(var)
|
100
|
+
if var.kind_of?(Symbol)
|
101
|
+
var = @@colors[var]
|
102
|
+
raise AmbientError, "Color not recognized" unless var
|
103
|
+
end
|
104
|
+
raise AmbientError, "Color out of range: #{var}" if (var < 0 || var > 36)
|
105
|
+
@color = var
|
106
|
+
end
|
107
|
+
|
108
|
+
# Sets the animation value for the Orb to be set to. This value can either
|
109
|
+
# be a numeric value between zero and 9, or one of the animation symbols,
|
110
|
+
# such as :none, :fast, :heartbeat
|
111
|
+
def animation=(var)
|
112
|
+
if var.kind_of?(Symbol)
|
113
|
+
var = @@animations[var]
|
114
|
+
raise AmbientError, "Animation not recognized" unless var
|
115
|
+
end
|
116
|
+
raise AmbientError, "Animation out of range: #{var}" if (var < 0 || var > 9)
|
117
|
+
@animation = var
|
118
|
+
end
|
119
|
+
|
120
|
+
# The Orb ID is always 11 letters and digits of the form XXX-XXX-XXX. The
|
121
|
+
# Orb ID is on the bottom of the Ambient Orb and must be registered for
|
122
|
+
# development access on the http://ambientdevices.com web site for the
|
123
|
+
# update to be successful.
|
124
|
+
def id=(var)
|
125
|
+
if ((var !~ /[A-Z0-9]{3}-[A-Z0-9]{3}-[A-Z0-9]{3}/) &&
|
126
|
+
(var != nil))
|
127
|
+
raise AmbientError, "ID must be of the form ABC-DEF-GHI: #{var}"
|
128
|
+
end
|
129
|
+
@id = var
|
130
|
+
end
|
131
|
+
|
132
|
+
# Takes a hash of up to three values:
|
133
|
+
# :id => Orb ID
|
134
|
+
# :color => Color value
|
135
|
+
# :animation => Animation value
|
136
|
+
def initialize(options = {})
|
137
|
+
self.color = options[:color] || 0
|
138
|
+
self.animation = options[:animation] || 0
|
139
|
+
self.id = options[:id] || nil
|
140
|
+
end
|
141
|
+
|
142
|
+
# Make the HTTP call to the Ambient Devices web site to cause your
|
143
|
+
# orb to be updated. The Orb ID supplied must be registered with Ambient
|
144
|
+
# Devices as using the developer channel for this call to succeed.
|
145
|
+
#
|
146
|
+
# Returns self, to allow this to work
|
147
|
+
#
|
148
|
+
# * Ambient::Orb.new(:id => 'ABC-DEF-GHI', :color => :red).update.update_at
|
149
|
+
def update
|
150
|
+
raise AmbientError, "No ID set for Ambient Orb" if id == nil
|
151
|
+
Net::HTTP.start(AMBIENT_HOST, AMBIENT_PORT) do |http|
|
152
|
+
begin
|
153
|
+
response = http.get("#{AMBIENT_URL}?devID=#{id}&anim=#{animation}&color=#{color}")
|
154
|
+
rescue Exception
|
155
|
+
raise AmbientError, "HTTP Get Failed"
|
156
|
+
end
|
157
|
+
|
158
|
+
xml = REXML::Document.new(response.body)
|
159
|
+
xml.elements.each("//response") do |resp|
|
160
|
+
raise AmbientError, resp.text unless resp.attributes["type"] == "OK"
|
161
|
+
end
|
162
|
+
xml.elements.each("//maxNextUpdate") do |update|
|
163
|
+
@update_at = DateTime::parse(update.text).new_offset(5.0/24.0)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
self
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
data/rakefile
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
require 'rake/contrib/rubyforgepublisher'
|
6
|
+
|
7
|
+
PKG_NAME = 'ambient'
|
8
|
+
PKG_VERSION = '0.1.0'
|
9
|
+
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
10
|
+
PKG_DESTINATION = ENV["AMBIENT_PKG_DESTINATION"] || "../#{PKG_NAME}"
|
11
|
+
|
12
|
+
RELEASE_NAME = "REL #{PKG_VERSION}"
|
13
|
+
|
14
|
+
RUBY_FORGE_PROJECT = "ambient"
|
15
|
+
RUBY_FORGE_USER = "tomfakes"
|
16
|
+
|
17
|
+
PKG_FILES = FileList[
|
18
|
+
'lib/**/*',
|
19
|
+
'test/**/*',
|
20
|
+
'rakefile',
|
21
|
+
'README',
|
22
|
+
'MIT-LICENSE'
|
23
|
+
]
|
24
|
+
|
25
|
+
$VERBOSE = nil
|
26
|
+
TEST_CHANGES_SINCE = Time.now - 600
|
27
|
+
|
28
|
+
desc "Run all the tests"
|
29
|
+
task :default => [ :test ]
|
30
|
+
|
31
|
+
desc "Generate API documentation"
|
32
|
+
task :doc => [ :apidoc ]
|
33
|
+
|
34
|
+
desc "Run the unit tests in test"
|
35
|
+
Rake::TestTask.new("test") { |t|
|
36
|
+
t.libs << "test"
|
37
|
+
t.pattern = 'test/*_test.rb'
|
38
|
+
t.verbose = true
|
39
|
+
}
|
40
|
+
|
41
|
+
desc "Generate documentation for Ambient Library"
|
42
|
+
Rake::RDocTask.new("apidoc") { |rdoc|
|
43
|
+
rdoc.rdoc_dir = 'doc'
|
44
|
+
rdoc.title = "Ambient Library Documentation"
|
45
|
+
rdoc.rdoc_files.include('README')
|
46
|
+
rdoc.rdoc_files.include('MIT-LICENSE')
|
47
|
+
rdoc.rdoc_files.include('lib/*.rb')
|
48
|
+
}
|
49
|
+
|
50
|
+
# Generate GEM ----------------------------------------------------------------------------
|
51
|
+
|
52
|
+
spec = Gem::Specification.new do |s|
|
53
|
+
s.name = 'ambient'
|
54
|
+
s.version = PKG_VERSION
|
55
|
+
s.summary = "Control an Ambient Orb or Beacon from a Ruby app."
|
56
|
+
s.description = <<-EOF
|
57
|
+
Allow the color and animation settings to be changed on an Ambient Ord or Beacon -
|
58
|
+
http://www.ambientdevices.com/developer/OrbWDK.pdf. This code uses the Ambient HTTP developer interface.
|
59
|
+
EOF
|
60
|
+
|
61
|
+
s.rdoc_options << '--exclude' << '.'
|
62
|
+
s.has_rdoc = false
|
63
|
+
|
64
|
+
s.files = PKG_FILES.to_a.delete_if {|f| f.include?('.svn')}
|
65
|
+
s.require_path = 'lib'
|
66
|
+
|
67
|
+
s.author = "Tom Fakes"
|
68
|
+
s.email = "tom@craz8.com"
|
69
|
+
s.homepage = "http://ambient.rubyforge.org"
|
70
|
+
s.rubyforge_project = "ambient"
|
71
|
+
end
|
72
|
+
|
73
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
74
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
|
2
|
+
require 'lib/ambient'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class AmbientTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@orb = Ambient::Orb.new
|
9
|
+
@good_id = ENV["AMBIENT_ORB_ID"] # Put your Orb ID !
|
10
|
+
flunk("No AMBIENT_ORB_ID in environment, needed to run tests") if @good_id == nil
|
11
|
+
@bad_id = "ABC-DEF-HIJ" # Invalid ID, but valid format
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_raise_if_invalid_id
|
15
|
+
assert_raise(Ambient::AmbientError) { @orb.update }
|
16
|
+
assert_raise(Ambient::AmbientError) { @orb.id = "12345" }
|
17
|
+
assert_raise(Ambient::AmbientError) { @orb.id = "abc-def-ghi" }
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_raise_if_bad_id
|
21
|
+
@orb.id = @bad_id
|
22
|
+
assert_raise(Ambient::AmbientError) { @orb.update }
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_default_attributes_ok
|
26
|
+
@orb.id = @good_id
|
27
|
+
@orb.update
|
28
|
+
assert_kind_of(DateTime, @orb.update_at, "Update date should be returned from @orb.update")
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_init_types
|
32
|
+
Ambient::Orb.new(:id => @good_id).update
|
33
|
+
Ambient::Orb.new(:id => @good_id, :color => :green, :animation => :heartbeat).update
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_set_color_in_range
|
37
|
+
@orb.id = @good_id
|
38
|
+
@orb.color = 26
|
39
|
+
|
40
|
+
@orb.color = :white
|
41
|
+
@orb.color = :red
|
42
|
+
@orb.color = :blue
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_set_color_out_of_range
|
46
|
+
assert_raise(Ambient::AmbientError) { @orb.color = 57 }
|
47
|
+
assert_raise(Ambient::AmbientError) { @orb.color = :pink }
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_set_animation_in_range
|
51
|
+
@orb.id = @good_id
|
52
|
+
@orb.animation = 0
|
53
|
+
@orb.animation = 9
|
54
|
+
|
55
|
+
@orb.animation = :slow
|
56
|
+
@orb.animation = :heartbeat
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_set_animation_out_of_range
|
60
|
+
@orb.id = @good_id
|
61
|
+
assert_raise(Ambient::AmbientError) { @orb.animation = 42 }
|
62
|
+
assert_raise(Ambient::AmbientError) { @orb.animation = -1 }
|
63
|
+
assert_raise(Ambient::AmbientError) { @orb.animation = :cool }
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.3
|
3
|
+
specification_version: 1
|
4
|
+
name: ambient
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2005-10-23
|
8
|
+
summary: Control an Ambient Orb or Beacon from a Ruby app.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: tom@craz8.com
|
12
|
+
homepage: http://ambient.rubyforge.org
|
13
|
+
rubyforge_project: ambient
|
14
|
+
description: "Allow the color and animation settings to be changed on an Ambient Ord or Beacon
|
15
|
+
- http://www.ambientdevices.com/developer/OrbWDK.pdf. This code uses the
|
16
|
+
Ambient HTTP developer interface."
|
17
|
+
autorequire:
|
18
|
+
default_executable:
|
19
|
+
bindir: bin
|
20
|
+
has_rdoc: false
|
21
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
22
|
+
requirements:
|
23
|
+
-
|
24
|
+
- ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.0
|
27
|
+
version:
|
28
|
+
platform: ruby
|
29
|
+
authors:
|
30
|
+
- Tom Fakes
|
31
|
+
files:
|
32
|
+
- lib/ambient.rb
|
33
|
+
- test/ambient_test.rb
|
34
|
+
- rakefile
|
35
|
+
- README
|
36
|
+
- MIT-LICENSE
|
37
|
+
test_files: []
|
38
|
+
rdoc_options:
|
39
|
+
- "--exclude"
|
40
|
+
- "."
|
41
|
+
extra_rdoc_files: []
|
42
|
+
executables: []
|
43
|
+
extensions: []
|
44
|
+
requirements: []
|
45
|
+
dependencies: []
|