Sha256: 9afe9f0199597c02887b06a9df0bbe65063b594a38de0fe55ca23728c988f42c

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require 'addressable'
require 'net/http'

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 content
          content_fetch(url)
        end

        def serialize
          url
        end

        private

        def content_fetch(uri, limit = 10)
          raise 'too many HTTP redirects' if limit.zero?

          response = Net::HTTP.get_response(URI(uri))

          case response
          when Net::HTTPSuccess then
            response.body
          when Net::HTTPRedirection then
            content_fetch(self.class.location_uri(uri, response['location']), limit - 1)
          else
            response.value
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
aranha-parsers-0.1.1 lib/aranha/parsers/source_address/http_get.rb
aranha-parsers-0.1.0 lib/aranha/parsers/source_address/http_get.rb
aranha-0.10.1 lib/aranha/parsers/source_address/http_get.rb
aranha-0.10.0 lib/aranha/parsers/source_address/http_get.rb
aranha-0.9.1 lib/aranha/parsers/source_address/http_get.rb