0

I want to access the Kippt API through Ruby without the usage of any external libraries whatsoever, i.e. everything that comes packed with Ruby is fine, but nothing else (except for the standard library).

How should I go about doing this? Please detail the process.

4

1 回答 1

1

这是非常基本的访问,表明它是可能的:

require "net/https"
require "uri"

uri = URI.parse( 'https://kippt.com/api/users/1/' )
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Get.new(uri.request_uri)

response = http.request(request)

data = JSON.parse( response.body )

 => {
  "username"=>"jorilallo", 
  "bio"=>"Co-founder of Kippt. I love building products.", 
  "app_url"=>"/jorilallo",
  "avatar_url"=>"https://d19weqihs4yh5u.cloudfront.net/avatars/147d86b9-0830-49d8-a449-0421a6a4bf05/160x160", 
  "twitter"=>"jorilallo", 
  "id"=>1, "github"=>"jorde",
  "website_url"=>"http://about.me/jorilallo", 
  "full_name"=>"Jori Lallo", 
  "dribbble"=>"jorilallo", 
  "counts"=>{"follows"=>1192, "followed_by"=>23628}, 
  "is_pro"=>true, "resource_uri"=>"/api/users/1/"
}

有相当多的工作要进行此演示并将其放入一些可重用的代码中,以应对基于 HTTP 的 API 的身份验证、发布参数、请求失败和其他标准问题。

我建议阅读http://www.rubyinside.com/nethttp-cheat-sheet-2940.html以获取有关如何更详细地构建和处理请求的一些示例。这就是我执行上述操作的方式(在写出答案之前,我之前从未直接使用过 Ruby 的 net/http,而我只是从该站点获取了一个看起来很可能的代码块)。

于 2013-07-01T19:59:08.037 回答