2012-02-19 57 views
0

我試圖用net/http與Yahoo Placemaker API進行交互,但似乎無法使其工作。以下是我迄今爲止:在Ruby中與Yahoo Placemaker交互

host = 'wherein.yahooapis.com' 
payload = { 
    'documentContent' => 'Columbus Ohio', 
    'appid' => APP_ID, 
    'outputType' => 'json', 
    'documentType' => 'text/plain' 
}.to_json 

req = Net::HTTP::Post.new('/v1/document', initheader = { 'Content-Type' =>'application/json'}) 
req.body = payload 
response = Net::HTTP.new(host).start {|http| http.request(req) } 
puts "Response #{response.code} #{response.message}: #{response.body}" 

這給了我下面的錯誤:

Errno::ECONNREFUSED: Connection refused - connect(2) 
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `initialize' 
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `open' 
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `block in connect' 
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/timeout.rb:54:in `timeout' 
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/timeout.rb:99:in `timeout' 
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:762:in `connect' 
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:755:in `do_start' 
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:744:in `start' 
    from /Users/Kyle/Desktop/skateparks-web/lib/yahoo/placemaker.rb:21:in `extract' 
    from (irb):3 
    from /Users/Kyle/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>' 

固定@host錯字後,我現在得到:

Response 400 Bad Request: 
<?xml version="1.0" encoding="UTF-8" ?> 
<yahoo:error xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" xmlns:cle="http://wherein.yahooapis.com/v1/schema.rng" xmlns:xml="http://www.w3.org/XML/1998/namespace" xml:lang="en"><yahoo:description><![CDATA[Please provide a document URL or content.]]></yahoo:description> 
<yahoo:detail><code>-9999</code> 
<cause>input</cause> 
</yahoo:detail></yahoo:error> 
+0

PlaceMaker在2012年成爲PlaceSpotter。 BOSS API(包括PlaceSpotter)將於2016年3月31日關閉。如果您正在尋找替代品或替代品,您應該查看開源項目CLAVIN或最近推出的Geoparser.io API。 – 2016-03-27 18:06:58

回答

1

你正在給他們發送json,他們似乎正在尋找郵件正文中的常規查詢字符串。另外,您應該使用httparty來使用Web服務,除非您有充分的理由不要:

require 'httparty' 

class Yahoo 
    include HTTParty 
    base_uri 'http://wherein.yahooapis.com/' 
end 

json = Yahoo.post '/v1/document', :body => {:documentContent => 'Columbus Ohio', :appid => APP_ID, :outputType => 'json', :documentType => 'text/plain'} 
+0

雖然這個解決方案可以工作,但我真的想用w /'net/http'作爲std庫的一部分,並且使用'httparty'會爲我添加另一個gem已經膨脹的應用程序。儘管我很欣賞這個幫助! – 2012-02-20 05:07:43

+1

那麼解決方案是在郵件正文中使用標準查詢字符串格式(foo = 1&bar = 2)。 httparty代碼只是一個獎金。 – pguardiario 2012-02-20 05:17:15

0

可能只是一個錯字? @host應該是host對不對?

+0

我修復了錯字,但現在我得到了一個不同的錯誤。我已經更新了我的問題w /輸出 – 2012-02-19 19:42:09