module AurSearch
Constants
- BASE_URL
- DEFAULT_LIMIT
- MAX_DESCRIPTION_LENGTH
- MAX_QUERY_LENGTH
- MIN_QUERY_LENGTH
- RPC_SEARCH
- USER_AGENT
Public Instance Methods
Source
# File lib/aur_search.rb, line 50 def browse_url(query) "#{BASE_URL}/packages?K=#{CGI.escape(query.to_s.strip)}" end
Source
# File lib/aur_search.rb, line 116 def default_fetch lambda do |url| RabbotHttp.fetch(url, user_agent: USER_AGENT) end end
Source
# File lib/aur_search.rb, line 86 def fetch_json(url, fetch: nil) body = (fetch || default_fetch).call(url) JSON.parse(body) end
Source
# File lib/aur_search.rb, line 109 def lookup(query, fetch: nil, limit: DEFAULT_LIMIT) SearchLookup.format( search(query, fetch: fetch, limit: limit), report: AurReport ) end
Source
# File lib/aur_search.rb, line 46 def package_url(name) "#{BASE_URL}/packages/#{name}" end
Source
# File lib/aur_search.rb, line 58 def parse_package(entry) name = entry["Name"].to_s.strip return nil if name.empty? { name: name, version: entry["Version"].to_s.strip, description: truncate_text(entry["Description"]), votes: entry["NumVotes"].to_i, maintainer: entry["Maintainer"].to_s.strip, out_of_date: !entry["OutOfDate"].nil? && entry["OutOfDate"] != 0, url: package_url(name) } end
Source
# File lib/aur_search.rb, line 33 def parse_query(text) parsed = SearchQuery.parse(text, max_length: MAX_QUERY_LENGTH) return parsed.merge(too_short: false) if parsed[:empty] return { query: nil, empty: true, too_short: true } if parsed[:query].length < MIN_QUERY_LENGTH parsed.merge(too_short: false) end
Source
# File lib/aur_search.rb, line 73 def parse_results(payload) data = payload.is_a?(String) ? JSON.parse(payload) : payload return { error: :fetch } if data["type"] == "error" results = Array(data["results"]).filter_map { |entry| parse_package(entry) } total = data["resultcount"].to_i total = results.length if total.zero? && results.any? { total: total, items: results } rescue StandardError { error: :fetch } end
Source
# File lib/aur_search.rb, line 91 def search(query, fetch: nil, limit: DEFAULT_LIMIT) parsed = parse_query(query) return { error: :usage } if parsed[:empty] || parsed[:too_short] url = search_url(parsed[:query]) payload = fetch_json(url, fetch: fetch) results = parse_results(payload) return { error: :fetch, query: parsed[:query] } if results[:error] { query: parsed[:query], total: results[:total], items: results[:items].first(limit) } rescue StandardError { error: :fetch, query: parsed&.dig(:query) || query.to_s.strip } end
Source
# File lib/aur_search.rb, line 41 def search_url(query, by: "name-desc") keyword = CGI.escape(query.to_s.strip) "#{RPC_SEARCH}/#{keyword}?by=#{by}" end
Source
# File lib/aur_search.rb, line 54 def truncate_text(text, max_length = MAX_DESCRIPTION_LENGTH) TextUtil.truncate(text, max_length) end
Source
# File lib/aur_search.rb, line 29 def usage_message "Usage: !aur <package name or keyword> — search the Arch User Repository (min 2 chars)" end