cosm-rb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,18 @@
1
+ module Cosm
2
+ module Parsers
3
+ module JSON
4
+ module SearchResultDefaults
5
+ include FeedDefaults
6
+
7
+ def from_json(json)
8
+ hash = ::JSON.parse(json)
9
+ hash['results'] = hash['results'].collect do |feed|
10
+ transform_1_0_0(feed)
11
+ end
12
+ hash
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ module Cosm
2
+ module Parsers
3
+ module JSON
4
+ module TriggerDefaults
5
+ def from_json(json)
6
+ ::JSON.parse(json)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,20 @@
1
+ module Cosm
2
+ module Parsers
3
+ module XML
4
+ module DatapointDefaults
5
+ def from_xml(xml)
6
+ xml = Nokogiri.parse(xml)
7
+ hash = {}
8
+ environment = xml.at_xpath("//xmlns:environment")
9
+ data = environment.at_xpath("xmlns:data")
10
+ datapoint = data.at_xpath("xmlns:datapoints")
11
+ value = datapoint.at_xpath("xmlns:value")
12
+ hash["value"] = value.content
13
+ hash["at"] = value.attributes["at"].value
14
+ hash
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,77 @@
1
+ module Cosm
2
+ module Parsers
3
+ module XML
4
+ module DatastreamDefaults
5
+ def from_xml(xml)
6
+ xml = Nokogiri.parse(xml)
7
+ case xml.root.attributes["version"].value
8
+ when "0.5.1"
9
+ transform_0_5_1(xml)
10
+ else # "5"
11
+ transform_5(xml)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ # As produced by http://cosm.com/api/v2/FEED_ID/datastreams/DATASTREAM_ID.xml
18
+ def transform_0_5_1(xml)
19
+ hash = {}
20
+ environment = xml.at_xpath("//xmlns:environment")
21
+ data = environment.at_xpath("xmlns:data")
22
+ hash["feed_id"] = environment.attributes["id"].value
23
+ hash["feed_creator"] = environment.attributes["creator"].value
24
+ hash["id"] = data.attributes["id"].value
25
+ if (tags = data.xpath("xmlns:tag").collect(&:content)).any?
26
+ hash["tags"] = tags.sort{|a,b| a.downcase <=> b.downcase}.join(',')
27
+ end
28
+ current_value = data.at_xpath("xmlns:current_value")
29
+ hash["current_value"] = current_value.content
30
+ hash["updated"] = current_value.attributes["at"].value
31
+ hash["min_value"] = data.at_xpath("xmlns:min_value").content
32
+ hash["max_value"] = data.at_xpath("xmlns:max_value").content
33
+ unit = data.at_xpath("xmlns:unit")
34
+ if unit
35
+ hash["unit_label"] = unit.content
36
+ hash["unit_symbol"] = unit.attributes["symbol"].value
37
+ hash["unit_type"] = unit.attributes["type"].value
38
+ end
39
+ hash["datapoints"] = data.xpath("xmlns:datapoints").collect do |datapoint|
40
+ value = datapoint.at_xpath("xmlns:value")
41
+ {
42
+ "value" => value.content,
43
+ "at" => value.attributes["at"].content
44
+ }
45
+ end
46
+ hash
47
+ end
48
+
49
+ # As produced by http://cosm.com/api/v1/FEED_ID/datastreams/DATASTREAM_ID.xml
50
+ def transform_5(xml)
51
+ hash = {}
52
+ environment = xml.at_xpath("//xmlns:environment")
53
+ data = environment.at_xpath("xmlns:data")
54
+ hash["feed_id"] = environment.attributes["id"].value
55
+ hash["feed_creator"] = "http://www.haque.co.uk"
56
+ hash["updated"] = environment.attributes["updated"].value
57
+ hash["id"] = data.attributes["id"].value
58
+ if (tags = data.xpath("xmlns:tag").collect(&:content)).any?
59
+ hash["tags"] = tags.sort{|a,b| a.downcase <=> b.downcase}.join(',')
60
+ end
61
+ current_value = data.at_xpath("xmlns:value")
62
+ hash["current_value"] = current_value.content
63
+ hash["min_value"] = current_value.attributes["minValue"].value
64
+ hash["max_value"] = current_value.attributes["maxValue"].value
65
+ unit = data.at_xpath("xmlns:unit")
66
+ if unit
67
+ hash["unit_label"] = unit.content
68
+ hash["unit_symbol"] = unit.attributes["symbol"].value
69
+ hash["unit_type"] = unit.attributes["type"].value
70
+ end
71
+ hash
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+
@@ -0,0 +1,127 @@
1
+ module Cosm
2
+ module Parsers
3
+ module XML
4
+ module FeedDefaults
5
+ def from_xml(xml)
6
+ xml = Nokogiri.parse(xml)
7
+ case xml.root.attributes["version"].value
8
+ when "0.5.1"
9
+ transform_0_5_1(xml)
10
+ else # "5"
11
+ transform_5(xml)
12
+ end
13
+ end
14
+
15
+ # As produced by http://cosm.com/api/v2/FEED_ID.xml
16
+ def transform_0_5_1(xml)
17
+ hash = {}
18
+ environment = xml.at_xpath("//xmlns:environment")
19
+ hash["updated"] = environment.attributes["updated"].value
20
+ hash["created"] = environment.attributes["created"].value
21
+ hash["creator"] = environment.attributes["creator"].value
22
+ hash["title"] = environment.at_xpath("xmlns:title").content
23
+ hash["feed"] = environment.at_xpath("xmlns:feed").content
24
+ hash["auto_feed_url"] = environment.at_xpath("xmlns:auto_feed_url").content
25
+ hash["status"] = environment.at_xpath("xmlns:status").content
26
+ hash["description"] = environment.at_xpath("xmlns:description").content
27
+ hash["icon"] = environment.at_xpath("xmlns:icon").content
28
+ hash["website"] = environment.at_xpath("xmlns:website").content
29
+ hash["email"] = environment.at_xpath("xmlns:email").content
30
+ hash["private"] = environment.at_xpath("xmlns:private").content
31
+ hash["tags"] = environment.xpath("xmlns:tag").collect(&:content).sort{|a,b| a.downcase <=> b.downcase}.join(',')
32
+ location = environment.at_xpath("xmlns:location")
33
+ if location
34
+ hash["location_name"] = location.at_xpath("xmlns:name").content
35
+ hash["location_lat"] = location.at_xpath("xmlns:lat").content
36
+ hash["location_lon"] = location.at_xpath("xmlns:lon").content
37
+ hash["location_ele"] = location.at_xpath("xmlns:ele").content
38
+ hash["location_domain"] = location.attributes["domain"].value
39
+ hash["location_exposure"] = location.attributes["exposure"].value
40
+ hash["location_disposition"] = location.attributes["disposition"].value
41
+ end
42
+ owner = environment.at_xpath("xmlns:user")
43
+ if owner
44
+ hash["owner_login"] = owner.at_xpath("xmlns:login").content
45
+ end
46
+ hash["datastreams"] = environment.xpath("xmlns:data").collect do |datastream|
47
+ current_value = datastream.at_xpath("xmlns:current_value")
48
+ unit = datastream.at_xpath("xmlns:unit")
49
+ if unit
50
+ unit_hash = {
51
+ "unit_label" => unit.content,
52
+ "unit_type" => unit.attributes["type"].value,
53
+ "unit_symbol" => unit.attributes["symbol"].value,
54
+ }
55
+ else
56
+ unit_hash = {}
57
+ end
58
+ {
59
+ "id" => datastream.attributes["id"].value,
60
+ "tags" => datastream.xpath("xmlns:tag").collect(&:content).sort{|a,b| a.downcase <=> b.downcase}.join(','),
61
+ "current_value" => current_value.content,
62
+ "updated" => current_value.attributes["at"].value,
63
+ "min_value" => datastream.at_xpath("xmlns:min_value").content,
64
+ "max_value" => datastream.at_xpath("xmlns:max_value").content,
65
+ "datapoints" => datastream.xpath("xmlns:datapoints").collect do |datapoint|
66
+ value = datapoint.at_xpath("xmlns:value")
67
+ {
68
+ "at" => value.attributes["at"].value,
69
+ "value" => value.content,
70
+ }
71
+ end
72
+ }.merge(unit_hash)
73
+ end
74
+ hash
75
+ end
76
+
77
+ # As produced by http://cosm.com/api/v1/FEED_ID.xml
78
+ def transform_5(xml)
79
+ hash = {}
80
+ environment = xml.at_xpath("//xmlns:environment")
81
+ hash["updated"] = environment.attributes["updated"].value
82
+ hash["creator"] = "http://www.haque.co.uk"
83
+ hash["title"] = environment.at_xpath("xmlns:title").content
84
+ hash["feed"] = environment.at_xpath("xmlns:feed").content
85
+ hash["status"] = environment.at_xpath("xmlns:status").content
86
+ hash["description"] = environment.at_xpath("xmlns:description").content
87
+ hash["icon"] = environment.at_xpath("xmlns:icon").content
88
+ hash["website"] = environment.at_xpath("xmlns:website").content
89
+ hash["email"] = environment.at_xpath("xmlns:email").content
90
+ location = environment.at_xpath("xmlns:location")
91
+ if location
92
+ hash["location_name"] = location.at_xpath("xmlns:name").content
93
+ hash["location_lat"] = location.at_xpath("xmlns:lat").content
94
+ hash["location_lon"] = location.at_xpath("xmlns:lon").content
95
+ hash["location_ele"] = location.at_xpath("xmlns:ele").content
96
+ hash["location_domain"] = location.attributes["domain"].value
97
+ hash["location_exposure"] = location.attributes["exposure"].value
98
+ hash["location_disposition"] = location.attributes["disposition"].value
99
+ end
100
+ hash["datastreams"] = environment.xpath("xmlns:data").collect do |datastream|
101
+ current_value = datastream.at_xpath("xmlns:value")
102
+ unit = datastream.at_xpath("xmlns:unit")
103
+ if unit
104
+ unit_hash = {
105
+ "unit_label" => unit.content,
106
+ "unit_type" => unit.attributes["type"].value,
107
+ "unit_symbol" => unit.attributes["symbol"].value,
108
+ }
109
+ else
110
+ unit_hash = {}
111
+ end
112
+ {
113
+ "id" => datastream.attributes["id"].value,
114
+ "tags" => datastream.xpath("xmlns:tag").collect(&:content).sort{|a,b| a.downcase <=> b.downcase}.join(','),
115
+ "current_value" => current_value.content,
116
+ "updated" => environment.attributes["updated"].value,
117
+ "min_value" => current_value.attributes["minValue"].value,
118
+ "max_value" => current_value.attributes["maxValue"].value,
119
+ }.merge(unit_hash)
120
+ end
121
+ hash
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+
@@ -0,0 +1,40 @@
1
+ module Cosm
2
+ module Parsers
3
+ module XML
4
+ module KeyDefaults
5
+ def from_xml(xml)
6
+ xml = Nokogiri.parse(xml)
7
+ hash = {}
8
+ hash["id"] = xml.at_xpath("//id").content if xml.at_xpath("//id")
9
+ hash["expires_at"] = xml.at_xpath("//expires-at").content if xml.at_xpath("//expires-at")
10
+ hash["key"] = xml.at_xpath("//api-key").content if xml.at_xpath("//api-key")
11
+ hash["label"] = xml.at_xpath("//label").content if xml.at_xpath("//label")
12
+ hash["user"] = xml.at_xpath("//user").content if xml.at_xpath("//user")
13
+ hash["private_access"] = xml.at_xpath("//private-access").content if xml.at_xpath("//private-access")
14
+
15
+ hash["permissions"] = xml.xpath("//key/permissions/permission").collect { |permission|
16
+ access_methods = permission.xpath("access-methods/access-method").collect { |method|
17
+ method.content.to_s.downcase
18
+ }
19
+ resources = permission.xpath("resources/resource").collect { |resource|
20
+ { "feed_id" => resource.at_xpath("feed-id").content,
21
+ "datastream_id" => resource.at_xpath("datastream-id").content,
22
+ "datastream_trigger_id" => resource.at_xpath("datastream-trigger-id").content
23
+ }.delete_if_nil_value
24
+ }
25
+ {
26
+ "referer" => permission.at_xpath("referer").content,
27
+ "source_ip" => permission.at_xpath("source-ip").content,
28
+ "private_access" => permission.at_xpath("private-access").content,
29
+ "access_methods" => access_methods,
30
+ "resources" => resources
31
+ }.delete_if_nil_value
32
+ }
33
+
34
+ hash
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+
@@ -0,0 +1,22 @@
1
+ module Cosm
2
+ module Parsers
3
+ module XML
4
+ module TriggerDefaults
5
+ def from_xml(xml)
6
+ xml = Nokogiri.parse(xml)
7
+ hash = {}
8
+ hash["id"] = xml.at_xpath("//id").content if xml.at_xpath("//id")
9
+ hash["url"] = xml.at_xpath("//url").content if xml.at_xpath("//url")
10
+ hash["trigger_type"] = xml.at_xpath("//trigger-type").content if xml.at_xpath("//trigger-type")
11
+ hash["threshold_value"] = xml.at_xpath("//threshold-value").content if xml.at_xpath("//threshold-value")
12
+ hash["notified_at"] = xml.at_xpath("//notified-at").content if xml.at_xpath("//notified-at")
13
+ hash["user"] = xml.at_xpath("//user").content if xml.at_xpath("//user")
14
+ hash["environment_id"] = xml.at_xpath("//environment-id").content if xml.at_xpath("//environment-id")
15
+ hash["stream_id"] = xml.at_xpath("//stream-id").content if xml.at_xpath("//stream-id")
16
+ hash
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,67 @@
1
+ module Cosm
2
+ class Permission
3
+ ALLOWED_KEYS = %w(label access_methods referer source_ip minimum_interval resources)
4
+ ALLOWED_KEYS.each { |key| attr_accessor(key.to_sym) }
5
+ NESTED_KEYS = %w(resources)
6
+
7
+ include Validations
8
+
9
+ def valid?
10
+ pass = true
11
+ if access_methods.nil? || access_methods.empty?
12
+ errors[:access_methods] = ["can't be blank"]
13
+ pass = false
14
+ end
15
+
16
+ resources.each do |resource|
17
+ unless resource.valid?
18
+ resource.errors.each do |attr, resource_errors|
19
+ errors["resources_#{attr}".to_sym] = ([*errors["resources_#{attr}".to_sym]] | [*resource_errors]).compact
20
+ end
21
+ pass = false
22
+ end
23
+ end
24
+
25
+ return pass
26
+ end
27
+
28
+ # Build an instance from a Hash only
29
+ def initialize(input = {})
30
+ self.attributes = input
31
+ end
32
+
33
+ def resources
34
+ return [] if @resources.nil?
35
+ @resources
36
+ end
37
+
38
+ def resources=(array)
39
+ return unless array.is_a?(Array)
40
+ @resources = []
41
+ array.each do |resource|
42
+ if resource.is_a?(Resource)
43
+ @resources << resource
44
+ elsif resource.is_a?(Hash)
45
+ @resources << Resource.new(resource)
46
+ end
47
+ end
48
+ end
49
+
50
+ def attributes
51
+ h = {}
52
+ ALLOWED_KEYS.each do |key|
53
+ value = self.send(key)
54
+ h[key] = value unless value.nil?
55
+ end
56
+ return h
57
+ end
58
+
59
+ def attributes=(input)
60
+ return if input.nil?
61
+ input.deep_stringify_keys!
62
+ ALLOWED_KEYS.each { |key| self.send("#{key}=", input[key]) }
63
+ NESTED_KEYS.each { |key| self.send("#{key}=", input["#{key}_attributes"]) unless input["#{key}_attributes"].nil? }
64
+ return attributes
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,43 @@
1
+ module Cosm
2
+ class Resource
3
+ ALLOWED_KEYS = %w(feed_id datastream_id datastream_trigger_id)
4
+ ALLOWED_KEYS.each { |key| attr_accessor(key.to_sym) }
5
+
6
+ include Validations
7
+
8
+ def valid?
9
+ pass = true
10
+ if feed_id.blank? && datastream_id.blank? && datastream_trigger_id.blank?
11
+ errors[:feed_id] = "Must supply at least one of feed_id (optionally with a datastream_id) or datastream_trigger_id"
12
+ pass = false
13
+ end
14
+
15
+ if feed_id.blank? && !datastream_id.blank?
16
+ errors[:feed_id] = ["can't be blank if we have supplied a datastream_id"]
17
+ pass = false
18
+ end
19
+
20
+ return pass
21
+ end
22
+
23
+ def initialize(input = {})
24
+ self.attributes = input
25
+ end
26
+
27
+ def attributes
28
+ h = {}
29
+ ALLOWED_KEYS.each do |key|
30
+ value = self.send(key)
31
+ h[key] = value unless value.nil?
32
+ end
33
+ return h
34
+ end
35
+
36
+ def attributes=(input)
37
+ return if input.nil?
38
+ input.deep_stringify_keys!
39
+ ALLOWED_KEYS.each { |key| self.send("#{key}=", input[key]) }
40
+ return attributes
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,63 @@
1
+ module Cosm
2
+ class SearchResult
3
+ ALLOWED_KEYS = %w(totalResults startIndex itemsPerPage results)
4
+ ALLOWED_KEYS.each { |key| attr_accessor(key.to_sym) }
5
+
6
+ include Cosm::Templates::JSON::SearchResultDefaults
7
+ include Cosm::Templates::XML::SearchResultDefaults
8
+ include Cosm::Parsers::JSON::SearchResultDefaults
9
+
10
+ @@feed_class = Cosm::Feed
11
+
12
+ def initialize(input = {})
13
+ if input.is_a?(Hash)
14
+ self.attributes = input
15
+ elsif input.strip[0...1].to_s == "{"
16
+ self.attributes = from_json(input)
17
+ end
18
+ end
19
+
20
+ def attributes
21
+ h = {}
22
+ ALLOWED_KEYS.each do |key|
23
+ value = self.send(key)
24
+ h[key] = value unless value.nil?
25
+ end
26
+ return h
27
+ end
28
+
29
+ def attributes=(input)
30
+ return if input.nil?
31
+ ALLOWED_KEYS.each { |key| self.send("#{key}=", input[key]) }
32
+ return attributes
33
+ end
34
+
35
+ def results=(array)
36
+ return unless array.is_a?(Array)
37
+ @results = []
38
+ array.each do |feed|
39
+ if feed.is_a?(@@feed_class)
40
+ @results << feed
41
+ elsif feed.is_a?(Hash)
42
+ @results << @@feed_class.new(feed)
43
+ end
44
+ end
45
+ end
46
+
47
+ def as_json(options = {})
48
+ options[:version] ||= "1.0.0"
49
+ generate_json(options[:version])
50
+ end
51
+
52
+ def to_json(options = {})
53
+ ::JSON.generate as_json(options)
54
+ end
55
+
56
+ def to_xml(options = {})
57
+ options[:version] ||= "0.5.1"
58
+ generate_xml(options[:version])
59
+ end
60
+
61
+ end
62
+ end
63
+
@@ -0,0 +1,6 @@
1
+ class String
2
+ def iso8601(precision = nil)
3
+ self
4
+ end
5
+ end
6
+
@@ -0,0 +1,32 @@
1
+ module Cosm
2
+ class Template
3
+ attr_accessor :subject
4
+ attr_accessor :presentation
5
+ attr_accessor :output
6
+
7
+ def initialize(subject, presentation, &block)
8
+ @subject = subject
9
+ @presentation = presentation
10
+ @output = {}
11
+ end
12
+
13
+ def method_missing(sym, *args, &block)
14
+ if block_given?
15
+ @output[sym] = @subject.instance_eval &block
16
+ else
17
+ @output[sym] = @subject.send(sym)
18
+ end
19
+ rescue NameError, NoMethodError => e
20
+ @output[sym] = nil
21
+ end
22
+
23
+ def id
24
+ method_missing(:id)
25
+ end
26
+
27
+ def output!(options = {})
28
+ !options[:include_blank] ? @output.reject {|k,v| v.nil? || v.blank?} : @output
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,22 @@
1
+ module Cosm
2
+ module Templates
3
+ module CSV
4
+ module DatapointDefaults
5
+ def generate_csv(version, options = {})
6
+ options[:depth] = 4 if options[:full]
7
+ case options[:depth].to_i
8
+ when 4
9
+ ::CSV.generate_line([feed_id, datastream_id, at.iso8601(6), value]).strip
10
+ when 3
11
+ ::CSV.generate_line([datastream_id, at.iso8601(6), value]).strip
12
+ when 2
13
+ ::CSV.generate_line([at.iso8601(6), value]).strip
14
+ else
15
+ ::CSV.generate_line([value]).strip
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,43 @@
1
+ module Cosm
2
+ module Templates
3
+ module CSV
4
+ module DatastreamDefaults
5
+ def generate_csv(version, options = {})
6
+ case version
7
+ when "2"
8
+ csv_2(options)
9
+ when "1"
10
+ csv_1
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def csv_2(options)
17
+ csv = []
18
+ options[:depth] = 4 if options[:full]
19
+ case options[:depth].to_i
20
+ when 4
21
+ datapoints.collect {|datapoint| csv << [feed_id, id, datapoint.at.iso8601(6), datapoint.value] }
22
+ csv << [feed_id, id, updated.iso8601(6), current_value] if csv.empty?
23
+ when 3
24
+ datapoints.collect {|datapoint| csv << [id, datapoint.at.iso8601(6), datapoint.value] }
25
+ csv << [id, updated.iso8601(6), current_value] if csv.empty?
26
+ when 1
27
+ datapoints.collect {|datapoint| csv << [datapoint.value] }
28
+ csv << [current_value] if csv.empty?
29
+ else
30
+ datapoints.collect {|datapoint| csv << [datapoint.at.iso8601(6), datapoint.value] }
31
+ csv << [updated.iso8601(6), current_value] if csv.empty?
32
+ end
33
+ csv.collect {|row| ::CSV.generate_line(row).strip }.join("\n")
34
+ end
35
+
36
+ def csv_1
37
+ ::CSV.generate_line([current_value]).strip
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+
@@ -0,0 +1,47 @@
1
+ module Cosm
2
+ module Templates
3
+ module CSV
4
+ module FeedDefaults
5
+ def generate_csv(version, options = {})
6
+ case version
7
+ when "2"
8
+ csv_2(options)
9
+ when "1"
10
+ csv_1
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def csv_2(options)
17
+ csv = []
18
+ if options[:full]
19
+ datastreams.collect do |datastream|
20
+ if datastream.datapoints.any?
21
+ datastream.datapoints.collect { |datapoint| csv << ::CSV.generate_line([id, datastream.id, datapoint.at.iso8601(6), datapoint.value]).strip }
22
+ else
23
+ csv << ::CSV.generate_line([id, datastream.id, datastream.updated.iso8601(6), datastream.current_value]).strip
24
+ end
25
+
26
+
27
+ end
28
+ else
29
+ datastreams.collect do |datastream|
30
+ if datastream.datapoints.any?
31
+ datastream.datapoints.collect { |datapoint| csv << ::CSV.generate_line([datastream.id, datapoint.at.iso8601(6), datapoint.value]).strip }
32
+ else
33
+ csv << ::CSV.generate_line([datastream.id, datastream.updated.iso8601(6), datastream.current_value]).strip
34
+ end
35
+ end
36
+ end
37
+ csv.join("\n")
38
+ end
39
+
40
+ def csv_1
41
+ ::CSV.generate_line(datastreams.collect {|ds| ds.current_value }).strip
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,14 @@
1
+ require 'cosm-rb/templates/xml_headers'
2
+ require 'cosm-rb/templates/json/feed_defaults'
3
+ require 'cosm-rb/templates/json/datastream_defaults'
4
+ require 'cosm-rb/templates/json/datapoint_defaults'
5
+ require 'cosm-rb/templates/json/trigger_defaults'
6
+ require 'cosm-rb/templates/json/search_result_defaults'
7
+ require 'cosm-rb/templates/json/key_defaults'
8
+ require 'cosm-rb/templates/xml/feed_defaults'
9
+ require 'cosm-rb/templates/xml/datastream_defaults'
10
+ require 'cosm-rb/templates/xml/datapoint_defaults'
11
+ require 'cosm-rb/templates/xml/search_result_defaults'
12
+ require 'cosm-rb/templates/csv/datapoint_defaults'
13
+ require 'cosm-rb/templates/csv/datastream_defaults'
14
+ require 'cosm-rb/templates/csv/feed_defaults'