Sha256: 0418b26ffd71e6f97aac21d0621d49526d4d4b4434bb0e9021e04a4f25eaf314

Contents?: true

Size: 1.36 KB

Versions: 10

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require 'yaml'
require 'active_support/core_ext/module/delegation'
require 'aranha/parsers/source_address/hash_http_get'
require 'aranha/parsers/source_address/hash_http_post'
require 'aranha/parsers/source_address/http_get'
require 'aranha/parsers/source_address/file'

module Aranha
  module Parsers
    class SourceAddress
      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))
        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

10 entries across 10 versions & 2 rubygems

Version Path
aranha-parsers-0.2.2 lib/aranha/parsers/source_address.rb
aranha-parsers-0.2.1 lib/aranha/parsers/source_address.rb
aranha-parsers-0.2.0 lib/aranha/parsers/source_address.rb
aranha-parsers-0.1.1 lib/aranha/parsers/source_address.rb
aranha-parsers-0.1.0 lib/aranha/parsers/source_address.rb
aranha-0.10.1 lib/aranha/parsers/source_address.rb
aranha-0.10.0 lib/aranha/parsers/source_address.rb
aranha-0.9.1 lib/aranha/parsers/source_address.rb
aranha-0.9.0 lib/aranha/parsers/source_address.rb
aranha-0.8.0 lib/aranha/parsers/source_address.rb