Module: Validator

Defined in:
lib/validator.nu.rb

Defined Under Namespace

Classes: RemoteException

Constant Summary collapse

CONTENT_ENCODING =
"UTF-8"
CONTENT_TYPE =
"text/html; charset=utf-8"
HOST =
"validator.nu"
GZIP =
false
PORT =
80
LAX_CONTENT_TYPE =
false

Class Method Summary collapse

Class Method Details

.get(url, options) ⇒ Object

TODO - some implementation notes. http.set_debug_output STDERR http.use_ssl = true if SSL headers = method.to_s == ‘errors’ ? { ‘Content-Type’ => ‘application/x-gzip’, ‘Accept’ => ‘application/x-gzip’ } : {} compressed_data = CGI::escape(Zlib::Deflate.deflate(data, Zlib::BEST_SPEED)) STDERR.puts uri



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/validator.nu.rb', line 67

def get(url, options)
  begin
    host = options[:host] || HOST
    port = options[:port] || PORT
    laxtype = options[:lax_content_type] || LAX_CONTENT_TYPE

    # http = Net::HTTP.new(host, port)
    uri = "http://#{host}/?&doc=#{CGI::escape(url.to_s)}&out=json"
    uri += "&laxtype=yes" if laxtype
    url = URI.parse uri
    #STDERR.puts url
    #url.port = port
    
    return Yajl::HttpStream.get(url)
    
  #  response = http.start do |http|
  #    http.get(url.path)
  #  end
  #  
  #  if response.kind_of? Net::HTTPSuccess
  #    return response.body
  #  else
  #    STDERR.puts response.body.inspect
  #    raise RemoteException.new("#{response.code}: #{response.message}")
  #  end
  #
  rescue Exception => e
    STDERR.puts "Error contacting validator.nu: #{e}"
    STDERR.puts e.backtrace.join("\n"), 'debug'
    raise e
  end
end

.nu(uri_or_document, options = {}) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/validator.nu.rb', line 53

def nu(uri_or_document, options={})
  if uri_or_document.kind_of?(URI::HTTP)
    get(uri_or_document, options)
  else
    post(uri_or_document, options)
  end
end

.post(document, options) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/validator.nu.rb', line 100

def post(document, options)
  begin
    host = options[:host] || HOST
    port = options[:port] || PORT
    content_type = options[:content_type] || CONTENT_TYPE
    content_encoding = options[:content_encoding] || CONTENT_ENCODING
    gzip = options[:gzip] || GZIP
    laxtype = options[:lax_content_type] || LAX_CONTENT_TYPE

    if gzip
      content_encoding = 'gzip'
      output = StringIO.new
      gz = Zlib::GzipWriter.new(output)
      gz.write(document)
      gz.close
      document = output.string
    end

    http = Net::HTTP.new(host, port)
    uri = "/?out=json"
    uri += "&laxtype=yes" if laxtype
    headers = { 'Content-Type' => content_type, 'Content-Encoding' => content_encoding }

    response = http.start do |http|
      http.post(uri, document, headers) 
    end
    
    if response.kind_of? Net::HTTPSuccess
      # TODO: should this not perhaps return parsed JSON like the get
      return response.body
    else
      STDERR.puts response.body.inspect
      raise RemoteException.new("#{response.code}: #{response.message}")
    end

  rescue Exception => e
    STDERR.puts "Error contacting validator.nu: #{e}"
    STDERR.puts e.backtrace.join("\n"), 'debug'
    raise e
  end
end