Sha256: c5f419baed6e13cbbe0591010fdb85371f3c81adc2be8d7f72c5eae595667d44

Contents?: true

Size: 1.28 KB

Versions: 14

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require 'addressable'
require 'aranha/parsers/source_address/fetch_content_error'
require 'faraday'
require 'faraday/retry'

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.new(
            "Get #{url} returned #{c.status.to_i}", c
          )
        end

        def serialize
          url
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
eac_tools-0.60.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.59.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.58.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.57.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.56.1 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.56.0 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.55.7 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.55.6 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.55.5 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.55.4 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
aranha-parsers-0.17.0 lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.55.3 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
aranha-parsers-0.16.0 lib/aranha/parsers/source_address/http_get.rb
eac_tools-0.55.2 sub/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb