From bc9b5015b9da55816b221f87060b508353ec5a2c Mon Sep 17 00:00:00 2001 From: eebbesen Date: Wed, 16 Oct 2024 15:24:41 -0500 Subject: [PATCH] convert to JSON, not to Ruby hash --- lib/sheet_zoukas/data_converter.rb | 4 +- spec/sheet_zoukas/data_converter_spec.rb | 59 ++++++++++++------------ spec/sheet_zoukas_spec.rb | 20 ++++---- 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/lib/sheet_zoukas/data_converter.rb b/lib/sheet_zoukas/data_converter.rb index 1f849d1..93ff0e8 100644 --- a/lib/sheet_zoukas/data_converter.rb +++ b/lib/sheet_zoukas/data_converter.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'json' + module SheetZoukas # convert spreadsheet data to JSON class DataConverter @@ -13,7 +15,7 @@ def convert @rows.map do |row| @headers.zip(row).to_h - end + end.to_json end private diff --git a/spec/sheet_zoukas/data_converter_spec.rb b/spec/sheet_zoukas/data_converter_spec.rb index f39b6da..6bd1883 100644 --- a/spec/sheet_zoukas/data_converter_spec.rb +++ b/spec/sheet_zoukas/data_converter_spec.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'json' require 'spec_helper' require 'sheet_zoukas/data_converter' @@ -86,41 +87,41 @@ ret = dc.convert expect(ret).to eq([ - { 'Place' => 'Slice Brothers', - 'Deal' => '2 slices for $5.99', - 'Deal Earned' => '', - 'Deal Used' => '03/30/2024', - 'Deal Starts' => '', - 'Deal Ends' => '', - 'Notes' => 'no longer active', - 'Money Saved' => '4.99', - 'Reward Type' => 'no longer active' }, - { 'Place' => 'Slice Brothers', - 'Deal' => '2 slices for $5.99', - 'Deal Earned' => '', - 'Deal Used' => '04/11/2024', - 'Deal Starts' => '', - 'Deal Ends' => '', - 'Notes' => 'no longer active', - 'Money Saved' => '4.99', - 'Reward Type' => 'no longer active' }, - { 'Place' => 'Pot Belly', - 'Deal' => '1 free sandwich with the purchase of sandwich between 04/01 and 04/07', - 'Deal Earned' => '04/04/2024', - 'Deal Used' => '04/08/2024', - 'Deal Starts' => '04/01/2024', - 'Deal Ends' => '04/07/2024', - 'Notes' => '', - 'Money Saved' => '10.66', - 'Reward Type' => 'rewards' } - ]) + { 'Place' => 'Slice Brothers', + 'Deal' => '2 slices for $5.99', + 'Deal Earned' => '', + 'Deal Used' => '03/30/2024', + 'Deal Starts' => '', + 'Deal Ends' => '', + 'Notes' => 'no longer active', + 'Money Saved' => '4.99', + 'Reward Type' => 'no longer active' }, + { 'Place' => 'Slice Brothers', + 'Deal' => '2 slices for $5.99', + 'Deal Earned' => '', + 'Deal Used' => '04/11/2024', + 'Deal Starts' => '', + 'Deal Ends' => '', + 'Notes' => 'no longer active', + 'Money Saved' => '4.99', + 'Reward Type' => 'no longer active' }, + { 'Place' => 'Pot Belly', + 'Deal' => '1 free sandwich with the purchase of sandwich between 04/01 and 04/07', + 'Deal Earned' => '04/04/2024', + 'Deal Used' => '04/08/2024', + 'Deal Starts' => '04/01/2024', + 'Deal Ends' => '04/07/2024', + 'Notes' => '', + 'Money Saved' => '10.66', + 'Reward Type' => 'rewards' } + ].to_json) end it 'handles empty sheet' do dc = described_class.new([[], []]) ret = dc.convert - expect(ret).to eq([{}]) + expect(ret).to eq([{}].to_json) end end end diff --git a/spec/sheet_zoukas_spec.rb b/spec/sheet_zoukas_spec.rb index 4cdc612..313e578 100644 --- a/spec/sheet_zoukas_spec.rb +++ b/spec/sheet_zoukas_spec.rb @@ -11,21 +11,23 @@ it 'retrieves sheet data' do # rubocop:disable RSpec/ExampleLength VCR.use_cassette('retrieve_sheet_json') do data = described_class.retrieve_sheet_json(ENV.fetch('GOOGLE_API_SPREADSHEET_ID_TEST', nil), 'Log') - expect(data[0]).to eq('Place' => 'Slice Brothers', - 'Deal' => '2 slices for $5.99', - 'Deal Earned' => '', - 'Deal Used' => '03/30', - 'Deal Starts' => '', - 'Deal Ends' => '', - 'Notes' => 'no longer active', - 'Money Saved' => '4.99', - 'Reward Type' => 'no longer active') + + expect(data).to include({ 'Place' => 'Slice Brothers', + 'Deal' => '2 slices for $5.99', + 'Deal Earned' => '', + 'Deal Used' => '03/30', + 'Deal Starts' => '', + 'Deal Ends' => '', + 'Notes' => 'no longer active', + 'Money Saved' => '4.99', + 'Reward Type' => 'no longer active' }.to_json) end end it 'fails when missing required environment variable' do # rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations missing = SheetZoukas::REQUIRED_VARS.first ENV.delete(missing) + expect do expect do described_class.retrieve_sheet_json(ENV.fetch('GOOGLE_API_SPREADSHEET_ID_TEST', nil), 'Log')