Sha256: 435b55f06ea7a75a6788f5e6458b0cd5c8d53493b4766f761221cb00b03a8b4f

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

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

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
          c = ::Curl::Easy.new(url)
          c.follow_location = true
          curl_perform(c)
          return c.body_str if c.status.to_i == 200

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

        def serialize
          url
        end

        private

        def curl_perform(curl)
          unless curl.perform
            raise(::Aranha::Parsers::SourceAddress::FetchContentError,
                  "Curl perform failed (URL: #{url})")
          end
        rescue Curl::Err::CurlError => e
          raise ::Aranha::Parsers::SourceAddress::FetchContentError, "CURL error: #{e.class.name}"
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
ehbrs-tools-0.28.2 vendor/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
ehbrs-tools-0.28.1 vendor/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
ehbrs-tools-0.28.0 vendor/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
ehbrs-tools-0.27.0 vendor/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb
aranha-parsers-0.8.5 lib/aranha/parsers/source_address/http_get.rb
ehbrs-tools-0.26.0 vendor/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb