Sha256: 7740c09bb7a8d3cba44874ec324210228ffb2110814fdf6d8ebbb665ca692ed6

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'url_finder/readers/html_reader'
require 'url_finder/readers/markdown_reader'
require 'url_finder/readers/sitemap_reader'
require 'url_finder/readers/string_reader'

module UrlFinder
  # Handles reader delegatation
  class Reader
    FORMAT_READERS = {
      'markdown' => MarkdownReader,
      'md' => MarkdownReader,
      'html' => HTMLReader,
      'sitemap' => SitemapReader,
      'sitemap.xml' => SitemapReader,
      'string' => StringReader,
      'txt' => StringReader,
    }.freeze

    # The raw content
    attr_reader :content

    # Instansiates reader
    def initialize(content, file_format)
      @content = content
      @file_format = file_format
    end

    # Returns the file format
    # @return [String] the file format
    def file_format
      @file_format.to_s.downcase
    end

    # Returns the appropriate reader for the given file format or raises error
    # @return [BaseReader] subclass of base reader
    def urls
      reader_klass = FORMAT_READERS.fetch(file_format) do
        raise(ArgumentError, "unknown format #{file_format}")
      end
      reader_klass.new(content)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
url_finder-0.2.2 lib/url_finder/reader.rb
url_finder-0.2.1 lib/url_finder/reader.rb
url_finder-0.2.0 lib/url_finder/reader.rb