xively-rb 0.2.09
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +13 -0
- data/.rbenv-version +1 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +91 -0
- data/CONTRIBUTING.md +95 -0
- data/Gemfile +15 -0
- data/LICENSE.md +13 -0
- data/README.md +10 -0
- data/Rakefile +25 -0
- data/ci/build_hudson.sh +24 -0
- data/init.rb +2 -0
- data/lib/xively-rb.rb +44 -0
- data/lib/xively-rb/array_extensions.rb +6 -0
- data/lib/xively-rb/base.rb +52 -0
- data/lib/xively-rb/base/instance_methods.rb +28 -0
- data/lib/xively-rb/client.rb +43 -0
- data/lib/xively-rb/datapoint.rb +72 -0
- data/lib/xively-rb/datastream.rb +127 -0
- data/lib/xively-rb/exceptions.rb +5 -0
- data/lib/xively-rb/feed.rb +109 -0
- data/lib/xively-rb/hash_extensions.rb +16 -0
- data/lib/xively-rb/helpers.rb +41 -0
- data/lib/xively-rb/key.rb +99 -0
- data/lib/xively-rb/nil_content.rb +15 -0
- data/lib/xively-rb/object_extensions.rb +6 -0
- data/lib/xively-rb/parsers/csv/datastream_defaults.rb +50 -0
- data/lib/xively-rb/parsers/csv/feed_defaults.rb +97 -0
- data/lib/xively-rb/parsers/defaults.rb +15 -0
- data/lib/xively-rb/parsers/json/datapoint_defaults.rb +16 -0
- data/lib/xively-rb/parsers/json/datastream_defaults.rb +58 -0
- data/lib/xively-rb/parsers/json/feed_defaults.rb +114 -0
- data/lib/xively-rb/parsers/json/key_defaults.rb +20 -0
- data/lib/xively-rb/parsers/json/search_result_defaults.rb +24 -0
- data/lib/xively-rb/parsers/json/trigger_defaults.rb +16 -0
- data/lib/xively-rb/parsers/xml/datapoint_defaults.rb +27 -0
- data/lib/xively-rb/parsers/xml/datastream_defaults.rb +53 -0
- data/lib/xively-rb/parsers/xml/errors.rb +7 -0
- data/lib/xively-rb/parsers/xml/feed_defaults.rb +83 -0
- data/lib/xively-rb/parsers/xml/helpers.rb +116 -0
- data/lib/xively-rb/parsers/xml/key_defaults.rb +46 -0
- data/lib/xively-rb/parsers/xml/trigger_defaults.rb +28 -0
- data/lib/xively-rb/permission.rb +67 -0
- data/lib/xively-rb/resource.rb +43 -0
- data/lib/xively-rb/search_result.rb +68 -0
- data/lib/xively-rb/string_extensions.rb +6 -0
- data/lib/xively-rb/templates/csv/datapoint_defaults.rb +22 -0
- data/lib/xively-rb/templates/csv/datastream_defaults.rb +43 -0
- data/lib/xively-rb/templates/csv/feed_defaults.rb +47 -0
- data/lib/xively-rb/templates/defaults.rb +14 -0
- data/lib/xively-rb/templates/json/datapoint_defaults.rb +15 -0
- data/lib/xively-rb/templates/json/datastream_defaults.rb +61 -0
- data/lib/xively-rb/templates/json/feed_defaults.rb +90 -0
- data/lib/xively-rb/templates/json/key_defaults.rb +39 -0
- data/lib/xively-rb/templates/json/search_result_defaults.rb +42 -0
- data/lib/xively-rb/templates/json/trigger_defaults.rb +21 -0
- data/lib/xively-rb/templates/xml/datapoint_defaults.rb +25 -0
- data/lib/xively-rb/templates/xml/datastream_defaults.rb +66 -0
- data/lib/xively-rb/templates/xml/feed_defaults.rb +112 -0
- data/lib/xively-rb/templates/xml/search_result_defaults.rb +118 -0
- data/lib/xively-rb/templates/xml_headers.rb +17 -0
- data/lib/xively-rb/trigger.rb +66 -0
- data/lib/xively-rb/validations.rb +9 -0
- data/lib/xively-rb/version.rb +3 -0
- data/spec/fixtures/models.rb +81 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/support/contain_datapoint_eeml_matcher.rb +16 -0
- data/spec/support/contain_datastream_eeml_matcher.rb +60 -0
- data/spec/support/contain_feed_eeml_matcher.rb +98 -0
- data/spec/support/datapoint_helper.rb +53 -0
- data/spec/support/datastream_helper.rb +324 -0
- data/spec/support/describe_eeml_matcher.rb +23 -0
- data/spec/support/feed_helper.rb +783 -0
- data/spec/support/fully_represent_datapoint_matcher.rb +30 -0
- data/spec/support/fully_represent_datastream_matcher.rb +96 -0
- data/spec/support/fully_represent_feed_matcher.rb +234 -0
- data/spec/support/fully_represent_key_matcher.rb +74 -0
- data/spec/support/fully_represent_search_result_matcher.rb +67 -0
- data/spec/support/fully_represent_trigger_matcher.rb +29 -0
- data/spec/support/key_helper.rb +74 -0
- data/spec/support/search_result_helper.rb +252 -0
- data/spec/support/trigger_helper.rb +51 -0
- data/spec/xively-rb/array_extensions_spec.rb +9 -0
- data/spec/xively-rb/base/instance_methods_spec.rb +109 -0
- data/spec/xively-rb/base_spec.rb +56 -0
- data/spec/xively-rb/client_spec.rb +51 -0
- data/spec/xively-rb/datapoint_spec.rb +187 -0
- data/spec/xively-rb/datastream_spec.rb +344 -0
- data/spec/xively-rb/feed_spec.rb +341 -0
- data/spec/xively-rb/hash_extensions_spec.rb +20 -0
- data/spec/xively-rb/helpers_spec.rb +56 -0
- data/spec/xively-rb/key_spec.rb +198 -0
- data/spec/xively-rb/parsers/csv/datastream_defaults_spec.rb +110 -0
- data/spec/xively-rb/parsers/csv/feed_defaults_spec.rb +234 -0
- data/spec/xively-rb/parsers/json/datapoint_defaults_spec.rb +21 -0
- data/spec/xively-rb/parsers/json/datastream_defaults_spec.rb +105 -0
- data/spec/xively-rb/parsers/json/feed_defaults_spec.rb +45 -0
- data/spec/xively-rb/parsers/json/key_defaults_spec.rb +14 -0
- data/spec/xively-rb/parsers/json/search_result_defaults_spec.rb +18 -0
- data/spec/xively-rb/parsers/json/trigger_defaults_spec.rb +22 -0
- data/spec/xively-rb/parsers/xml/datapoint_defaults_spec.rb +19 -0
- data/spec/xively-rb/parsers/xml/datastream_defaults_spec.rb +148 -0
- data/spec/xively-rb/parsers/xml/feed_defaults_spec.rb +254 -0
- data/spec/xively-rb/parsers/xml/key_defaults_spec.rb +22 -0
- data/spec/xively-rb/parsers/xml/trigger_defaults_spec.rb +22 -0
- data/spec/xively-rb/search_result_spec.rb +257 -0
- data/spec/xively-rb/string_extensions_spec.rb +12 -0
- data/spec/xively-rb/templates/csv/datapoint_defaults_spec.rb +41 -0
- data/spec/xively-rb/templates/csv/datastream_defaults_spec.rb +131 -0
- data/spec/xively-rb/templates/csv/feed_defaults_spec.rb +78 -0
- data/spec/xively-rb/templates/json/datapoint_defaults_spec.rb +14 -0
- data/spec/xively-rb/templates/json/datastream_defaults_spec.rb +170 -0
- data/spec/xively-rb/templates/json/feed_defaults_spec.rb +399 -0
- data/spec/xively-rb/templates/json/key_defaults_spec.rb +29 -0
- data/spec/xively-rb/templates/json/search_result_defaults_spec.rb +37 -0
- data/spec/xively-rb/templates/json/trigger_defaults_spec.rb +19 -0
- data/spec/xively-rb/templates/xml/datapoint_defaults_spec.rb +14 -0
- data/spec/xively-rb/templates/xml/datastream_defaults_spec.rb +113 -0
- data/spec/xively-rb/templates/xml/feed_defaults_spec.rb +131 -0
- data/spec/xively-rb/templates/xml/search_result_defaults_spec.rb +44 -0
- data/spec/xively-rb/trigger_spec.rb +157 -0
- data/xively-rb.gemspec +41 -0
- metadata +333 -0
@@ -0,0 +1,29 @@
|
|
1
|
+
RSpec::Matchers.define :fully_represent_trigger do |format, formatted_trigger|
|
2
|
+
match do |trigger|
|
3
|
+
if format.to_sym == :xml
|
4
|
+
match_xml_trigger(trigger, formatted_trigger)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
failure_message_for_should do |trigger|
|
9
|
+
"expected #{trigger} to fully represent #{formatted_trigger}"
|
10
|
+
end
|
11
|
+
|
12
|
+
description do
|
13
|
+
"expected #{formatted_datapoint.class} to be fully represented"
|
14
|
+
end
|
15
|
+
|
16
|
+
def match_xml_trigger(trigger, formatted_trigger)
|
17
|
+
xml = Nokogiri.parse(formatted_trigger)
|
18
|
+
trigger.id.should == xml.at_xpath("//id").content
|
19
|
+
trigger.url.should == xml.at_xpath("//url").content
|
20
|
+
trigger.trigger_type.should == xml.at_xpath("//trigger-type").content
|
21
|
+
trigger.threshold_value.should == xml.at_xpath("//threshold-value").content
|
22
|
+
trigger.notified_at.should == xml.at_xpath("//notified-at").content
|
23
|
+
trigger.user.should == xml.at_xpath("//user").content
|
24
|
+
trigger.environment_id.should == xml.at_xpath("//environment-id").content
|
25
|
+
trigger.stream_id.should == xml.at_xpath("//stream-id").content
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
@@ -0,0 +1,74 @@
|
|
1
|
+
def key_as_(format, options = {})
|
2
|
+
# Default data
|
3
|
+
# This data is based on http://api.xively.com/v2/feeds/504
|
4
|
+
case format.to_s
|
5
|
+
when 'hash', 'json'
|
6
|
+
data = { "id" => 40, "key" => "abcdefghasdfaoisdj109usasdf0a9sf", "label" => "Our awesome label",
|
7
|
+
"user" => "lebreeze", "expires_at" => 12345, "private_access" => true,
|
8
|
+
"permissions" => [
|
9
|
+
{ "source_ip" => "127.0.0.1", "referer" => "http://xively.com",
|
10
|
+
"access_methods" => %w(get put post delete),
|
11
|
+
"resources" => [
|
12
|
+
{ "feed_id" => 424, "datastream_id" => "1" }
|
13
|
+
]
|
14
|
+
}
|
15
|
+
]}
|
16
|
+
when 'xml'
|
17
|
+
data = <<XML
|
18
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
19
|
+
<key>
|
20
|
+
<id>40</id>
|
21
|
+
<api-key>abcdefghasdfaoisdj109usasdf0a9sf</api-key>
|
22
|
+
<expires-at>12345</expires-at>
|
23
|
+
<user>lebreeze</user>
|
24
|
+
<label>Our awesome label</label>
|
25
|
+
<private-access>true</private-access>
|
26
|
+
<permissions>
|
27
|
+
<permission>
|
28
|
+
<referer>http://xively.com</referer>
|
29
|
+
<source-ip>127.0.0.1</source-ip>
|
30
|
+
<access-methods>
|
31
|
+
<access-method>GET</access-method>
|
32
|
+
<access-method>PUT</access-method>
|
33
|
+
<access-method>POST</access-method>
|
34
|
+
<access-method>DELETE</access-method>
|
35
|
+
</access-methods>
|
36
|
+
<resources>
|
37
|
+
<resource>
|
38
|
+
<feed-id>424</feed-id>
|
39
|
+
<datastream-id>1</datastream-id>
|
40
|
+
</resource>
|
41
|
+
</resources>
|
42
|
+
</permission>
|
43
|
+
</permissions>
|
44
|
+
</key>
|
45
|
+
XML
|
46
|
+
end
|
47
|
+
|
48
|
+
# Add extra options we passed
|
49
|
+
if options[:with]
|
50
|
+
options[:with].each do |field, value|
|
51
|
+
data[field.to_s] = value
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Remove options we don't need
|
56
|
+
if options[:except]
|
57
|
+
options[:except].each do |field,_|
|
58
|
+
data.delete(field.to_s)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Return the feed in the requested format
|
63
|
+
case format.to_s
|
64
|
+
when 'hash'
|
65
|
+
data
|
66
|
+
when 'json'
|
67
|
+
MultiJson.dump({ "key" => data })
|
68
|
+
when 'xml'
|
69
|
+
data
|
70
|
+
else
|
71
|
+
raise "#{format} undefined"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
@@ -0,0 +1,252 @@
|
|
1
|
+
def search_result_as_(format, options = {})
|
2
|
+
# Default data
|
3
|
+
# This data is based on http://api.xively.com/v2/feeds/504
|
4
|
+
case format.to_s
|
5
|
+
when 'hash'
|
6
|
+
data = {
|
7
|
+
"totalResults" => "10000",
|
8
|
+
"itemsPerPage" => "100",
|
9
|
+
"startIndex" => "0",
|
10
|
+
"results" => [{
|
11
|
+
"updated"=>Time.parse('2011-01-02'),
|
12
|
+
"title"=>"Xively Office Environment",
|
13
|
+
"private"=>false,
|
14
|
+
"id"=>504,
|
15
|
+
"icon"=>"http://xively.com/logo.png",
|
16
|
+
"website"=>"http://xively.com",
|
17
|
+
"tags" => "kittens , sofa, aardvark",
|
18
|
+
"description"=>"Sensors in xively.com's headquarters.",
|
19
|
+
"feed" => "http://test.host/testfeed.html?random=890299&rand2=91",
|
20
|
+
"email"=>"abc@example.com",
|
21
|
+
"status"=>"live",
|
22
|
+
"creator"=>"http://xively.com/users/skeletor",
|
23
|
+
'location_domain' => 'physical',
|
24
|
+
'location_lon' => -0.0807666778564453,
|
25
|
+
'location_disposition' => 'fixed',
|
26
|
+
'location_ele' => '23.0',
|
27
|
+
'location_exposure' => 'indoor',
|
28
|
+
'location_lat' => 51.5235375648154,
|
29
|
+
'location_name' => 'office',
|
30
|
+
"datastreams" => [
|
31
|
+
{
|
32
|
+
"updated" => Time.parse('2011-01-02'),
|
33
|
+
"max_value"=>658.0,
|
34
|
+
"unit_type"=>"",
|
35
|
+
"min_value"=>0.0,
|
36
|
+
"unit_label"=>"",
|
37
|
+
"current_value"=>"14",
|
38
|
+
"id"=>"0",
|
39
|
+
"tags"=>"humidity,Temperature, freakin lasers",
|
40
|
+
"datapoints" => [{
|
41
|
+
"value" => "1",
|
42
|
+
"at" => Time.parse("2011-03-02T15:59:56.895922Z")
|
43
|
+
},
|
44
|
+
{
|
45
|
+
"value" => "1",
|
46
|
+
"at" => Time.parse("2011-03-02T16:00:07.188648Z")
|
47
|
+
},
|
48
|
+
{
|
49
|
+
"value" => "2",
|
50
|
+
"at" => Time.parse("2011-03-02T16:00:18.416500Z")
|
51
|
+
}],
|
52
|
+
"unit_symbol"=>""},
|
53
|
+
{
|
54
|
+
"updated" => Time.parse('2011-01-02'),
|
55
|
+
"max_value"=>980.0,
|
56
|
+
"unit_type"=>"",
|
57
|
+
"min_value"=>0.0,
|
58
|
+
"unit_label"=>"label",
|
59
|
+
"current_value"=>"813",
|
60
|
+
"id"=>"1",
|
61
|
+
"tags"=>"light level",
|
62
|
+
"unit_symbol"=>""},
|
63
|
+
{
|
64
|
+
"updated" => Time.parse('2011-01-02'),
|
65
|
+
"max_value"=>774.0,
|
66
|
+
"unit_type"=>"some type",
|
67
|
+
"min_value"=>158.0,
|
68
|
+
"unit_label"=>"some measure",
|
69
|
+
"current_value"=>"318",
|
70
|
+
"id"=>"2",
|
71
|
+
"tags"=>"Temperature",
|
72
|
+
"unit_symbol"=>"some % symbol"},
|
73
|
+
{
|
74
|
+
"updated" => Time.parse('2011-01-02'),
|
75
|
+
"max_value"=>0.0,
|
76
|
+
"unit_type"=>"",
|
77
|
+
"min_value"=>0.0,
|
78
|
+
"unit_label"=>"",
|
79
|
+
"current_value"=>"0",
|
80
|
+
"id"=>"3",
|
81
|
+
"tags"=>"door 1",
|
82
|
+
"unit_symbol"=>"symbol"},
|
83
|
+
{
|
84
|
+
"updated" => Time.parse('2011-01-02'),
|
85
|
+
"max_value"=>0.0,
|
86
|
+
"unit_type"=>"",
|
87
|
+
"min_value"=>0.0,
|
88
|
+
"unit_label"=>"",
|
89
|
+
"current_value"=>"0",
|
90
|
+
"id"=>"4",
|
91
|
+
"tags"=>"door 2",
|
92
|
+
"unit_symbol"=>""},
|
93
|
+
{
|
94
|
+
"updated" => Time.parse('2011-01-02'),
|
95
|
+
"max_value"=>40.0,
|
96
|
+
"unit_type"=>"",
|
97
|
+
"min_value"=>0.0,
|
98
|
+
"unit_label"=>"",
|
99
|
+
"current_value"=>"40",
|
100
|
+
"id"=>"5",
|
101
|
+
"unit_symbol"=>""},
|
102
|
+
{
|
103
|
+
"updated" => Time.parse('2011-01-02'),
|
104
|
+
"max_value"=>32767.0,
|
105
|
+
"unit_type"=>"",
|
106
|
+
"min_value"=>-32768.0,
|
107
|
+
"unit_label"=>"",
|
108
|
+
"current_value"=>"15545",
|
109
|
+
"id"=>"6",
|
110
|
+
"tags"=>"successes",
|
111
|
+
"unit_symbol"=>""}]
|
112
|
+
}]
|
113
|
+
}
|
114
|
+
when 'json'
|
115
|
+
data = search_result_as_json(options[:version] || "1.0.0")
|
116
|
+
when 'xml'
|
117
|
+
raise "Not implemented"
|
118
|
+
end
|
119
|
+
|
120
|
+
# Add extra options we passed
|
121
|
+
if options[:with]
|
122
|
+
options[:with].each do |field, value|
|
123
|
+
data[field.to_s] = value
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# Remove options we don't need
|
128
|
+
if options[:except]
|
129
|
+
options[:except].each do |field,_|
|
130
|
+
data.delete(field.to_s)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# Return the feed in the requested format
|
135
|
+
case format.to_s
|
136
|
+
when 'hash'
|
137
|
+
data
|
138
|
+
when 'json'
|
139
|
+
MultiJson.dump(data)
|
140
|
+
when 'xml'
|
141
|
+
data
|
142
|
+
else
|
143
|
+
raise "#{format} undefined"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def search_result_as_json(version)
|
148
|
+
case version
|
149
|
+
when "1.0.0"
|
150
|
+
{
|
151
|
+
"totalResults" => "10000",
|
152
|
+
"itemsPerPage" => "100",
|
153
|
+
"startIndex" => "0",
|
154
|
+
'results' => [{
|
155
|
+
'title' => 'Xively Office Environment',
|
156
|
+
'status' => 'live',
|
157
|
+
'updated' => '2011-02-16T16:21:01.834174Z',
|
158
|
+
'tags' => ['hq', 'office'],
|
159
|
+
'description' => 'Sensors in xively.com\'s headquarters.',
|
160
|
+
'website' => 'http://xively.com/',
|
161
|
+
'private' => 'false',
|
162
|
+
'creator' => 'http://xively.com/users/skeletor',
|
163
|
+
'version' => '1.0.0',
|
164
|
+
'id' => 504,
|
165
|
+
'location' =>
|
166
|
+
{ 'domain' => 'physical',
|
167
|
+
'lon' => -0.0807666778564453,
|
168
|
+
'disposition' => 'fixed',
|
169
|
+
'ele' => '23.0',
|
170
|
+
'exposure' => 'indoor',
|
171
|
+
'lat' => 51.5235375648154,
|
172
|
+
'name' => 'office'
|
173
|
+
},
|
174
|
+
'feed' => 'http://api.xively.com/v2/feeds/504.json',
|
175
|
+
'datastreams' =>
|
176
|
+
[
|
177
|
+
{'min_value' => '0.0',
|
178
|
+
'at' => '2011-02-16T16:21:01.834174Z',
|
179
|
+
'tags' => ['humidity'],
|
180
|
+
"unit" => {
|
181
|
+
"symbol" => "cm",
|
182
|
+
"label" => "cms",
|
183
|
+
"type" => "metric"
|
184
|
+
|
185
|
+
},
|
186
|
+
|
187
|
+
'current_value' => '14',
|
188
|
+
'max_value' => '658.0',
|
189
|
+
'id' => '0'
|
190
|
+
},
|
191
|
+
{'min_value' => '0.0',
|
192
|
+
'at' => '2011-02-16T16:21:01.834174Z',
|
193
|
+
'tags' => ['light level'],
|
194
|
+
'current_value' => '717',
|
195
|
+
'max_value' => '980.0',
|
196
|
+
'id' => '1'
|
197
|
+
},
|
198
|
+
{'min_value' => '158.0',
|
199
|
+
'at' => '2011-02-16T16:21:01.834174Z',
|
200
|
+
'tags' => ['Temperature'],
|
201
|
+
'current_value' => '316',
|
202
|
+
'max_value' => '774.0',
|
203
|
+
'id' => '2',
|
204
|
+
"datapoints" => [{
|
205
|
+
"value" => "1",
|
206
|
+
"at" => "2011-03-02T15:59:56.895922Z"
|
207
|
+
},
|
208
|
+
{
|
209
|
+
"value" => "1",
|
210
|
+
"at" => "2011-03-02T16:00:07.188648Z"
|
211
|
+
},
|
212
|
+
{
|
213
|
+
"value" => "2",
|
214
|
+
"at" => "2011-03-02T16:00:18.416500Z"
|
215
|
+
}]
|
216
|
+
},
|
217
|
+
{'min_value' => '0.0',
|
218
|
+
'at' => '2011-02-16T16:21:01.834174Z',
|
219
|
+
'tags' => ['door 1'],
|
220
|
+
'current_value' => '0',
|
221
|
+
'max_value' => '0.0',
|
222
|
+
'id' => '3'
|
223
|
+
},
|
224
|
+
{'min_value' => '0.0',
|
225
|
+
'at' => '2011-02-16T16:21:01.834174Z',
|
226
|
+
'tags' => ['door 2'],
|
227
|
+
'current_value' => '0',
|
228
|
+
'max_value' => '0.0',
|
229
|
+
'id' => '4'
|
230
|
+
},
|
231
|
+
{'min_value' => '0.0',
|
232
|
+
'at' => '2011-02-16T16:21:01.834174Z',
|
233
|
+
'tags' => ['failures'],
|
234
|
+
'current_value' => '40',
|
235
|
+
'max_value' => '40.0',
|
236
|
+
'id' => '5'
|
237
|
+
},
|
238
|
+
{'min_value' => '-32768.0',
|
239
|
+
'at' => '2011-02-16T16:21:01.834174Z',
|
240
|
+
'tags' => ['successes'],
|
241
|
+
'current_value' => '2638',
|
242
|
+
'max_value' => '32767.0',
|
243
|
+
'id' => '6'
|
244
|
+
}
|
245
|
+
]
|
246
|
+
}]
|
247
|
+
}
|
248
|
+
else
|
249
|
+
raise "No such JSON version"
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
def trigger_as_(format, options = {})
|
2
|
+
# Default data
|
3
|
+
# This data is based on http://api.xively.com/v2/feeds/504
|
4
|
+
case format.to_s
|
5
|
+
when 'hash'
|
6
|
+
data = {"threshold_value"=>"44", "notified_at"=>"", "url"=>"http://www.postbin.org/zc9sca", "trigger_type"=>"eq", "id"=>40, "stream_id"=>"1", "environment_id"=>424, "user"=>"lebreeze"}
|
7
|
+
when 'json'
|
8
|
+
data = {"threshold_value"=>"44", "notified_at"=>"", "url"=>"http://www.postbin.org/zc9sca", "trigger_type"=>"eq", "id"=>40, "stream_id"=>"1", "environment_id"=>424, "user"=>"lebreeze"}
|
9
|
+
when 'xml'
|
10
|
+
data = <<XML
|
11
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
12
|
+
<datastream-trigger>
|
13
|
+
<id type="integer">40</id>
|
14
|
+
<url>http://www.postbin.org/zc9sca</url>
|
15
|
+
<trigger-type>eq</trigger-type>
|
16
|
+
<threshold-value type="float">44</threshold-value>
|
17
|
+
<notified-at type="datetime"></notified-at>
|
18
|
+
<user>lebreeze</user>
|
19
|
+
<environment-id type="integer">424</environment-id>
|
20
|
+
<stream-id>1</stream-id>
|
21
|
+
</datastream-trigger>
|
22
|
+
XML
|
23
|
+
end
|
24
|
+
|
25
|
+
# Add extra options we passed
|
26
|
+
if options[:with]
|
27
|
+
options[:with].each do |field, value|
|
28
|
+
data[field.to_s] = value
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Remove options we don't need
|
33
|
+
if options[:except]
|
34
|
+
options[:except].each do |field,_|
|
35
|
+
data.delete(field.to_s)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Return the feed in the requested format
|
40
|
+
case format.to_s
|
41
|
+
when 'hash'
|
42
|
+
data
|
43
|
+
when 'json'
|
44
|
+
MultiJson.dump(data)
|
45
|
+
when 'xml'
|
46
|
+
data
|
47
|
+
else
|
48
|
+
raise "#{format} undefined"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Xively::Base::InstanceMethods do
|
4
|
+
before(:each) do
|
5
|
+
@feed = Feed.new
|
6
|
+
@feed.updated = Time.parse("20110202")
|
7
|
+
@feed.title = "Feed Title"
|
8
|
+
@feed.private = true
|
9
|
+
@feed.icon = "http://xively.com/logo.png"
|
10
|
+
@feed.website = "http://xively.com"
|
11
|
+
@feed.tags = "kittens, sofa, aardvark"
|
12
|
+
@feed.description = "Test feed"
|
13
|
+
@feed.feed = "http://test.host/testfeed.html?random=890299&rand2=91"
|
14
|
+
@feed.email = "abc@example.com"
|
15
|
+
@feed.creator = "http://www.xively.com"
|
16
|
+
@feed.location_name = "London"
|
17
|
+
@feed.location_disposition = "fixed"
|
18
|
+
@feed.location_domain = "here"
|
19
|
+
@feed.location_ele = "2000"
|
20
|
+
@feed.location_exposure = "naked"
|
21
|
+
@feed.location_lat = 51.5423
|
22
|
+
@feed.location_lon = 3.243
|
23
|
+
@datastream1 = Datastream.new
|
24
|
+
datastream_as_(:hash, :with => {"stream_id" => "0"}, :except => ["datapoints"]).each do |k,v|
|
25
|
+
@datastream1.send("#{k}=", v)
|
26
|
+
end
|
27
|
+
# @feed.datastreams.create!(datastream_as_(:hash, :with => {"stream_id" => "0"}, :except => ["datapoints"]))
|
28
|
+
@datastream2 = Datastream.new
|
29
|
+
datastream_as_(:hash, :with => {"stream_id" => "two"}, :except => ["datapoints"]).each do |k,v|
|
30
|
+
@datastream2.send("#{k}=", v)
|
31
|
+
end
|
32
|
+
@feed.datastreams = [@datastream1, @datastream2]
|
33
|
+
datapoints = []
|
34
|
+
10.times do
|
35
|
+
datapoint = Datapoint.new
|
36
|
+
datapoint.value = "#{rand(10000)}"
|
37
|
+
datapoint.at = Time.now - rand(10000) * 60
|
38
|
+
datapoints << datapoint
|
39
|
+
end
|
40
|
+
@datastream2.datapoints = datapoints
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#to_xively" do
|
44
|
+
it "should return the appropriate Xively Object" do
|
45
|
+
@feed.to_xively.should be_kind_of(Xively::Feed)
|
46
|
+
@datastream1.to_xively.should be_kind_of(Xively::Datastream)
|
47
|
+
@datastream2.datapoints.first.to_xively.should be_kind_of(Xively::Datapoint)
|
48
|
+
end
|
49
|
+
|
50
|
+
context "Feed" do
|
51
|
+
it "should map the default fields by default" do
|
52
|
+
Xively::Feed.should_receive(:new).with(@feed.attributes)
|
53
|
+
@feed.to_xively
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "Datastream" do
|
58
|
+
it "should map the default fields merged with the optionals by default" do
|
59
|
+
Xively::Datastream.should_receive(:new).with(@datastream2.attributes.merge({"id" => "two"}))
|
60
|
+
@datastream2.to_xively
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "Datapoint" do
|
65
|
+
it "should map the default fields by default" do
|
66
|
+
Xively::Datapoint.should_receive(:new).with(@datastream2.datapoints.first.attributes)
|
67
|
+
@datastream2.datapoints.first.to_xively
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "custom mappings" do
|
73
|
+
it "should allow custom method mappings" do
|
74
|
+
class CustomFeed
|
75
|
+
extend Xively::Base
|
76
|
+
|
77
|
+
undef_method :id if method_defined?(:id)
|
78
|
+
attr_accessor :title
|
79
|
+
is_xively :feed, {:feed => :custom_method}
|
80
|
+
|
81
|
+
def custom_method
|
82
|
+
"I haz customer"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
feed = CustomFeed.new
|
86
|
+
feed.title = "Name"
|
87
|
+
Xively::Feed.should_receive(:new).with({"feed" => feed.custom_method, "title" => feed.title})
|
88
|
+
feed.to_xively
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should not rely on datastreams using this gem" do
|
92
|
+
class NewCustomFeed
|
93
|
+
extend Xively::Base
|
94
|
+
|
95
|
+
attr_accessor :title
|
96
|
+
is_xively :feed
|
97
|
+
|
98
|
+
def datastreams
|
99
|
+
["fake datastream"]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
feed = NewCustomFeed.new
|
104
|
+
feed.title = "Name"
|
105
|
+
feed.to_xively
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|