module AlaSpeciesDescription
Constants
- EOL_PAGE_BASE
- EOL_SEARCH_BASE
- MIN_PARAGRAPH_LENGTH
- PREFERRED_TITLES
Public Instance Methods
Source
# File lib/ala_species_description.rb, line 125 def clean_html(html) Nokogiri::HTML(html.to_s).text.gsub(/\s+/, " ").strip end
Source
# File lib/ala_species_description.rb, line 44 def description_for_name(name, fetch: nil) lookup_page_ids(name, fetch: fetch).each do |page_id| payload = load_page(page_id, fetch: fetch) paragraph = extract_paragraph(payload) return paragraph if paragraph end nil end
Source
# File lib/ala_species_description.rb, line 91 def extract_paragraph(payload) objects = Array(payload.dig("taxonConcept", "dataObjects")) return nil if objects.empty? chosen = select_data_object(objects) return nil unless chosen full_description(chosen["description"]) end
Source
# File lib/ala_species_description.rb, line 113 def full_description(html) doc = Nokogiri::HTML(html.to_s) paragraphs = doc.css("p").map { |node| node.text.gsub(/\s+/, " ").strip }.reject(&:empty?) text = if paragraphs.empty? clean_html(html) else paragraphs.join("\n") end text = text.gsub(/\s+/, " ").strip if paragraphs.length <= 1 text.length >= MIN_PARAGRAPH_LENGTH ? text : nil end
Source
# File lib/ala_species_description.rb, line 67 def load_page(page_id, fetch: nil) url = page_url(page_id) if fetch JSON.parse(fetch.call(url)) else RabbotHttp.fetch_json(url) end end
Source
# File lib/ala_species_description.rb, line 63 def lookup_page_id(name, fetch: nil) lookup_page_ids(name, fetch: fetch).first end
Source
# File lib/ala_species_description.rb, line 53 def lookup_page_ids(name, fetch: nil) url = search_url(name) payload = if fetch JSON.parse(fetch.call(url)) else RabbotHttp.fetch_json(url) end parse_page_ids(payload, name) end
Source
# File lib/ala_species_description.rb, line 21 def page_url(page_id) "#{EOL_PAGE_BASE}/#{page_id.to_i}.json?" \ "language=en&images_per_page=0&videos_per_page=0&sounds_per_page=0&maps_per_page=0" \ "&texts_per_page=30&subjects=overview&licenses=all&details=true&references=true&vetted=0" end
Source
# File lib/ala_species_description.rb, line 27 def paragraph_for(match, fetch: nil) return nil unless match.is_a?(Hash) name = match[:name].to_s.strip return nil if name.empty? paragraph = description_for_name(name, fetch: fetch) return paragraph if paragraph common = match[:common_name].to_s.strip if !common.empty? && !common.casecmp?(name) description_for_name(common, fetch: fetch) end rescue StandardError nil end
Source
# File lib/ala_species_description.rb, line 76 def parse_page_id(payload, name) parse_page_ids(payload, name).first end
Source
# File lib/ala_species_description.rb, line 80 def parse_page_ids(payload, name) return [] unless payload.is_a?(Hash) results = Array(payload["results"]) return [] if results.empty? exact = results.select { |row| row["title"].to_s.casecmp?(name) } candidates = exact.empty? ? results : exact candidates.map { |row| row["id"] }.compact end
Source
# File lib/ala_species_description.rb, line 16 def search_url(name) query = CGI.escape(name.to_s.strip) "#{EOL_SEARCH_BASE}?q=#{query}&page=1&exact=true" end
Source
# File lib/ala_species_description.rb, line 101 def select_data_object(objects) candidates = objects.select { |row| row["language"].to_s == "en" } return nil if candidates.empty? PREFERRED_TITLES.each do |title| match = candidates.find { |row| row["title"].to_s.strip.casecmp?(title) } return match if match end candidates.max_by { |row| clean_html(row["description"]).length } end