Reputation: 2906
I'm trying to develop a model method that will scrape a url that comes in from a post request to the controller. While I'm able to successfully use Nokogiri from the controller, when I try to move the logic into the model I'm always getting the error:
Errno::ENOENT: No such file or directory @ rb_sysopen - "www.url.com"
Why won't Rails allow me to implement Nokogiri in a model?
The model method I have setup as:
require 'nokogiri'
require 'open-uri'
class UrlContent < ApplicationRecord
validates :content, presence: true
def self.parser(url)
binding.pry
html = open(url)
doc = Nokogiri::HTML(html)
content = doc.css('h1, h2, h3, a').map(&:text).join(" ")
content
end
end
And my controller looks like:
require 'nokogiri'
require 'open-uri'
class UrlContentsController < ApplicationController
skip_before_filter :verify_authenticity_token
def create
content = UrlContent.parser(params[:content])
binding.pry
render layout: false
end
def index
@url_contents = UrlContent.all
render json: @url_contents, status: 200, layout: false
end
end
Upvotes: 1
Views: 171
Reputation: 434665
Looks like you're calling open
with something that you think is a URL but really isn't. You say this:
html = open(url)
and you're getting a complaint about open
not being able to find a file named 'www.url.com'
so you're actually saying:
html = open('www.url.com')
when you mean something more like:
html = open('http://www.url.com')
You should be checking your URLs and adding schemes if they don't have them already. I usually use Addressable for this sort of thing, something like this:
uri = Addressable::URI.parse(url)
url = "http://#{url}" if(!uri.scheme)
Upvotes: 3