Faraday::TimeoutError in Spree::Products#show , Net::ReadTimeout with #TCPSocket:(closed)
So i am trying to check if the URL is valid and exists, i tried sending request_head with http to get the response, i also did the same with faraday. My probloem is when i try doing that in the console it works perfectly console
Faraday.head("http://localhost:3000/"+url_for(controller: "spree/products", action: "show", id: "creme-mains-nourrissante", locale: 'fr')).status
It returns 200 which is what i need
My code is : menu_helper.rb
def url_valid?(url_string)
Faraday.head(url_string).status == 200
end
def url_change (lang)
url = ""
controller = Rails.application.routes.recognize_path(request.path_info)[:controller]
action = Rails.application.routes.recognize_path(request.path_info)[:action]
...
...
if controller == "spree/products" && action == "show"
if lang == :en
i = Spree::Product::Translation.where(spree_product_id: Spree::Product.find_by(slug: params[:id]).id).where.not(slug: params[:id]).ids
id = Spree::Product::Translation.find(i[0]).slug
return url_valid?("http://"+request.host_with_port+url_for(controller: "spree/products", action: action, id: id, locale: 'fr')) ? url_for(controller: "spree/products", action: action, id: id, locale: 'fr') : "http://"+request.host_with_port+"/"+Spree::Taxon.find_by(code: 'best_sales')
elsif lang == :fr
i = Spree::Product::Translation.where(spree_product_id: Spree::Product.find_by(slug: params[:id]).id).where.not(slug: params[:id]).ids
id = Spree::Product::Translation.find(i[0]).slug
############# i call the url_valid? in return
return url_valid?("http://"+request.host_with_port+url_for(controller: "spree/products", action: action, id: id, locale: 'en')) ? url_for(controller: "spree/products", action: action, id: id, locale: 'en') : "http://"+request.host_with_port+"/"+Spree::Taxon.find_by(code: 'best_sales')
end
end
end
I also tried :
require "net/http"
url = URI.parse("http://www.google.com/")
req = Net::HTTP.new(url.host, url.port)
res = req.request_head(url.path)
After waiting for a while i get a timeout error, Faraday::TimeoutError in Spree::Products#show , Net::ReadTimeout with #TCPSocket:(closed)
Any help will be appreciated, thanks