How to open a file with with ruby? - ruby-on-rails

I am trying to open a file on rails user model with ruby and call it from user controller but it kept throwing me back wrong number of arguments (given 0, expected 1..3).
This is my file directory 'app' ,'assets', 'files', 'test_list.txt'
'app' ,'controllers', 'users controller'
can you help?thanks
class User < ApplicationRecord
def self.my_method
my_array = []
file = File.join(Rails.root, 'app' 'models','assets', 'files', 'test_list.txt')
File.open.each do |line|
my_array << line.gsub!(/\n?/, "")
end
return my_array.to_s
end
end
class UsersController < ApplicationController
require 'open-uri'
require 'net/http'
def show
# uri = URI('https://gist.githubusercontent.com/Kalagan/3b26be21cbf65b62cf05ab549433314e/raw')
# data = Net::HTTP.get(uri)
# anagrams = data.split(/\n/)
#vari = User.my_method
#query = params[:query]
#results = anagrams.select { |word| #query.split('').sort.join == word.split('').sort.join }
end
end

You're passing nothing to the open method. Pass the filename
Change
File.open
to
File.open(file)
open method needs to know at least the filename it has to open

I think you missed a comma.You can write the below code.
file = File.join(Rails.root, 'app', 'models','assets', 'files', 'test_list.txt')
and for reading the content
File.read(file) do |file|
file.each do |line|
p line
end
end

Related

Rails invalid byte sequence in UTF-8 when using htmlentities gem

So I have the controller who scrapes the entire html of a page and stores it into mysql database. Before I store the data I want to encode it using the htmlentities gem. My issue is that with some websites it works ok e.g https://www.lookagain.co.uk/ but with others I get invalid byte sequence in UTF-8 such as https://www.google.co.uk/ and I do not know why. At first I though it might be something wrong with the database so I have changed all the fields to LONGTEXT but the problem still persists
Controller:
class PageScraperController < ApplicationController
require 'nokogiri'
require 'open-uri'
require 'diffy'
require 'htmlentities'
def scrape
#url = watched_link_params[:url].to_s
puts "LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOG#{#url}"
#page = Nokogiri::HTML(open(#url))
coder = HTMLEntities.new
#encodedHTML = coder.encode(#page)
create
end
def index
#savedHTML = ScrapedPage.all
end
def show
#savedHTML = ScrapedPage.find(id)
end
def new
#savedHTML = ScrapedPage.new
end
def create
#savedHTML = ScrapedPage.create(domain: #url, html: #encodedHTML, css: '', javascript: '')
if #savedHTML.save
puts "ADDED TO THE DATABASE"
redirect_to(root_path)
else
puts "FAILED TO ADD TO THE DATABASE"
end
end
def edit
end
def upadate
end
def delete
#watched_links = ScrapedPage.find(params[:id])
end
def destroy
#watched_links = ScrapedPage.find(params[:id])
#watched_links.destroy
redirect_to(root_path)
end
def watched_link_params
params.require(:default).permit(:url)
end
end

Rails won't read a link with nokogiri and open-uri

I have a controller which gets a url passed as a parameter and I am trying to scrape the entire page at that url. But when I try to read the url I get the following error: No such file or directory # rb_sysopen - www.google.com
Controller:
lass PageScraperController < ApplicationController
require 'nokogiri'
require 'open-uri'
require 'diffy'
require 'htmlentities'
def scrape
require 'open-uri'
#url = watched_link_params.to_s
#url = #url.slice(9..#url.length-3)
puts "LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOG#{#url}"
page = Nokogiri::HTML(open(#url))
# coder = HTMLEntities.new
# encodedHTML = coder.encode(page)
puts page
end
def watched_link_params
params.require(:default).permit(:url)
end
end
Try this:
def scrape
#url = watched_link_params[:url]
page = Nokogiri::HTML(open(#url))
puts page
end
You will need to pass in the entire url, including the protocol designator; that is to say, you need to use http://www.google.com instead of www.google.com:
>> params = ActionController::Parameters.new(default: {url: 'http://www.google.com'})
>> watched_link_params = params.require(:default).permit(:url)
>> #url = watched_link_params[:url]
"http://www.google.com"
>> page = Nokogiri::HTML(open(#url))

Running into "uninitialized constant" with whenever gem

Trying to use whenever gem today. Running into this error uninitialized constant EntriesController::RedditScrapper ... how do I fix this?
Current Controller
class EntriesController < ApplicationController
def index
#entries = Entry.all
end
def scrape
RedditScrapper.scrape
respond_to do |format|
format.html { redirect_to entries_url, notice: 'Entries were successfully scraped.' }
format.json { entriesArray.to_json }
end
end
end
lib/reddit_scrapper.rb
require 'open-uri'
module RedditScrapper
def self.scrape
doc = Nokogiri::HTML(open("https://www.reddit.com/"))
entries = doc.css('.entry')
entriesArray = []
entries.each do |entry|
title = entry.css('p.title > a').text
link = entry.css('p.title > a')[0]['href']
entriesArray << Entry.new({ title: title, link: link })
end
if entriesArray.map(&:valid?)
entriesArray.map(&:save!)
end
end
end
config/schedule.rb
RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + '/')
every 2.minutes do
runner "RedditScrapper.scrape", :environment => "development"
end
Please help me to figure out the right runner task to write in ...
Application.rb
require_relative 'boot'
require 'rails/all'
Bundler.require(*Rails.groups)
module ScrapeModel
class Application < Rails::Application
config.autoload_paths << Rails.root.join('lib')
end
end
Rails doesn't auto load the lib folder. You need to add the following line to your config/application.rb:
config.autoload_paths << Rails.root.join('lib')
From what I can tell, you've defined RedditScrapper as a module, but you are trying to use it as a class... (ie calling a method on it).
You can either: turn it into a class (just change module to class) OR define all relevant methods as module_functions
The former is probably preferable given your chosen usage.

Import .csv format file into openproject

After setting up the apache server When we choose the .csv file to be imported I get the following error message:
TypeError in ProductImportsController#create:
no implicit conversion of ActiveSupport::HashWithIndifferentAccess into String
My product_imports_controller.rb:
require 'csv'
class ProductImportsController < ApplicationController
def new
#product_import = ProductImport.new
end
def create
csv_text = File.read(params[:product_import])
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
ProductImport.create!(row.to_hash)
end
end
end
Try this:
The issue is that there is a security aspect with hashes in Controllers.
You try row.to_hash.permit! but i'll doubt that it will work
Create it in the model:
require 'csv'
class ProductImportsController < ApplicationController
def new
#product_import = ProductImport.new
end
def create
csv_text = File.read(params[:product_import])
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
ProductImport.create_from_csv(row.to_hash)
end
end
end
class ProductImport < ActiveRecord::Base
def self.create_from_csv(data)
self.create!(data)
end
end

Unable to access the class in lib from helper module in rails

Hi I am working on rails app. In the function makeRequestToAPI1 of ArticlesHelper module, I am calling the API1 class's (this class is present in lib/) method "createRequest(request). But it is giving me "NoMethodError".
articles_helper.rb
require '././lib/ThirdPartyLibs/api1'
require '././lib/ThirdPartyLibs/api2'
module ArticlesHelper
include EnumsHelper
def makeRequestToAPI1(request)
# here as request to API is same as the original request
# so I am directly passing the request to the API
response = API1.createRequest(request)
Rails.logger.debug "makeRequestToAPI1: #{response}"
end
def makeRequestToAPI2(request)
requestToAPI2 = {}
requestToAPI2.merge!({:items => request[:items]})
requestToAPI2.merge!({:pickup => request[:pickup]})
requestToAPI2.merge!({:drop => request[:drop]})
#response = API2.createRequest(requestToAPI2)
#Rails.logger.debug "makeRequestToAPI2: #{response}"
end
end
api1.rb
class API1
#class << self
def self.createRequest(request)
#response = {:etd => 10, :eta => 20}
end
end
What am I missing here?
Can you please try adding following line to applicaion.rb
config.autoload_paths += %W(#{config.root}/lib)

Resources