Module: Biodiversity::Parser

Extended by:
FFI::Library
Defined in:
lib/biodiversity/parser.rb

Overview

Parser provides a namespace for functions to parse scientific names.

Constant Summary collapse

POINTER_SIZE =
FFI.type_size(:pointer)

Class Method Summary collapse

Class Method Details

.output(parsed, simple) ⇒ Object


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/biodiversity/parser.rb', line 60

def self.output(parsed, simple)
  if simple
    csv = CSV.new(parsed)
    parsed = csv.read[0]
    {
      id: parsed[0],
      verbatim: parsed[1],
      cardinality: parsed[2],
      canonicalName: {
        full: parsed[3],
        simple: parsed[4],
        stem: parsed[5]
      },
      authorship: parsed[6],
      year: parsed[7],
      quality: parsed[8]
    }
  else
    JSON.parse(parsed, symbolize_names: true)
  end
end

.parse(name, simple = false) ⇒ Object


35
36
37
38
39
40
41
42
# File 'lib/biodiversity/parser.rb', line 35

def self.parse(name, simple = false)
  format = simple ? 'csv' : 'compact'

  parsed = nil
  callback = FFI::Function.new(:void, [:string]) { |str| parsed = str }
  parse_go(name, format, callback)
  output(parsed, simple)
end

.parse_ary(ary, simple = false) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/biodiversity/parser.rb', line 44

def self.parse_ary(ary, simple = false)
  format = simple ? 'csv' : 'compact'
  in_ptr = FFI::MemoryPointer.new(:pointer, ary.length)

  in_ptr.write_array_of_pointer(
    ary.map { |s| FFI::MemoryPointer.from_string(s) }
  )

  out_ary = []
  callback = FFI::Function.new(:void, [:string]) do |str|
    out_ary << output(str, simple)
  end
  parse_ary_go(in_ptr, ary.length, format, callback)
  out_ary
end