From 0cace96b7f0e140506805c1439b8dac1dd0daf2c Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Sun, 15 May 2016 15:58:57 +1000 Subject: [PATCH] Revised pull request --- lib/aemo/nmi.rb | 13 +++++----- spec/lib/aemo/nmi_spec.rb | 54 ++++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/lib/aemo/nmi.rb b/lib/aemo/nmi.rb index f0ee6b0..f364814 100644 --- a/lib/aemo/nmi.rb +++ b/lib/aemo/nmi.rb @@ -429,19 +429,20 @@ class NMI # # @param nmi [String] the National Meter Identifier (NMI) # @param options [Hash] a hash of options + # @option options [Hash] :msats_detail MSATS details as per #parse_msats_detail requirements # @return [AEMO::NMI] an instance of AEMO::NMI is returned def initialize(nmi, options = {}) raise ArgumentError, 'NMI is not a string' unless nmi.is_a?(String) raise ArgumentError, 'NMI is not 10 characters' unless nmi.length == 10 raise ArgumentError, 'NMI is not constructed with valid characters' unless AEMO::NMI.valid_nmi?(nmi) - @nmi = nmi - @meters = [] - @roles = {} - @data_streams = [] - @msats_detail = options['msats_detail'] + @nmi = nmi + @meters = [] + @roles = {} + @data_streams = [] + @msats_detail = options[:msats_detail] - parse_msats_detail unless options['msats_detail'].nil? + parse_msats_detail unless @msats_detail.nil? end # A function to validate the instance's nmi value diff --git a/spec/lib/aemo/nmi_spec.rb b/spec/lib/aemo/nmi_spec.rb index a8acef7..f3a11e9 100644 --- a/spec/lib/aemo/nmi_spec.rb +++ b/spec/lib/aemo/nmi_spec.rb @@ -27,23 +27,27 @@ # CLASS METHODS # --- describe '.valid_nmi?(nmi)' do - it 'should validate nmi' do - json.each do |nmi| - expect(AEMO::NMI.valid_nmi?(nmi['nmi'])).to eq(true) + context 'valid' do + it 'should validate nmi' do + json.each do |nmi| + expect(AEMO::NMI.valid_nmi?(nmi['nmi'])).to eq(true) + end end end - it 'should invalidate' do - expect(AEMO::NMI.valid_nmi?('OOOOOOOOOO')).to eq(false) - end - it 'should invalidate' do - expect(AEMO::NMI.valid_nmi?('NM100')).to eq(false) - end - it 'should invalidate' do - expect { AEMO::NMI.valid_nmi? }.to raise_error(ArgumentError) + context 'invalid' do + it 'should invalidate' do + expect(AEMO::NMI.valid_nmi?('OOOOOOOOOO')).to eq(false) + end + it 'should invalidate' do + expect(AEMO::NMI.valid_nmi?('NM100')).to eq(false) + end + it 'should invalidate' do + expect { AEMO::NMI.valid_nmi? }.to raise_error(ArgumentError) + end end end - describe '.self.valid_checksum?(nmi, checksum)' do + describe '.valid_checksum?(nmi, checksum)' do it 'should validate valid nmi and checksums' do json.each do |nmi| expect(AEMO::NMI.valid_checksum?(nmi['nmi'], nmi['checksum'])).to eq(true) @@ -51,7 +55,7 @@ end end - describe '.self.network?(nmi)' do + describe '.network?(nmi)' do it 'should return a network for an allocated NMI' do network = AEMO::NMI.network('NCCCC00000') expect(network.to_a[0].last[:title]).to eq('Ausgrid') @@ -66,14 +70,24 @@ # INSTANCE METHODS # --- describe '#initialize' do - it 'should raise an ArgumentError error' do - expect { AEMO::NMI.new('OOOOOOOOOO') }.to raise_error(ArgumentError) - end - it 'should raise an ArgumentError error' do - expect { AEMO::NMI.new('NM100') }.to raise_error(ArgumentError) + context 'valid' do + it 'should return a valid NMI' do + expect(AEMO::NMI.new('NM10000000')).to be_a(AEMO::NMI) + end + it 'should return a valid NMI with MSATS' do + expect(AEMO::NMI.new('NM10000000', { msats_detail: {} })).to be_a(AEMO::NMI) + end end - it 'should raise an ArgumentError error' do - expect { AEMO::NMI.new }.to raise_error(ArgumentError) + context 'invalid' do + it 'should raise an ArgumentError error' do + expect { AEMO::NMI.new('OOOOOOOOOO') }.to raise_error(ArgumentError) + end + it 'should raise an ArgumentError error' do + expect { AEMO::NMI.new('NM100') }.to raise_error(ArgumentError) + end + it 'should raise an ArgumentError error' do + expect { AEMO::NMI.new }.to raise_error(ArgumentError) + end end end describe '#valid_nmi?' do