Sha256: 159094d3b72c5c7dd9a78c8c2ee35ccd0475c6529cee0d95d318ba549b88c219

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require 'httparty'
require 'json'

module Nunchaku
  class Checker
    attr_reader :url, :checker_uri, :user_agent

    def initialize(url, options = {})
      options = defaults.merge(options)

      @url         = url
      @checker_uri = options[:checker_uri]
      @user_agent  = options[:user_agent]
    end

    def raw
      @raw ||= JSON.parse vnu_request
    end

    def messages
      @messages ||= raw['messages'].map { |message| Nunchaku::Message.new(message) }
    end

    def errors
      messages.select { |message| message.type == 'error' }
    end

    def warnings
      messages.select { |message| message.subtype == 'warning' }
    end

    private

    def vnu_request
      HTTParty.get(vnu_request_querystring).body
    end

    def vnu_request_querystring
      s  = "#{checker_uri}?out=json&doc=#{escaped @url}"
      s += "&useragent=#{escaped @user_agent}" if @user_agent
      s
    end

    def escaped(str)
      CGI::escape str
    end

    def defaults
      { checker_uri: 'http://validator.w3.org/nu/' }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nunchaku-0.2.1 lib/nunchaku/checker.rb