11# Searches Primo Search API and formats results
22#
33class 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
0 commit comments