module AuslibSearch
Constants
- RECORD_TYPES
Public Instance Methods
Source
# File lib/auslib_search.rb, line 143 def api_error?(data) message = data["message"].to_s.downcase message.include?("api key") end
Source
# File lib/auslib_search.rb, line 44 def browse_url(query) params = { keyword: query } "#{AuslibConfig::BROWSE_BASE}?#{URI.encode_www_form(params)}" end
Source
# File lib/auslib_search.rb, line 148 def default_fetch(api_key) lambda do |url, **| headers = {} key = api_key.to_s.strip headers["X-API-KEY"] = key unless key.empty? RabbotHttp.fetch(url, user_agent: AuslibConfig::USER_AGENT, headers: headers) end end
Source
# File lib/auslib_search.rb, line 64 def extract_record_entries(records) entries = [] if records.key?("item") Array(records["item"]).each do |wrapper| wrapper.each { |type, record| entries << [type.to_s, record] } end else RECORD_TYPES.each do |type| Array(records[type]).each { |record| entries << [type, record] } end end entries end
Source
# File lib/auslib_search.rb, line 157 def fetch_json(url, api_key:, fetch: nil) body = (fetch || default_fetch(api_key)).call(url) JSON.parse(body) end
Source
# File lib/auslib_search.rb, line 49 def first_text(value) case value when Array first_text(value.first) when Hash first_text(value["title"] || value[:title]) else value.to_s.strip end end
Source
# File lib/auslib_search.rb, line 189 def lookup(query, api_key: nil, fetch: nil) SearchLookup.format( search(query, api_key: api_key, fetch: fetch), report: AuslibReport, config_errors: true ) end
Source
# File lib/auslib_search.rb, line 93 def parse_article(record) { title: first_text(record["heading"]), contributor: first_text(record["title"]), date: first_text(record["date"]), kind: "newspaper", record_id: record["id"].to_s, url: record["troveUrl"].to_s.strip } end
Source
# File lib/auslib_search.rb, line 28 def parse_query(text) SearchQuery.parse(text, max_length: AuslibConfig::MAX_QUERY_LENGTH) end
Source
# File lib/auslib_search.rb, line 104 def parse_record(record_type, record) case record_type.to_s when "work" parse_work(record) when "article" parse_article(record) when "list", "people" { title: first_text(record["title"] || record["name"]), contributor: first_text(record["contributor"]), date: first_text(record["issued"] || record["date"]), kind: record_type.to_s, record_id: record["id"].to_s, url: record["troveUrl"].to_s.strip } end end
Source
# File lib/auslib_search.rb, line 122 def parse_results(payload) data = payload.is_a?(String) ? JSON.parse(payload) : payload return { total: 0, items: [], error: :config } if api_error?(data) items = [] total = 0 Array(data["category"]).each do |category| records = category.fetch("records", {}) total += records["total"].to_i extract_record_entries(records).each do |record_type, record| parsed = parse_record(record_type, record) items << parsed if parsed && !parsed[:title].empty? end end { total: total, items: items.first(AuslibConfig::MAX_RESULTS) } end
Source
# File lib/auslib_search.rb, line 78 def parse_work(record) kind = Array(record["type"]).map(&:to_s).reject(&:empty?).first kind = kind.to_s.downcase kind = "book" if kind.empty? { title: first_text(record["title"]), contributor: first_text(record["contributor"]), date: first_text(record["issued"]), kind: kind, record_id: record["id"].to_s, url: record["troveUrl"].to_s.strip } end
Source
# File lib/auslib_search.rb, line 162 def search(query, api_key:, fetch: nil, limit: AuslibConfig::MAX_RESULTS) parsed = parse_query(query) return { error: :usage } if parsed[:empty] key = AuslibConfig.api_key(key: api_key) return { error: :config } if key.empty? url = search_url(parsed[:query], limit: limit) payload = fetch_json(url, api_key: key, fetch: fetch) if api_error?(payload) return { error: :config, query: parsed[:query] } end results = parse_results(payload) if results[:error] == :config return { error: :config, query: parsed[:query] } end { query: parsed[:query], total: results[:total], items: results[:items] } rescue StandardError { error: :fetch, query: parsed&.dig(:query) || query.to_s.strip } end
Source
# File lib/auslib_search.rb, line 32 def search_url(query, limit: AuslibConfig::MAX_RESULTS) params = { q: query, category: AuslibConfig::SEARCH_CATEGORIES.join(","), encoding: "json", n: limit, s: "*", sortby: "relevance" } "#{AuslibConfig::API_BASE}/result?#{URI.encode_www_form(params)}" end
Source
# File lib/auslib_search.rb, line 60 def truncate_text(text, max_length) TextUtil.truncate(text, max_length) end
Source
# File lib/auslib_search.rb, line 24 def usage_message "Usage: !auslib <query> — search Australian library collections via Trove" end