[英]HTTParty ' get ' returns nil : Ruby
我是Ruby on Rails框架的新手。 我正在Coursera上的Ruby on Rails教程中进行练习,为此我必须使用HTTParty从Coursera API获取有关某些课程的信息,并在屏幕上显示此信息。 但是不幸的是我陷入了一个问题。
这是我的代码,可从Coursera API获取信息并返回
class Coursera
include HTTParty
base_uri 'https://api.coursera.org/api/catalog.v1/courses'
default_params fields: "smallIcon,shortDescription", q: "search"
format :json
def self.for term
response=get("", query: { query: term})["elements"]
if(response==nil)
puts "Got Negative response!"
else
puts "Got Positive response!"
return response
end
end
end
有人可以指出为什么“ get”返回零。 我确定我会犯一些错误,但是有人可以指出吗? 提前致谢
奇怪的。 看起来Coursera并不总是返回相同的响应。
require 'httparty'
class Coursera
include HTTParty
base_uri 'https://api.coursera.org/api/catalog.v1/courses'
default_params fields: "smallIcon,shortDescription", q: "search"
format :json
def self.for term
response = get("", query: {query: term})
puts response.request.last_uri.to_s
elements = response["elements"]
if elements
puts "Got Positive response!"
response
else
puts "Got Negative response!"
end
end
end
p Coursera.for 'python'
有时会返回:
https://api.coursera.org/api/catalog.v1/courses?fields=smallIcon%2CshortDescription&q=search&query=python
Got Negative response!
nil
有时 :
https://api.coursera.org/api/catalog.v1/courses?fields=smallIcon%2CshortDescription&q=search&query=python
Got Positive response!
#<HTTParty::Response:0x2660970 parsed_response={"elements"=>[{"id"=>119, "shortName"=>"scicomp", "name"=>"High Performance Scientific Computing", "shortDescription"=>"Programming-oriented course on effectively using modern computers to solve scientific computing problems arising in the physical/engineering sciences and other fields. Provides an introduction to efficient serial and parallel computing using Fortran 90, OpenMP, MPI, and Python, and software development tools such as version control, Makefiles, and debugging.", "smallIcon"=>"https://d15cw65ipctsrr.cloudfront.net/00/621b9b2597807229ed0fa605f96cdc/HighPerformanceComputingIma.jpg", "links"=>{}}, {"id"=>87, "shortName"=>"compphoto", "name"=>"Computational Photography", "shortDescription"=>"In this course you will learn about the basics of
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.