Yahoo検索APIをStream Parsing

read_status_lineはnet/httpからのパクリ。
もう少し高水準のAPIってないもんかなぁ…と思うのはRubyにスポイルされているせいかも。

require 'socket'
require "rexml/document"
require "rexml/streamlistener"

def send_get(path, sock)
  sock.write("GET #{path} HTTP/1.0\n\n")
end

def read_status_line(sock)
  l = sock.readline
  m = /\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)\s*(.*)\z/in.match(l.chomp) or
    raise "wrong status line: #{l.dump}"
  m.captures
end

class Handler
  include REXML::StreamListener
  def tag_start name, attrs
    p name
  end
end

s = TCPSocket.new('api.search.yahoo.co.jp', 'http')

begin
  send_get("/WebSearchService/V1/webSearch?appid=《適当なアプリケーションID》&query=《検索する文字列》", s)
  httpv, code, msg = read_status_line(s)

  if code == "200"
    (l = s.readline) until l == "\r\n"
    REXML::Document.parse_stream(s, Handler.new)
  end
ensure
  s.close
end