xively-rb 0.2.09

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. data/.gitignore +13 -0
  2. data/.rbenv-version +1 -0
  3. data/.rspec +2 -0
  4. data/.rvmrc +1 -0
  5. data/.travis.yml +7 -0
  6. data/CHANGELOG.md +91 -0
  7. data/CONTRIBUTING.md +95 -0
  8. data/Gemfile +15 -0
  9. data/LICENSE.md +13 -0
  10. data/README.md +10 -0
  11. data/Rakefile +25 -0
  12. data/ci/build_hudson.sh +24 -0
  13. data/init.rb +2 -0
  14. data/lib/xively-rb.rb +44 -0
  15. data/lib/xively-rb/array_extensions.rb +6 -0
  16. data/lib/xively-rb/base.rb +52 -0
  17. data/lib/xively-rb/base/instance_methods.rb +28 -0
  18. data/lib/xively-rb/client.rb +43 -0
  19. data/lib/xively-rb/datapoint.rb +72 -0
  20. data/lib/xively-rb/datastream.rb +127 -0
  21. data/lib/xively-rb/exceptions.rb +5 -0
  22. data/lib/xively-rb/feed.rb +109 -0
  23. data/lib/xively-rb/hash_extensions.rb +16 -0
  24. data/lib/xively-rb/helpers.rb +41 -0
  25. data/lib/xively-rb/key.rb +99 -0
  26. data/lib/xively-rb/nil_content.rb +15 -0
  27. data/lib/xively-rb/object_extensions.rb +6 -0
  28. data/lib/xively-rb/parsers/csv/datastream_defaults.rb +50 -0
  29. data/lib/xively-rb/parsers/csv/feed_defaults.rb +97 -0
  30. data/lib/xively-rb/parsers/defaults.rb +15 -0
  31. data/lib/xively-rb/parsers/json/datapoint_defaults.rb +16 -0
  32. data/lib/xively-rb/parsers/json/datastream_defaults.rb +58 -0
  33. data/lib/xively-rb/parsers/json/feed_defaults.rb +114 -0
  34. data/lib/xively-rb/parsers/json/key_defaults.rb +20 -0
  35. data/lib/xively-rb/parsers/json/search_result_defaults.rb +24 -0
  36. data/lib/xively-rb/parsers/json/trigger_defaults.rb +16 -0
  37. data/lib/xively-rb/parsers/xml/datapoint_defaults.rb +27 -0
  38. data/lib/xively-rb/parsers/xml/datastream_defaults.rb +53 -0
  39. data/lib/xively-rb/parsers/xml/errors.rb +7 -0
  40. data/lib/xively-rb/parsers/xml/feed_defaults.rb +83 -0
  41. data/lib/xively-rb/parsers/xml/helpers.rb +116 -0
  42. data/lib/xively-rb/parsers/xml/key_defaults.rb +46 -0
  43. data/lib/xively-rb/parsers/xml/trigger_defaults.rb +28 -0
  44. data/lib/xively-rb/permission.rb +67 -0
  45. data/lib/xively-rb/resource.rb +43 -0
  46. data/lib/xively-rb/search_result.rb +68 -0
  47. data/lib/xively-rb/string_extensions.rb +6 -0
  48. data/lib/xively-rb/templates/csv/datapoint_defaults.rb +22 -0
  49. data/lib/xively-rb/templates/csv/datastream_defaults.rb +43 -0
  50. data/lib/xively-rb/templates/csv/feed_defaults.rb +47 -0
  51. data/lib/xively-rb/templates/defaults.rb +14 -0
  52. data/lib/xively-rb/templates/json/datapoint_defaults.rb +15 -0
  53. data/lib/xively-rb/templates/json/datastream_defaults.rb +61 -0
  54. data/lib/xively-rb/templates/json/feed_defaults.rb +90 -0
  55. data/lib/xively-rb/templates/json/key_defaults.rb +39 -0
  56. data/lib/xively-rb/templates/json/search_result_defaults.rb +42 -0
  57. data/lib/xively-rb/templates/json/trigger_defaults.rb +21 -0
  58. data/lib/xively-rb/templates/xml/datapoint_defaults.rb +25 -0
  59. data/lib/xively-rb/templates/xml/datastream_defaults.rb +66 -0
  60. data/lib/xively-rb/templates/xml/feed_defaults.rb +112 -0
  61. data/lib/xively-rb/templates/xml/search_result_defaults.rb +118 -0
  62. data/lib/xively-rb/templates/xml_headers.rb +17 -0
  63. data/lib/xively-rb/trigger.rb +66 -0
  64. data/lib/xively-rb/validations.rb +9 -0
  65. data/lib/xively-rb/version.rb +3 -0
  66. data/spec/fixtures/models.rb +81 -0
  67. data/spec/spec_helper.rb +29 -0
  68. data/spec/support/contain_datapoint_eeml_matcher.rb +16 -0
  69. data/spec/support/contain_datastream_eeml_matcher.rb +60 -0
  70. data/spec/support/contain_feed_eeml_matcher.rb +98 -0
  71. data/spec/support/datapoint_helper.rb +53 -0
  72. data/spec/support/datastream_helper.rb +324 -0
  73. data/spec/support/describe_eeml_matcher.rb +23 -0
  74. data/spec/support/feed_helper.rb +783 -0
  75. data/spec/support/fully_represent_datapoint_matcher.rb +30 -0
  76. data/spec/support/fully_represent_datastream_matcher.rb +96 -0
  77. data/spec/support/fully_represent_feed_matcher.rb +234 -0
  78. data/spec/support/fully_represent_key_matcher.rb +74 -0
  79. data/spec/support/fully_represent_search_result_matcher.rb +67 -0
  80. data/spec/support/fully_represent_trigger_matcher.rb +29 -0
  81. data/spec/support/key_helper.rb +74 -0
  82. data/spec/support/search_result_helper.rb +252 -0
  83. data/spec/support/trigger_helper.rb +51 -0
  84. data/spec/xively-rb/array_extensions_spec.rb +9 -0
  85. data/spec/xively-rb/base/instance_methods_spec.rb +109 -0
  86. data/spec/xively-rb/base_spec.rb +56 -0
  87. data/spec/xively-rb/client_spec.rb +51 -0
  88. data/spec/xively-rb/datapoint_spec.rb +187 -0
  89. data/spec/xively-rb/datastream_spec.rb +344 -0
  90. data/spec/xively-rb/feed_spec.rb +341 -0
  91. data/spec/xively-rb/hash_extensions_spec.rb +20 -0
  92. data/spec/xively-rb/helpers_spec.rb +56 -0
  93. data/spec/xively-rb/key_spec.rb +198 -0
  94. data/spec/xively-rb/parsers/csv/datastream_defaults_spec.rb +110 -0
  95. data/spec/xively-rb/parsers/csv/feed_defaults_spec.rb +234 -0
  96. data/spec/xively-rb/parsers/json/datapoint_defaults_spec.rb +21 -0
  97. data/spec/xively-rb/parsers/json/datastream_defaults_spec.rb +105 -0
  98. data/spec/xively-rb/parsers/json/feed_defaults_spec.rb +45 -0
  99. data/spec/xively-rb/parsers/json/key_defaults_spec.rb +14 -0
  100. data/spec/xively-rb/parsers/json/search_result_defaults_spec.rb +18 -0
  101. data/spec/xively-rb/parsers/json/trigger_defaults_spec.rb +22 -0
  102. data/spec/xively-rb/parsers/xml/datapoint_defaults_spec.rb +19 -0
  103. data/spec/xively-rb/parsers/xml/datastream_defaults_spec.rb +148 -0
  104. data/spec/xively-rb/parsers/xml/feed_defaults_spec.rb +254 -0
  105. data/spec/xively-rb/parsers/xml/key_defaults_spec.rb +22 -0
  106. data/spec/xively-rb/parsers/xml/trigger_defaults_spec.rb +22 -0
  107. data/spec/xively-rb/search_result_spec.rb +257 -0
  108. data/spec/xively-rb/string_extensions_spec.rb +12 -0
  109. data/spec/xively-rb/templates/csv/datapoint_defaults_spec.rb +41 -0
  110. data/spec/xively-rb/templates/csv/datastream_defaults_spec.rb +131 -0
  111. data/spec/xively-rb/templates/csv/feed_defaults_spec.rb +78 -0
  112. data/spec/xively-rb/templates/json/datapoint_defaults_spec.rb +14 -0
  113. data/spec/xively-rb/templates/json/datastream_defaults_spec.rb +170 -0
  114. data/spec/xively-rb/templates/json/feed_defaults_spec.rb +399 -0
  115. data/spec/xively-rb/templates/json/key_defaults_spec.rb +29 -0
  116. data/spec/xively-rb/templates/json/search_result_defaults_spec.rb +37 -0
  117. data/spec/xively-rb/templates/json/trigger_defaults_spec.rb +19 -0
  118. data/spec/xively-rb/templates/xml/datapoint_defaults_spec.rb +14 -0
  119. data/spec/xively-rb/templates/xml/datastream_defaults_spec.rb +113 -0
  120. data/spec/xively-rb/templates/xml/feed_defaults_spec.rb +131 -0
  121. data/spec/xively-rb/templates/xml/search_result_defaults_spec.rb +44 -0
  122. data/spec/xively-rb/trigger_spec.rb +157 -0
  123. data/xively-rb.gemspec +41 -0
  124. metadata +333 -0
@@ -0,0 +1,81 @@
1
+ class Feed
2
+ extend Xively::Base
3
+
4
+ is_xively :feed
5
+ attr_accessor :datastreams
6
+ attr_accessor :feed
7
+ attr_accessor :creator
8
+ attr_accessor :title
9
+ attr_accessor :website
10
+ attr_accessor :icon
11
+ attr_accessor :description
12
+ attr_accessor :updated
13
+ attr_accessor :email
14
+ attr_accessor :private
15
+ attr_accessor :tags
16
+ attr_accessor :location_disposition
17
+ attr_accessor :location_domain
18
+ attr_accessor :location_ele
19
+ attr_accessor :location_exposure
20
+ attr_accessor :location_lat
21
+ attr_accessor :location_lon
22
+ attr_accessor :location_name
23
+
24
+ def attributes
25
+ attributes = {}
26
+ Xively::Feed::ALLOWED_KEYS.each do |key|
27
+ attributes[key] = self.send(key) if self.respond_to?(key)
28
+ end
29
+ attributes
30
+ end
31
+ end
32
+
33
+ class Datastream
34
+ extend Xively::Base
35
+
36
+ is_xively :datastream, {:id => :stream_id}
37
+
38
+ attr_accessor :feed
39
+ attr_accessor :feed_id
40
+ attr_accessor :datapoints
41
+ attr_accessor :id
42
+ attr_accessor :stream_id
43
+ attr_accessor :feed_creator
44
+ attr_accessor :current_value
45
+ attr_accessor :min_value
46
+ attr_accessor :max_value
47
+ attr_accessor :unit_label
48
+ attr_accessor :unit_type
49
+ attr_accessor :unit_symbol
50
+ attr_accessor :tags
51
+ attr_accessor :updated
52
+
53
+ def attributes
54
+ attributes = {}
55
+ Xively::Datastream::ALLOWED_KEYS.each do |key|
56
+ attributes[key] = self.send(key) if self.respond_to?(key)
57
+ end
58
+ attributes
59
+ end
60
+
61
+ end
62
+
63
+ class Datapoint
64
+ extend Xively::Base
65
+
66
+ is_xively :datapoint
67
+ attr_accessor :datastream_id
68
+ attr_accessor :at
69
+ attr_accessor :value
70
+
71
+ def attributes
72
+ attributes = {}
73
+ Xively::Datapoint::ALLOWED_KEYS.each do |key|
74
+ attributes[key] = self.send(key) if self.respond_to?(key)
75
+ end
76
+ attributes
77
+ end
78
+
79
+
80
+ end
81
+
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'rspec'
5
+ require 'webmock/rspec'
6
+ require 'time'
7
+
8
+ if !defined?(JRUBY_VERSION)
9
+ if ENV["COVERAGE"] == "on"
10
+ require 'simplecov'
11
+ SimpleCov.start do
12
+ add_filter "/spec/"
13
+ add_filter "/lib/xively-rb.rb"
14
+ add_filter "/vendor/"
15
+ minimum_coverage 100
16
+ end
17
+ end
18
+ end
19
+
20
+ Dir['./spec/support/**/*.rb'].map {|f| require f}
21
+
22
+ $:.push File.expand_path("../lib", __FILE__)
23
+ require 'xively-rb'
24
+
25
+ require File.dirname(__FILE__) + '/fixtures/models.rb'
26
+
27
+ RSpec.configure do |c|
28
+ c.run_all_when_everything_filtered = true
29
+ end
@@ -0,0 +1,16 @@
1
+ RSpec::Matchers.define :contain_datapoint_eeml_for_version do |eeml_version|
2
+ match do |xml|
3
+ hash = datapoint_as_(:hash)
4
+ environment = xml.at_xpath("//xmlns:environment")
5
+ datapoint = environment.at_xpath("//xmlns:data").at_xpath("xmlns:datapoints")
6
+ value = datapoint.at_xpath("xmlns:value")
7
+ value.content.should == hash["value"]
8
+ value.attributes["at"].value.should == hash["at"].iso8601(6)
9
+ end
10
+
11
+ failure_message_for_should do |xml|
12
+ "expected #{xml} to describe eeml version #{eeml_version}"
13
+ end
14
+ end
15
+
16
+
@@ -0,0 +1,60 @@
1
+ RSpec::Matchers.define :contain_datastream_eeml_for_version do |eeml_version|
2
+ match do |xml|
3
+ hash = datastream_as_(:hash)
4
+ case eeml_version
5
+ when "0.5.1"
6
+ environment = xml.at_xpath("//xmlns:environment")
7
+ environment.attributes["updated"].value.should == hash["updated"].iso8601(6)
8
+ environment.attributes["id"].value.should == hash["feed_id"]
9
+ environment.attributes["creator"].value.should == hash["feed_creator"]
10
+ datastream = environment.at_xpath("//xmlns:data")
11
+ datastream.attributes["id"].value.should == hash["id"]
12
+ tags = datastream.xpath("//xmlns:tag")
13
+ hash["tags"].split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase}.each_with_index do |tag, index|
14
+ tags[index].content.should == tag
15
+ end
16
+ current_value = datastream.at_xpath("//xmlns:current_value")
17
+ current_value.content.should == hash["current_value"]
18
+ current_value.attributes["at"].value.should == hash["updated"].iso8601(6)
19
+ datastream.at_xpath("//xmlns:max_value").content.should == hash["max_value"].to_s
20
+ datastream.at_xpath("//xmlns:min_value").content.should == hash["min_value"].to_s
21
+ unit = datastream.at_xpath("//xmlns:unit")
22
+ unit.content.should == hash["unit_label"]
23
+ unit.attributes["type"].value.should == hash["unit_type"]
24
+ unit.attributes["symbol"].value.should == hash["unit_symbol"]
25
+ datapoints = datastream.at_xpath("xmlns:datapoints")
26
+ datapoints.xpath("xmlns:value").each do |datapoint|
27
+ dp = hash["datapoints"].detect{|dp| dp["at"].iso8601(6) == datapoint.attributes["at"].value}
28
+ datapoint.content.should == dp["value"]
29
+ end
30
+ when "5"
31
+ environment = xml.at_xpath("//xmlns:environment")
32
+ environment.attributes["updated"].value.should == hash["updated"].iso8601
33
+ environment.attributes["id"].value.should == hash["feed_id"]
34
+ environment.attributes["creator"].value.should == "http://www.haque.co.uk"
35
+ datastream = environment.at_xpath("//xmlns:data")
36
+ datastream.attributes["id"].value.should == hash["id"]
37
+ tags = datastream.xpath("//xmlns:tag")
38
+ hash["tags"].split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase}.each_with_index do |tag, index|
39
+ tags[index].content.should == tag
40
+ end
41
+ current_value = datastream.at_xpath("//xmlns:value")
42
+ current_value.content.should == hash["current_value"]
43
+ current_value.attributes["minValue"].value.should == hash["min_value"].to_s
44
+ current_value.attributes["maxValue"].value.should == hash["max_value"].to_s
45
+ unit = datastream.at_xpath("//xmlns:unit")
46
+ unit.content.should == hash["unit_label"]
47
+ unit.attributes["type"].value.should == hash["unit_type"]
48
+ unit.attributes["symbol"].value.should == hash["unit_symbol"]
49
+
50
+ else
51
+ false
52
+ end
53
+ end
54
+
55
+ failure_message_for_should do |xml|
56
+ "expected #{xml} to describe eeml version #{eeml_version}"
57
+ end
58
+ end
59
+
60
+
@@ -0,0 +1,98 @@
1
+ RSpec::Matchers.define :contain_feed_eeml_for_version do |eeml_version|
2
+ match do |xml|
3
+ hash = feed_as_(:hash)
4
+ case eeml_version
5
+ when "0.5.1"
6
+ environment = xml.at_xpath("//xmlns:environment")
7
+ environment.attributes["updated"].value.should == hash["updated"].iso8601(6)
8
+ environment.attributes["created"].value.should == hash["created"].iso8601(6)
9
+ environment.attributes["id"].value.should == hash["id"].to_s
10
+ environment.attributes["creator"].value.should == hash["creator"]
11
+ environment.at_xpath("//xmlns:title").content.should == hash["title"]
12
+ environment.at_xpath("//xmlns:feed").content.should == "#{hash["feed"]}.xml"
13
+ environment.at_xpath("//xmlns:status").content.should == hash["status"]
14
+ environment.at_xpath("//xmlns:private").content.should == hash["private"].to_s
15
+ environment.at_xpath("//xmlns:description").content.should == hash["description"]
16
+ environment.at_xpath("//xmlns:icon").content.should == hash["icon"]
17
+ environment.at_xpath("//xmlns:website").content.should == hash["website"]
18
+ environment.at_xpath("//xmlns:email").content.should == hash["email"]
19
+ environment.at_xpath("//xmlns:product_id").content.should == hash["product_id"]
20
+ environment.at_xpath("//xmlns:device_serial").content.should == hash["device_serial"]
21
+ tags = environment.xpath("//xmlns:tag")
22
+ hash["tags"].split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase}.each_with_index do |tag, index|
23
+ tags[index].content.should == tag
24
+ end
25
+ location = environment.at_xpath("//xmlns:location")
26
+ location.attributes["domain"].value.should == hash["location_domain"]
27
+ location.attributes["exposure"].value.should == hash["location_exposure"]
28
+ location.attributes["disposition"].value.should == hash["location_disposition"]
29
+ location.at_xpath("//xmlns:name").content.should == hash["location_name"]
30
+ location.at_xpath("//xmlns:lat").content.should == hash["location_lat"].to_s
31
+ location.at_xpath("//xmlns:lon").content.should == hash["location_lon"].to_s
32
+ location.at_xpath("//xmlns:ele").content.should == hash["location_ele"]
33
+ hash["datastreams"].each do |ds_hash|
34
+ datastream = environment.at_xpath("//xmlns:data[@id=#{ds_hash["id"]}]")
35
+ tags = datastream.xpath("xmlns:tag")
36
+ ds_hash["tags"].split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase}.each_with_index do |tag, index|
37
+ tags[index].content.should == tag
38
+ end if ds_hash["tags"]
39
+ current_value = datastream.at_xpath("xmlns:current_value")
40
+ current_value.content.should == ds_hash["current_value"]
41
+ current_value.attributes["at"].value.should == ds_hash["updated"].iso8601(6)
42
+ datastream.at_xpath("xmlns:max_value").content.should == ds_hash["max_value"].to_s
43
+ datastream.at_xpath("xmlns:min_value").content.should == ds_hash["min_value"].to_s
44
+ unit = datastream.at_xpath("xmlns:unit")
45
+ unit.content.should == ds_hash["unit_label"] unless ds_hash["unit_label"].empty?
46
+ unit.attributes["type"].value.should == ds_hash["unit_type"] unless ds_hash["unit_type"].empty?
47
+ unit.attributes["symbol"].value.should == ds_hash["unit_symbol"] unless ds_hash["unit_symbol"].empty?
48
+ ds_hash["datapoints"].each do |dp_hash|
49
+ datapoint = datastream.at_xpath("xmlns:datapoints").at_xpath("xmlns:value[@at=\"#{dp_hash["at"].iso8601(6)}\"]")
50
+ datapoint.content.should == dp_hash["value"]
51
+ end if ds_hash["datapoints"]
52
+ end
53
+ when "5"
54
+ environment = xml.at_xpath("//xmlns:environment")
55
+ environment.attributes["updated"].value.should == hash["updated"].iso8601
56
+ environment.attributes["id"].value.should == hash["id"].to_s
57
+ environment.attributes["creator"].value.should == "http://www.haque.co.uk"
58
+ environment.at_xpath("//xmlns:title").content.should == hash["title"]
59
+ environment.at_xpath("//xmlns:feed").content.should == "#{hash["feed"]}.xml"
60
+ environment.at_xpath("//xmlns:status").content.should == hash["status"]
61
+ environment.at_xpath("//xmlns:description").content.should == hash["description"]
62
+ environment.at_xpath("//xmlns:icon").content.should == hash["icon"]
63
+ environment.at_xpath("//xmlns:website").content.should == hash["website"]
64
+ environment.at_xpath("//xmlns:email").content.should == hash["email"]
65
+ location = environment.at_xpath("//xmlns:location")
66
+ location.attributes["domain"].value.should == hash["location_domain"]
67
+ location.attributes["exposure"].value.should == hash["location_exposure"]
68
+ location.attributes["disposition"].value.should == hash["location_disposition"]
69
+ location.at_xpath("//xmlns:name").content.should == hash["location_name"]
70
+ location.at_xpath("//xmlns:lat").content.should == hash["location_lat"].to_s
71
+ location.at_xpath("//xmlns:lon").content.should == hash["location_lon"].to_s
72
+ location.at_xpath("//xmlns:ele").content.should == hash["location_ele"]
73
+ hash["datastreams"].each do |ds_hash|
74
+ datastream = environment.at_xpath("//xmlns:data[@id=#{ds_hash["id"]}]")
75
+ tags = datastream.xpath("xmlns:tag")
76
+ ds_hash["tags"].split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase}.each_with_index do |tag, index|
77
+ tags[index].content.should == tag
78
+ end if ds_hash["tags"]
79
+ current_value = datastream.at_xpath("xmlns:value")
80
+ current_value.content.should == ds_hash["current_value"]
81
+ current_value.attributes["minValue"].value.should == ds_hash["min_value"].to_s
82
+ current_value.attributes["maxValue"].value.should == ds_hash["max_value"].to_s
83
+ unit = datastream.at_xpath("xmlns:unit")
84
+ unit.content.should == ds_hash["unit_label"] unless ds_hash["unit_label"].empty?
85
+ unit.attributes["type"].value.should == ds_hash["unit_type"] unless ds_hash["unit_type"].empty?
86
+ unit.attributes["symbol"].value.should == ds_hash["unit_symbol"] unless ds_hash["unit_symbol"].empty?
87
+ end
88
+ else
89
+ false
90
+ end
91
+ end
92
+
93
+ failure_message_for_should do |xml|
94
+ "expected #{xml} to contain eeml version #{eeml_version}"
95
+ end
96
+ end
97
+
98
+
@@ -0,0 +1,53 @@
1
+ def datapoint_as_(format, options = {})
2
+ case format.to_s
3
+ when 'hash'
4
+ data = {
5
+ "at" => Time.parse('2011-01-02'),
6
+ "value" => "2000"
7
+ }
8
+ when 'json'
9
+ data = {
10
+ 'at' => '2011-02-16T16:21:01.834174Z',
11
+ 'value' => "2000"
12
+ }
13
+ when 'xml'
14
+ data = <<-XML
15
+ <?xml version="1.0" encoding="UTF-8"?>
16
+ <eeml xmlns="http://www.eeml.org/xsd/0.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.5.1" xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd">
17
+ <environment>
18
+ <data>
19
+ <datapoints>
20
+ <value at="2011-03-02T15:59:56.895922Z">2000</value>
21
+ </datapoints>
22
+ </data>
23
+ </environment>
24
+ </eeml>
25
+ XML
26
+ end
27
+
28
+ # Add extra options we passed
29
+ if options[:with]
30
+ options[:with].each do |field, value|
31
+ data[field.to_s] = value
32
+ end
33
+ end
34
+
35
+ # Remove options we don't need
36
+ if options[:except]
37
+ options[:except].each do |field,_|
38
+ data.delete(field.to_s)
39
+ end
40
+ end
41
+
42
+ # Return the feed in the requested format
43
+ case format.to_s
44
+ when 'hash'
45
+ data
46
+ when 'json'
47
+ MultiJson.dump(data)
48
+ when 'xml'
49
+ data
50
+ else
51
+ raise "#{format} undefined"
52
+ end
53
+ end
@@ -0,0 +1,324 @@
1
+ def datastream_as_(format, options = {})
2
+ case format.to_s
3
+ when 'hash'
4
+ data = {
5
+ "updated" => Time.parse('2011-01-02'),
6
+ "max_value"=>658.0,
7
+ "unit_type"=>"derived SI",
8
+ "min_value"=>0.0,
9
+ "unit_label"=>"percentage",
10
+ "current_value"=>"14",
11
+ "id"=>"0",
12
+ "tags"=>"humidity,Temperature ,freakin lasers",
13
+ "unit_symbol"=>"%",
14
+ "feed_id" => "24568",
15
+ "feed_creator" => "Dennis",
16
+ "datapoints" => [{
17
+ "value" => "1",
18
+ "at" => Time.parse("2011-03-02T15:59:56.895922Z")
19
+ },
20
+ {
21
+ "value" => "1",
22
+ "at" => Time.parse("2011-03-02T16:00:07.188648Z")
23
+ },
24
+ {
25
+ "value" => "2",
26
+ "at" => Time.parse("2011-03-02T16:00:18.416500Z")
27
+ }]
28
+ }
29
+ when 'json'
30
+ data = datastream_as_json(options[:version] || "1.0.0")
31
+ when 'xml'
32
+ data = datastream_as_xml(options[:version] || "0.5.1", options[:except_node], options[:omit_version])
33
+ when 'csv'
34
+ data = datastream_as_csv(options[:version] || "plain")
35
+ end
36
+
37
+ # Add extra options we passed
38
+ if options[:with]
39
+ options[:with].each do |field, value|
40
+ data[field.to_s] = value
41
+ end
42
+ end
43
+
44
+ # Remove options we don't need
45
+ if options[:except]
46
+ options[:except].each do |field,_|
47
+ data.delete(field.to_s)
48
+ end
49
+ end
50
+
51
+ # Return the feed in the requested format
52
+ case format.to_s
53
+ when 'hash'
54
+ data
55
+ when 'json'
56
+ MultiJson.dump(data)
57
+ when 'xml'
58
+ data
59
+ when 'csv'
60
+ data
61
+ else
62
+ raise "#{format} undefined"
63
+ end
64
+
65
+ end
66
+
67
+ def datastream_as_json(version)
68
+ case version
69
+ when "1.0.0"
70
+ {
71
+ 'min_value' => '0.0',
72
+ 'at' => '2011-02-16T16:21:01.834174Z',
73
+ 'tags' => ['humidity', 'Temperature', 'freakin lasers'],
74
+ 'current_value' => '14',
75
+ 'max_value' => '658.0',
76
+ 'datapoints_function' => 'average',
77
+ "datapoints" => [{
78
+ "value" => "1",
79
+ "at" => "2011-03-02T15:59:56.895922Z"
80
+ },
81
+ {
82
+ "value" => "1",
83
+ "at" => "2011-03-02T16:00:07.188648Z"
84
+ },
85
+ {
86
+ "value" => "2",
87
+ "at" => "2011-03-02T16:00:18.416500Z"
88
+ }],
89
+ 'id' => '0',
90
+ "unit" => {
91
+ "type" => "derived SI",
92
+ "symbol" => "%",
93
+ "label" => "percentage"
94
+ },
95
+ 'version' => '1.0.0'
96
+ }
97
+ when "1.0.0-minimal"
98
+ {
99
+ "current_value" => "294",
100
+ "max_value" => "697.0",
101
+ "min_value" => "0.0",
102
+ "id" => "1"
103
+ }
104
+ when "1.0.0-minimal_timestamp"
105
+ {
106
+ "current_value" => "294",
107
+ "max_value" => "697.0",
108
+ "min_value" => "0.0",
109
+ "id" => "1",
110
+ "at" => "2011-03-02T16:00:18.416500Z"
111
+ }
112
+ when "0.6-alpha"
113
+ {
114
+ "tags" => ["humidity"],
115
+ "values" => [{
116
+ "min_value" => "0.0",
117
+ "recorded_at" => "2011-02-22T14:28:50Z",
118
+ "value" => "129",
119
+ "max_value" => "658.0"
120
+ }],
121
+ "id" => "0",
122
+ "unit" => {
123
+ "type" => "derived SI",
124
+ "symbol" => "%",
125
+ "label" => "percentage"
126
+ },
127
+ 'version' => '0.6-alpha'
128
+ }
129
+ else
130
+ raise "Datastream as JSON #{version} not implemented"
131
+ end
132
+ end
133
+
134
+ def datastream_as_xml(version, except_node = nil, omit_version = true)
135
+ case version
136
+ when "0.5.1"
137
+ if except_node == :tag
138
+ xml = <<XML
139
+ <?xml version="1.0" encoding="UTF-8"?>
140
+ <eeml xmlns="http://www.eeml.org/xsd/0.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #{omit_version ? '' : 'version="0.5.1"'} xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd">
141
+ <environment updated="2011-02-16T16:21:01.834174Z" id="504" creator="http://appdev.loc:3000/users/occaecati">
142
+ <data id="0">
143
+ <current_value at="2011-02-16T16:21:01.834174Z">14</current_value>
144
+ <max_value>658.0</max_value>
145
+ <min_value>0.0</min_value>
146
+ <unit type="derivedSI" symbol="A">Alpha</unit>
147
+ </data>
148
+ </environment>
149
+ </eeml>
150
+ XML
151
+ elsif except_node == :unit
152
+ xml = <<XML
153
+ <?xml version="1.0" encoding="UTF-8"?>
154
+ <eeml xmlns="http://www.eeml.org/xsd/0.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #{omit_version ? '' : 'version="0.5.1"'} xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd">
155
+ <environment updated="2011-02-16T16:21:01.834174Z" id="504" creator="http://appdev.loc:3000/users/occaecati">
156
+ <data id="0">
157
+ <tag>freakin lasers</tag>
158
+ <tag>humidity</tag>
159
+ <tag>Temperature</tag>
160
+ <current_value at="2011-02-16T16:21:01.834174Z">14</current_value>
161
+ <max_value>658.0</max_value>
162
+ <min_value>0.0</min_value>
163
+ </data>
164
+ </environment>
165
+ </eeml>
166
+ XML
167
+ elsif except_node == :unit_attributes
168
+ xml = <<XML
169
+ <?xml version="1.0" encoding="UTF-8"?>
170
+ <eeml xmlns="http://www.eeml.org/xsd/0.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #{omit_version ? '' : 'version="0.5.1"'} xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd">
171
+ <environment updated="2011-02-16T16:21:01.834174Z" id="504" creator="http://appdev.loc:3000/users/occaecati">
172
+ <data id="0">
173
+ <tag>freakin lasers</tag>
174
+ <tag>humidity</tag>
175
+ <tag>Temperature</tag>
176
+ <current_value at="2011-02-16T16:21:01.834174Z">14</current_value>
177
+ <max_value>658.0</max_value>
178
+ <min_value>0.0</min_value>
179
+ <unit>Alpha</unit>
180
+ </data>
181
+ </environment>
182
+ </eeml>
183
+ XML
184
+ elsif except_node == :timestamps
185
+ xml = <<XML
186
+ <?xml version="1.0" encoding="UTF-8"?>
187
+ <eeml xmlns="http://www.eeml.org/xsd/0.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #{omit_version ? '' : 'version="0.5.1"'} xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd">
188
+ <environment id="504" creator="http://appdev.loc:3000/users/occaecati">
189
+ <data id="0">
190
+ <tag>freakin lasers</tag>
191
+ <tag>humidity</tag>
192
+ <tag>Temperature</tag>
193
+ <current_value>14</current_value>
194
+ <max_value>658.0</max_value>
195
+ <min_value>0.0</min_value>
196
+ <unit type="derivedSI" symbol="A">Alpha</unit>
197
+ </data>
198
+ </environment>
199
+ </eeml>
200
+ XML
201
+ else
202
+ xml = <<XML
203
+ <?xml version="1.0" encoding="UTF-8"?>
204
+ <eeml xmlns="http://www.eeml.org/xsd/0.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #{omit_version ? '' : 'version="0.5.1"'} xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd">
205
+ <environment updated="2011-02-16T16:21:01.834174Z" id="504" creator="http://appdev.loc:3000/users/occaecati">
206
+ <data id="0">
207
+ <tag>freakin lasers</tag>
208
+ <tag>humidity</tag>
209
+ <tag>Temperature</tag>
210
+ <current_value at="2011-02-16T16:21:01.834174Z">14</current_value>
211
+ <max_value>658.0</max_value>
212
+ <min_value>0.0</min_value>
213
+ <unit type="derivedSI" symbol="A">Alpha</unit>
214
+ <datapoints>
215
+ <value at="2011-03-02T15:59:56.895922Z">1</value>
216
+ <value at="2011-03-02T16:00:07.188648Z">1</value>
217
+ <value at="2011-03-02T16:00:18.416500Z">2</value>
218
+ </datapoints>
219
+ </data>
220
+ </environment>
221
+ </eeml>
222
+ XML
223
+ end
224
+ when "5"
225
+ if except_node == :tag
226
+ xml = <<XML
227
+ <?xml version="1.0" encoding="UTF-8"?>
228
+ <eeml xmlns="http://www.eeml.org/xsd/005" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #{omit_version ? '' : 'version="5"'} xsi:schemaLocation="http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd">
229
+ <environment updated="2011-02-16T16:21:01.834174Z" id="504" creator="http://appdev.loc:3000/users/occaecati">
230
+ <data id="0">
231
+ <value maxValue="658.0" minValue="658">14</value>
232
+ <unit type="derivedSI" symbol="A">Alpha</unit>
233
+ </data>
234
+ </environment>
235
+ </eeml>
236
+ XML
237
+ elsif except_node == :unit
238
+ xml = <<XML
239
+ <?xml version="1.0" encoding="UTF-8"?>
240
+ <eeml xmlns="http://www.eeml.org/xsd/005" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="5" xsi:schemaLocation="http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd">
241
+ <environment updated="2011-02-16T16:21:01.834174Z" id="504" creator="http://appdev.loc:3000/users/occaecati">
242
+ <data id="0">
243
+ <tag>freakin lasers</tag>
244
+ <tag>humidity</tag>
245
+ <tag>Temperature</tag>
246
+ <value maxValue="658.0" minValue="658">14</value>
247
+ </data>
248
+ </environment>
249
+ </eeml>
250
+ XML
251
+ elsif except_node == :unit_attributes
252
+ xml = <<XML
253
+ <?xml version="1.0" encoding="UTF-8"?>
254
+ <eeml xmlns="http://www.eeml.org/xsd/005" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #{omit_version ? '' : 'version="5"'} xsi:schemaLocation="http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd">
255
+ <environment updated="2011-02-16T16:21:01.834174Z" id="504" creator="http://appdev.loc:3000/users/occaecati">
256
+ <data id="0">
257
+ <tag>freakin lasers</tag>
258
+ <tag>humidity</tag>
259
+ <tag>Temperature</tag>
260
+ <value maxValue="658.0" minValue="658">14</value>
261
+ <unit>Alpha</unit>
262
+ </data>
263
+ </environment>
264
+ </eeml>
265
+ XML
266
+ elsif except_node == :value_attributes
267
+ xml = <<XML
268
+ <?xml version="1.0" encoding="UTF-8"?>
269
+ <eeml xmlns="http://www.eeml.org/xsd/005" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="5" xsi:schemaLocation="http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd">
270
+ <environment updated="2011-02-16T16:21:01.834174Z" id="504" creator="http://appdev.loc:3000/users/occaecati">
271
+ <data id="0">
272
+ <tag>freakin lasers</tag>
273
+ <tag>humidity</tag>
274
+ <tag>Temperature</tag>
275
+ <value>14</value>
276
+ <unit type="derivedSI" symbol="A">Alpha</unit>
277
+ </data>
278
+ </environment>
279
+ </eeml>
280
+ XML
281
+ elsif except_node == :timestamps
282
+ xml = <<XML
283
+ <?xml version="1.0" encoding="UTF-8"?>
284
+ <eeml xmlns="http://www.eeml.org/xsd/005" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #{omit_version ? '' : 'version="5"'} xsi:schemaLocation="http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd">
285
+ <environment id="504" creator="http://appdev.loc:3000/users/occaecati">
286
+ <data id="0">
287
+ <tag>freakin lasers</tag>
288
+ <tag>humidity</tag>
289
+ <tag>Temperature</tag>
290
+ <value maxValue="658.0" minValue="658">14</value>
291
+ <unit type="derivedSI" symbol="A">Alpha</unit>
292
+ </data>
293
+ </environment>
294
+ </eeml>
295
+ XML
296
+ else
297
+ xml = <<XML
298
+ <?xml version="1.0" encoding="UTF-8"?>
299
+ <eeml xmlns="http://www.eeml.org/xsd/005" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #{omit_version ? '' : 'version="5"'} xsi:schemaLocation="http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd">
300
+ <environment updated="2011-02-16T16:21:01.834174Z" id="504" creator="http://appdev.loc:3000/users/occaecati">
301
+ <data id="0">
302
+ <tag>freakin lasers</tag>
303
+ <tag>humidity</tag>
304
+ <tag>Temperature</tag>
305
+ <value maxValue="658.0" minValue="658">14</value>
306
+ <unit type="derivedSI" symbol="A">Alpha</unit>
307
+ </data>
308
+ </environment>
309
+ </eeml>
310
+ XML
311
+ end
312
+ else
313
+ raise "Datastream as XML #{version} not implemented"
314
+ end
315
+ end
316
+
317
+ def datastream_as_csv(version)
318
+ case version
319
+ when "timestamped"
320
+ return "2011-02-16T16:21:01.834174Z,14"
321
+ else
322
+ return "14"
323
+ end
324
+ end