Sha256: 7628c35e429019cc236cf22cb4eb7f7885c4b07758aa080141d26f3e5d36cba6

Contents?: true

Size: 990 Bytes

Versions: 1

Compression:

Stored size: 990 Bytes

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=#{@url}"
      s += "&useragent=#{@user_agent}" if @user_agent
      s
    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.0 lib/nunchaku/checker.rb