简体   繁体   English

Ruby连接到REST服务

[英]Ruby connect to REST service

I need to access a REST JSon Service. 我需要访问REST JSon服务。 It's just connecting to an HTTPS using Basic authentication and performing some GETs and POSTs. 它只是使用基本身份验证连接到HTTPS并执行一些GET和POST。 This is a Ruby script running from a crontab. 这是从crontab运行的Ruby脚本。

These are the requirements: 这些是要求:

  • Ruby 1.8.7 Ruby 1.8.7
  • As light as possible (I'm not willing to install Rails). 尽可能轻(我不愿意安装Rails)。

Should I use HTTParty? 我应该使用HTTParty吗? I tried to install the gem and use it from irb with no luck. 我试图安装该宝石,并从irb运气不佳。 Apparently it has some dependencies. 显然,它具有一些依赖性。 Should I go for other library like net/http? 我应该去其他图书馆,例如net / http吗?

If HTTParty is the way to go, can you please include some instructions on how to get it to work in an IRB console? 如果要使用HTTParty,能否请您提供一些说明,说明如何使其在IRB控制台中工作?

Whenever I am connecting to a REST/JSON/API or just a site directly I go for HTTParty / Merhanize / Nokogiri depending on what exactly I need to do with the site. 每当我连接到REST / JSON / API或仅直接连接到站点时,我都会选择HTTParty / Merhanize / Nokogiri,这取决于我对站点的确切要求。 I also would be sure to check Ruby Toolbox in their HTTPClient category for other popular gems. 我还将确保检查其HTTPClient类别中的Ruby Toolbox ,以了解其他流行的宝石。

>> require "httparty"
=> true
>> response = HTTParty.get("http://justinherrick.com/posts/what-it-means-to-stay-dry.json")
=> #<HTTParty::Response:0x104ceac70 @parsed_response={"slug"=>"what-it-means-to-stay-dry", "pubdate"=>"2012-02-06", "created_at"=>"2012-02-06T22:08:05+00:00", "category"=>"Programming", "title"=>"What it means to stay DRY", "body"=>"A very common and useful programming idiom...

与HTTParty相比, ActiveResource可能以一种更为优雅的方式实现了这一目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM