Skip to content

Commit 73e1c12

Browse files
committed
Add validation for Primo env
1 parent 4dcaf0f commit 73e1c12

File tree

2 files changed

+80
-12
lines changed

2 files changed

+80
-12
lines changed

app/models/primo_search.rb

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
# Searches Primo Search API and formats results
22
#
33
class PrimoSearch
4-
PRIMO_API_URL = ENV.fetch('PRIMO_API_URL', nil).freeze
5-
PRIMO_API_KEY = ENV.fetch('PRIMO_API_KEY', nil)
6-
PRIMO_SCOPE = ENV.fetch('PRIMO_SCOPE', nil)
7-
PRIMO_TAB = ENV.fetch('PRIMO_TAB', nil)
8-
PRIMO_VID = ENV.fetch('PRIMO_VID', nil)
94

105
def initialize
11-
@primo_http = HTTP.persistent(PRIMO_API_URL)
6+
validate_env
7+
@primo_http = HTTP.persistent(primo_api_url)
128
@results = {}
139
end
1410

@@ -17,7 +13,7 @@ def search(term, per_page)
1713
result = @primo_http.timeout(http_timeout)
1814
.headers(
1915
accept: 'application/json',
20-
Authorization: "apikey #{PRIMO_API_KEY}"
16+
Authorization: "apikey #{primo_api_key}"
2117
)
2218
.get(url)
2319

@@ -28,6 +24,41 @@ def search(term, per_page)
2824

2925
private
3026

27+
def validate_env
28+
missing_vars = []
29+
30+
missing_vars << 'PRIMO_API_URL' if primo_api_url.nil?
31+
missing_vars << 'PRIMO_API_KEY' if primo_api_key.nil?
32+
missing_vars << 'PRIMO_SCOPE' if primo_scope.nil?
33+
missing_vars << 'PRIMO_TAB' if primo_tab.nil?
34+
missing_vars << 'PRIMO_VID' if primo_vid.nil?
35+
36+
return if missing_vars.empty?
37+
38+
raise ArgumentError, "Required Primo environment variables are not set: #{missing_vars.join(', ')}"
39+
end
40+
41+
# Environment variable accessors
42+
def primo_api_url
43+
ENV.fetch('PRIMO_API_URL', nil)
44+
end
45+
46+
def primo_api_key
47+
ENV.fetch('PRIMO_API_KEY', nil)
48+
end
49+
50+
def primo_scope
51+
ENV.fetch('PRIMO_SCOPE', nil)
52+
end
53+
54+
def primo_tab
55+
ENV.fetch('PRIMO_TAB', nil)
56+
end
57+
58+
def primo_vid
59+
ENV.fetch('PRIMO_VID', nil)
60+
end
61+
3162
# Initial search term sanitization
3263
def clean_term(term)
3364
term.strip.tr(' :,', '+').gsub(/\++/, '+')
@@ -36,19 +67,19 @@ def clean_term(term)
3667
# Constructs the search URL with required parameters for Primo API
3768
def search_url(term, per_page)
3869
[
39-
PRIMO_API_URL,
70+
primo_api_url,
4071
'/search?q=any,contains,',
4172
clean_term(term),
4273
'&vid=',
43-
PRIMO_VID,
74+
primo_vid,
4475
'&tab=',
45-
PRIMO_TAB,
76+
primo_tab,
4677
'&scope=',
47-
PRIMO_SCOPE,
78+
primo_scope,
4879
'&limit=',
4980
per_page,
5081
'&apikey=',
51-
PRIMO_API_KEY
82+
primo_api_key
5283
].join
5384
end
5485

test/models/primo_search_test.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
11
require 'test_helper'
22

33
class PrimoSearchTest < ActiveSupport::TestCase
4+
# Assumes env is correctly set in .env.test.
5+
test 'initializes successfully with all required environment variables' do
6+
assert_nothing_raised do
7+
PrimoSearch.new
8+
end
9+
end
10+
11+
test 'raises error when PRIMO_API_URL is missing' do
12+
ClimateControl.modify(PRIMO_API_URL: nil) do
13+
error = assert_raises(ArgumentError) do
14+
PrimoSearch.new
15+
end
16+
assert_match(/PRIMO_API_URL/, error.message)
17+
assert_match(/Required Primo environment variables are not set/, error.message)
18+
end
19+
end
20+
21+
test 'raises error when PRIMO_API_KEY is missing' do
22+
ClimateControl.modify(PRIMO_API_KEY: nil) do
23+
error = assert_raises(ArgumentError) do
24+
PrimoSearch.new
25+
end
26+
assert_match(/PRIMO_API_KEY/, error.message)
27+
end
28+
end
29+
30+
test 'raises error when multiple environment variables are missing' do
31+
ClimateControl.modify(PRIMO_API_URL: nil, PRIMO_SCOPE: nil, PRIMO_VID: nil) do
32+
error = assert_raises(ArgumentError) do
33+
PrimoSearch.new
34+
end
35+
assert_match(/PRIMO_API_URL/, error.message)
36+
assert_match(/PRIMO_SCOPE/, error.message)
37+
assert_match(/PRIMO_VID/, error.message)
38+
end
39+
end
40+
441
test 'search returns results' do
542
VCR.use_cassette('primo_search_success') do
643
search = PrimoSearch.new

0 commit comments

Comments
 (0)