使用Curb gem仅打印特定标题

时间:2014-10-31 17:57:37

标签: ruby curl curb

我有一个关于Ruby gem的问题 - Curb。我正在玩这个宝石并拥有这段代码:

require 'curb'
require 'colorize'

def err(msg)
  puts
  puts msg.red
  puts 'HOWTO: '.white + './script.rb <domain>'.red
  puts
end

target = ARGV[0] || err("You forgot something....")

Curl::Easy.perform(target) do |curl| 
  curl.headers["User-Agent"] = "Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.7.7) Gecko/20050421"
  curl.verbose = true
end

例如,当我在google.com上试用它时,我会收到此标题(我不会将完整的结果放在脚本中):

Host: google.com
Accept: */*
User-Agent: Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.7.7) Gecko/20050421

* STATE: DO => DO_DONE handle 0x1c8dd80; (connection #0) 
* STATE: DO_DONE => WAITPERFORM handle 0x1c8dd80; (connection #0) 
* STATE: WAITPERFORM => PERFORM handle 0x1c8dd80; (connection #0) 
* additional stuff not fine transfer.c:1037: 0 0
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 302 Found
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
< Location: https://www.google.cz/?gfe_rd=cr&ei=2stTVO2eJumg8we6woGoCg
< Content-Length: 259
< Date: Fri, 31 Oct 2014 17:50:18 GMT
< Server: GFE/2.0
< Alternate-Protocol: 443:quic,p=0.01

我的问题,有什么办法,如何通过Curb只打印特定的标题?例如,我只想输出这个标题,如下所示:

Content-Type: text/html; charset=UTF-8
Location: https://www.google.cz/?gfe_rd=cr&ei=2stTVO2eJumg8we6woGoCg
Server: GFE/2.0

再也没有了。有没有怎么做通过这个宝石?或者,如果您有任何想法如何使用其他宝石,请告诉我。

1 个答案:

答案 0 :(得分:0)

自己解析它并不是最困难的事情。

这正是“Get response headers from Curb”提出的建议。