class ParsleyStore

Constants

LOCAL

database numbers for Redis

SLAVE
VERSION

Public Class Methods

new(local_db = LOCAL, slave_db = SLAVE, opts = {}) click to toggle source
# File lib/parsley-store.rb, line 14
def initialize(local_db = LOCAL, slave_db = SLAVE, opts = {})
  redis_host = opts[:redis_host] || "0.0.0.0"
  @parser = ScientificNameParser.new
  @local = Redis.new(host: redis_host)
  @local.select(local_db)
end

Public Instance Methods

parse(scientific_name, opts = {}) click to toggle source
# File lib/parsley-store.rb, line 21
def parse(scientific_name, opts = {})
  @canonical_only = !!opts[:canonical_only]
  @scientific_name = scientific_name

  parsed_data = get_redis_data
  return parsed_data if parsed_data

  cache_parsed_data(parse_scientific_name)
end

Private Instance Methods

cache_parsed_data(parsed_data) click to toggle source
# File lib/parsley-store.rb, line 61
def cache_parsed_data(parsed_data)
  if @canonical_only
    @local.hset(@scientific_name,
                'parsed',
                parsed_data[:scientificName][:parsed])
    @local.hset(@scientific_name,
                'parser_version',
                parsed_data[:scientificName][:parser_version])
    @local.hset(@scientific_name,
                'canonical', parsed_data[:scientificName][:canonical])
    parsed_data[:scientificName][:canonical]
  else
    serialized = parsed_data.to_json
    @local.set @scientific_name, serialized
    parsed_data
  end
end
get_redis_data() click to toggle source
# File lib/parsley-store.rb, line 33
def get_redis_data
  if @canonical_only
    begin
      stored = @local.hget(@scientific_name, 'canonical')
    rescue RuntimeError
      @local.flushdb
      stored = nil
    end
    return stored if stored
  else
    stored = @local.get(@scientific_name)
    return JSON.parse(stored, symbolize_names: true) if stored
  end
end
parse_scientific_name() click to toggle source
# File lib/parsley-store.rb, line 48
def parse_scientific_name
  begin
    @parser.parse(@scientific_name)
  rescue
    @parser = ScientificNameParser.new
    @parser.parse(@scientific_name) rescue {
      scientificName: {parsed: false,
                       parser_version: ScientificNameParser::VERSION,
                       anonical: nil,
                       verbatim: @scientific_name}}
  end
end