Sha256: 3b0389a76d4ca5bd0f6c1b8a0c0cf3e533cbde26f42751dbd743b071ea4f150b

Contents?: true

Size: 733 Bytes

Versions: 3

Compression:

Stored size: 733 Bytes

Contents

module Nin
  class Parser
    def initialize(desc)
      @desc = desc
      @date = extract_date
      @tags = extract_tags
    end

    def call
      [@desc, @date, @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)

      Chronic.parse(date).strftime('%Y-%m-%d')
    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

3 entries across 3 versions & 1 rubygems

Version Path
nin-1.0.1 lib/nin/parser.rb
nin-1.0.0 lib/nin/parser.rb
nin-0.7.0 lib/nin/parser.rb