Skip to content

Commit

Permalink
Environment variable check (#20)
Browse files Browse the repository at this point in the history
* Refactoring environment vars check and adding to gem

* Logging changes for next release
  • Loading branch information
eebbesen authored Oct 14, 2024
1 parent 145338b commit e118c04
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.1.0] - 2024-10-13

- Initial release v0.1.0

## [0.2.0] - UNRELEASED

- Added check for required environment variables before call to Google Sheets API
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sheet_zoukas (0.1.0)
sheet_zoukas (0.2.0)
google-apis-sheets_v4

GEM
Expand Down
4 changes: 4 additions & 0 deletions lib/sheet_zoukas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
require_relative 'sheet_zoukas/data_converter'
require_relative 'sheet_zoukas/google_sheets'
require_relative 'sheet_zoukas/version'
require_relative 'sheet_zoukas/utils'

# Retrieve Google Sheets data
module SheetZoukas
class Error < StandardError; end
REQUIRED_VARS = %w[GOOGLE_ACCOUNT_TYPE GOOGLE_API_KEY GOOGLE_CLIENT_EMAIL GOOGLE_CLIENT_ID GOOGLE_PRIVATE_KEY].freeze

class << self
def retrieve_sheet_json(sheet_id, tab_name, range = nil)
exit 1 unless SheetZoukas::Utils.vars_present?(REQUIRED_VARS, 'required for Google Sheets API calls')

sheet = GoogleSheets.new
data = sheet.retrieve_sheet(sheet_id, tab_name, range)
DataConverter.new(data.values).convert
Expand Down
17 changes: 17 additions & 0 deletions lib/sheet_zoukas/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module SheetZoukas
# Utility methods
class Utils
def self.vars_present?(required_vars, message)
all_present = true
required_vars.each do |var|
if ENV.fetch(var, '').strip.empty?
all_present = false
puts "⛔️ #{var} #{message}."
end
end
all_present
end
end
end
2 changes: 1 addition & 1 deletion lib/sheet_zoukas/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module SheetZoukas
VERSION = '0.1.0'
VERSION = '0.2.0'
end
42 changes: 42 additions & 0 deletions spec/sheet_zoukas/utils_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

# rubocop:disable RSpec/MultipleExpectations

require 'spec_helper'
require 'sheet_zoukas/utils'

RSpec.describe SheetZoukas::Utils do
describe '.vars_present?' do
before { ENV.store('PRESENT_VAR', 'present') }

it 'returns false when missing vars' do
expect do
expect(described_class).not_to be_vars_present(%w[MISSING_VAR PRESENT_VAR], 'is missing')
end.to output("⛔️ MISSING_VAR is missing.\n").to_stdout
end

it 'returns true when no missing vars' do
expect do
expect(described_class).to be_vars_present(%w[PRESENT_VAR], 'is missing')
end.not_to output.to_stdout
end

it 'returns false when variable empty string' do
ENV.store('EMPTY_VAR', '')

expect do
expect(described_class).not_to be_vars_present(%w[PRESENT_VAR EMPTY_VAR], 'is missing')
end.to output("⛔️ EMPTY_VAR is missing.\n").to_stdout
end

it 'returns false when variable blank string' do
ENV.store('EMPTY_VAR', ' ')

expect do
expect(described_class).not_to be_vars_present(%w[PRESENT_VAR EMPTY_VAR], 'is missing')
end.to output("⛔️ EMPTY_VAR is missing.\n").to_stdout
end
end
end

# rubocop:enable RSpec/MultipleExpectations
12 changes: 12 additions & 0 deletions spec/sheet_zoukas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,17 @@
'Reward Type' => 'no longer active')
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', nil), 'Log')
end.to raise_error(SystemExit) do |error| # rubocop:disable Style/MultilineBlockChain
expect(error.status).to eq(1)
end
end.to output("⛔️ #{missing} required for Google Sheets API calls.\n").to_stdout
end
end
end
21 changes: 4 additions & 17 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,7 @@
config.filter_sensitive_data('<SPREADSHEET_ID>') { ENV.fetch('GOOGLE_API_SPREADSHEET_ID', nil) }
end

REQUIRED_VARS = %w[GOOGLE_ACCOUNT_TYPE GOOGLE_API_KEY GOOGLE_CLIENT_EMAIL GOOGLE_CLIENT_ID GOOGLE_PRIVATE_KEY
GOOGLE_API_SPREADSHEET_ID].freeze

def check_vars
err = false
REQUIRED_VARS.each do |var|
if ENV.fetch(var, nil)&.chars
puts "✅ #{var}"
else
err = true
puts "⛔️ #{var} required for tests to run."
end
end
exit 1 if err
end

check_vars
# make sure all vars required for testing are present
require 'sheet_zoukas/utils'
REQUIRED_VARS_TEST = (SheetZoukas::REQUIRED_VARS + ['GOOGLE_API_SPREADSHEET_ID']).freeze
exit 1 unless SheetZoukas::Utils.vars_present?(REQUIRED_VARS_TEST, 'required for tests to run')

0 comments on commit e118c04

Please sign in to comment.