Class: Reading::Parsing::Rows::Regular::Sources

Inherits:
Column show all
Defined in:
lib/reading/parsing/rows/regular_columns/sources.rb

Overview

Constant Summary collapse

SOURCES_PARSING_ERRORS =
{
  "The ISBN/ASIN must be placed last in the Sources column" =>
    ->(source) {
      source.match?(/\A#{ISBN_REGEX}/o) || source.match(/\A#{ASIN_REGEX}/o)
    },
}

Constants inherited from Column

Column::SHARED_REGEXES

Class Method Summary collapse

Methods inherited from Column

column_name, regexes_before_formats, segment_group_separator, split_by_segment?, split_by_segment_group?, to_sym

Class Method Details

.flatten_into_arraysObject



23
24
25
# File 'lib/reading/parsing/rows/regular_columns/sources.rb', line 23

def self.flatten_into_arrays
  %i[extra_info series_names series_volumes]
end

.regexes(segment_index) ⇒ Object



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
# File 'lib/reading/parsing/rows/regular_columns/sources.rb', line 54

def self.regexes(segment_index)
  [
    # ISBN/ASIN and length (without sources)
    (%r{\A
      (
        (?<isbn>(\d{3}[-\s]?)?[A-Z\d]{10})
        ,?(\s+|\z)
      )?
      (
        (?<length_pages>\d+)p?
        |
        (?<length_time>\d+:\d\d)
      )?
    \z}x if segment_index.zero?),
    # sources, ISBN/ASIN, length
    (%r{\A
      (
        (?<sources>.+?)
        ,?(\s+|\z)
      )?
      (
        (
          (?<isbn>#{ISBN_REGEX})
          |
          (?<asin>#{ASIN_REGEX})
        )
        ,?(\s+|\z)
      )?
      (
        (?<length_pages>\d+)p?
        |
        (?<length_time>\d+:\d\d)
      )?
    \z}xo if segment_index.zero?),
    *Column::SHARED_REGEXES[:series_and_extra_info],
  ].compact
end

.segment_separatorObject



19
20
21
# File 'lib/reading/parsing/rows/regular_columns/sources.rb', line 19

def self.segment_separator
  /\s*--\s*/
end

.split_by_format?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/reading/parsing/rows/regular_columns/sources.rb', line 15

def self.split_by_format?
  true
end

.tweaksObject



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
# File 'lib/reading/parsing/rows/regular_columns/sources.rb', line 27

def self.tweaks
  {
    sources: -> {
      comma = /\s*,\s*/
      space_before_url = / (?=https?:\/\/)/
      sources = _1.split(Regexp.union(comma, space_before_url))

      # Split by space after URL.
      sources = sources.flat_map { |src|
        if src.match?(/\Ahttps?:\/\//)
          src.split(" ", 2)
        else
          src
        end
      }

      SOURCES_PARSING_ERRORS.each do |message, check|
        if sources.any? { |source| check.call(source) }
          raise ParsingError, message
        end
      end

      sources
    },
  }
end