eeml 0.0.21 → 0.0.22

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.
data/.gitignore CHANGED
@@ -1,5 +1,7 @@
1
1
  pkg
2
+ coverage/
2
3
  doc
3
4
  eeml.gemspec
4
5
  client_config.yml
6
+ coverage/
5
7
  .*.swp
data/CHANGELOG CHANGED
@@ -1,3 +1,14 @@
1
+ commit 57a5f081eed2a218960c3bcc465edd3d586ebd2e
2
+ Author: Levent Ali <lebreeze@gmail.com>
3
+ Date: Thu Sep 16 16:47:55 2010 +0100
4
+
5
+ Version 0.0.22
6
+
7
+ Eeml::Environment.new_from_csv implemented
8
+ Added CSV parsers for v1 and v2
9
+ Exception raising in CSV parsers
10
+ Ripped out output rendering (to_json, to_eeml)
11
+
1
12
  commit 4c88cb1fe13661dbba20fca4877491c54a265a2a
2
13
  Author: Paul Bellamy <paul@connectedenvironments.com>
3
14
  Date: Fri Aug 20 19:12:34 2010 +0100
data/Manifest CHANGED
@@ -7,16 +7,14 @@ example.rb
7
7
  lib/blank.rb
8
8
  lib/eeml.rb
9
9
  lib/eeml/constants.rb
10
- lib/eeml/csv_parser.rb
10
+ lib/eeml/csv_parser_v1.rb
11
+ lib/eeml/csv_parser_v2.rb
11
12
  lib/eeml/environment.rb
12
13
  lib/eeml/environment_builder.rb
13
14
  lib/eeml/exceptions.rb
14
15
  lib/eeml/json_environment_parser_v005.rb
15
16
  lib/eeml/json_environment_parser_v006.rb
16
- lib/eeml/json_output.rb
17
- lib/eeml/libxml_eeml_output_v005.rb
18
17
  lib/eeml/libxml_eeml_parser_v005.rb
19
- lib/eeml/output_registry.rb
20
18
  schemas/eeml/005.xsd
21
19
  test/data/complete_namespaced.xml
22
20
  test/data/difficult_tag.xml
data/Rakefile CHANGED
@@ -14,6 +14,7 @@ begin
14
14
  gem.version = Eeml::VERSION
15
15
  gem.add_dependency("libxml-ruby", ">=1.1.3")
16
16
  gem.add_dependency("json", ">=1.4.3")
17
+ gem.add_dependency("lightcsv", ">=0.2.2")
17
18
  gem.files.exclude("test/data/real_xmls/*")
18
19
  end
19
20
  rescue LoadError
@@ -0,0 +1,17 @@
1
+ module Eeml
2
+ class CsvParserV1
3
+
4
+ def self.make_environment_from_csv(csv_content)
5
+ csv = LightCsv.parse(csv_content)
6
+ environment = Environment.new
7
+
8
+ raise(CsvEncodingError, "Currently Pachube can only accept csv for your most recent set of values. You have submitted #{csv.size} rows of data.") unless csv.size == 1
9
+
10
+ csv.first.each do |datastream_value|
11
+ environment.datastreams << DataStream.new(:value => datastream_value)
12
+ end
13
+ environment
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ class CsvEncodingError < StandardError ; end
2
+
3
+ module Eeml
4
+ class CsvParserV2
5
+
6
+ def self.make_environment_from_csv(csv_content)
7
+ csv = LightCsv.parse(csv_content)
8
+ environment = Environment.new
9
+
10
+ csv.each do |datastream|
11
+ raise(CsvEncodingError, "CSV is invalid. Double check you are properly encoding line breaks.") if datastream.length == 3 && datastream[1].include?('\n')
12
+ raise(CsvEncodingError, "CSV is invalid. Incorrect number of fields.") if datastream.length != 2
13
+ environment.datastreams << DataStream.new(:value => datastream.last, :identifier => datastream.first)
14
+ end
15
+ environment
16
+ end
17
+
18
+ end
19
+ end
@@ -130,17 +130,22 @@ module Eeml
130
130
 
131
131
  # Create multiple Environment objects from an EEML string containing multiple `environment` stanzas.
132
132
  def self.new_list_from_eeml(xml_str)
133
- return EnvironmentBuilder.build_list_from_xml(xml_str)
133
+ EnvironmentBuilder.build_list_from_xml(xml_str)
134
134
  end
135
135
 
136
136
  # Create a new Environment object from an EEML string.
137
137
  def self.new_from_eeml(xml_str)
138
- return EnvironmentBuilder.build_from_xml(xml_str)
138
+ EnvironmentBuilder.build_from_xml(xml_str)
139
139
  end
140
140
 
141
141
  # Create a new Environment object from a JSON string.
142
142
  def self.new_from_json(json_str)
143
- return EnvironmentBuilder.build_from_json(json_str)
143
+ EnvironmentBuilder.build_from_json(json_str)
144
+ end
145
+
146
+ # Create a new Environment object from a CSV string
147
+ def self.new_from_csv(csv_str, version)
148
+ EnvironmentBuilder.build_from_csv(csv_str, version)
144
149
  end
145
150
 
146
151
  def update_datastreams_from_csv_values!(csv_values)
@@ -166,16 +171,6 @@ module Eeml
166
171
  end
167
172
  end
168
173
 
169
- def to_eeml(version = Constants::EEML['0.5.0'][:version])
170
- outputter = OutputRegistry.get_xml_output_for(version)
171
- outputter.to_eeml(self)
172
- end
173
-
174
- def to_json
175
- outputter = OutputRegistry.get_json_output_for
176
- outputter.to_json(self)
177
- end
178
-
179
174
  def private
180
175
  if @private
181
176
  return true
@@ -1,3 +1,5 @@
1
+ require 'eeml/csv_parser_v1'
2
+ require 'eeml/csv_parser_v2'
1
3
  require 'eeml/libxml_eeml_parser_v005'
2
4
  require 'eeml/libxml_eeml_parser_v051'
3
5
  require 'eeml/json_environment_parser_v005'
@@ -41,6 +43,15 @@ module Eeml
41
43
  end
42
44
  return parser.make_environment_from_hash(json)
43
45
  end
46
+
47
+ def self.build_from_csv(csv_str, version = :v1)
48
+ if version == :v2
49
+ parser = CsvParserV2
50
+ else
51
+ parser = CsvParserV1
52
+ end
53
+ parser.make_environment_from_csv(csv_str)
54
+ end
44
55
  end
45
56
  end
46
57
 
@@ -49,7 +49,7 @@ module Eeml
49
49
  return if datastreams_hash.nil?
50
50
  datastreams = []
51
51
  datastreams_hash.each do |datastream_hash|
52
- datastream = DataStream.new
52
+ datastream = DataStream.new
53
53
  raise MissingAttribute.new('id', "data") if datastream_hash['id'].nil?
54
54
  datastream.identifier = datastream_hash['id']
55
55
  datastream.tags = datastream_hash['tags'] unless datastream_hash['tags'].nil?
@@ -68,7 +68,7 @@ module Eeml
68
68
  def extract_environment_from_node(env_node, env_to_populate)
69
69
  env = env_to_populate
70
70
  env.identifier = env_node['id']
71
- env.updated = Time.gm(*ParseDate.parsedate(env_node['updated'])).utc if !env_node['updated'].nil?
71
+ env.updated = Time.parse(env_node['updated']) if !env_node['updated'].nil?
72
72
 
73
73
  env.creator = env_node['creator']
74
74
 
@@ -163,6 +163,7 @@ module Eeml
163
163
  value.min_value = min_value_node.content if min_value_node
164
164
  value.max_value = max_value_node.content if max_value_node
165
165
  value.value = value_node.content.strip
166
+ value.recorded_at = value_node['at'] unless value_node['at'].blank?
166
167
 
167
168
  data.add_value(value)
168
169
 
data/lib/eeml.rb CHANGED
@@ -5,17 +5,17 @@ $:.unshift(File.dirname(__FILE__))
5
5
  require 'blank'
6
6
  require 'libxml'
7
7
  require 'json'
8
+ require 'lightcsv'
8
9
  require 'eeml/constants'
9
10
  require 'eeml/exceptions'
10
11
  require 'eeml/environment'
11
12
  require 'eeml/environment_builder'
12
- require 'eeml/output_registry'
13
13
  require 'time'
14
14
 
15
15
  module Eeml
16
16
 
17
17
  # library version number
18
- VERSION = '0.0.21'
18
+ VERSION = '0.0.22'
19
19
 
20
20
  # TODO: put in some configuration file, not here
21
21
  LOCAL_EEML_SCHEMA_LOCATION = 'schemas/eeml/005.xsd'
@@ -1,11 +1,39 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
2
  <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">
3
- <environment>
3
+ <environment updated="2010-09-23T09:54:57.873029Z" creator="http://www.pachube.com">
4
+ <title>title here</title>
5
+ <feed>http://appdev.loc:3000/api/feeds/.xml</feed>
6
+ <status>live</status>
7
+ <description>description here</description>
8
+ <icon>http://example.com/some/icon.gif</icon>
9
+ <website>http://example.com/studio/</website>
10
+ <email>someone@example.com</email>
11
+ <private>false</private>
12
+ <location domain="physical" exposure="outdoor" disposition="mobile">
13
+ <name>Up on the roof (somewhere)</name>
14
+ <lat>50.1</lat>
15
+ <lon>48.7</lon>
16
+ <ele>1.34</ele>
17
+ </location>
4
18
  <data id="0">
5
- <tag>sometag</tag>
6
- <current_value>65</current_value>
7
- <max_value>100.0</max_value>
8
- <min_value>4.0</min_value>
19
+ <tag>tagD0</tag>
20
+ <current_value at="2010-09-23T09:54:57.873029Z">0</current_value>
21
+ <max_value>1022.0</max_value>
22
+ <min_value>-9999.0</min_value>
23
+ <unit type="basicSI" symbol="C">Celsius</unit>
24
+ </data>
25
+ <data id="1">
26
+ <current_value at="2010-09-23T09:54:57.873029Z">33</current_value>
27
+ <max_value>1023.0</max_value>
28
+ <min_value>0.0</min_value>
29
+ </data>
30
+ <data id="2">
31
+ <tag>tagD2a</tag>
32
+ <tag>tagD2b</tag>
33
+ <tag>tagD2c</tag>
34
+ <current_value at="2010-09-23T09:54:57.873029Z">42.1</current_value>
35
+ <max_value>1021.0</max_value>
36
+ <min_value>23.4</min_value>
9
37
  </data>
10
38
  </environment>
11
39
  </eeml>
@@ -0,0 +1,22 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+ require 'lib/eeml/csv_parser_v2.rb'
3
+ class TestCsvParserV1 < Test::Unit::TestCase
4
+
5
+ test "should raise exception if too many rows" do
6
+ csv = "20,305\n30,1305"
7
+ exception = assert_raises CsvEncodingError do
8
+ CsvParserV1.make_environment_from_csv(csv)
9
+ end
10
+ assert_equal "Currently Pachube can only accept csv for your most recent set of values. You have submitted 2 rows of data.", exception.message
11
+ end
12
+
13
+ test "should parse and return Environment object with datastreams" do
14
+ csv = "20,10n5,fsa,432,6"
15
+ environment = CsvParserV1.make_environment_from_csv(csv)
16
+ assert_equal 5, environment.datastreams.length
17
+ csv.split(',').each_with_index do |value, index|
18
+ assert_nil environment.datastreams[index].identifier
19
+ assert_equal value, environment.datastreams[index].value
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+ require 'lib/eeml/csv_parser_v2.rb'
3
+ class TestCsvParserV2 < Test::Unit::TestCase
4
+
5
+ test "should raise exception if line breaks have dodgy characters" do
6
+ csv = "20,305\\n30,1305"
7
+ exception = assert_raises CsvEncodingError do
8
+ CsvParserV2.make_environment_from_csv(csv)
9
+ end
10
+ assert_equal "CSV is invalid. Double check you are properly encoding line breaks.", exception.message
11
+ end
12
+
13
+ test "should raise exception if too many fields" do
14
+ csv = "20,54,305\n30,1305"
15
+ exception = assert_raises CsvEncodingError do
16
+ CsvParserV2.make_environment_from_csv(csv)
17
+ end
18
+ assert_equal "CSV is invalid. Incorrect number of fields.", exception.message
19
+ end
20
+
21
+ test "should parse and return Environment object with datastreams" do
22
+ csv = "20,10\n5,6"
23
+ environment = CsvParserV2.make_environment_from_csv(csv)
24
+ assert_equal 2, environment.datastreams.length
25
+ assert_equal "20", environment.datastreams.first.identifier
26
+ assert_equal "10", environment.datastreams.first.value
27
+
28
+ assert_equal "5", environment.datastreams.last.identifier
29
+ assert_equal "6", environment.datastreams.last.value
30
+ end
31
+ end
@@ -587,101 +587,8 @@ class TestEnvironment < Test::Unit::TestCase
587
587
  assert_equal values, env.values_quick
588
588
  end
589
589
 
590
- # --- -----------------------------------------------------
591
- # --- eeml output stuff -----------------------------------------------------
592
- # --- -----------------------------------------------------
593
-
594
- test "should output to xml ok" do
595
- env = create_env_from_xml_file_one
596
- xml_output = env.to_eeml
597
- assert_not_nil xml_output
598
- #File.open('out.xml', 'w') {|f| f.write xml_output }
599
-
600
- expected_xml = File.read('test/data/doc_1.xml')
601
- assert_equal expected_xml, xml_output
602
- end
603
-
604
- test "should output to eeml 0.5.1 ok" do
605
- env = create_env_from_xml_file('eeml_datastream_051.xml')
606
- xml_output = env.to_eeml('0.5.1')
607
- assert_not_nil xml_output
608
-
609
- expected_xml = File.read('test/data/eeml_datastream_051.xml')
610
- assert_equal expected_xml, xml_output
611
-
612
- end
613
-
614
- implement "output - check that 'feed' url is correctly assembled" do
615
- end
616
-
617
- test "to_eeml of empty env should work" do
618
- env = Environment.new
619
- output_xml = env.to_eeml
620
- #TODO: Review this 'expected' xml document for when env's empty. I've cheated so far.
621
- # Perhaps we don't allow serialization at all when basics are absent.
622
- expected_xml = File.read('test/data/out_empty.xml')
623
- assert_equal expected_xml, output_xml
624
- end
625
-
626
- # --- -----------------------------------------------------
627
- # --- json output stuff -----------------------------------------------------
628
- # --- -----------------------------------------------------
629
- #DEV NOTE: use jsondiff tool to visual-diff expected and actual json http://tlrobinson.net/projects/js/jsondiff/
630
-
631
- #TODO: decide what trailing whitespace should be output after json, and eeml, and correct tests
632
- test "basic output in public json format" do
633
- input_xml = File.read('test/data/doc_2.xml')
634
- env = create_env_from_xml_string(input_xml)
635
- output_json = env.to_json
636
- expected_json = File.read('test/data/doc_2_expected.json').rstrip
637
- expected_hash = JSON.parse(expected_json)
638
- output_hash = JSON.parse(output_json)
639
- assert_json_hashes_equal(expected_hash, output_hash)
640
- end
641
-
642
- test "basic output in public json format - no location" do
643
- input_xml = File.read('test/data/doc_2.xml')
644
- input_xml = remove_first_node_called('location', input_xml)
645
- env = create_env_from_xml_string(input_xml)
646
- output_json = env.to_json
647
- output_hash = JSON.parse(output_json)
648
-
649
- expected_json = File.read('test/data/doc_2_expected.json')
650
- expected_hash = JSON.parse(expected_json)
651
- assert_not_nil expected_hash.delete("location")
652
- assert_json_hashes_equal(expected_hash, output_hash)
653
- end
654
-
655
- test "basic output in public json format - location but no latitude" do
656
- input_xml = File.read('test/data/doc_2.xml')
657
- input_xml = remove_first_node_called('lat', input_xml)
658
- env = create_env_from_xml_string(input_xml)
659
- actual_json = env.to_json
660
- actual_hash = JSON.parse(actual_json)
661
-
662
- assert_nil env.location.latitude
663
- expected_json = File.read('test/data/doc_2_expected.json')
664
- expected_hash = JSON.parse(expected_json)
665
- expected_hash["location"].delete("lat")
666
- #File.open('tmp_expected.json', 'w') {|f| f.write expected_hash.to_json }
667
- #File.open('tmp_actual.json', 'w') {|f| f.write actual_hash.to_json }
668
-
669
- assert_json_hashes_equal(expected_hash, actual_hash)
670
- end
671
-
672
- test "basic output in public json format - minimal env" do
673
- env = Environment.new
674
- assert_nil env.identifier
675
- output_json = env.to_json
676
- assert_not_nil output_json
677
- end
678
-
679
590
  # --- convenience stuff - don't put tests under here ------------------------------
680
591
 
681
-
682
-
683
-
684
-
685
592
  def create_env_from_xml_string(string)
686
593
  environment = Environment.new_from_eeml(string)
687
594
  assert_not_nil environment
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eeml
3
3
  version: !ruby/object:Gem::Version
4
- hash: 53
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 0
9
- - 21
10
- version: 0.0.21
8
+ - 22
9
+ version: 0.0.22
11
10
  platform: ruby
12
11
  authors:
13
12
  - Neill Bogie
@@ -18,7 +17,7 @@ autorequire:
18
17
  bindir: bin
19
18
  cert_chain: []
20
19
 
21
- date: 2010-08-20 00:00:00 +01:00
20
+ date: 2010-09-24 00:00:00 +01:00
22
21
  default_executable:
23
22
  dependencies:
24
23
  - !ruby/object:Gem::Dependency
@@ -29,7 +28,6 @@ dependencies:
29
28
  requirements:
30
29
  - - ">="
31
30
  - !ruby/object:Gem::Version
32
- hash: 21
33
31
  segments:
34
32
  - 1
35
33
  - 1
@@ -45,7 +43,6 @@ dependencies:
45
43
  requirements:
46
44
  - - ">="
47
45
  - !ruby/object:Gem::Version
48
- hash: 1
49
46
  segments:
50
47
  - 1
51
48
  - 4
@@ -53,6 +50,21 @@ dependencies:
53
50
  version: 1.4.3
54
51
  type: :runtime
55
52
  version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: lightcsv
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
63
+ - 2
64
+ - 2
65
+ version: 0.2.2
66
+ type: :runtime
67
+ version_requirements: *id003
56
68
  description: Simple little library for programmatically manipulating EEML documents, in particular this library is designed to work with the Pachube web service.
57
69
  email: sam.mulube@gmail.com
58
70
  executables: []
@@ -73,19 +85,16 @@ files:
73
85
  - lib/blank.rb
74
86
  - lib/eeml.rb
75
87
  - lib/eeml/constants.rb
76
- - lib/eeml/csv_parser.rb
88
+ - lib/eeml/csv_parser_v1.rb
89
+ - lib/eeml/csv_parser_v2.rb
77
90
  - lib/eeml/environment.rb
78
91
  - lib/eeml/environment_builder.rb
79
92
  - lib/eeml/exceptions.rb
80
93
  - lib/eeml/json_environment_parser_v005.rb
81
94
  - lib/eeml/json_environment_parser_v006.rb
82
95
  - lib/eeml/json_environment_parser_v100.rb
83
- - lib/eeml/json_output.rb
84
- - lib/eeml/libxml_eeml_output_v005.rb
85
- - lib/eeml/libxml_eeml_output_v051.rb
86
96
  - lib/eeml/libxml_eeml_parser_v005.rb
87
97
  - lib/eeml/libxml_eeml_parser_v051.rb
88
- - lib/eeml/output_registry.rb
89
98
  - schemas/eeml/0.5.1.xsd
90
99
  - schemas/eeml/005.xsd
91
100
  - test/data/.gitignore
@@ -111,6 +120,8 @@ files:
111
120
  - test/data/no_namespace.xml
112
121
  - test/data/out_empty.xml
113
122
  - test/libxml_test_helper.rb
123
+ - test/test_csv_parser_v1.rb
124
+ - test/test_csv_parser_v2.rb
114
125
  - test/test_environment.rb
115
126
  - test/test_helper.rb
116
127
  - test/test_libxml_eeml_parser_v005.rb
@@ -129,7 +140,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
140
  requirements:
130
141
  - - ">="
131
142
  - !ruby/object:Gem::Version
132
- hash: 3
133
143
  segments:
134
144
  - 0
135
145
  version: "0"
@@ -138,7 +148,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
148
  requirements:
139
149
  - - ">="
140
150
  - !ruby/object:Gem::Version
141
- hash: 3
142
151
  segments:
143
152
  - 0
144
153
  version: "0"
@@ -151,7 +160,9 @@ specification_version: 3
151
160
  summary: Simple little library for programmatically manipulating EEML documents.
152
161
  test_files:
153
162
  - test/test_helper.rb
154
- - test/test_libxml_test_helper.rb
155
163
  - test/test_libxml_eeml_parser_v005.rb
164
+ - test/test_libxml_test_helper.rb
156
165
  - test/test_environment.rb
166
+ - test/test_csv_parser_v2.rb
157
167
  - test/libxml_test_helper.rb
168
+ - test/test_csv_parser_v1.rb
@@ -1,15 +0,0 @@
1
- module Eeml
2
- class CsvParser
3
-
4
- #update the datastreams for the given environment, using the content in the given csv string.
5
- def update_datastreams_from_csv(csv_content, environment)
6
- # split values at commas
7
- datastream_values = csv_content.split(/,/)
8
-
9
- # now strip any whitespace
10
- datastream_values.map! { |d| d.strip }
11
-
12
- end
13
-
14
- end
15
- end
@@ -1,87 +0,0 @@
1
- module Eeml
2
-
3
- class JsonOutput # :nodoc:
4
-
5
- def to_json(environment, options = { :full => true })
6
- hash = to_public_json_data(environment, options)
7
- hash.to_json
8
- end
9
-
10
- def to_public_json_data(environment, options = { :full => true })
11
- environment_hash = { :id => Integer(environment.identifier), #we WANT this to fail early when we start using non-numeric identifiers
12
- :title => environment.title,
13
- :feed => environment.feed_url,
14
- :status => environment.status,
15
- :description => environment.description,
16
- :website => environment.website,
17
- :email => environment.email,
18
- :icon => environment.icon,
19
- :version => EEML['0.5.0'][:version],
20
- :tags => environment.tags }
21
-
22
- environment_hash[:updated] = environment.updated.utc.iso8601 unless environment.updated.nil? #TODO: was retrieved_at
23
-
24
- environment_hash.delete_if { |key, value| value.blank? }
25
-
26
- unless environment.location.nil?
27
-
28
- # add location data
29
- location_hash = { :domain => environment.location.domain,
30
- :exposure => environment.location.exposure,
31
- :disposition => environment.location.disposition,
32
- :name => environment.location.name,
33
- :ele => environment.location.elevation #currently, ele is output as a string
34
- }
35
- location_hash[:lat] = Float(environment.location.latitude) if environment.location.latitude
36
- location_hash[:lon] = Float(environment.location.longitude) if environment.location.longitude
37
-
38
- location_hash.delete_if { |key, value| value.blank? }
39
-
40
- environment_hash[:location] = location_hash unless location_hash.empty?
41
- end
42
-
43
- # if :full option given, add our datastream data into the returned json
44
- if options[:full] == true
45
- datastream_array = []
46
-
47
- # add datastream data
48
- environment.datastreams.each do |datastream|
49
- datastream_array << datastream_to_json(datastream)
50
- end
51
-
52
- environment_hash[:datastreams] = datastream_array unless datastream_array.empty?
53
- end
54
-
55
- return environment_hash
56
- end
57
-
58
- protected
59
- def datastream_to_json(datastream)
60
- datastream_hash = { :id => datastream.identifier }
61
-
62
- datastream_hash[:tags] = datastream.tags unless datastream.tags.empty?
63
-
64
- value_hash = { :min_value => datastream.min_value.to_s,
65
- :max_value => datastream.max_value.to_s,
66
- :current_value => datastream.value.to_s }
67
-
68
- value_hash.delete_if { |key, value| value.blank? }
69
-
70
- datastream_hash[:value] = value_hash unless value_hash.empty?
71
-
72
- unit_hash = { :type => datastream.unit_type.to_s,
73
- :symbol => datastream.unit_symbol.to_s,
74
- :label => datastream.unit_value.to_s }
75
-
76
- unit_hash.delete_if { |key, value| value.blank? }
77
-
78
- datastream_hash[:unit] = unit_hash unless unit_hash.empty?
79
-
80
- return datastream_hash
81
- end
82
-
83
-
84
-
85
- end
86
- end
87
-
@@ -1,119 +0,0 @@
1
- module Eeml
2
- class LibXMLEemlOutputV005 # :nodoc:
3
- include LibXML
4
-
5
- # main method. creates an EEML 5 document for the given environment.
6
- def to_eeml(environment)
7
- doc = XML::Document.new
8
- eeml = doc.root = XML::Node.new('eeml')
9
- XML::Namespace.new(eeml, nil, Constants::EEML['0.5.0'][:href])
10
- XML::Namespace.new(eeml, 'xsi', Constants::XSI_NAMESPACE)
11
- eeml['version'] = Constants::EEML['0.5.0'][:version]
12
- eeml['xsi:schemaLocation'] = Constants::EEML['0.5.0'][:schema_location]
13
- eeml << xml_node_for_environment(environment)
14
-
15
- return doc.to_s(:encoding => XML::Encoding::UTF_8)
16
- end
17
-
18
- def xml_node_for_environment(environment)
19
- environment_node = XML::Node.new('environment')
20
-
21
- #TODO: write all these strings out safely for xml
22
-
23
- environment_node['updated'] = environment.updated.utc.iso8601 unless environment.updated.nil?
24
- environment_node['id'] = environment.identifier.to_s unless environment.identifier.blank?
25
- environment_node['creator'] = environment.creator.to_s unless environment.creator.blank?
26
-
27
- unless environment.title.blank?
28
- environment_node << title_node = XML::Node.new('title')
29
- title_node << environment.title
30
- end
31
-
32
- unless environment.feed_url.blank?
33
- environment_node << feed_node = XML::Node.new('feed')
34
- feed_node << environment.feed_url
35
- end
36
-
37
- unless environment.status.blank?
38
- environment_node << status_node = XML::Node.new('status')
39
- status_node << environment.status
40
- end
41
-
42
- unless environment.description.blank?
43
- environment_node << description_node = XML::Node.new('description')
44
- description_node << environment.description
45
- end
46
-
47
- unless environment.icon.blank?
48
- environment_node << icon_node = XML::Node.new('icon')
49
- icon_node << environment.icon
50
- end
51
-
52
- unless environment.website.blank?
53
- environment_node << website_node = XML::Node.new('website')
54
- website_node << environment.website
55
- end
56
-
57
- unless environment.email.blank?
58
- environment_node << email_node = XML::Node.new('email')
59
- email_node << environment.email
60
- end
61
-
62
- unless environment.location.nil?
63
- environment_node << location_node = XML::Node.new('location')
64
- location_node['domain'] = environment.location.domain
65
- location_node['exposure'] = environment.location.exposure unless environment.location.exposure.blank?
66
- location_node['disposition'] = environment.location.disposition unless environment.location.disposition.blank?
67
-
68
- unless environment.location.name.blank?
69
- location_node << location_name_node = XML::Node.new('name')
70
- location_name_node << environment.location.name
71
- end
72
-
73
- location_node << lat_node = XML::Node.new('lat')
74
- lat_node << environment.location.latitude
75
-
76
- location_node << lng_node = XML::Node.new('lon')
77
- lng_node << environment.location.longitude
78
-
79
- unless environment.location.elevation.blank?
80
- location_node << elevation_node = XML::Node.new('ele')
81
- elevation_node << environment.location.elevation
82
- end
83
- end
84
-
85
- environment.datastreams.each do |datastream|
86
- environment_node << datastream_to_xml_node(datastream)
87
- end
88
-
89
- return environment_node
90
- end
91
-
92
- def datastream_to_xml_node(datastream)
93
- datastream_node = XML::Node.new('data')
94
- datastream_node['id'] = datastream.identifier.to_s
95
-
96
- datastream.tags.each do |tag|
97
- tag_node = XML::Node.new('tag')
98
- tag_node << tag
99
- datastream_node << tag_node
100
- end
101
-
102
- datastream_node << value_node = XML::Node.new('value')
103
-
104
- value_node['minValue'] = datastream.min_value.to_s unless datastream.min_value.to_s.empty?
105
- value_node['maxValue'] = datastream.max_value.to_s unless datastream.max_value.to_s.empty?
106
-
107
- value_node << datastream.value.to_s
108
-
109
- unless datastream.unit_value.to_s.empty? && datastream.unit_type.to_s.empty? && datastream.unit_symbol.to_s.empty?
110
- datastream_node << unit_node = XML::Node.new('unit')
111
- unit_node['type'] = datastream.unit_type.to_s unless datastream.unit_type.to_s.empty?
112
- unit_node['symbol'] = datastream.unit_symbol.to_s unless datastream.unit_symbol.to_s.empty?
113
- unit_node << datastream.unit_value.to_s unless datastream.unit_value.to_s.empty?
114
- end
115
-
116
- return datastream_node
117
- end
118
- end
119
- end
@@ -1,124 +0,0 @@
1
- module Eeml
2
- class LibXMLEemlOutputV051 # :nodoc:
3
- include LibXML
4
-
5
- @@eeml_version = Constants::EEML['0.5.1']
6
- # main method. creates an EEML 0.5.1 document for the given environment.
7
- def to_eeml(environment)
8
- doc = XML::Document.new
9
- eeml = doc.root = XML::Node.new('eeml')
10
- XML::Namespace.new(eeml, nil, @@eeml_version[:href])
11
- XML::Namespace.new(eeml, 'xsi', Constants::XSI_NAMESPACE)
12
- eeml['version'] = @@eeml_version[:version]
13
- eeml['xsi:schemaLocation'] = @@eeml_version[:schema_location]
14
- eeml << xml_node_for_environment(environment)
15
- return doc.to_s(:encoding => XML::Encoding::UTF_8)
16
- end
17
-
18
- def xml_node_for_environment(environment)
19
- environment_node = XML::Node.new('environment')
20
-
21
- #TODO: write all these strings out safely for xml
22
-
23
- environment_node['updated'] = environment.updated.utc.iso8601 unless environment.updated.nil?
24
- environment_node['id'] = environment.identifier.to_s unless environment.identifier.blank?
25
- environment_node['creator'] = environment.creator.to_s unless environment.creator.blank?
26
-
27
- unless environment.title.blank?
28
- environment_node << title_node = XML::Node.new('title')
29
- title_node << environment.title
30
- end
31
-
32
- unless environment.feed_url.blank?
33
- environment_node << feed_node = XML::Node.new('feed')
34
- feed_node << environment.feed_url
35
- end
36
-
37
- unless environment.status.blank?
38
- environment_node << status_node = XML::Node.new('status')
39
- status_node << environment.status
40
- end
41
-
42
- unless environment.description.blank?
43
- environment_node << description_node = XML::Node.new('description')
44
- description_node << environment.description
45
- end
46
-
47
- unless environment.icon.blank?
48
- environment_node << icon_node = XML::Node.new('icon')
49
- icon_node << environment.icon
50
- end
51
-
52
- unless environment.website.blank?
53
- environment_node << website_node = XML::Node.new('website')
54
- website_node << environment.website
55
- end
56
-
57
- unless environment.email.blank?
58
- environment_node << email_node = XML::Node.new('email')
59
- email_node << environment.email
60
- end
61
-
62
- environment.tags.each do |tag|
63
- tag_node = XML::Node.new('tag')
64
- tag_node << tag
65
- datastream_node << tag_node
66
- end
67
-
68
- unless environment.location.nil?
69
- environment_node << location_node = XML::Node.new('location')
70
- location_node['domain'] = environment.location.domain
71
- location_node['exposure'] = environment.location.exposure unless environment.location.exposure.blank?
72
- location_node['disposition'] = environment.location.disposition unless environment.location.disposition.blank?
73
-
74
- unless environment.location.name.blank?
75
- location_node << location_name_node = XML::Node.new('name')
76
- location_name_node << environment.location.name
77
- end
78
-
79
- location_node << lat_node = XML::Node.new('lat')
80
- lat_node << environment.location.latitude
81
-
82
- location_node << lng_node = XML::Node.new('lon')
83
- lng_node << environment.location.longitude
84
-
85
- unless environment.location.elevation.blank?
86
- location_node << elevation_node = XML::Node.new('ele')
87
- elevation_node << environment.location.elevation
88
- end
89
- end
90
-
91
- environment.datastreams.each do |datastream|
92
- environment_node << datastream_to_xml_node(datastream)
93
- end
94
-
95
- return environment_node
96
- end
97
-
98
- def datastream_to_xml_node(datastream)
99
- datastream_node = XML::Node.new('data')
100
- datastream_node['id'] = datastream.identifier.to_s
101
-
102
- datastream.tags.each do |tag|
103
- tag_node = XML::Node.new('tag')
104
- tag_node << tag
105
- datastream_node << tag_node
106
- end
107
-
108
- datastream_node << value_node = XML::Node.new('current_value')
109
- datastream_node << max_value_node = XML::Node.new('max_value', datastream.max_value.to_s) unless datastream.max_value.to_s.empty?
110
- datastream_node << min_value_node = XML::Node.new('min_value', datastream.min_value.to_s) unless datastream.min_value.to_s.empty?
111
-
112
- value_node << datastream.value.to_s
113
-
114
- unless datastream.unit_value.to_s.empty? && datastream.unit_type.to_s.empty? && datastream.unit_symbol.to_s.empty?
115
- datastream_node << unit_node = XML::Node.new('unit')
116
- unit_node['type'] = datastream.unit_type.to_s unless datastream.unit_type.to_s.empty?
117
- unit_node['symbol'] = datastream.unit_symbol.to_s unless datastream.unit_symbol.to_s.empty?
118
- unit_node << datastream.unit_value.to_s unless datastream.unit_value.to_s.empty?
119
- end
120
-
121
- return datastream_node
122
- end
123
- end
124
- end
@@ -1,18 +0,0 @@
1
- require 'eeml/libxml_eeml_output_v005'
2
- require 'eeml/libxml_eeml_output_v051'
3
- require 'eeml/json_output'
4
-
5
- module Eeml
6
- class OutputRegistry # :nodoc:
7
- #look at the given xml, build and return a new v005 or 006 parser, accordingly
8
- def self.get_xml_output_for(version = Constants::EEML['0.5.0'][:version])
9
- return LibXMLEemlOutputV051.new if version == Constants::EEML['0.5.1'][:version]
10
- LibXMLEemlOutputV005.new
11
- end
12
-
13
- def self.get_json_output_for(version = Constants::EEML['0.5.0'][:version])
14
- JsonOutput.new
15
- end
16
-
17
- end
18
- end