我已经采用了一些类似于内联5或6个法拉第请求的代码,这是我第一次使用它。我正在抽象出一个模块。这是我第一次使用这个库,我目前有:
module CoreService
def self.net_connection param_url
conn = Faraday.new(:url => param_url ) do |c|
c.use Faraday::Request::UrlEncoded # encode request params as "www-form-urlencoded"
c.use Faraday::Response::Logger # log request & response to STDOUT
c.use Faraday::Adapter::NetHttp # perform requests with Net::HTTP
end
return conn
end
def self.success? arg
if [200,302].include? arg.status
return true
else
return false
end
end
end
并使用如:
conn=CoreService.net_connection SOME::URL
url_val = "/other/things"
response = conn.get url_val
if CoreService.success? response
puts "THAT WAS A SUCCESS"
else
puts "THAT WAS NOT A SUCCESS"
end
然而,这感觉有点不优雅。我是否会更好地写这种不同的方式,如monkeypatching响应?目前的法拉第响应工作是否成功?实例方法,但只检查200。
答案 0 :(得分:0)
根据VERSION = '1.0.0'
,有一种response.success?
方法会在http响应代码200至299上返回true。