module AlaSpecies
Constants
- API_BASE
Public Instance Methods
Source
# File lib/ala_species.rb, line 52 def best_match(query, fetch: nil) results = search(query, fetch: fetch) return nil if results.empty? q = query.to_s.strip exact_sci = results.find { |row| row[:name].casecmp?(q) } return exact_sci if exact_sci common_matches = results.select do |row| row[:common_name].casecmp?(q) || row[:name].casecmp?(q) end return common_matches.max_by { |row| row[:occurrence_count] } if common_matches.any? species = results.select { |row| row[:rank] == "species" } species.max_by { |row| row[:occurrence_count] } || results.first end
Source
# File lib/ala_species.rb, line 76 def format_species(query, match, fetch: nil) description = AlaSpeciesDescription.paragraph_for(match, fetch: fetch) AlaSpeciesReport.format(query, match, description: description) end
Source
# File lib/ala_species.rb, line 69 def is_real_species?(name, fetch: nil) match = best_match(name, fetch: fetch) return false unless match match[:rank] == "species" && match[:occurrence_count].positive? end
Source
# File lib/ala_species.rb, line 31 def parse_results(payload) return [] unless payload.is_a?(Hash) Array(payload.dig("searchResults", "results")).filter_map do |row| next unless row.is_a?(Hash) rank = row["rank"].to_s.strip next if rank.empty? { name: row["name"].to_s.strip, common_name: (row["commonNameSingle"] || row["commonName"]).to_s.strip, rank: rank, occurrence_count: row["occurrenceCount"].to_i, kingdom: row["kingdom"].to_s.strip, family: row["family"].to_s.strip, guid: row["guid"].to_s.strip } end end
Source
# File lib/ala_species.rb, line 19 def search(query, limit: 5, fetch: nil) url = search_url(query, limit: limit) payload = if fetch JSON.parse(fetch.call(url)) else RabbotHttp.fetch_json(url) end parse_results(payload) rescue StandardError [] end
Source
# File lib/ala_species.rb, line 14 def search_url(query, limit: 5) q = CGI.escape(query.to_s.strip) "#{API_BASE}?q=#{q}&page=0&pageSize=#{limit}" end