I've adopted some code which has like 5 or 6 Faraday requests inline and is my first time working with it. I am abstracting out to a module. This is the first time I am using this library and I currently have:
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
and would use like:
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
However, this feels a bit inelegant. Would I be better off writing this differently such as monkeypatching the response? The current Faraday response bojects supports a success? instance method but that is only checking for 200.