Sha256: e1e426946c11bf8419f0c5f257338de73f06f1a12f05a088c2f198bbf0037311

Contents?: true

Size: 1003 Bytes

Versions: 1

Compression:

Stored size: 1003 Bytes

Contents

# frozen_string_literal: true

require 'csv'
require 'http'
require 'nokogiri'
require 'kramdown'

module VerifyUrls
  class Reader
    attr_reader :file, :file_path

    def initialize(file_path, file_format = nil)
      @file = File.read(file_path)
      @file_path = file_path
      @file_format = file_format
    end

    def urls
      case file_format.to_s.downcase
      when 'markdown', 'md' then markdown_urls(file)
      when 'html' then html_urls(file)
      when 'csv' then csv_urls(file)
      else
        raise(ArgumentError, "unknown format #{file_format}")
      end
    end

    def file_format
       @file_format || @file_path.split('.').last
    end

    private

    def csv_urls(file)
      CSV.parse(file).map(&:first).compact
    end

    def html_urls(file)
      document = Nokogiri::HTML(file)
      document.css('a').map { |e| e['href'] }.compact
    end

    def markdown_urls(file)
      html = Kramdown::Document.new(file).to_html
      html_urls(html)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
verify_urls-0.1.0 lib/verify_urls/reader.rb