module PacmanSearch
Constants
- BASE_URL
- DEFAULT_LIMIT
- MAX_DESCRIPTION_LENGTH
- MAX_QUERY_LENGTH
- MIN_QUERY_LENGTH
- SEARCH_JSON
- USER_AGENT
Public Instance Methods
Source
# File lib/pacman_search.rb, line 50 def browse_url(query) "#{BASE_URL}/packages/?q=#{CGI.escape(query.to_s.strip)}" end
Source
# File lib/pacman_search.rb, line 125 def default_fetch lambda do |url| RabbotHttp.fetch(url, user_agent: USER_AGENT) end end
Source
# File lib/pacman_search.rb, line 95 def fetch_json(url, fetch: nil) body = (fetch || default_fetch).call(url) JSON.parse(body) end
Source
# File lib/pacman_search.rb, line 58 def format_version(epoch:, pkgver:, pkgrel:) version = "#{pkgver}-#{pkgrel}" epoch.to_i.positive? ? "#{epoch}:#{version}" : version end
Source
# File lib/pacman_search.rb, line 118 def lookup(query, fetch: nil, limit: DEFAULT_LIMIT) SearchLookup.format( search(query, fetch: fetch, limit: limit), report: PacmanReport ) end
Source
# File lib/pacman_search.rb, line 46 def package_url(repo:, arch:, name:) "#{BASE_URL}/packages/#{repo}/#{arch}/#{name}/" end
Source
# File lib/pacman_search.rb, line 63 def parse_package(entry) name = entry["pkgname"].to_s.strip repo = entry["repo"].to_s.strip arch = entry["arch"].to_s.strip return nil if name.empty? || repo.empty? || arch.empty? { name: name, version: format_version( epoch: entry["epoch"], pkgver: entry["pkgver"].to_s.strip, pkgrel: entry["pkgrel"].to_s.strip ), description: truncate_text(entry["pkgdesc"]), repo: repo, arch: arch, maintainer: Array(entry["maintainers"]).first.to_s.strip, flagged: !entry["flag_date"].nil? && !entry["flag_date"].to_s.strip.empty?, url: package_url(repo: repo, arch: arch, name: name) } end
Source
# File lib/pacman_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/pacman_search.rb, line 85 def parse_results(payload) data = payload.is_a?(String) ? JSON.parse(payload) : payload return { error: :fetch } unless data["valid"] results = Array(data["results"]).filter_map { |entry| parse_package(entry) } { total: results.length, items: results } rescue StandardError { error: :fetch } end
Source
# File lib/pacman_search.rb, line 100 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], limit: limit) 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/pacman_search.rb, line 41 def search_url(query, limit: DEFAULT_LIMIT) keyword = CGI.escape(query.to_s.strip) "#{SEARCH_JSON}?q=#{keyword}&limit=#{limit}" end
Source
# File lib/pacman_search.rb, line 54 def truncate_text(text, max_length = MAX_DESCRIPTION_LENGTH) TextUtil.truncate(text, max_length) end
Source
# File lib/pacman_search.rb, line 29 def usage_message "Usage: !pacman <package name or keyword> — search official Arch Linux repos (min 2 chars)" end