obix 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +128 -0
- data/Rakefile +7 -0
- data/lib/obix.rb +55 -0
- data/lib/obix/alarms.rb +48 -0
- data/lib/obix/builder.rb +45 -0
- data/lib/obix/configuration.rb +79 -0
- data/lib/obix/errors.rb +6 -0
- data/lib/obix/history.rb +65 -0
- data/lib/obix/network.rb +76 -0
- data/lib/obix/objects.rb +46 -0
- data/lib/obix/objects/base.rb +97 -0
- data/lib/obix/objects/boolean.rb +13 -0
- data/lib/obix/objects/date.rb +15 -0
- data/lib/obix/objects/duration.rb +14 -0
- data/lib/obix/objects/enumerable.rb +13 -0
- data/lib/obix/objects/error.rb +26 -0
- data/lib/obix/objects/feed.rb +14 -0
- data/lib/obix/objects/float.rb +16 -0
- data/lib/obix/objects/integer.rb +15 -0
- data/lib/obix/objects/list.rb +14 -0
- data/lib/obix/objects/object.rb +20 -0
- data/lib/obix/objects/operation.rb +28 -0
- data/lib/obix/objects/reference.rb +10 -0
- data/lib/obix/objects/string.rb +14 -0
- data/lib/obix/objects/time.rb +15 -0
- data/lib/obix/objects/uri.rb +13 -0
- data/lib/obix/tag.rb +43 -0
- data/lib/obix/types.rb +15 -0
- data/lib/obix/types/boolean.rb +25 -0
- data/lib/obix/types/date.rb +20 -0
- data/lib/obix/types/duration.rb +21 -0
- data/lib/obix/types/float.rb +18 -0
- data/lib/obix/types/integer.rb +18 -0
- data/lib/obix/types/string.rb +18 -0
- data/lib/obix/types/time.rb +20 -0
- data/lib/obix/types/type.rb +17 -0
- data/lib/obix/types/uri.rb +28 -0
- data/lib/obix/version.rb +3 -0
- data/lib/obix/watch.rb +119 -0
- data/obix.gemspec +31 -0
- data/test/fixtures/alarmqueryout.xml +19 -0
- data/test/fixtures/alarmsubject.xml +5 -0
- data/test/fixtures/history.xml +9 -0
- data/test/fixtures/historyqueryout.xml +15 -0
- data/test/fixtures/invalid.xml +1 -0
- data/test/fixtures/objects/boolean.xml +1 -0
- data/test/fixtures/objects/date.xml +1 -0
- data/test/fixtures/objects/duration.xml +1 -0
- data/test/fixtures/objects/enumerable.xml +1 -0
- data/test/fixtures/objects/error.xml +1 -0
- data/test/fixtures/objects/feed.xml +4 -0
- data/test/fixtures/objects/float.xml +1 -0
- data/test/fixtures/objects/integer.xml +1 -0
- data/test/fixtures/objects/list.xml +4 -0
- data/test/fixtures/objects/object.xml +1 -0
- data/test/fixtures/objects/operation.xml +1 -0
- data/test/fixtures/objects/reference.xml +1 -0
- data/test/fixtures/objects/string.xml +1 -0
- data/test/fixtures/objects/time.xml +1 -0
- data/test/fixtures/objects/uri.xml +1 -0
- data/test/fixtures/valid.xml +11 -0
- data/test/fixtures/watch.xml +8 -0
- data/test/fixtures/watchout.xml +5 -0
- data/test/fixtures/watchservice.xml +3 -0
- data/test/obix/alarms_test.rb +36 -0
- data/test/obix/builder_test.rb +32 -0
- data/test/obix/configuration_test.rb +32 -0
- data/test/obix/history_test.rb +54 -0
- data/test/obix/network_test.rb +134 -0
- data/test/obix/objects/base_test.rb +54 -0
- data/test/obix/objects/boolean_test.rb +17 -0
- data/test/obix/objects/date_test.rb +16 -0
- data/test/obix/objects/duration_test.rb +16 -0
- data/test/obix/objects/enumerable_test.rb +17 -0
- data/test/obix/objects/error_test.rb +47 -0
- data/test/obix/objects/feed_test.rb +16 -0
- data/test/obix/objects/float_test.rb +16 -0
- data/test/obix/objects/integer_test.rb +16 -0
- data/test/obix/objects/list_test.rb +17 -0
- data/test/obix/objects/object_test.rb +17 -0
- data/test/obix/objects/operation_test.rb +37 -0
- data/test/obix/objects/reference_test.rb +16 -0
- data/test/obix/objects/string_test.rb +16 -0
- data/test/obix/objects/time_test.rb +16 -0
- data/test/obix/objects/uri_test.rb +16 -0
- data/test/obix/objects_test.rb +13 -0
- data/test/obix/tag_test.rb +42 -0
- data/test/obix/types/boolean_test.rb +18 -0
- data/test/obix/types/date_test.rb +20 -0
- data/test/obix/types/duration_test.rb +18 -0
- data/test/obix/types/float_test.rb +18 -0
- data/test/obix/types/integer_test.rb +18 -0
- data/test/obix/types/string_test.rb +18 -0
- data/test/obix/types/time_test.rb +20 -0
- data/test/obix/types/uri_test.rb +48 -0
- data/test/obix/watch_test.rb +169 -0
- data/test/obix_test.rb +42 -0
- data/test/test_helper.rb +18 -0
- metadata +357 -0
data/lib/obix/network.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require "net/http"
|
2
|
+
|
3
|
+
module OBIX
|
4
|
+
module Network
|
5
|
+
include Net
|
6
|
+
|
7
|
+
extend self
|
8
|
+
|
9
|
+
# Make a get request.
|
10
|
+
#
|
11
|
+
# url - A String describing where the request should be sent.
|
12
|
+
# object - A Objects::Object or derivative thereof describing the OBIX element to send.
|
13
|
+
def get url
|
14
|
+
url = URI url
|
15
|
+
request = HTTP::Get.new url.path
|
16
|
+
|
17
|
+
dispatch request, to: url
|
18
|
+
end
|
19
|
+
|
20
|
+
# Make a post request.
|
21
|
+
#
|
22
|
+
# url - A String describing where the request should be sent.
|
23
|
+
# object - A Objects::Object or derivative thereof describing the OBIX element to send.
|
24
|
+
def post url, object = nil
|
25
|
+
url = URI url
|
26
|
+
request = HTTP::Post.new url.path
|
27
|
+
|
28
|
+
if object
|
29
|
+
request.body = object.to_xml
|
30
|
+
end
|
31
|
+
|
32
|
+
dispatch request, to: url
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
# Dispatch the given request.
|
38
|
+
#
|
39
|
+
# request - A derivative of Net::HTTPGenericRequest describing the request.
|
40
|
+
# options - A Hash with query options:
|
41
|
+
# :to - An URI instance describing where the request should be sent.
|
42
|
+
#
|
43
|
+
# Returns a Net::HTTPResponse instance.
|
44
|
+
def dispatch request, options
|
45
|
+
url = options.fetch :to
|
46
|
+
|
47
|
+
if url.relative?
|
48
|
+
url = URI.join "#{OBIX.configuration.scheme}://#{OBIX.configuration.host}", url
|
49
|
+
end
|
50
|
+
|
51
|
+
options = {
|
52
|
+
use_ssl: true,
|
53
|
+
verify_mode: OpenSSL::SSL::VERIFY_NONE,
|
54
|
+
open_timeout: 10,
|
55
|
+
read_timeout: OBIX.configuration.timeout
|
56
|
+
}
|
57
|
+
|
58
|
+
response = HTTP.start url.host, url.port, options do |http|
|
59
|
+
request.basic_auth OBIX.configuration.username, OBIX.configuration.password
|
60
|
+
http.request request
|
61
|
+
end
|
62
|
+
|
63
|
+
unless response.code == "200"
|
64
|
+
raise Error, "The server responded with an unexpected status code #{response.code} for #{url}."
|
65
|
+
end
|
66
|
+
|
67
|
+
response.body
|
68
|
+
rescue ::Timeout::Error
|
69
|
+
raise Timeout, "The remote server did not respond in a timely fashion"
|
70
|
+
end
|
71
|
+
|
72
|
+
class Error < ::OBIX::Error; end
|
73
|
+
|
74
|
+
class Timeout < Error; end
|
75
|
+
end
|
76
|
+
end
|
data/lib/obix/objects.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module OBIX
|
2
|
+
module Objects
|
3
|
+
autoload :Base, "obix/objects/base"
|
4
|
+
autoload :Object, "obix/objects/object"
|
5
|
+
autoload :Float, "obix/objects/float"
|
6
|
+
autoload :Integer, "obix/objects/integer"
|
7
|
+
autoload :Boolean, "obix/objects/boolean"
|
8
|
+
autoload :String, "obix/objects/string"
|
9
|
+
autoload :Enumerable, "obix/objects/enumerable"
|
10
|
+
autoload :Time, "obix/objects/time"
|
11
|
+
autoload :Date, "obix/objects/date"
|
12
|
+
autoload :List, "obix/objects/list"
|
13
|
+
autoload :Operation, "obix/objects/operation"
|
14
|
+
autoload :Duration, "obix/objects/duration"
|
15
|
+
autoload :URI, "obix/objects/uri"
|
16
|
+
autoload :Reference, "obix/objects/reference"
|
17
|
+
autoload :Error, "obix/objects/error"
|
18
|
+
autoload :Feed, "obix/objects/feed"
|
19
|
+
|
20
|
+
@objects = [
|
21
|
+
Object, Float, Integer, Boolean, String, Enumerable, Time,
|
22
|
+
Date, List, Operation, Duration, URI, Reference, Error, Feed
|
23
|
+
]
|
24
|
+
|
25
|
+
# List objects.
|
26
|
+
#
|
27
|
+
# Returns an array of OBIX::Objects::Object instances or derivatives thereof.
|
28
|
+
def self.list
|
29
|
+
@objects
|
30
|
+
end
|
31
|
+
|
32
|
+
# Find an object by the given tag.
|
33
|
+
#
|
34
|
+
# tag - A String or Symbol describing a tag.
|
35
|
+
#
|
36
|
+
# Returns a derivative of OBIX::Objects::Base.
|
37
|
+
def self.find tag
|
38
|
+
object = @objects.find do |object|
|
39
|
+
object.new.tag.to_s == tag.to_s
|
40
|
+
end or raise OBIX::Objects::UnknownObjectError, "Unknown element '#{tag}'"
|
41
|
+
end
|
42
|
+
|
43
|
+
class UnknownObjectError < StandardError; end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module OBIX
|
2
|
+
module Objects
|
3
|
+
|
4
|
+
class Base
|
5
|
+
extend Tag
|
6
|
+
|
7
|
+
attr_accessor :objects, :parent
|
8
|
+
|
9
|
+
def initialize &block
|
10
|
+
@attributes = {}
|
11
|
+
@objects = []
|
12
|
+
|
13
|
+
if block_given?
|
14
|
+
builder = OBIX::Builder.new self, &block
|
15
|
+
|
16
|
+
@objects = builder.objects
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Find the object by the given name.
|
21
|
+
#
|
22
|
+
# name - A String or Symbol describing the "name" attribute of an object.
|
23
|
+
def find name
|
24
|
+
object = objects.find { |o| o.name == name.to_s }
|
25
|
+
end
|
26
|
+
|
27
|
+
# Serialize the object as a Nokogiri::XML::Node.
|
28
|
+
def to_node
|
29
|
+
Nokogiri::XML::Builder.new do |xml|
|
30
|
+
build xml
|
31
|
+
end.doc.root
|
32
|
+
end
|
33
|
+
|
34
|
+
# Serialize the object as an XML String.
|
35
|
+
def to_xml
|
36
|
+
to_node.document.to_xml
|
37
|
+
end
|
38
|
+
|
39
|
+
# Serialize the object as a human-readable String.
|
40
|
+
def to_s
|
41
|
+
attributes = @attributes.map do |key, value|
|
42
|
+
"#{key}: \"#{value}\""
|
43
|
+
end.join " "
|
44
|
+
|
45
|
+
"#<#{self.class} #{attributes}>"
|
46
|
+
end
|
47
|
+
|
48
|
+
class << self
|
49
|
+
|
50
|
+
# Parse an XML element as OBIX.
|
51
|
+
#
|
52
|
+
# element - A Nokogiri::XML::Node describing an element.
|
53
|
+
# parent - An Objects::Object instance or derivative thereof describing
|
54
|
+
# the object's parent.
|
55
|
+
#
|
56
|
+
# Returns an Object instance or derivative thereof.
|
57
|
+
def parse element, parent = nil
|
58
|
+
klass = Objects.find element.name
|
59
|
+
|
60
|
+
object = klass.new
|
61
|
+
|
62
|
+
element.attributes.each do |name, attribute|
|
63
|
+
object.send "#{name.underscore}=", attribute.value unless attribute.namespace
|
64
|
+
end
|
65
|
+
|
66
|
+
element.children.each do |child|
|
67
|
+
object.objects.push parse(child, object) unless child.is_a? Nokogiri::XML::Text
|
68
|
+
end
|
69
|
+
|
70
|
+
object.parent = parent
|
71
|
+
|
72
|
+
if object.is_a? Error
|
73
|
+
object.raise
|
74
|
+
end
|
75
|
+
|
76
|
+
object
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
protected
|
82
|
+
|
83
|
+
# Build the object as an element in a tree.
|
84
|
+
#
|
85
|
+
# xml - A Nokogiri::XML::Builder instance.
|
86
|
+
def build builder
|
87
|
+
builder.send tag, @attributes do |builder|
|
88
|
+
objects.each do |object|
|
89
|
+
object.build builder
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module OBIX
|
2
|
+
module Objects
|
3
|
+
|
4
|
+
# Dates represent a day in time.
|
5
|
+
class Date < Object
|
6
|
+
tag :date
|
7
|
+
|
8
|
+
attribute :val, type: Types::Date, default: nil
|
9
|
+
attribute :min, type: Types::Date
|
10
|
+
attribute :max, type: Types::Date
|
11
|
+
attribute :tz, type: Types::String
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module OBIX
|
2
|
+
module Objects
|
3
|
+
|
4
|
+
# Durations represent a relative duration of time.
|
5
|
+
class Duration < Object
|
6
|
+
tag :reltime
|
7
|
+
|
8
|
+
attribute :val, type: Types::Duration, default: 0
|
9
|
+
attribute :min, type: Types::Duration
|
10
|
+
attribute :max, type: Types::Duration
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module OBIX
|
2
|
+
module Objects
|
3
|
+
|
4
|
+
# Enumerables represent a value which must match a finite set of values.
|
5
|
+
class Enumerable < Object
|
6
|
+
tag :enum
|
7
|
+
|
8
|
+
attribute :val, type: Types::String, default: nil
|
9
|
+
attribute :range, type: Types::String
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module OBIX
|
2
|
+
module Objects
|
3
|
+
|
4
|
+
# Errors represent errors.
|
5
|
+
class Error < Object
|
6
|
+
tag :err
|
7
|
+
|
8
|
+
# Raise an exception with this error.
|
9
|
+
def raise
|
10
|
+
klass = case is
|
11
|
+
when "obix:BadUriErr"
|
12
|
+
OBIX::BadURIError
|
13
|
+
when "obix:UnsupportedErr"
|
14
|
+
OBIX::UnsupportedError
|
15
|
+
when "obix:PermissionErr"
|
16
|
+
OBIX::PermissionError
|
17
|
+
else
|
18
|
+
OBIX::Error
|
19
|
+
end
|
20
|
+
|
21
|
+
super klass, self.to_xml
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module OBIX
|
2
|
+
module Objects
|
3
|
+
|
4
|
+
# Floats represent floating point numbers.
|
5
|
+
class Float < Object
|
6
|
+
tag :real
|
7
|
+
|
8
|
+
attribute :val, type: Types::Float, default: 0.0
|
9
|
+
attribute :min, type: Types::Float
|
10
|
+
attribute :max, type: Types::Float
|
11
|
+
attribute :unit, type: Types::String
|
12
|
+
attribute :precision, type: Types::Integer
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module OBIX
|
2
|
+
module Objects
|
3
|
+
|
4
|
+
# Integers represent an integer number.
|
5
|
+
class Integer < Object
|
6
|
+
tag :int
|
7
|
+
|
8
|
+
attribute :val, type: Types::Integer, default: 0
|
9
|
+
attribute :min, type: Types::Integer
|
10
|
+
attribute :max, type: Types::Integer
|
11
|
+
attribute :unit, type: Types::String
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module OBIX
|
2
|
+
module Objects
|
3
|
+
|
4
|
+
# Lists represent a collection of other objects.
|
5
|
+
class List < Object
|
6
|
+
tag :list
|
7
|
+
|
8
|
+
attribute :of, type: Types::String
|
9
|
+
attribute :min, type: Types::Integer
|
10
|
+
attribute :max, type: Types::Integer
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module OBIX
|
2
|
+
module Objects
|
3
|
+
|
4
|
+
# Objects are the root abstraction in oBIX.
|
5
|
+
class Object < Base
|
6
|
+
tag :obj
|
7
|
+
|
8
|
+
attribute :name, type: Types::String
|
9
|
+
attribute :href, type: Types::URI, default: ""
|
10
|
+
attribute :is, type: Types::String
|
11
|
+
attribute :null, type: Types::Boolean
|
12
|
+
attribute :icon, type: Types::String
|
13
|
+
attribute :display_name, type: Types::String
|
14
|
+
attribute :display, type: Types::String
|
15
|
+
attribute :writable, type: Types::Boolean
|
16
|
+
attribute :status, type: Types::String
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "net/http"
|
2
|
+
|
3
|
+
module OBIX
|
4
|
+
module Objects
|
5
|
+
|
6
|
+
# Operations represent operations.
|
7
|
+
class Operation < Object
|
8
|
+
tag :op
|
9
|
+
|
10
|
+
attribute :in, type: Types::String
|
11
|
+
attribute :out, type: Types::String
|
12
|
+
|
13
|
+
# Invoke the operation.
|
14
|
+
#
|
15
|
+
# object - An OBIX::Objects::Object or derivative thereof. Defaults to an object
|
16
|
+
# that implements the "obix:Nil" contract.
|
17
|
+
#
|
18
|
+
# Returns an OBIX::Objects::Object or derivative thereof describing
|
19
|
+
# the result of the operation.
|
20
|
+
def invoke object = OBIX::Objects::Object.new
|
21
|
+
string = Network.post href, object
|
22
|
+
|
23
|
+
OBIX.parse string: string
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module OBIX
|
2
|
+
module Objects
|
3
|
+
|
4
|
+
# Strings represent strings of characters.
|
5
|
+
class String < Object
|
6
|
+
tag :str
|
7
|
+
|
8
|
+
attribute :val, type: Types::String, default: ""
|
9
|
+
attribute :min, type: Types::Integer
|
10
|
+
attribute :max, type: Types::Integer
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|