cosm-rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/.gitignore +12 -0
  2. data/.rbenv-version +1 -0
  3. data/.rspec +2 -0
  4. data/.rvmrc +1 -0
  5. data/.travis.yml +10 -0
  6. data/CHANGELOG.md +91 -0
  7. data/Gemfile +4 -0
  8. data/MIT-LICENSE +20 -0
  9. data/README.md +195 -0
  10. data/Rakefile +35 -0
  11. data/ci/build_hudson.sh +24 -0
  12. data/cosm-rb.gemspec +40 -0
  13. data/init.rb +2 -0
  14. data/lib/cosm-rb/array_extensions.rb +6 -0
  15. data/lib/cosm-rb/base/instance_methods.rb +28 -0
  16. data/lib/cosm-rb/base.rb +52 -0
  17. data/lib/cosm-rb/client.rb +7 -0
  18. data/lib/cosm-rb/datapoint.rb +71 -0
  19. data/lib/cosm-rb/datastream.rb +136 -0
  20. data/lib/cosm-rb/feed.rb +108 -0
  21. data/lib/cosm-rb/hash_extensions.rb +16 -0
  22. data/lib/cosm-rb/helpers.rb +42 -0
  23. data/lib/cosm-rb/key.rb +98 -0
  24. data/lib/cosm-rb/nil_content.rb +15 -0
  25. data/lib/cosm-rb/object_extensions.rb +6 -0
  26. data/lib/cosm-rb/parsers/csv/datastream_defaults.rb +12 -0
  27. data/lib/cosm-rb/parsers/csv/feed_defaults.rb +38 -0
  28. data/lib/cosm-rb/parsers/defaults.rb +13 -0
  29. data/lib/cosm-rb/parsers/json/datapoint_defaults.rb +12 -0
  30. data/lib/cosm-rb/parsers/json/datastream_defaults.rb +53 -0
  31. data/lib/cosm-rb/parsers/json/feed_defaults.rb +103 -0
  32. data/lib/cosm-rb/parsers/json/key_defaults.rb +15 -0
  33. data/lib/cosm-rb/parsers/json/search_result_defaults.rb +18 -0
  34. data/lib/cosm-rb/parsers/json/trigger_defaults.rb +12 -0
  35. data/lib/cosm-rb/parsers/xml/datapoint_defaults.rb +20 -0
  36. data/lib/cosm-rb/parsers/xml/datastream_defaults.rb +77 -0
  37. data/lib/cosm-rb/parsers/xml/feed_defaults.rb +127 -0
  38. data/lib/cosm-rb/parsers/xml/key_defaults.rb +40 -0
  39. data/lib/cosm-rb/parsers/xml/trigger_defaults.rb +22 -0
  40. data/lib/cosm-rb/permission.rb +67 -0
  41. data/lib/cosm-rb/resource.rb +43 -0
  42. data/lib/cosm-rb/search_result.rb +63 -0
  43. data/lib/cosm-rb/string_extensions.rb +6 -0
  44. data/lib/cosm-rb/template.rb +32 -0
  45. data/lib/cosm-rb/templates/csv/datapoint_defaults.rb +22 -0
  46. data/lib/cosm-rb/templates/csv/datastream_defaults.rb +43 -0
  47. data/lib/cosm-rb/templates/csv/feed_defaults.rb +47 -0
  48. data/lib/cosm-rb/templates/defaults.rb +14 -0
  49. data/lib/cosm-rb/templates/json/datapoint_defaults.rb +15 -0
  50. data/lib/cosm-rb/templates/json/datastream_defaults.rb +66 -0
  51. data/lib/cosm-rb/templates/json/feed_defaults.rb +134 -0
  52. data/lib/cosm-rb/templates/json/key_defaults.rb +41 -0
  53. data/lib/cosm-rb/templates/json/search_result_defaults.rb +47 -0
  54. data/lib/cosm-rb/templates/json/trigger_defaults.rb +21 -0
  55. data/lib/cosm-rb/templates/xml/datapoint_defaults.rb +25 -0
  56. data/lib/cosm-rb/templates/xml/datastream_defaults.rb +66 -0
  57. data/lib/cosm-rb/templates/xml/feed_defaults.rb +110 -0
  58. data/lib/cosm-rb/templates/xml/search_result_defaults.rb +116 -0
  59. data/lib/cosm-rb/templates/xml_headers.rb +17 -0
  60. data/lib/cosm-rb/trigger.rb +65 -0
  61. data/lib/cosm-rb/validations.rb +9 -0
  62. data/lib/cosm-rb/version.rb +3 -0
  63. data/lib/cosm-rb.rb +29 -0
  64. data/spec/cosm-rb/array_extensions_spec.rb +9 -0
  65. data/spec/cosm-rb/base/instance_methods_spec.rb +109 -0
  66. data/spec/cosm-rb/base_spec.rb +56 -0
  67. data/spec/cosm-rb/client_spec.rb +7 -0
  68. data/spec/cosm-rb/datapoint_spec.rb +169 -0
  69. data/spec/cosm-rb/datastream_spec.rb +301 -0
  70. data/spec/cosm-rb/feed_spec.rb +298 -0
  71. data/spec/cosm-rb/hash_extensions_spec.rb +20 -0
  72. data/spec/cosm-rb/helpers_spec.rb +56 -0
  73. data/spec/cosm-rb/key_spec.rb +178 -0
  74. data/spec/cosm-rb/parsers/csv/datastream_defaults_spec.rb +12 -0
  75. data/spec/cosm-rb/parsers/csv/feed_defaults_spec.rb +35 -0
  76. data/spec/cosm-rb/parsers/json/datapoint_defaults_spec.rb +15 -0
  77. data/spec/cosm-rb/parsers/json/datastream_defaults_spec.rb +82 -0
  78. data/spec/cosm-rb/parsers/json/feed_defaults_spec.rb +18 -0
  79. data/spec/cosm-rb/parsers/json/key_defaults_spec.rb +8 -0
  80. data/spec/cosm-rb/parsers/json/search_result_defaults_spec.rb +12 -0
  81. data/spec/cosm-rb/parsers/json/trigger_defaults_spec.rb +16 -0
  82. data/spec/cosm-rb/parsers/xml/datapoint_defaults_spec.rb +19 -0
  83. data/spec/cosm-rb/parsers/xml/datastream_defaults_spec.rb +63 -0
  84. data/spec/cosm-rb/parsers/xml/feed_defaults_spec.rb +65 -0
  85. data/spec/cosm-rb/parsers/xml/key_defaults_spec.rb +16 -0
  86. data/spec/cosm-rb/parsers/xml/trigger_defaults_spec.rb +16 -0
  87. data/spec/cosm-rb/search_result_spec.rb +258 -0
  88. data/spec/cosm-rb/string_extensions_spec.rb +12 -0
  89. data/spec/cosm-rb/template_spec.rb +74 -0
  90. data/spec/cosm-rb/templates/csv/datapoint_defaults_spec.rb +41 -0
  91. data/spec/cosm-rb/templates/csv/datastream_defaults_spec.rb +131 -0
  92. data/spec/cosm-rb/templates/csv/feed_defaults_spec.rb +78 -0
  93. data/spec/cosm-rb/templates/json/datapoint_defaults_spec.rb +14 -0
  94. data/spec/cosm-rb/templates/json/datastream_defaults_spec.rb +170 -0
  95. data/spec/cosm-rb/templates/json/feed_defaults_spec.rb +397 -0
  96. data/spec/cosm-rb/templates/json/key_defaults_spec.rb +29 -0
  97. data/spec/cosm-rb/templates/json/search_result_defaults_spec.rb +37 -0
  98. data/spec/cosm-rb/templates/json/trigger_defaults_spec.rb +19 -0
  99. data/spec/cosm-rb/templates/xml/datapoint_defaults_spec.rb +14 -0
  100. data/spec/cosm-rb/templates/xml/datastream_defaults_spec.rb +113 -0
  101. data/spec/cosm-rb/templates/xml/feed_defaults_spec.rb +131 -0
  102. data/spec/cosm-rb/templates/xml/search_result_defaults_spec.rb +44 -0
  103. data/spec/cosm-rb/trigger_spec.rb +117 -0
  104. data/spec/fixtures/models.rb +81 -0
  105. data/spec/spec_helper.rb +17 -0
  106. data/spec/support/contain_datapoint_eeml_matcher.rb +16 -0
  107. data/spec/support/contain_datastream_eeml_matcher.rb +60 -0
  108. data/spec/support/contain_feed_eeml_matcher.rb +96 -0
  109. data/spec/support/datapoint_helper.rb +53 -0
  110. data/spec/support/datastream_helper.rb +300 -0
  111. data/spec/support/describe_eeml_matcher.rb +23 -0
  112. data/spec/support/feed_helper.rb +771 -0
  113. data/spec/support/fully_represent_datapoint_matcher.rb +30 -0
  114. data/spec/support/fully_represent_datastream_matcher.rb +92 -0
  115. data/spec/support/fully_represent_feed_matcher.rb +226 -0
  116. data/spec/support/fully_represent_key_matcher.rb +74 -0
  117. data/spec/support/fully_represent_search_result_matcher.rb +67 -0
  118. data/spec/support/fully_represent_trigger_matcher.rb +29 -0
  119. data/spec/support/key_helper.rb +74 -0
  120. data/spec/support/search_result_helper.rb +252 -0
  121. data/spec/support/trigger_helper.rb +51 -0
  122. metadata +363 -0
@@ -0,0 +1,15 @@
1
+ module Cosm
2
+ module Templates
3
+ module JSON
4
+ module DatapointDefaults
5
+ def generate_json(version = nil)
6
+ template = Template.new(self, :json)
7
+ template.at {at.iso8601(6)}
8
+ template.value
9
+ template.output!
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,66 @@
1
+ module Cosm
2
+ module Templates
3
+ module JSON
4
+ module DatastreamDefaults
5
+ def generate_json(version, options={})
6
+ case version
7
+ when "1.0.0"
8
+ json_1_0_0 options
9
+ when "0.6-alpha"
10
+ json_0_6_alpha options
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ # As used by http://cosm.com/api/v2/FEED_ID/datastreams/DATASTREAM_ID.json
17
+ def json_1_0_0(options={})
18
+ template = Template.new(self, :json)
19
+ template.id
20
+ template.version {"1.0.0"}
21
+ template.at {updated.iso8601(6)}
22
+ template.current_value
23
+ template.max_value {max_value.to_s}
24
+ template.min_value {min_value.to_s}
25
+ template.tags {parse_tag_string(tags)}
26
+ template.unit {unit_hash(options)}
27
+ template.datapoints_function
28
+ template.datapoints do
29
+ datapoints.collect do |datapoint|
30
+ {
31
+ :at => datapoint.at.iso8601(6),
32
+ :value => datapoint.value
33
+ }
34
+ end
35
+ end if datapoints.any?
36
+ template.output! options
37
+ end
38
+
39
+ # As used by http://cosm.com/api/v1/FEED_ID/datastreams/DATASTREAM_ID.json
40
+ def json_0_6_alpha(options={})
41
+ template = Template.new(self, :json)
42
+ template.id
43
+ template.version {"0.6-alpha"}
44
+ template.values {
45
+ [{ :recorded_at => updated.iso8601,
46
+ :value => current_value,
47
+ :max_value => max_value.to_s,
48
+ :min_value => min_value.to_s }.delete_if_nil_value]
49
+ }
50
+ template.tags {parse_tag_string(tags)}
51
+ template.unit {unit_hash(options)}
52
+ template.output! options
53
+ end
54
+
55
+ def unit_hash(options={})
56
+ hash = { :type => unit_type,
57
+ :symbol => unit_symbol,
58
+ :label => unit_label }
59
+ !options[:include_blank] ? hash.delete_if_nil_value : hash
60
+ end
61
+
62
+ end
63
+ end
64
+ end
65
+ end
66
+
@@ -0,0 +1,134 @@
1
+ module Cosm
2
+ module Templates
3
+ module JSON
4
+ module FeedDefaults
5
+
6
+ include Cosm::Helpers
7
+
8
+ def generate_json(version, options = {})
9
+ case version
10
+ when "1.0.0"
11
+ json_1_0_0 options
12
+ when "0.6-alpha"
13
+ json_0_6_alpha options
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ # As used by http://cosm.com/api/v2/FEED_ID.json
20
+ def json_1_0_0(options = {})
21
+ template = Template.new(self, :json)
22
+ template.id
23
+ template.title
24
+ template.private {private.to_s}
25
+ template.icon
26
+ template.website
27
+ template.tags {parse_tag_string(tags)} if tags
28
+ template.description
29
+ template.feed {feed.blank? ? "" : "#{feed}.json"}
30
+ template.auto_feed_url
31
+ template.status
32
+ template.updated {updated.iso8601(6)}
33
+ template.created {created.iso8601(6)}
34
+ template.email
35
+ template.creator
36
+ if owner_login
37
+ template.user do |user|
38
+ {
39
+ :login => owner_login
40
+ }
41
+ end
42
+ end
43
+ template.version {"1.0.0"}
44
+ if datastreams
45
+ template.datastreams do
46
+ datastreams.collect do |ds|
47
+ if ds.datapoints.any?
48
+ datapoints = ds.datapoints.collect {|dp| {:value => dp.value, :at => dp.at.iso8601(6)}}
49
+ end
50
+ {
51
+ :id => ds.id,
52
+ :at => ds.updated.iso8601(6),
53
+ :max_value => ds.max_value.to_s,
54
+ :min_value => ds.min_value.to_s,
55
+ :current_value => ds.current_value,
56
+ :tags => parse_tag_string(ds.tags),
57
+ :unit => unit_hash(ds, options),
58
+ :datapoints => datapoints
59
+ }.delete_if_nil_value
60
+ end
61
+ end
62
+ end
63
+ template.location {location_hash(options)}
64
+ template.output! options
65
+ end
66
+
67
+ # As used by http://cosm.com/api/v1/FEED_ID.json
68
+ def json_0_6_alpha(options={})
69
+ template = Template.new(self, :json)
70
+ template.id
71
+ template.title
72
+ template.icon
73
+ template.website
74
+ template.description
75
+ template.feed {"#{feed}.json"}
76
+ template.status
77
+ template.updated {updated.iso8601(6)}
78
+ template.email
79
+ template.version {"0.6-alpha"}
80
+ if datastreams
81
+ template.datastreams do
82
+ datastreams.collect do |ds|
83
+ {
84
+ :id => ds.id,
85
+ :values => [{
86
+ :max_value => ds.max_value.to_s,
87
+ :min_value => ds.min_value.to_s,
88
+ :value => ds.current_value,
89
+ :recorded_at => ds.updated.iso8601
90
+ }.delete_if_nil_value],
91
+ :tags => parse_tag_string(ds.tags),
92
+ :unit => unit_hash(ds, options)
93
+ }.delete_if_nil_value
94
+ end
95
+ end
96
+ end
97
+ template.location {location_hash(options)}
98
+ template.output! options
99
+ end
100
+
101
+ private
102
+
103
+ def location_hash(options={})
104
+ hash = { :disposition => location_disposition,
105
+ :name => location_name,
106
+ :exposure => location_exposure,
107
+ :domain => location_domain,
108
+ :ele => location_ele,
109
+ :lat => location_lat,
110
+ :lon => location_lon }
111
+ hash[:waypoints] = format_location_waypoints if location_waypoints
112
+ !options[:include_blank] ? (hash.delete_if_nil_value if location_disposition || location_name || location_exposure || location_domain || location_ele || location_lat || location_lon) : hash
113
+ end
114
+
115
+ def format_location_waypoints
116
+ output = []
117
+ location_waypoints.each{ |item|
118
+ output << item
119
+ output.last[:at] = output.last[:at].iso8601(6)
120
+ }
121
+ output
122
+ end
123
+
124
+ def unit_hash(datastream, options={})
125
+ hash = { :type => datastream.unit_type,
126
+ :symbol => datastream.unit_symbol,
127
+ :label => datastream.unit_label }
128
+ !options[:include_blank] ? (hash.delete_if_nil_value if datastream.unit_type || datastream.unit_label || datastream.unit_symbol) : hash
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
134
+
@@ -0,0 +1,41 @@
1
+ module Cosm
2
+ module Templates
3
+ module JSON
4
+ module KeyDefaults
5
+ def generate_json(options={})
6
+ template = Template.new(self, :json)
7
+ template.key do |k|
8
+ if self.permissions
9
+ s = self.permissions.collect { |s|
10
+ if s.resources
11
+ res = s.resources.collect { |r| { :feed_id => r.feed_id, :datastream_id => r.datastream_id, :datastream_trigger_id => r.datastream_trigger_id }.delete_if_nil_value
12
+ }
13
+ end
14
+ {
15
+ :referer => s.referer,
16
+ :source_ip => s.source_ip,
17
+ :label => s.label,
18
+ :minimum_interval => s.minimum_interval,
19
+ :access_methods => s.access_methods.collect { |a| a.to_s.downcase },
20
+ :resources => res
21
+ }
22
+ }
23
+ end
24
+
25
+ {
26
+ :id => id,
27
+ :expires_at => expires_at.respond_to?(:iso8601) ? expires_at.iso8601(6) : expires_at,
28
+ :api_key => key,
29
+ :user => user,
30
+ :label => label,
31
+ :private_access => private_access,
32
+ :permissions => s
33
+ }
34
+ end
35
+ template.output! options
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,47 @@
1
+ module Cosm
2
+ module Templates
3
+ module JSON
4
+ module SearchResultDefaults
5
+
6
+ include Helpers
7
+
8
+ def generate_json(version)
9
+ case version
10
+ when "1.0.0"
11
+ json_1_0_0
12
+ when "0.6-alpha"
13
+ json_0_6_alpha
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ # As used by http://cosm.com/api/v2/feeds.json
20
+ def json_1_0_0
21
+ template = Template.new(self, :json)
22
+ template.totalResults
23
+ template.startIndex
24
+ template.itemsPerPage
25
+ if results
26
+ template.results {results.collect{|f| f.generate_json("1.0.0")}}
27
+ end
28
+ template.output!
29
+ end
30
+
31
+ # As used by http://cosm.com/api/v1/feeds.json
32
+ def json_0_6_alpha
33
+ template = Template.new(self, :json)
34
+ template.totalResults
35
+ template.startIndex
36
+ template.itemsPerPage
37
+ if results
38
+ template.results {results.collect{|f| f.generate_json("0.6-alpha")}}
39
+ end
40
+ template.output!
41
+ end
42
+
43
+ end
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,21 @@
1
+ module Cosm
2
+ module Templates
3
+ module JSON
4
+ module TriggerDefaults
5
+ def generate_json(version = nil)
6
+ template = Template.new(self, :json)
7
+ template.id
8
+ template.threshold_value
9
+ template.notified_at
10
+ template.url
11
+ template.trigger_type
12
+ template.stream_id
13
+ template.environment_id
14
+ template.user
15
+ template.output!
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,25 @@
1
+ module Cosm
2
+ module Templates
3
+ module XML
4
+ module DatapointDefaults
5
+ include XMLHeaders
6
+ def generate_xml(version = nil)
7
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
8
+ xml.eeml(_eeml_0_5_1) do |eeml|
9
+ eeml.environment do |environment|
10
+ environment.data do |data|
11
+ data.datapoints do |datapoints|
12
+ datapoints.value(value, :at => at.iso8601(6))
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ builder.to_xml
19
+ end
20
+
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,66 @@
1
+ module Cosm
2
+ module Templates
3
+ module XML
4
+ module DatastreamDefaults
5
+ include XMLHeaders
6
+ include Helpers
7
+
8
+ def generate_xml(version, options={})
9
+ case version
10
+ when "0.5.1"
11
+ xml_0_5_1 options
12
+ when "5"
13
+ xml_5 options
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ # As used by http://cosm.com/api/v2/FEED_ID/datastreams/DATASTREAM_ID.xml
20
+ def xml_0_5_1(options={})
21
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
22
+ xml.eeml(_eeml_0_5_1) do |eeml|
23
+ eeml.environment(:updated => updated.iso8601(6), :id => feed_id, :creator => feed_creator) do |environment|
24
+ environment.data(:id => id) do |data|
25
+ parse_tag_string(tags).each do |tag|
26
+ data.tag tag
27
+ end if tags
28
+ data.current_value current_value, :at => updated.iso8601(6)
29
+ data.max_value max_value if max_value
30
+ data.min_value min_value if min_value
31
+ data.unit unit_label, {:type => unit_type, :symbol => unit_symbol}.delete_if_nil_value if unit_label || unit_type || unit_symbol
32
+ data.datapoints({"function" => datapoints_function}.delete_if_nil_value) do
33
+ datapoints.each do |datapoint|
34
+ data.value(datapoint.value, "at" => datapoint.at.iso8601(6))
35
+ end
36
+ end if datapoints.any?
37
+ end
38
+ end
39
+ end
40
+ end
41
+ builder.to_xml
42
+ end
43
+
44
+ # As used by http://cosm.com/api/v1/FEED_ID/datastreams/DATASTREAM_ID.xml
45
+ def xml_5(options={})
46
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
47
+ xml.eeml(_eeml_5) do |eeml|
48
+ eeml.environment(:updated => updated.iso8601, :id => feed_id, :creator => "http://www.haque.co.uk") do |environment|
49
+ environment.data(:id => id) do |data|
50
+ parse_tag_string(tags).each do |tag|
51
+ data.tag tag
52
+ end if tags
53
+ data.value current_value, {:minValue => min_value, :maxValue => max_value}.delete_if_nil_value
54
+ data.unit unit_label, {:type => unit_type, :symbol => unit_symbol}.delete_if_nil_value if unit_label || unit_type || unit_symbol
55
+ end
56
+ end
57
+ end
58
+ end
59
+ builder.to_xml
60
+ end
61
+
62
+ end
63
+ end
64
+ end
65
+ end
66
+
@@ -0,0 +1,110 @@
1
+ module Cosm
2
+ module Templates
3
+ module XML
4
+ module FeedDefaults
5
+ include XMLHeaders
6
+ include Helpers
7
+
8
+ def generate_xml(version, options={})
9
+ case version
10
+ when "0.5.1"
11
+ xml_0_5_1 options
12
+ when "5"
13
+ xml_5 options
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ # As used by http://cosm.com/api/v2/FEED_ID/datastreams/DATASTREAM_ID.xml
20
+ def xml_0_5_1(options={})
21
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
22
+ xml.eeml(_eeml_0_5_1) do |eeml|
23
+ eeml.environment(:updated => updated.iso8601(6), :created => created.iso8601(6), :id => id, :creator => creator) do |environment|
24
+ environment.title title unless title.blank?
25
+ environment.feed "#{feed}.xml" unless feed.blank?
26
+ environment.auto_feed_url auto_feed_url unless auto_feed_url.blank?
27
+ environment.status status unless status.blank?
28
+ environment.description description unless description.blank?
29
+ environment.icon icon unless icon.blank?
30
+ environment.website website unless website.blank?
31
+ environment.email email unless email.blank?
32
+ environment.private_ self.private.to_s
33
+ parse_tag_string(tags).each do |tag|
34
+ environment.tag tag
35
+ end if tags
36
+ environment.location({:disposition => location_disposition, :exposure => location_exposure, :domain => location_domain}.delete_if_nil_value) do |location|
37
+ location.name location_name
38
+ location.lat location_lat
39
+ location.lon location_lon
40
+ location.ele location_ele
41
+ end
42
+ environment.user do |user|
43
+ user.login owner_login
44
+ end if owner_login
45
+ datastreams.each do |ds|
46
+ environment.data(:id => ds.id) do |data|
47
+ parse_tag_string(ds.tags).each do |tag|
48
+ data.tag tag
49
+ end if ds.tags
50
+ data.current_value ds.current_value, :at => ds.updated.iso8601(6)
51
+ data.max_value ds.max_value if ds.max_value
52
+ data.min_value ds.min_value if ds.min_value
53
+ if ds.unit_symbol || ds.unit_type
54
+ units = {:type => ds.unit_type, :symbol => ds.unit_symbol}.delete_if_nil_value
55
+ end
56
+ data.unit ds.unit_label, units if !ds.unit_label.empty? || !units.empty?
57
+ data.datapoints do
58
+ ds.datapoints.each do |datapoint|
59
+ data.value(datapoint.value, "at" => datapoint.at.iso8601(6))
60
+ end
61
+ end if ds.datapoints.any?
62
+ end
63
+ end if datastreams
64
+ end
65
+ end
66
+ end
67
+ builder.to_xml
68
+ end
69
+
70
+ # As used by http://cosm.com/api/v1/FEED_ID/datastreams/DATASTREAM_ID.xml
71
+ def xml_5(options={})
72
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
73
+ xml.eeml(_eeml_5) do |eeml|
74
+ eeml.environment(:updated => updated.iso8601, :id => id, :creator => "http://www.haque.co.uk") do |environment|
75
+ environment.title title unless title.blank?
76
+ environment.feed "#{feed}.xml" unless feed.blank?
77
+ environment.status status unless status.blank?
78
+ environment.description description unless description.blank?
79
+ environment.icon icon unless icon.blank?
80
+ environment.website website unless website.blank?
81
+ environment.email email unless email.blank?
82
+ environment.location({:disposition => location_disposition, :exposure => location_exposure, :domain => location_domain}.delete_if_nil_value) do |location|
83
+ location.name location_name
84
+ location.lat location_lat
85
+ location.lon location_lon
86
+ location.ele location_ele
87
+ end if location_disposition || location_exposure || location_domain || location_name || location_lat || location_lon || location_ele
88
+ datastreams.each do |ds|
89
+ environment.data(:id => ds.id) do |data|
90
+ parse_tag_string(ds.tags).each do |tag|
91
+ data.tag tag
92
+ end if ds.tags
93
+ data.value ds.current_value, {:minValue => ds.min_value, :maxValue => ds.max_value}.delete_if_nil_value
94
+ if ds.unit_symbol || ds.unit_type
95
+ units = {:type => ds.unit_type, :symbol => ds.unit_symbol}.delete_if_nil_value
96
+ end
97
+ data.unit ds.unit_label, units if !ds.unit_label.empty? || !units.empty?
98
+ end
99
+ end if datastreams
100
+ end
101
+ end
102
+ end
103
+ builder.to_xml
104
+ end
105
+
106
+ end
107
+ end
108
+ end
109
+ end
110
+
@@ -0,0 +1,116 @@
1
+ module Cosm
2
+ module Templates
3
+ module XML
4
+ module SearchResultDefaults
5
+ include XMLHeaders
6
+ include Helpers
7
+
8
+ def generate_xml(version)
9
+ case version
10
+ when "0.5.1"
11
+ xml_0_5_1
12
+ when "5"
13
+ xml_5
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ # As used by http://cosm.com/api/v2/feeds.xml
20
+ def xml_0_5_1
21
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
22
+ xml.eeml(_eeml_0_5_1.merge({"xmlns:opensearch" => "http://a9.com/-/spec/opensearch/1.1/"})) do |eeml|
23
+ eeml['opensearch'].totalResults totalResults
24
+ eeml['opensearch'].startIndex startIndex
25
+ eeml['opensearch'].itemsPerPage itemsPerPage
26
+ results.each do |env|
27
+ eeml.environment(:created => env.created.iso8601(6), :updated => env.updated.iso8601(6), :id => env.id, :creator => env.creator) do |environment|
28
+ environment.title env.title
29
+ environment.feed "#{env.feed}.xml"
30
+ environment.status env.status
31
+ environment.description env.description
32
+ environment.icon env.icon
33
+ environment.website env.website
34
+ environment.email env.email
35
+ environment.private_ env.private
36
+ parse_tag_string(env.tags).each do |tag|
37
+ environment.tag tag
38
+ end if env.tags
39
+ environment.location({:disposition => env.location_disposition, :exposure => env.location_exposure, :domain => env.location_domain}.delete_if_nil_value) do |location|
40
+ location.name env.location_name
41
+ location.lat env.location_lat
42
+ location.lon env.location_lon
43
+ location.ele env.location_ele
44
+ end if env.location_disposition || env.location_exposure || env.location_domain || env.location_name || env.location_lat || env.location_lon || env.location_ele
45
+ env.datastreams.each do |ds|
46
+ environment.data(:id => ds.id) do |data|
47
+ parse_tag_string(ds.tags).each do |tag|
48
+ data.tag tag
49
+ end if ds.tags
50
+ data.current_value ds.current_value, :at => ds.updated.iso8601(6)
51
+ data.max_value ds.max_value if ds.max_value
52
+ data.min_value ds.min_value if ds.min_value
53
+ if ds.unit_symbol || ds.unit_type
54
+ units = {:type => ds.unit_type, :symbol => ds.unit_symbol}.delete_if_nil_value
55
+ end
56
+ data.unit ds.unit_label, units if !ds.unit_label.empty? || !units.empty?
57
+ data.datapoints do
58
+ ds.datapoints.each do |datapoint|
59
+ data.value(datapoint.value, "at" => datapoint.at.iso8601(6))
60
+ end
61
+ end if ds.datapoints.any?
62
+ end
63
+ end if env.datastreams
64
+ end
65
+ end
66
+ end
67
+ end
68
+ builder.to_xml
69
+ end
70
+
71
+ # As used by http://cosm.com/api/v1/feeds.xml
72
+ def xml_5
73
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
74
+ xml.eeml(_eeml_5.merge({"xmlns:opensearch" => "http://a9.com/-/spec/opensearch/1.1/"})) do |eeml|
75
+ eeml['opensearch'].totalResults totalResults
76
+ eeml['opensearch'].startIndex startIndex
77
+ eeml['opensearch'].itemsPerPage itemsPerPage
78
+ results.each do |env|
79
+ eeml.environment(:updated => env.updated.iso8601, :id => env.id, :creator => "http://www.haque.co.uk") do |environment|
80
+ environment.title env.title
81
+ environment.feed "#{env.feed}.xml"
82
+ environment.status env.status
83
+ environment.description env.description
84
+ environment.icon env.icon
85
+ environment.website env.website
86
+ environment.email env.email
87
+ environment.location({:disposition => env.location_disposition, :exposure => env.location_exposure, :domain => env.location_domain}.delete_if_nil_value) do |location|
88
+ location.name env.location_name
89
+ location.lat env.location_lat
90
+ location.lon env.location_lon
91
+ location.ele env.location_ele
92
+ end
93
+ env.datastreams.each do |ds|
94
+ environment.data(:id => ds.id) do |data|
95
+ parse_tag_string(ds.tags).each do |tag|
96
+ data.tag tag
97
+ end if ds.tags
98
+ data.value ds.current_value, {:minValue => ds.min_value, :maxValue => ds.max_value}.delete_if_nil_value
99
+ if ds.unit_symbol || ds.unit_type
100
+ units = {:type => ds.unit_type, :symbol => ds.unit_symbol}.delete_if_nil_value
101
+ end
102
+ data.unit ds.unit_label, units if !ds.unit_label.empty? || !units.empty?
103
+ end
104
+ end if env.datastreams
105
+ end
106
+ end
107
+ end
108
+ end
109
+ builder.to_xml
110
+ end
111
+
112
+ end
113
+ end
114
+ end
115
+ end
116
+
@@ -0,0 +1,17 @@
1
+ module Cosm
2
+ module Templates
3
+ module XMLHeaders
4
+
5
+ private
6
+
7
+ def _eeml_0_5_1
8
+ {: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"}
9
+ end
10
+
11
+ def _eeml_5
12
+ {: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"}
13
+ end
14
+
15
+ end
16
+ end
17
+ end