module GovauCkan
Constants
- STYLE_TITLE
Public Instance Methods
Source
# File lib/govau_ckan.rb, line 52 def clean_notes(text) TextUtil.squish(text)[0, 120] end
Source
# File lib/govau_ckan.rb, line 74 def dataset_url(name) id = CGI.escape(name.to_s.strip) "#{GovauConfig::CKAN_BASE}/package_show?id=#{id}" end
Source
# File lib/govau_ckan.rb, line 79 def fetch_dataset(name, fetch: nil) url = dataset_url(name) payload = if fetch JSON.parse(fetch.call(url)) else RabbotHttp.fetch_json(url) end parse_dataset(payload) rescue StandardError nil end
Source
# File lib/govau_ckan.rb, line 107 def format_dataset(row, name: nil) if row.nil? || row[:name].to_s.empty? return format_dataset_missing(name || row&.dig(:name)) end lines = [ IrcFormat.report_header(tag: "GOVAU", title: "data.gov.au ยท #{row[:name]}", style: STYLE_TITLE), row[:title] ] org = row[:organization].empty? ? "unknown publisher" : row[:organization] lines << "#{org} ยท #{row[:num_resources]} resources" lines << row[:notes] unless row[:notes].empty? lines << IrcFormat.report_footer("CKAN dataset", "data.gov.au") lines.join("\n") end
Source
# File lib/govau_ckan.rb, line 124 def format_dataset_missing(name) lines = [ IrcFormat.report_header(tag: "GOVAU", title: "data.gov.au ยท dataset", style: STYLE_TITLE), "No dataset found for #{name.to_s.strip}." ] lines << IrcFormat.report_footer("CKAN search", "data.gov.au") lines.join("\n") end
Source
# File lib/govau_ckan.rb, line 56 def format_search(query, results) lines = [ IrcFormat.report_header(tag: "GOVAU", title: "data.gov.au ยท #{query}", style: STYLE_TITLE) ] if results.empty? lines << "No datasets found." else results.each do |row| org = row[:organization].empty? ? "unknown publisher" : row[:organization] lines << "#{row[:title]} โ #{org}" end end lines << IrcFormat.report_footer("CKAN search", "data.gov.au") lines.join("\n") end
Source
# File lib/govau_ckan.rb, line 91 def parse_dataset(payload) return nil unless payload.is_a?(Hash) && payload["success"] row = payload["result"] return nil unless row.is_a?(Hash) { title: row["title"].to_s.strip, name: row["name"].to_s.strip, organization: row.dig("organization", "title").to_s.strip, notes: clean_notes(row["notes"]), num_resources: row["num_resources"].to_i, created: row["metadata_created"].to_s.strip } end
Source
# File lib/govau_ckan.rb, line 37 def parse_results(payload) return [] unless payload.is_a?(Hash) && payload["success"] Array(payload.dig("result", "results")).filter_map do |row| next unless row.is_a?(Hash) { title: row["title"].to_s.strip, name: row["name"].to_s.strip, organization: row.dig("organization", "title").to_s.strip, notes: clean_notes(row["notes"]) } end end
Source
# File lib/govau_ckan.rb, line 25 def search_datasets(query, rows: GovauConfig::MAX_SEARCH_ROWS, fetch: nil) url = search_url(query, rows: rows) payload = if fetch JSON.parse(fetch.call(url)) else RabbotHttp.fetch_json(url) end parse_results(payload) rescue StandardError [] end
Source
# File lib/govau_ckan.rb, line 20 def search_url(query, rows: GovauConfig::MAX_SEARCH_ROWS) q = CGI.escape(query.to_s.strip) "#{GovauConfig::CKAN_BASE}/package_search?q=#{q}&rows=#{rows}" end