eeml 0.0.29 → 0.0.32

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,7 +21,7 @@ module Eeml
21
21
  EEML["0.5.0"] =
22
22
  {
23
23
  :href => 'http://www.eeml.org/xsd/005',
24
- :version => '5',
24
+ :versions => ['5', '0.5.0'],
25
25
  :schema_location => 'http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd'
26
26
  }
27
27
 
@@ -29,18 +29,18 @@ module Eeml
29
29
  EEML["0.5.1"] =
30
30
  {
31
31
  :href => 'http://www.eeml.org/xsd/0.5.1',
32
- :version => '0.5.1',
32
+ :versions => '0.5.1',
33
33
  :schema_location => 'http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd'
34
34
  }
35
35
 
36
36
  JSON_API["0.6-alpha"] =
37
37
  {
38
- :version => '0.6-alpha'
38
+ :versions => ['0.6', '0.6-alpha']
39
39
  }
40
40
 
41
41
  JSON_API["1.0.0"] =
42
42
  {
43
- :version => "1.0.0"
43
+ :versions => ['1.0.0']
44
44
  }
45
45
 
46
46
 
@@ -7,9 +7,8 @@ module Eeml
7
7
 
8
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
9
 
10
- csv.first.each do |datastream_value|
11
- environment.datastreams << DataStream.new(:value => datastream_value)
12
- end
10
+ environment.add_datastreams(csv.first.collect { |datastream_value| DataStream.new(:value => datastream_value) })
11
+
13
12
  environment
14
13
  end
15
14
  end
@@ -9,11 +9,13 @@ module Eeml
9
9
 
10
10
  csv = strip_content(csv)
11
11
 
12
- csv.each do |datastream|
12
+ datastreams = csv.collect do |datastream|
13
13
  raise(CsvEncodingError, "CSV is invalid. Double check you are properly encoding line breaks.") if datastream.length == 3 && datastream[1].include?('\n')
14
14
  raise(CsvEncodingError, "CSV is invalid. Incorrect number of fields.") if datastream.length != 2
15
- environment.datastreams << DataStream.new(:value => datastream.last, :identifier => datastream.first)
15
+ DataStream.new(:value => datastream.last, :identifier => datastream.first)
16
16
  end
17
+ environment.add_datastreams(datastreams)
18
+
17
19
  environment
18
20
  end
19
21
  end
@@ -1,5 +1,6 @@
1
1
  module Eeml
2
2
  include Eeml::Constants
3
+ include Eeml::Exceptions
3
4
 
4
5
  # One of the component classes of Environment. Represents the location of the environment. Environments can only have a single location object.
5
6
  class Location
@@ -124,6 +125,13 @@ module Eeml
124
125
  def add_datastream(datastream)
125
126
  #TODO: consider checking for unique identifier
126
127
  datastreams << datastream
128
+ if dups = datastreams.map(&:identifier).compact.uniq!
129
+ raise Eeml::Exceptions::DuplicateDataStreams, "Duplicate Datastream IDs: #{dups.join(', ')}"
130
+ end
131
+ end
132
+
133
+ def add_datastreams(array)
134
+ array.each { |d| add_datastream d }
127
135
  end
128
136
 
129
137
  def remove_last_datastream
@@ -32,18 +32,20 @@ module Eeml
32
32
 
33
33
  def self.build_from_json(json_str)
34
34
  json = JSON.parse(json_str)
35
- if json["version"].to_i == Constants::EEML['0.5.0'][:version].to_i
35
+ if Constants::EEML['0.5.0'][:versions].include? json["version"].to_s
36
36
  parser = JsonEnvironmentParserV005.new
37
- elsif json["version"] == Constants::JSON_API['1.0.0'][:version]
37
+ elsif Constants::EEML['0.5.1'][:versions].include? json["version"].to_s
38
+ parser = JsonEnvironmentParserV005.new
39
+ elsif Constants::JSON_API['1.0.0'][:versions].include? json["version"].to_s
38
40
  parser = JsonEnvironmentParserV100.new
39
- elsif json["version"].to_f == Constants::JSON_API['0.6-alpha'][:version].to_f
41
+ elsif Constants::JSON_API['0.6-alpha'][:versions].include? json["version"].to_s
40
42
  parser = JsonEnvironmentParserV006.new
41
43
  else
42
- raise # raise something so we jump to the rescue block below
44
+ raise Eeml::Exceptions::BadVersion# raise something so we jump to the rescue block below
43
45
  end
44
46
  return parser.make_environment_from_hash(json)
45
- rescue
46
- raise JSON::ParserError, "Invalid version specification. Permitted versions are #{Constants::EEML['0.5.0'][:version]}, #{Constants::JSON_API["0.6-alpha"][:version]} and #{Constants::JSON_API["1.0.0"][:version]}"
47
+ rescue Eeml::Exceptions::BadVersion, TypeError
48
+ raise JSON::ParserError, "Invalid version specification. Permitted versions are #{Constants::EEML['0.5.0'][:versions].first}, #{Constants::JSON_API["0.6-alpha"][:versions].first} and #{Constants::JSON_API["1.0.0"][:versions].first}"
47
49
  end
48
50
 
49
51
  def self.build_from_csv(csv_str, version = :v1)
@@ -23,9 +23,9 @@ module Eeml
23
23
  class DataHasMultipleUnits < BadEeml; end
24
24
  class DataHasMultipleValues < BadEeml; end
25
25
  class NoDataStreams < BadEeml; end
26
+ class DuplicateDataStreams < BadEeml; end
26
27
  class MissingNamespace < BadEeml; end
27
28
 
28
-
29
29
  #A structured exception which holds info about what was missing and from where.
30
30
  #Note: Some reasons we don't just hold everything in an unstructured exception message:
31
31
  #1. some bits might be useful for dev but not for the public,
@@ -57,7 +57,9 @@ module Eeml
57
57
  def to_s
58
58
  "Missing attribute '#@attribute_name' from node '#@node_name'"
59
59
  end
60
- end
60
+ end
61
+
62
+ class BadVersion < StandardError; end
61
63
  end
62
64
  end
63
65
 
@@ -18,7 +18,7 @@ module Eeml
18
18
 
19
19
  env.location = buildLocation(env_hash)
20
20
 
21
- env.datastreams = buildDatastreams(env_hash)
21
+ env.add_datastreams(buildDatastreams(env_hash))
22
22
 
23
23
  return env
24
24
  end
@@ -18,7 +18,7 @@ module Eeml
18
18
 
19
19
  env.location = buildLocation(env_hash)
20
20
 
21
- env.datastreams = buildDatastreams(env_hash)
21
+ env.add_datastreams(buildDatastreams(env_hash))
22
22
 
23
23
  return env
24
24
  end
@@ -20,7 +20,7 @@ module Eeml
20
20
 
21
21
  env.location = buildLocation(env_hash)
22
22
 
23
- env.datastreams = buildDatastreams(env_hash)
23
+ env.add_datastreams(buildDatastreams(env_hash))
24
24
 
25
25
  return env
26
26
  end
@@ -87,7 +87,7 @@ module Eeml
87
87
 
88
88
  datastream_nodes = env_node.find('x:data', "x:#{@@eeml_version[:href]}")
89
89
  # raise NoDataStreams.new, "no datastreams found" if datastream_nodes.empty?
90
- env.datastreams = extractDataStreams(datastream_nodes) unless datastream_nodes.empty?
90
+ env.add_datastreams(extractDataStreams(datastream_nodes)) unless datastream_nodes.empty?
91
91
 
92
92
  return env
93
93
  end
@@ -91,7 +91,7 @@ module Eeml
91
91
 
92
92
  datastream_nodes = env_node.find('x:data', "x:#{@@eeml_version[:href]}")
93
93
  # raise NoDataStreams.new, "no datastreams found" if datastream_nodes.empty?
94
- env.datastreams = extractDataStreams(datastream_nodes) unless datastream_nodes.empty?
94
+ env.add_datastreams(extractDataStreams(datastream_nodes)) unless datastream_nodes.empty?
95
95
 
96
96
  return env
97
97
  end
data/lib/eeml.rb CHANGED
@@ -16,7 +16,7 @@ require 'time'
16
16
  module Eeml
17
17
 
18
18
  # library version number
19
- VERSION = '0.0.29'
19
+ VERSION = '0.0.32'
20
20
 
21
21
  # TODO: put in some configuration file, not here
22
22
  LOCAL_EEML_SCHEMA_LOCATION = 'schemas/eeml/005.xsd'
@@ -679,6 +679,45 @@ CSV
679
679
  assert_equal "CSV version could not be detected", exception.message
680
680
  end
681
681
 
682
+ # --- Other Unit Tests -------------------------------------------------------
683
+ # --- -------------------------------------------------------
684
+ # --- Stuff that gets used in lots of places ---------------------------------
685
+
686
+ test "environment#add_datastreams should add a group of datastreams" do
687
+ e = Environment.new
688
+ values = %w(e1 e2 e3)
689
+ e.add_datastreams values.collect {|v| DataStream.new(:value => v)}
690
+ assert_equal values.size, e.datastreams.size
691
+ end
692
+
693
+ test "environment#add_datastreams should raise an exception if passed duplicate datastreams" do
694
+ e = Environment.new
695
+ stream_ids = %w(ds1 ds2 ds1 ds3)
696
+ assert_raise Exceptions::DuplicateDataStreams, "Duplicate Datastream ID: ds1" do
697
+ e.add_datastreams stream_ids.collect {|id| DataStream.new(:identifier => id)}
698
+ end
699
+ end
700
+
701
+ test "environment#add_datastreams should raise an exception if datastream ids conflict with existing stream ids" do
702
+ e = Environment.new
703
+ stream_ids = %w(ds1 ds2 ds3)
704
+ e.add_datastreams stream_ids.collect {|id| DataStream.new(:identifier => id)}
705
+
706
+ assert_raise Exceptions::DuplicateDataStreams, "Duplicate Datastream ID: ds2" do
707
+ e.add_datastreams [DataStream.new(:identifier => "ds2")]
708
+ end
709
+ end
710
+
711
+ test "environment#add_datastream should raise an exception if datastream ids conflict with existing stream ids" do
712
+ e = Environment.new
713
+ stream_ids = %w(ds1 ds2 ds3)
714
+ e.add_datastreams stream_ids.collect {|id| DataStream.new(:identifier => id)}
715
+
716
+ assert_raise Exceptions::DuplicateDataStreams, "Duplicate Datastream ID: ds3" do
717
+ e.add_datastream DataStream.new(:identifier => "ds3")
718
+ end
719
+ end
720
+
682
721
  # --- convenience stuff - don't put tests under here ------------------------------
683
722
 
684
723
  def create_env_from_xml_string(string)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eeml
3
3
  version: !ruby/object:Gem::Version
4
- hash: 37
4
+ hash: 95
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 29
10
- version: 0.0.29
9
+ - 32
10
+ version: 0.0.32
11
11
  platform: ruby
12
12
  authors:
13
13
  - Neill Bogie
@@ -18,7 +18,7 @@ autorequire:
18
18
  bindir: bin
19
19
  cert_chain: []
20
20
 
21
- date: 2011-01-17 00:00:00 +00:00
21
+ date: 2011-02-14 00:00:00 +00:00
22
22
  default_executable:
23
23
  dependencies:
24
24
  - !ruby/object:Gem::Dependency