obix 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/Guardfile +5 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +128 -0
  7. data/Rakefile +7 -0
  8. data/lib/obix.rb +55 -0
  9. data/lib/obix/alarms.rb +48 -0
  10. data/lib/obix/builder.rb +45 -0
  11. data/lib/obix/configuration.rb +79 -0
  12. data/lib/obix/errors.rb +6 -0
  13. data/lib/obix/history.rb +65 -0
  14. data/lib/obix/network.rb +76 -0
  15. data/lib/obix/objects.rb +46 -0
  16. data/lib/obix/objects/base.rb +97 -0
  17. data/lib/obix/objects/boolean.rb +13 -0
  18. data/lib/obix/objects/date.rb +15 -0
  19. data/lib/obix/objects/duration.rb +14 -0
  20. data/lib/obix/objects/enumerable.rb +13 -0
  21. data/lib/obix/objects/error.rb +26 -0
  22. data/lib/obix/objects/feed.rb +14 -0
  23. data/lib/obix/objects/float.rb +16 -0
  24. data/lib/obix/objects/integer.rb +15 -0
  25. data/lib/obix/objects/list.rb +14 -0
  26. data/lib/obix/objects/object.rb +20 -0
  27. data/lib/obix/objects/operation.rb +28 -0
  28. data/lib/obix/objects/reference.rb +10 -0
  29. data/lib/obix/objects/string.rb +14 -0
  30. data/lib/obix/objects/time.rb +15 -0
  31. data/lib/obix/objects/uri.rb +13 -0
  32. data/lib/obix/tag.rb +43 -0
  33. data/lib/obix/types.rb +15 -0
  34. data/lib/obix/types/boolean.rb +25 -0
  35. data/lib/obix/types/date.rb +20 -0
  36. data/lib/obix/types/duration.rb +21 -0
  37. data/lib/obix/types/float.rb +18 -0
  38. data/lib/obix/types/integer.rb +18 -0
  39. data/lib/obix/types/string.rb +18 -0
  40. data/lib/obix/types/time.rb +20 -0
  41. data/lib/obix/types/type.rb +17 -0
  42. data/lib/obix/types/uri.rb +28 -0
  43. data/lib/obix/version.rb +3 -0
  44. data/lib/obix/watch.rb +119 -0
  45. data/obix.gemspec +31 -0
  46. data/test/fixtures/alarmqueryout.xml +19 -0
  47. data/test/fixtures/alarmsubject.xml +5 -0
  48. data/test/fixtures/history.xml +9 -0
  49. data/test/fixtures/historyqueryout.xml +15 -0
  50. data/test/fixtures/invalid.xml +1 -0
  51. data/test/fixtures/objects/boolean.xml +1 -0
  52. data/test/fixtures/objects/date.xml +1 -0
  53. data/test/fixtures/objects/duration.xml +1 -0
  54. data/test/fixtures/objects/enumerable.xml +1 -0
  55. data/test/fixtures/objects/error.xml +1 -0
  56. data/test/fixtures/objects/feed.xml +4 -0
  57. data/test/fixtures/objects/float.xml +1 -0
  58. data/test/fixtures/objects/integer.xml +1 -0
  59. data/test/fixtures/objects/list.xml +4 -0
  60. data/test/fixtures/objects/object.xml +1 -0
  61. data/test/fixtures/objects/operation.xml +1 -0
  62. data/test/fixtures/objects/reference.xml +1 -0
  63. data/test/fixtures/objects/string.xml +1 -0
  64. data/test/fixtures/objects/time.xml +1 -0
  65. data/test/fixtures/objects/uri.xml +1 -0
  66. data/test/fixtures/valid.xml +11 -0
  67. data/test/fixtures/watch.xml +8 -0
  68. data/test/fixtures/watchout.xml +5 -0
  69. data/test/fixtures/watchservice.xml +3 -0
  70. data/test/obix/alarms_test.rb +36 -0
  71. data/test/obix/builder_test.rb +32 -0
  72. data/test/obix/configuration_test.rb +32 -0
  73. data/test/obix/history_test.rb +54 -0
  74. data/test/obix/network_test.rb +134 -0
  75. data/test/obix/objects/base_test.rb +54 -0
  76. data/test/obix/objects/boolean_test.rb +17 -0
  77. data/test/obix/objects/date_test.rb +16 -0
  78. data/test/obix/objects/duration_test.rb +16 -0
  79. data/test/obix/objects/enumerable_test.rb +17 -0
  80. data/test/obix/objects/error_test.rb +47 -0
  81. data/test/obix/objects/feed_test.rb +16 -0
  82. data/test/obix/objects/float_test.rb +16 -0
  83. data/test/obix/objects/integer_test.rb +16 -0
  84. data/test/obix/objects/list_test.rb +17 -0
  85. data/test/obix/objects/object_test.rb +17 -0
  86. data/test/obix/objects/operation_test.rb +37 -0
  87. data/test/obix/objects/reference_test.rb +16 -0
  88. data/test/obix/objects/string_test.rb +16 -0
  89. data/test/obix/objects/time_test.rb +16 -0
  90. data/test/obix/objects/uri_test.rb +16 -0
  91. data/test/obix/objects_test.rb +13 -0
  92. data/test/obix/tag_test.rb +42 -0
  93. data/test/obix/types/boolean_test.rb +18 -0
  94. data/test/obix/types/date_test.rb +20 -0
  95. data/test/obix/types/duration_test.rb +18 -0
  96. data/test/obix/types/float_test.rb +18 -0
  97. data/test/obix/types/integer_test.rb +18 -0
  98. data/test/obix/types/string_test.rb +18 -0
  99. data/test/obix/types/time_test.rb +20 -0
  100. data/test/obix/types/uri_test.rb +48 -0
  101. data/test/obix/watch_test.rb +169 -0
  102. data/test/obix_test.rb +42 -0
  103. data/test/test_helper.rb +18 -0
  104. metadata +357 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c01e1acd9c0991b24c9c60188e66a864760e6497
4
+ data.tar.gz: 6ab23c6b03f95b153b17bd1546a4e1c4838f753d
5
+ SHA512:
6
+ metadata.gz: 17c06e109456a511132d9add1fedb40fe580b7a72f689b9fad8d76a3d336d79e38d4cfac234956b58ca4bacc565258bf576078ce3900db5a9ac1baf2e8095403
7
+ data.tar.gz: 675c562b85336c9f3c53681cee97cd1da87d10322838777cf4036d85ceb015b69d447edc61b93cc78974d18c8daca76d58c5eeb97de8f4ed14f18ba655978223
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in obix.gemspec
4
+ gemspec
@@ -0,0 +1,5 @@
1
+ guard 'minitest' do
2
+ watch(%r|^test/(.*)\/?(.*)_test\.rb|)
3
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
4
+ watch(%r|^test/test_helper\.rb|) { "test" }
5
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Johannes Gorset
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,128 @@
1
+ # OBIX
2
+
3
+ [![Code Climate](https://codeclimate.com/github/hyperoslo/obix.png)](https://codeclimate.com/github/hyperoslo/obix)
4
+
5
+ Ruby OBIX parser.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'obix'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install obix
20
+
21
+ ## Usage
22
+
23
+ ### Parse an object
24
+
25
+ ```xml
26
+ <!-- thermostat.xml -->
27
+ <obj href="http://domain/thermostat">
28
+ <real name="spaceTemp" unit="obix:units/fahrenheit" val="67.2"/>
29
+ <real name="setpoint" unit="obix:units/fahrenheit" val="72.0"/>
30
+ <bool name="furnaceOn" val="true"/>
31
+ </obj>
32
+ ```
33
+
34
+ ```ruby
35
+ # thermostat.rb
36
+ thermostat = OBIX.parse file: "thermostat.xml"
37
+
38
+ thermostat.href # => "http://domain/thermostat/"
39
+
40
+ temperature = thermostat.objects.find { |obj| obj.name == "spaceTemp" }
41
+ temperature.val # => 67.2
42
+ ```
43
+
44
+ ### Build an object
45
+
46
+ ```ruby
47
+ # thermostat.rb
48
+ object = OBIX::Builder.new
49
+ obj href: "http://domain/thermostat" do
50
+ real name: "spaceTemp", unit: "obix:units/fahrenheit" val: 67.2
51
+ real name: "setpoint", unit: "obix:units/fahrenheit" val: 72.0
52
+ bool name: "furnaceOn", val: true
53
+ end
54
+ end
55
+ ```
56
+
57
+ ### Invoke an operation
58
+
59
+ ```xml
60
+ <!-- furnace.xml -->
61
+ <obj href="http://domain/furnace">
62
+ <bool name="active" val="false"/>
63
+ <op name="activate" href="http://domain/furnace/activate" in="obix:Nil" out="obix:Nil"/>
64
+ <op name="deactivate" href="http://domain/furnace/deactivate" in="obix:Nil" out="obix:Nil"/>
65
+ </obj>
66
+ ```
67
+
68
+ ```ruby
69
+ # furnace.rb
70
+ furnace = OBIX.parse file: "furnace.xml"
71
+
72
+ activate = furnace.objects.find { |obj| obj.name == "activate" }
73
+ activate.invoke
74
+ ```
75
+
76
+ ### Create a watch
77
+
78
+ ```ruby
79
+ # Create a new watch...
80
+ watch = OBIX::Watch.make url: "http://domain/watchservice"
81
+
82
+ # ... or connect to an existing watch
83
+ watch = OBIX::Watch.connect url: "http://domain/watchservice/watch1"
84
+
85
+ # Add objects to the watch
86
+ watch.add ["/thermostat", "/furnace"]
87
+
88
+ # Remove objects from the watch
89
+ watch.remove ["/furnace"]
90
+
91
+ # Poll everything
92
+ watch.all
93
+
94
+ # Poll changes
95
+ watch.changes
96
+
97
+ # Delete the watch
98
+ watch.delete
99
+ ```
100
+
101
+ ### Query an object's history
102
+
103
+ ```ruby
104
+ history = OBIX::History.new url: "http://domain/history"
105
+
106
+ history.query start: 2.year.ago, end: 1.year.ago
107
+ ```
108
+
109
+ ### Query alarms
110
+
111
+ ```ruby
112
+ alarms = OBIX::Alarms.new url: "http://domain/alarmservice"
113
+
114
+ alarms.query start: 2.years.ago, end: 1.year.ago
115
+ ```
116
+
117
+ ## Contributing
118
+
119
+ 1. Fork it
120
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
121
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
122
+ 4. Push to the branch (`git push origin my-new-feature`)
123
+ 5. Create new Pull Request
124
+
125
+ ## Credits
126
+
127
+ Hyper made this. We're a digital communications agency with a passion for good code,
128
+ and if you're using this library we probably want to hire you.
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ end
@@ -0,0 +1,55 @@
1
+ require "obix/version"
2
+ require "nokogiri"
3
+ require "net/http"
4
+ require "obix/errors"
5
+
6
+ module OBIX
7
+ autoload :Objects, "obix/objects"
8
+ autoload :Types, "obix/types"
9
+ autoload :Watch, "obix/watch"
10
+ autoload :Alarms, "obix/alarms"
11
+ autoload :History, "obix/history"
12
+ autoload :Tag, "obix/tag"
13
+ autoload :Builder, "obix/builder"
14
+ autoload :Configuration, "obix/configuration"
15
+ autoload :Network, "obix/network"
16
+
17
+ # Parse the given source as OBIX.
18
+ #
19
+ # source - A Hash describing the source.
20
+ # :string - A String describing oBIX data.
21
+ # :file - A String describing a file that contains oBIX data.
22
+ # :url - A String describing a URL that the oBIX data may be loaded from.
23
+ #
24
+ # Returns an OBIX::Object instance.
25
+ def self.parse source
26
+ string = source[:string]
27
+ url = source[:url]
28
+ file = source[:file]
29
+
30
+ if url
31
+ string = Network.get url
32
+ end
33
+
34
+ if file
35
+ string = File.read file
36
+ end
37
+
38
+ document = Nokogiri::XML.parse string do |config|
39
+ config.noblanks
40
+ config.strict
41
+ end
42
+
43
+ object = OBIX::Objects::Object.parse document.root
44
+
45
+ object
46
+ end
47
+
48
+ def self.configuration
49
+ Configuration
50
+ end
51
+
52
+ def self.configure &block
53
+ Configuration.configure &block
54
+ end
55
+ end
@@ -0,0 +1,48 @@
1
+ module OBIX
2
+
3
+ class Alarms
4
+
5
+ # Initialize an alarm service.
6
+ #
7
+ # source - A Hash of options (see OBIX#parse for details).
8
+ def initialize source
9
+ @alarms = OBIX.parse source
10
+ end
11
+
12
+ # The number of alarms.
13
+ #
14
+ # Returns an Integer.
15
+ def count
16
+ @alarms.objects.find { |o| o.name == "count" }.val
17
+ end
18
+
19
+ # Query alarms.
20
+ #
21
+ # options - A Hash of options:
22
+ # :start - A DateTime instance describing the earliest time to query history for.
23
+ # :end - A DateTime instance describing the latest time to query history for.
24
+ def query options
25
+ from = options.fetch :start
26
+ to = options.fetch :end
27
+
28
+ query = @alarms.objects.find { |o| o.name == "query" }
29
+
30
+ filter = OBIX::Builder.new do
31
+ obj do
32
+ abstime name: "start", val: from.iso8601
33
+ abstime name: "end", val: to.iso8601
34
+ end
35
+ end.object
36
+
37
+ alarms = query.invoke filter
38
+
39
+ alarms.objects.find { |o| o.name == "data" }
40
+ end
41
+
42
+ def to_s
43
+ @alarms.to_s
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,45 @@
1
+ module OBIX
2
+
3
+ class Builder
4
+ attr_reader :objects
5
+
6
+ # Initialize a new builder.
7
+ #
8
+ # parent - An Objects::Object instance or derivative thereof describing the parent
9
+ # of the object that will be built.
10
+ def initialize parent = nil, &block
11
+ @parent = parent
12
+ @objects = []
13
+
14
+ instance_eval &block
15
+ end
16
+
17
+ def object
18
+ objects.first
19
+ end
20
+
21
+ # Respond to methods matching OBIX tags.
22
+ def method_missing method, attributes = {}, &block
23
+ klass = Objects.find method
24
+
25
+ object = klass.new &block
26
+
27
+ attributes.each do |key, value|
28
+ object.send "#{key}=", value
29
+ end
30
+
31
+ object.parent = @parent
32
+
33
+ if @parent
34
+ @objects.push object
35
+ else
36
+ if @objects.empty?
37
+ @objects.push object
38
+ else
39
+ raise ArgumentError, "Object already has a root"
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,79 @@
1
+ module OBIX
2
+
3
+ module Configuration
4
+
5
+ def self.configure &block
6
+ instance_eval &block
7
+ end
8
+
9
+ def self.definitions
10
+ @definitions ||= {}
11
+ end
12
+
13
+ def self.definitions= hash
14
+ @definitions = hash
15
+ end
16
+
17
+ # The username to use when communicating with the server.
18
+ #
19
+ # username - A String describing the username.
20
+ def self.username= username
21
+ definitions[:username] = username
22
+ end
23
+
24
+ # The username to use when communicating with the server.
25
+ def self.username
26
+ definitions[:username]
27
+ end
28
+
29
+ # The password to use when communicating with the server.
30
+ #
31
+ # password - A String describing the password.
32
+ def self.password= password
33
+ definitions[:password] = password
34
+ end
35
+
36
+ # The password to use when communicating with the server.
37
+ def self.password
38
+ definitions[:password]
39
+ end
40
+
41
+ # The scheme of the server.
42
+ #
43
+ # host - A String describing the host.
44
+ def self.scheme= scheme
45
+ definitions[:scheme] = scheme
46
+ end
47
+
48
+ # The scheme of the server.
49
+ def self.scheme
50
+ definitions[:scheme] || "http"
51
+ end
52
+
53
+ # The host of the server.
54
+ #
55
+ # host - A String describing the host.
56
+ def self.host= host
57
+ definitions[:host] = host
58
+ end
59
+
60
+ # The host of the server.
61
+ def self.host
62
+ definitions[:host]
63
+ end
64
+
65
+ # Timeout.
66
+ #
67
+ # seconds - An Integer describing timeout in seconds.
68
+ def self.timeout= seconds
69
+ definitions[:timeout] = seconds
70
+ end
71
+
72
+ # Timeout.
73
+ def self.timeout
74
+ definitions[:timeout] || 30
75
+ end
76
+
77
+ end
78
+
79
+ end
@@ -0,0 +1,6 @@
1
+ module OBIX
2
+ class Error < StandardError; end
3
+ class BadURIError < Error; end
4
+ class UnsupportedError < Error; end
5
+ class PermissionError < Error; end
6
+ end
@@ -0,0 +1,65 @@
1
+ module OBIX
2
+
3
+ class History
4
+
5
+ # Initialize a history.
6
+ #
7
+ # source - A Hash of options (see OBIX#parse for details).
8
+ def initialize source
9
+ @history = OBIX.parse source
10
+ end
11
+
12
+ # The number of history records contained by the history.
13
+ #
14
+ # Returns an Integer.
15
+ def count
16
+ @history.objects.find { |o| o.name == "count" }.val
17
+ end
18
+
19
+ # The timestamp of the oldest record contained by the history.
20
+ #
21
+ # Returns a DateTime instance.
22
+ def start
23
+ @history.objects.find { |o| o.name == "start" }.val
24
+ end
25
+
26
+ # The timestamp of the newest record contained by the history.
27
+ def end
28
+ @history.objects.find { |o| o.name == "end" }.val
29
+ end
30
+
31
+ # The timezone of the history data.
32
+ def timezone
33
+ @history.objects.find { |o| o.name == "tz" }.val
34
+ end
35
+
36
+ # Query the history records contained by the history.
37
+ #
38
+ # options - A Hash of options:
39
+ # :start - A DateTime instance describing the earliest time to query history for.
40
+ # :end - A DateTime instance describing the latest time to query history for.
41
+ def query options
42
+ from = options.fetch :start
43
+ to = options.fetch :end
44
+
45
+ query = @history.objects.find { |o| o.name == "query" }
46
+
47
+ filter = OBIX::Builder.new do
48
+ obj do
49
+ abstime name: "start", val: from.iso8601
50
+ abstime name: "end", val: to.iso8601
51
+ end
52
+ end.object
53
+
54
+ history = query.invoke filter
55
+
56
+ history.objects.find { |o| o.name == "data" }
57
+ end
58
+
59
+ def to_s
60
+ @history.to_s
61
+ end
62
+
63
+ end
64
+
65
+ end