Sha256: c389a2659ac309dfd41921cc5d1d24365e58d91bcdf6f5106fe1534ddb65960d

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require 'addressable'
require 'aranha/parsers/source_address/fetch_content_error'
require 'eac_envs/http/error'
require 'eac_envs/http/request'

module Aranha
  module Parsers
    class SourceAddress
      class HttpGet
        class << self
          def location_uri(source_uri, location)
            ::Addressable::URI.join(source_uri, location).to_s
          end

          def valid_source?(source)
            source.to_s =~ %r{\Ahttps?://}
          end
        end

        attr_reader :source

        def initialize(source)
          @source = source.to_s
        end

        def ==(other)
          self.class == other.class && source == other.source
        end

        def url
          source
        end

        def final_url
          content unless @final_url
          @final_url
        end

        # @return [String]
        # @raise [Aranha::Parsers::SourceAddress::FetchContentError]
        def content
          request = ::EacEnvs::Http::Request.new.url(url).retry(true).follow_redirect(true)
                      .header('user-agent', self.class.name)
          request.response.body_str!
        rescue ::EacEnvs::Http::Error => e
          raise ::Aranha::Parsers::SourceAddress::FetchContentError.new(e.message, request)
        end

        def serialize
          url
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aranha-parsers-0.23.1 lib/aranha/parsers/source_address/http_get.rb