Skip to content

Commit

Permalink
convert to JSON, not to Ruby hash
Browse files Browse the repository at this point in the history
  • Loading branch information
eebbesen committed Oct 16, 2024
1 parent b6cc880 commit bc9b501
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
4 changes: 3 additions & 1 deletion lib/sheet_zoukas/data_converter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'json'

module SheetZoukas
# convert spreadsheet data to JSON
class DataConverter
Expand All @@ -13,7 +15,7 @@ def convert

@rows.map do |row|
@headers.zip(row).to_h
end
end.to_json
end

private
Expand Down
59 changes: 30 additions & 29 deletions spec/sheet_zoukas/data_converter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'json'
require 'spec_helper'
require 'sheet_zoukas/data_converter'

Expand Down Expand Up @@ -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
20 changes: 11 additions & 9 deletions spec/sheet_zoukas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit bc9b501

Please sign in to comment.