Sha256: 4a15bb02135231433473ae83b4cf1b71d823ded0049d35ffa0239a80c20ab5cc

Contents?: true

Size: 1.27 KB

Versions: 17

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require 'active_support/core_ext/module/delegation'
require 'eac_ruby_utils/require_sub'
require 'yaml'

module Aranha
  module Parsers
    class SourceAddress
      ::EacRubyUtils.require_sub __FILE__

      class << self
        SUBS = [
          ::Aranha::Parsers::SourceAddress::HashHttpGet,
          ::Aranha::Parsers::SourceAddress::HashHttpPost,
          ::Aranha::Parsers::SourceAddress::HttpGet,
          ::Aranha::Parsers::SourceAddress::File
        ].freeze

        def detect_sub(source)
          return source.sub if source.is_a?(self)

          SUBS.each do |sub|
            return sub.new(source) if sub.valid_source?(source)
          end
          raise "No content fetcher found for source \"#{source}\""
        end

        def deserialize(string)
          new(string =~ %r{\A[a-z]+://} ? string.strip : ::YAML.load(string)) # rubocop:disable Security/YAMLLoad
        end

        def from_file(path)
          deserialize(::File.read(path))
        end
      end

      attr_reader :sub

      def initialize(source)
        @sub = self.class.detect_sub(source)
      end

      delegate :content, :url, to: :sub

      def to_s
        sub.url
      end

      def serialize
        sub.serialize.strip + "\n"
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
ehbrs-tools-0.23.0 vendor/aranha-parsers/lib/aranha/parsers/source_address.rb
aranha-parsers-0.7.1 lib/aranha/parsers/source_address.rb
ehbrs-tools-0.22.0 vendor/aranha-parsers/lib/aranha/parsers/source_address.rb
ehbrs-tools-0.21.0 vendor/aranha-parsers/lib/aranha/parsers/source_address.rb
ehbrs-tools-0.20.0 vendor/aranha-parsers/lib/aranha/parsers/source_address.rb
ehbrs-tools-0.19.0 vendor/aranha-parsers/lib/aranha/parsers/source_address.rb
ehbrs-tools-0.18.0 vendor/aranha-parsers/lib/aranha/parsers/source_address.rb
ehbrs-tools-0.17.0 vendor/aranha-parsers/lib/aranha/parsers/source_address.rb
ehbrs-tools-0.16.5 vendor/aranha-parsers/lib/aranha/parsers/source_address.rb
ehbrs-tools-0.16.4 vendor/aranha-parsers/lib/aranha/parsers/source_address.rb
ehbrs-tools-0.16.3 vendor/aranha-parsers/lib/aranha/parsers/source_address.rb
ehbrs-tools-0.16.2 vendor/aranha-parsers/lib/aranha/parsers/source_address.rb
aranha-parsers-0.7.0 lib/aranha/parsers/source_address.rb
aranha-parsers-0.6.1 lib/aranha/parsers/source_address.rb
ehbrs-tools-0.16.1 vendor/aranha-parsers/lib/aranha/parsers/source_address.rb
aranha-parsers-0.6.0 lib/aranha/parsers/source_address.rb
aranha-parsers-0.5.0 lib/aranha/parsers/source_address.rb