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
- .csv_row(row) ⇒ Object
- .output(parsed, simple) ⇒ Object
- .parse(name, simple: false, with_cultivars: false) ⇒ Object
- .parse_ary(ary, simple: false, with_cultivars: false) ⇒ Object
Class Method Details
.csv_row(row) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/biodiversity/parser.rb', line 84 def self.csv_row(row) { id: row[0], verbatim: row[1], cardinality: row[2].to_i, canonical: { stem: row[3], simple: row[4], full: row[5] }, authorship: row[6], year: row[7], quality: row[8].to_i } end |
.output(parsed, simple) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/biodiversity/parser.rb', line 71 def self.output(parsed, simple) if simple parsed = parsed.force_encoding('UTF-8') csv = CSV.new(parsed) row = csv.readlines[0] csv_row(row) else parsed = JSON.parse(parsed, symbolize_names: true) parsed[:parserVersion] = Biodiversity.gnparser_version parsed end end |
.parse(name, simple: false, with_cultivars: false) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/biodiversity/parser.rb', line 36 def self.parse(name, simple: false, with_cultivars: false) format = simple ? 'csv' : 'compact' with_details = simple ? 0 : 1 with_cultivars = with_cultivars ? 1 : 0 parsed, ptr = parse_go(name, format, with_details, with_cultivars) free_mem(ptr) output(parsed, simple) end |
.parse_ary(ary, simple: false, with_cultivars: false) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/biodiversity/parser.rb', line 46 def self.parse_ary(ary, simple: false, with_cultivars: false) format = simple ? 'csv' : 'compact' with_details = simple ? 0 : 1 with_cultivars = with_cultivars ? 1 : 0 in_ptr = FFI::MemoryPointer.new(:pointer, ary.length) in_ptr.write_array_of_pointer( ary.map { |s| FFI::MemoryPointer.from_string(s) } ) parsed, ptr = parse_ary_go(in_ptr, ary.length, format, with_details, with_cultivars) free_mem(ptr) if simple CSV.new(parsed.force_encoding('UTF-8')).map do |row| csv_row(row) end else JSON.parse(parsed, symbolize_names: true).map do |item| item[:parserVersion] = Biodiversity.gnparser_version item end end end |