I am looking for a way to check if the API URL exists before making a request using HTTParty. Currently, if the URL does not exist, the HTTParty request errors.
I tried following this Check if URL exists in Ruby:
api_url = "http://api.meetup.com/LA-Computer-Science-Reading-Group/events?photo-host=public&page=20&sig_id=215634693&sig=0be729af948c1a17ce5f35faa9fcedd5ae22de56"
require "net/http"
url_request = URI.parse(api_url)
req = Net::HTTP.new(url_request.host, url_request.port)
res = req.request_head(url_request.path)
@check = res
response = HTTParty.get(api_url)
view:
<%= @check.inspect %>
#<Net::HTTPOK 200 OK readbody=true>
But if I change the URL to something that does not exist, such as:
http://api.meeeeeetup.com/LA-Computer-Science-Reading-Group/events?photo-host=public&page=20&sig_id=215634693&sig=0be729af948c1a17ce5f35faa9fcedd5ae22de56`
The page still errors with getaddrinfo: nodename nor servname provided, or not known
Also, the URL request I want to check is a XML request:
http://api.indeed.com/ads/apisearch?publisher=#{publisher_key}&q=java&l=austin%2C+tx&sort=&radius=&st=&jt=&start=&limit=&fromage=&filter=&latlong=1&co=us&chnl=&userip=&useragent=&v=2
and using the idea above, this API URL does not return a successful call. Instead it returns a #<Net::HTTPMethodNotAllowed 405 Method Not Allowed readbody=true>
.
I'm not sure about emulating a browser to check the results that way or if there is an easier way to check for validity of the API URL request before the HTTParty request.