Sha256: e4c802b2a26664d368d95552c03623fc2e3514c5ac4ad8fcda12e2e8412dd51f

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

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

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

2 entries across 2 versions & 1 rubygems

Version Path
aranha-parsers-0.15.1 lib/aranha/parsers/source_address/http_get.rb
aranha-parsers-0.15.0 lib/aranha/parsers/source_address/http_get.rb