Sha256: 92e1d11a3f7b5766652750599eb887168be489ccc61aea921d2a9d48123d883f

Contents?: true

Size: 769 Bytes

Versions: 2

Compression:

Stored size: 769 Bytes

Contents

module Nin
  class Parser
    def initialize(desc)
      @desc = desc
    end

    def call
      [@desc, extract_date, extract_tags]
    end

    private

    def extract_date
      date_pattern = /@[A-Z0-9,-\/]+/i
      date         = @desc.scan(date_pattern).last

      return nil if date.nil?

      date.gsub!('@', '')
      strip_tags(date_pattern)

      begin
        Chronic.parse(date).to_date
      rescue NoMethodError
        raise InvalidDateFormatError
      end
    end

    def extract_tags
      tag_pattern = /#[A-Z0-9_]+/i
      tags        = @desc.scan(tag_pattern).map { |tag| tag.gsub!('#', '') }

      strip_tags(tag_pattern)

      tags
    end

    def strip_tags(pattern)
      @desc.gsub!(pattern, '')
      @desc.strip!
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nin-1.3.0 lib/nin/parser.rb
nin-1.2.0 lib/nin/parser.rb