Sha256: d6c6ba3c8fe62d87e4eeee4c272efb5c19e60742062e7864b9f32fcf76b3c3e4

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

class LinkOracle
  class Request
    attr_reader :url

    def initialize(url)
      @url = url
    end

    def parsed_url
      validate_url
      validate_request
      results
    end

    def results
      {
        parsed_data: parsed_data,
        url: url
      }
    end

    def validate_request
      raise error_class if error_class
    end

    def validate_url
      !!URI.parse(url)
    rescue URI::InvalidURIError
      raise InvalidUrl, url
    end

    def response
      @response ||= request
    end

    def request
      c = ::Curl::Easy.new(url)
      c.follow_location = true
      begin
        c.perform
      rescue ::Curl::Err::SSLConnectError
        c.ssl_version = 3
        c.perform
      end
      c
    end

    def error_class
      return if response.response_code == 200
      {
        404 => PageNotFound,
        403 => PermissionDenied
      }[response.response_code] || BadThingsHappened
    end

    def parsed_data
      ::Nokogiri::HTML.parse(response.body_str)
    rescue
      raise ParsingError
    end
  end

  class PageNotFound < StandardError; end
  class PermissionDenied < StandardError; end
  class BadThingsHappened < StandardError; end
  class InvalidUrl < StandardError; end
  class ParsingError < StandardError; end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
link_oracle-0.0.7 lib/link_oracle/request.rb