Class: Natour::SpeciesList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/natour/species_list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, date, type, group, title, description, items) ⇒ SpeciesList

Returns a new instance of SpeciesList.



14
15
16
17
18
19
20
21
22
# File 'lib/natour/species_list.rb', line 14

def initialize(path, date, type, group, title, description, items)
  @path = path
  @date = date
  @type = type
  @group = group
  @title = title
  @description = description
  @items = items
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



8
9
10
# File 'lib/natour/species_list.rb', line 8

def date
  @date
end

#descriptionObject (readonly)

Returns the value of attribute description.



12
13
14
# File 'lib/natour/species_list.rb', line 12

def description
  @description
end

#groupObject (readonly)

Returns the value of attribute group.



10
11
12
# File 'lib/natour/species_list.rb', line 10

def group
  @group
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/natour/species_list.rb', line 7

def path
  @path
end

#titleObject (readonly)

Returns the value of attribute title.



11
12
13
# File 'lib/natour/species_list.rb', line 11

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/natour/species_list.rb', line 9

def type
  @type
end

Class Method Details

.load_file(filename) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/natour/species_list.rb', line 24

def self.load_file(filename)
  block = IO.binread(filename, 128)
  header = if block.unpack('CC') == [0xff, 0xfe]
             block[2..].force_encoding('utf-16le').encode('utf-8')
           elsif block.unpack('CCC') == [0xef, 0xbb, 0xbf]
             block[3..].force_encoding('utf-8')
           else
             block
           end

  case header
  when /^Primary/
    CSV.open(filename, 'r:windows-1252:utf-8', headers: true, liberal_parsing: true) do |csv|
      date = DateUtils.parse(Pathname(filename).basename).compact.first
      items = csv.map { |row| Species.new(row[1], row[0]) }
                 .sort_by(&:name_de).uniq
      [SpeciesList.new(filename, date, :kosmos_vogelfuehrer, :birds, nil, nil, items)]
    end
  when /^Name/
    CSV.open(filename, 'r:bom|utf-8', headers: true) do |csv|
      date = DateUtils.parse(Pathname(filename).basename).compact.first
      items = csv.map { |row| Species.new(row[1], row[0]) }
                 .sort_by(&:name_de).uniq
      [SpeciesList.new(filename, date, :birdlife_vogelfuehrer, :birds, nil, nil, items)]
    end
  when /^<\?xml.*?www\.ornitho\.ch/m
    date = DateUtils.parse(Pathname(filename).basename).compact.first
    doc = Nokogiri.XML(File.read(filename, mode: 'r:utf-8'))
    folder = doc.at('/xmlns:kml/xmlns:Document/xmlns:Folder/xmlns:Folder/xmlns:Folder')
    name = folder.at('./xmlns:name').text
    items = folder.xpath('./xmlns:Placemark/xmlns:description')
                  .map(&:text)
                  .map { |description| Species.new(*description.scan(/&gt;([^&(]+)&lt;/).flatten.reverse) }
                  .sort_by(&:name_de).uniq
    [SpeciesList.new(filename, date, :ornitho_ch, :birds, name, nil, items)]
  when /^(Favoriten|NUMMER_FLORA)/
    CSV.open(filename, 'r:bom|utf-8', col_sep: ';', skip_blanks: true) do |csv|
      chunks = csv.reject { |row| row.count == 1 }
                  .map { |row| row[0] == 'NUMMER_FLORA' ? ['Favoriten'] : row }
                  .slice_before { |row| row.count == 1 || row.count == 3 }
                  .reject { |rows| rows.count == 1 }
      chunks.map do |rows|
        name, description = rows.shift
        date = DateUtils.parse(name, Pathname(filename).basename).compact.first
        items = rows.map { |row| Species.new(row[1], row[2]) }
                    .sort_by(&:name).uniq
        SpeciesList.new(
          filename,
          date,
          :flora_helvetica,
          :plants,
          name&.gsub(/^(\d{4}-)?\d{2}-\d{2}( |_|-)?/, ''),
          description,
          items
        )
      end
    end
  when /^obs_id/
    CSV.open(filename, 'r:bom|utf-16le:utf-8', col_sep: "\t", headers: true) do |csv|
      date = DateUtils.parse(Pathname(filename).basename).compact.first
      items = csv.select { |row| row[0] }
                 .map { |row| Species.new(row[11], nil) }
                 .sort_by(&:name).uniq
      [SpeciesList.new(filename, date, :info_flora, :plants, nil, nil, items)]
    end
  else
    []
  end
end

Instance Method Details

#each(&block) ⇒ Object



96
97
98
# File 'lib/natour/species_list.rb', line 96

def each(&block)
  @items.each(&block)
end