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,19 @@
1
+ <obj>
2
+ <int name="count" val="2"/>
3
+ <abstime name="start" val="2012-01-01T00:00:00"/>
4
+ <abstime name="end" val="2013-01-01T00:00:00"/>
5
+ <list name="data" of="obix:Alarm">
6
+ <obj>
7
+ <ref name="source" href="/thermostat"/>
8
+ <abstime name="timestamp" val="2012-01-01T00:00:00"/>
9
+ </obj>
10
+ <obj>
11
+ <ref name="source" href="/thermostat"/>
12
+ <abstime name="timestamp" val="2012-01-02T00:00:00"/>
13
+ </obj>
14
+ <obj>
15
+ <ref name="source" href="/thermostat"/>
16
+ <abstime name="timestamp" val="2012-01-03T00:00:00"/>
17
+ </obj>
18
+ </list>
19
+ </obj>
@@ -0,0 +1,5 @@
1
+ <obj>
2
+ <int name="count" min="0" val="3"/>
3
+ <op name="query" in="obix:AlarmFilter" out="obix:AlarmQueryOut"/>
4
+ <feed name="feed" in="obix:AlarmFilter" of="obix:Alarm"/>
5
+ </obj>
@@ -0,0 +1,9 @@
1
+ <obj href="http://domain/history/" is="obix:History">
2
+ <int name="count" val="100"/>
3
+ <abstime name="start" val="2012-01-01T00:00:00"/>
4
+ <abstime name="end" val="2013-01-01T00:00:00"/>
5
+ <op name="query" href="query/" in="obix:HistoryFilter" out="obix:HistoryQueryOut"/>
6
+ <op name="rollup" href="rollup/" in="obix:HistoryRollupIn" out="obix:HistoryRollupOut"/>
7
+ <feed name="feed" href="feed/" of="obix:HistoryRecord" in="obix:HistoryFilter"/>
8
+ <str name="tz" val="America/New_York"/>
9
+ </obj>
@@ -0,0 +1,15 @@
1
+ <obj href="obix:HistoryQueryOut">
2
+ <int name="count" val="2"/>
3
+ <abstime name="start" val="2012-01-01T00:00:00"/>
4
+ <abstime name="end" val="2013-01-01T00:00:00"/>
5
+ <list name="data" of="obix:HistoryRecord">
6
+ <obj>
7
+ <abstime name="timestamp" val="2012-01-01T00:00:00"/>
8
+ <real name="value" val="6.4"/>
9
+ </obj>
10
+ <obj>
11
+ <abstime name="timestamp" val="2012-01-01T01:00:00"/>
12
+ <real name="value" val="6.3"/>
13
+ </obj>
14
+ </list>
15
+ </obj>
@@ -0,0 +1 @@
1
+ <unknown val="unknown"/>
@@ -0,0 +1 @@
1
+ <bool val="true" range="http://example.org/range"/>
@@ -0,0 +1 @@
1
+ <date val="2007-11-26"/>
@@ -0,0 +1 @@
1
+ <reltime val="PT15S"/>
@@ -0,0 +1 @@
1
+ <enum range="/enums/OffSlowFast" val="slow"/>
@@ -0,0 +1 @@
1
+ <err display="uh oh"/>
@@ -0,0 +1,4 @@
1
+ <feed of="obix:Alarm">
2
+ <obj name="one"/>
3
+ <obj name="two"/>
4
+ </feed>
@@ -0,0 +1 @@
1
+ <real val="41.06"/>
@@ -0,0 +1 @@
1
+ <int val="52"/>
@@ -0,0 +1,4 @@
1
+ <list of="obix:str">
2
+ <str val="one"/>
3
+ <str val="two"/>
4
+ </list>
@@ -0,0 +1 @@
1
+ <obj name="object"/>
@@ -0,0 +1 @@
1
+ <op href="http://domain/operate" in="obix:Nil" out="obix:Nil"/>
@@ -0,0 +1 @@
1
+ <ref href="http://obix.org/"/>
@@ -0,0 +1 @@
1
+ <str val="hello world"/>
@@ -0,0 +1 @@
1
+ <abstime val="2005-03-09T13:30:00Z"/>
@@ -0,0 +1 @@
1
+ <uri val="http://obix.org/"/>
@@ -0,0 +1,11 @@
1
+ <?xml version="1.0"?>
2
+ <obj href="http://myhome/thermostat">
3
+ <real name="spaceTemp" unit="obix:units/fahrenheit" val="67.2"/>
4
+ <real name="setpoint" unit="obix:units/fahrenheit" val="72.0"/>
5
+ <bool name="furnaceOn" val="true"/>
6
+ <obj name="foo">
7
+ <obj name="bar">
8
+ <obj name="baz"/>
9
+ </obj>
10
+ </obj>
11
+ </obj>
@@ -0,0 +1,8 @@
1
+ <obj href="http://domain/watchservice/watch32/" is="obix:Watch" display="Obix Watch">
2
+ <reltime name="lease" val="PT10S" href="http://domain/watchservice/watch32/lease/" display="10sec" displayName="Lease" writable="true"/>
3
+ <op name="add" href="http://domain/watchservice/watch32/add/" in="obix:WatchIn" out="obix:WatchOut"/>
4
+ <op name="remove" href="http://domain/watchservice/watch32/remove/" in="obix:WatchIn" out="obix:Nil"/>
5
+ <op name="pollChanges" href="http://domain/watchservice/watch32/pollChanges/" in="obix:Nil" out="obix:WatchOut"/>
6
+ <op name="pollRefresh" href="http://domain/watchservice/watch32/pollRefresh/" in="obix:Nil" out="obix:WatchOut"/>
7
+ <op name="delete" href="http://domain/watchservice/watch32/delete/" in="obix:Nil" out="obix:Nil"/>
8
+ </obj>
@@ -0,0 +1,5 @@
1
+ <obj is="obix:WatchOut">
2
+ <list name="values">
3
+ <real val="39.28" href="/thermostat/" display="39.3"/>
4
+ </list>
5
+ </obj>
@@ -0,0 +1,3 @@
1
+ <obj href="http://domain/watchsservice/">
2
+ <op name="make" href="http://domain/watchservice/make/" in="obix:Nil" out="obix:Watch"/>
3
+ </obj>
@@ -0,0 +1,36 @@
1
+ require "obix"
2
+ require "minitest/unit"
3
+ require "minitest/autorun"
4
+ require "active_support/all"
5
+
6
+ require "test_helper"
7
+
8
+ class AlarmsTest < MiniTest::Unit::TestCase
9
+ def test_initialize
10
+ alarms = OBIX::Alarms.new file: "test/fixtures/alarmsubject.xml"
11
+
12
+ assert_instance_of OBIX::Alarms, alarms
13
+ end
14
+
15
+ def test_count
16
+ alarms = OBIX::Alarms.new file: "test/fixtures/alarmsubject.xml"
17
+
18
+ assert_equal 3, alarms.count
19
+ end
20
+
21
+ def test_query
22
+ alarms = OBIX::Alarms.new file: "test/fixtures/alarmsubject.xml"
23
+
24
+ OBIX::Objects::Operation.any_instance.
25
+ expects(
26
+ :invoke
27
+ ).
28
+ returns(
29
+ OBIX.parse file: "test/fixtures/alarmqueryout.xml"
30
+ )
31
+
32
+ alarms = alarms.query start: 1.year.ago, end: 0.seconds.ago
33
+
34
+ assert_instance_of OBIX::Objects::List, alarms
35
+ end
36
+ end
@@ -0,0 +1,32 @@
1
+ require "obix"
2
+ require "minitest/unit"
3
+ require "minitest/autorun"
4
+ require "nokogiri"
5
+ require "active_support/all"
6
+
7
+ require "test_helper"
8
+
9
+ class BuilderTest < MiniTest::Unit::TestCase
10
+ def test_builds
11
+ obix = OBIX::Builder.new do
12
+ obj name: "foo", href: "bar" do
13
+ obj name: "bar" do
14
+ obj name: "baz"
15
+ end
16
+ end
17
+ end
18
+
19
+ assert_equal "foo", obix.object.name
20
+ assert_equal "bar", obix.object.objects.first.name
21
+ assert_equal "baz", obix.object.objects.first.objects.first.name
22
+ end
23
+
24
+ def test_only_allows_a_single_root_element
25
+ assert_raises ArgumentError do
26
+ obix = OBIX::Builder.new do
27
+ obj name: "foo"
28
+ obj name: "bar"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ require "obix"
2
+ require "minitest/unit"
3
+ require "minitest/autorun"
4
+ require "active_support/all"
5
+
6
+ require "test_helper"
7
+
8
+ class ConfigurationTest < MiniTest::Unit::TestCase
9
+ def setup
10
+ @definitions = OBIX.configuration.definitions.clone
11
+ end
12
+
13
+ def teardown
14
+ OBIX.configuration.definitions = @definitions
15
+ end
16
+
17
+ def test_configures
18
+ OBIX::Configuration.configure do |config|
19
+ config.username = "username"
20
+ config.password = "password"
21
+ config.scheme = "scheme"
22
+ config.host = "host"
23
+ config.timeout = 20
24
+ end
25
+
26
+ assert_equal "username", OBIX::Configuration.username
27
+ assert_equal "password", OBIX::Configuration.password
28
+ assert_equal "scheme", OBIX::Configuration.scheme
29
+ assert_equal "host", OBIX::Configuration.host
30
+ assert_equal 20, OBIX::Configuration.timeout
31
+ end
32
+ end
@@ -0,0 +1,54 @@
1
+ require "obix"
2
+ require "minitest/unit"
3
+ require "minitest/autorun"
4
+ require "active_support/all"
5
+
6
+ require "test_helper"
7
+
8
+ class HistoryTest < MiniTest::Unit::TestCase
9
+ def test_initialize
10
+ history = OBIX::History.new file: "test/fixtures/history.xml"
11
+
12
+ assert_instance_of OBIX::History, history
13
+ end
14
+
15
+ def test_count
16
+ history = OBIX::History.new file: "test/fixtures/history.xml"
17
+
18
+ assert_equal 100, history.count
19
+ end
20
+
21
+ def test_start
22
+ history = OBIX::History.new file: "test/fixtures/history.xml"
23
+
24
+ assert_equal DateTime.parse("2012-01-01 00:00:00"), history.start
25
+ end
26
+
27
+ def test_end
28
+ history = OBIX::History.new file: "test/fixtures/history.xml"
29
+
30
+ assert_equal DateTime.parse("2013-01-01 00:00:00"), history.end
31
+ end
32
+
33
+ def test_timezone
34
+ history = OBIX::History.new file: "test/fixtures/history.xml"
35
+
36
+ assert_equal "America/New_York", history.timezone
37
+ end
38
+
39
+ def test_query
40
+ history = OBIX::History.new file: "test/fixtures/history.xml"
41
+
42
+ OBIX::Objects::Operation.any_instance.
43
+ expects(
44
+ :invoke
45
+ ).
46
+ returns(
47
+ OBIX.parse file: "test/fixtures/historyqueryout.xml"
48
+ )
49
+
50
+ records = history.query start: 1.year.ago, end: 0.seconds.ago
51
+
52
+ assert_instance_of OBIX::Objects::List, records
53
+ end
54
+ end
@@ -0,0 +1,134 @@
1
+ require "obix"
2
+ require "minitest/unit"
3
+ require "minitest/autorun"
4
+ require "active_support/all"
5
+
6
+ require "test_helper"
7
+
8
+ class NetworkTest < MiniTest::Unit::TestCase
9
+ include Net
10
+
11
+ def test_get
12
+ HTTP.
13
+ expects(
14
+ :start
15
+ ).
16
+ with(
17
+ "example.org", 80, {
18
+ use_ssl: true,
19
+ verify_mode: OpenSSL::SSL::VERIFY_NONE,
20
+ open_timeout: 10,
21
+ read_timeout: OBIX.configuration.timeout
22
+ }
23
+ ).
24
+ returns(
25
+ stub code: "200", body: "<body>"
26
+ )
27
+
28
+ body = OBIX::Network.get "http://example.org/"
29
+
30
+ assert_equal "<body>", body
31
+ end
32
+
33
+ def test_post
34
+ HTTP.
35
+ expects(
36
+ :start
37
+ ).
38
+ with(
39
+ "example.org", 80, {
40
+ use_ssl: true,
41
+ verify_mode: OpenSSL::SSL::VERIFY_NONE,
42
+ open_timeout: 10,
43
+ read_timeout: OBIX.configuration.timeout
44
+ }
45
+ ).
46
+ returns(
47
+ stub code: "200", body: "<body>"
48
+ )
49
+
50
+ body = OBIX::Network.post "http://example.org/"
51
+
52
+ assert_equal "<body>", body
53
+ end
54
+
55
+ def test_post_with_object
56
+ HTTP.
57
+ expects(
58
+ :start
59
+ ).
60
+ with(
61
+ "example.org", 80, {
62
+ use_ssl: true,
63
+ verify_mode: OpenSSL::SSL::VERIFY_NONE,
64
+ open_timeout: 10,
65
+ read_timeout: OBIX.configuration.timeout
66
+ }
67
+ ).
68
+ returns(
69
+ stub code: "200", body: "<body>"
70
+ )
71
+
72
+ object = mock
73
+
74
+ object.
75
+ expects(
76
+ :to_xml
77
+ ).
78
+ returns(
79
+ fixture "objects/string.xml"
80
+ )
81
+
82
+ body = OBIX::Network.post "http://example.org/", object
83
+
84
+ assert_equal "<body>", body
85
+ end
86
+
87
+ def test_bad_response
88
+ HTTP.
89
+ expects(
90
+ :start
91
+ ).
92
+ returns(
93
+ stub code: "500", body: nil
94
+ )
95
+
96
+ assert_raises OBIX::Network::Error do
97
+ OBIX::Network.get "http://example.org/"
98
+ end
99
+ end
100
+
101
+ def test_expands_relative_urls
102
+ HTTP.
103
+ expects(
104
+ :start
105
+ ).
106
+ with(
107
+ "example.org", 80, {
108
+ use_ssl: true,
109
+ verify_mode: OpenSSL::SSL::VERIFY_NONE,
110
+ open_timeout: 10,
111
+ read_timeout: OBIX.configuration.timeout
112
+ }
113
+ ).
114
+ returns(
115
+ stub code: "200", body: "<body>"
116
+ )
117
+
118
+ body = OBIX::Network.get "/relative/url"
119
+ end
120
+
121
+ def test_times_out
122
+ HTTP.
123
+ expects(
124
+ :start
125
+ ).
126
+ raises(
127
+ ::Timeout::Error
128
+ )
129
+
130
+ assert_raises OBIX::Network::Timeout do
131
+ OBIX::Network.get "http://example.org/"
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,54 @@
1
+ require "obix"
2
+ require "minitest/unit"
3
+ require "minitest/autorun"
4
+ require "active_support/all"
5
+
6
+ require "test_helper"
7
+
8
+ class BaseTest < MiniTest::Unit::TestCase
9
+ def test_parses_valid_obix
10
+ thermostat = OBIX.parse file: "test/fixtures/valid.xml"
11
+
12
+ assert_equal "http://myhome/thermostat", thermostat.href
13
+
14
+ temperature = thermostat.objects.find { |obj| obj.name == "spaceTemp" }
15
+
16
+ assert_equal thermostat, temperature.parent
17
+ assert_equal 67.2, temperature.val
18
+ assert_equal "obix:units/fahrenheit", temperature.unit
19
+ end
20
+
21
+ def test_fails_to_parse_invalid_obix
22
+ assert_raises OBIX::Objects::UnknownObjectError do
23
+ unknown = OBIX.parse file: "test/fixtures/invalid.xml"
24
+ end
25
+ end
26
+
27
+ def test_finds_objects
28
+ builder = OBIX::Builder.new do
29
+ obj name: "foo" do
30
+ obj name: "bar"
31
+ end
32
+ end
33
+
34
+ foo = builder.object
35
+
36
+ bar = foo.find :bar
37
+
38
+ assert_equal "bar", bar.name
39
+ end
40
+
41
+ def test_serializes_to_string
42
+ thermostat = OBIX.parse file: "test/fixtures/valid.xml"
43
+
44
+ assert_equal "#<OBIX::Objects::Object href: \"http://myhome/thermostat\">", thermostat.to_s
45
+ end
46
+
47
+ def test_serializes_to_xml
48
+ xml = fixture "valid.xml"
49
+
50
+ thermostat = OBIX.parse string: xml
51
+
52
+ assert_equal xml, thermostat.to_xml
53
+ end
54
+ end