Sha256: 0fa88253cd317c0665c4dca7241c3364cf61a756497c1416152adb8ce045008b

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 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
    end

    def response
      @response ||= request
    end

    def request
      ::RestClient.get url
    end

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

    def parsed_data
      ::Nokogiri::HTML.parse(response.body)
    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

3 entries across 3 versions & 1 rubygems

Version Path
link_oracle-0.0.6 lib/link_oracle/request.rb
link_oracle-0.0.5 lib/link_oracle/request.rb
link_oracle-0.0.4 lib/link_oracle/request.rb