obix 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,15 @@
1
+ module OBIX
2
+ module Objects
3
+
4
+ # Times represent an absolute point in time.
5
+ class Time < Object
6
+ tag :abstime
7
+
8
+ attribute :val, type: Types::Time, default: nil
9
+ attribute :min, type: Types::Time
10
+ attribute :max, type: Types::Time
11
+ attribute :tz, type: Types::String
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ module OBIX
2
+ module Objects
3
+
4
+ # URIs represent a URI reference.
5
+ class URI < Object
6
+ tag :uri
7
+
8
+ attribute :val, type: Types::String
9
+ end
10
+
11
+ end
12
+ end
13
+
@@ -0,0 +1,43 @@
1
+ require "active_support/all"
2
+
3
+ module OBIX
4
+ module Tag
5
+
6
+ # Define a method for accessing the name of the tag.
7
+ #
8
+ # name - A Symbol or String describing the name of the tag.
9
+ def tag name
10
+ define_method :tag do
11
+ name
12
+ end
13
+ end
14
+
15
+ # Define a method for accessing the given attribute.
16
+ #
17
+ # name - A Symbol describing the name of the attribute.
18
+ # options - A Hash of query options:
19
+ # :type - A class describing the type of this attribute (defaults to nil).
20
+ def attribute name, options = {}
21
+ klass = options.fetch :type, nil
22
+ default = options.fetch :default, nil
23
+
24
+ name = name.to_s.underscore
25
+
26
+ define_method name do
27
+ value = @attributes[name] || default
28
+
29
+ if klass
30
+ type = klass.new self
31
+ type.cast value
32
+ else
33
+ value
34
+ end
35
+ end
36
+
37
+ define_method "#{name}=" do |value|
38
+ @attributes.store name, value
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ module OBIX
2
+
3
+ module Types
4
+ autoload :Type, "obix/types/type"
5
+ autoload :String, "obix/types/string"
6
+ autoload :Float, "obix/types/float"
7
+ autoload :Boolean, "obix/types/boolean"
8
+ autoload :Integer, "obix/types/integer"
9
+ autoload :Time, "obix/types/time"
10
+ autoload :Date, "obix/types/date"
11
+ autoload :Duration, "obix/types/duration"
12
+ autoload :URI, "obix/types/uri"
13
+ end
14
+
15
+ end
@@ -0,0 +1,25 @@
1
+ module OBIX
2
+ module Types
3
+
4
+ class Boolean < Type
5
+
6
+ # Cast the given value to a boolean.
7
+ #
8
+ # value - A string that is either "true" or "false".
9
+ #
10
+ # Returns a boolean.
11
+ def cast value
12
+ case value
13
+ when "true"
14
+ true
15
+ when "false"
16
+ false
17
+ else
18
+ raise StandardError, "Could not cast #{value} to a boolean"
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ require "date"
2
+
3
+ module OBIX
4
+ module Types
5
+
6
+ class Date < Type
7
+
8
+ # Cast the given value to a date.
9
+ #
10
+ # value - A string that may be parsed by `Date.parse`.
11
+ #
12
+ # Returns a string.
13
+ def cast value
14
+ ::Date.parse value
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ require "rdf/xsd"
2
+
3
+ module OBIX
4
+ module Types
5
+
6
+ class Duration < Type
7
+
8
+ # Cast the given value to an integer describing seconds.
9
+ #
10
+ # value - A String describing an XSD
11
+ #
12
+ # Returns an Integer.
13
+ def cast value
14
+ duration = RDF::Literal::Duration.new value
15
+ duration.to_i
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ module OBIX
2
+ module Types
3
+
4
+ class Float < Type
5
+
6
+ # Cast the given value to a float.
7
+ #
8
+ # value - Any object that responds to `to_f`.
9
+ #
10
+ # Returns a float.
11
+ def cast value
12
+ value.to_f
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module OBIX
2
+ module Types
3
+
4
+ class Integer < Type
5
+
6
+ # Cast the given value to an integer.
7
+ #
8
+ # value - Any object that responds to `to_i`.
9
+ #
10
+ # Returns an integer.
11
+ def cast value
12
+ value.to_i
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module OBIX
2
+ module Types
3
+
4
+ class String < Type
5
+
6
+ # Cast the given value to a string.
7
+ #
8
+ # value - Any object that responds to `to_s`.
9
+ #
10
+ # Returns a string.
11
+ def cast value
12
+ value.to_s
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ require "date"
2
+
3
+ module OBIX
4
+ module Types
5
+
6
+ class Time < Type
7
+
8
+ # Cast the given value to a time.
9
+ #
10
+ # value - A string that may be parsed by `Time.parse`.
11
+ #
12
+ # Returns a string.
13
+ def cast value
14
+ ::DateTime.parse value
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ module OBIX
2
+ module Types
3
+
4
+ class Type
5
+
6
+ # Initialize the Type.
7
+ #
8
+ # element - An Objects::Object instance or derivative thereof describing
9
+ # the element that this type belongs to.
10
+ def initialize element
11
+ @element = element
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ require "uri"
2
+
3
+ module OBIX
4
+ module Types
5
+
6
+ class URI < Type
7
+
8
+ # Cast the given value to a URI.
9
+ #
10
+ # value - Any object that responds to `to_s`.
11
+ #
12
+ # Returns a string.
13
+ def cast value
14
+ parent = @element.parent
15
+
16
+ if parent
17
+ url = ::URI.join parent.href, value
18
+ else
19
+ url = ::URI.join "#{OBIX.configuration.scheme}://#{OBIX.configuration.host}", value
20
+ end
21
+
22
+ url.to_s
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module OBIX
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,119 @@
1
+ module OBIX
2
+
3
+ class Watch
4
+
5
+ # Initialize a watch.
6
+ #
7
+ # watch - An OBIX object that implements the Watch contract.
8
+ def initialize watch
9
+ @watch = watch
10
+ end
11
+
12
+ # Reference the watch.
13
+ #
14
+ # Returns a String describing the URL to the watch.
15
+ def url
16
+ @watch.href
17
+ end
18
+
19
+ # Reference the lease of the watch.
20
+ #
21
+ # Returns an Integer describing the lease in seconds.
22
+ def lease
23
+ @watch.objects.find { |o| o.name == "lease" }.val
24
+ end
25
+
26
+ # Add objects to the watch.
27
+ #
28
+ # list - An Array of String instances describing objects to add to the watch.
29
+ def add list
30
+ add = @watch.objects.find { |obj| obj.name == "add" }
31
+
32
+ obix = OBIX::Builder.new do |obix|
33
+ obix.obj is: "obix:WatchIn" do |obix|
34
+ obix.list name: "hrefs" do |obix|
35
+ list.each do |item|
36
+ obix.uri val: item
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ add.invoke obix.object
43
+ end
44
+
45
+ # Remove objects from the watch.
46
+ #
47
+ # list - An Array of String instances describing objects to remove from the watch.
48
+ def remove list
49
+ remove = @watch.objects.find { |obj| obj.name == "remove" }
50
+
51
+ obix = OBIX::Builder.new do |obix|
52
+ obix.obj is: "obix:WatchIn" do |obix|
53
+ obix.list name: "hrefs" do |obix|
54
+ list.each do |item|
55
+ obix.uri val: item
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ remove.invoke obix.object
62
+ end
63
+
64
+ # Poll objects that have changed
65
+ def changes
66
+ poll = @watch.objects.find { |obj| obj.name == "pollChanges" }
67
+
68
+ poll.invoke.objects.find do |object|
69
+ object.name == "values"
70
+ end.objects
71
+ end
72
+
73
+ # Poll all objects
74
+ def all
75
+ poll = @watch.objects.find { |obj| obj.name == "pollRefresh" }
76
+
77
+ poll.invoke.objects.find do |object|
78
+ object.name == "values"
79
+ end.objects
80
+ end
81
+
82
+ # Delete the watch.
83
+ def delete
84
+ @watch.objects.find do |object|
85
+ object.name == "delete"
86
+ end.invoke
87
+ end
88
+
89
+ def to_s
90
+ @watch.to_s
91
+ end
92
+
93
+ class << self
94
+ alias make new
95
+
96
+ # Create a new watch.
97
+ #
98
+ # source - A Hash of options (see OBIX#parse for details).
99
+ def make source
100
+ service = OBIX.parse source
101
+
102
+ make = service.objects.find do |object|
103
+ object.name == "make"
104
+ end
105
+
106
+ new make.invoke
107
+ end
108
+
109
+ # Connect to an existing watch.
110
+ #
111
+ # source - A Hash of options (see OBIX#parse for details).
112
+ def connect source
113
+ new OBIX.parse source
114
+ end
115
+ end
116
+
117
+ end
118
+
119
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'obix/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "obix"
8
+ gem.version = OBIX::VERSION
9
+ gem.authors = ["Johannes Gorset"]
10
+ gem.email = ["jgorset@gmail.com"]
11
+ gem.description = "oBIX parser"
12
+ gem.summary = "oBIX parser"
13
+ gem.homepage = "http://github.com/hyperoslo/ruby-obix"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "rake"
21
+ gem.add_development_dependency "mocha"
22
+ gem.add_development_dependency "guard"
23
+ gem.add_development_dependency "guard-minitest"
24
+ gem.add_development_dependency "rb-inotify"
25
+ gem.add_development_dependency "rb-fsevent"
26
+ gem.add_development_dependency "rb-fchange"
27
+ gem.add_runtime_dependency "nokogiri"
28
+ gem.add_runtime_dependency "rdf-xsd"
29
+ gem.add_runtime_dependency "equivalent-xml"
30
+ gem.add_runtime_dependency "activesupport"
31
+ end