Sha256: 01341569b5f0a0aee487291bb3b8885f68f3f22ddd7d720baba09de381425444

Contents?: true

Size: 1.27 KB

Versions: 35

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require 'addressable'
require 'curb'
require 'aranha/parsers/source_address/fetch_content_error'
require 'faraday_middleware'

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

        def content
          conn = ::Faraday.new do |f|
            f.request :retry # retry transient failures
            f.response :follow_redirects # follow redirects
          end
          c = conn.get(url)
          return c.body if c.status == 200

          raise ::Aranha::Parsers::SourceAddress::FetchContentError,
                "Get #{url} returned #{c.status.to_i}"
        end

        def serialize
          url
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 3 rubygems

Version Path
eac_tools-0.55.1 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.55.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.54.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.53.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.52.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.51.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.50.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.49.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.48.1 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.48.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.47.2 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.47.1 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.47.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.46.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.45.2 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.45.1 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.45.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.44.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.43.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.42.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb