I try to load a yaml file into an array but it fails with undefined method `join' for "a b c":String
# Check certain temporarily emails
# Throw notice not accepted use other email
require 'yaml'
bad_hostnames = YAML::load(File.read("#{Rails.root}/config/bad_hosts.yml"))
if /^(#{bad_hostnames.join("|")})$/.match(host)
errors.add(:email, "Please not use a disposable mailbox")
end
So i required yaml before and on top of the model, controller where I load the yml in:
require 'yaml'
Still same result, in rails console this works flawlessly, what am I missing?
The above code is inside my user.rb model, in console it works
EDIT:
bad_hosts.yml looks like (shortened) 1 provider the line
0-mail.com
10minutemail.com
30minutemail.com
4warding.net
Your .yml file is not a YAML file.
This would make it .yml file.
- 0-mail.com
- 10minutemail.com
- 30minutemail.com
- 4warding.net
But when you want to load just a file line by line try the following:
lines = IO.readlines("#{Rails.root}/config/bad_hosts.yml")
# note: lines end in "\n"
Related
I have the following code to connect my rails app to my FTP. This works great. However, I want to use open-uri to open the csv file so I can parse it. Any ideas how to do this? I think it's an easy thing to do but I'm missing something.
require 'net/ftp'
ftp = Net::FTP.new
ftp.connect("xxx.xxx.xx.xxx",21)
ftp.login("xxxxx","xxxx")
ftp.chdir("/")
ftp.passive = true
puts ftp.list("TEST.csv")
You'll need to use #gettextfile.
A) Get the file to a local temporary file and read its content
# Creating a tmp file can be done differently as well.
# It may also be omitted, in which case `gettextfile`
# will create a file in the current directory.
Dir::Tmpname.create(['TEST', ['.csv']) do |file_name|
ftp.gettextfile('TEST.csv', file_name)
content = File.read(file_name)
end
B) Pass a block to gettextfile and get the content one line at a time
content = ''
ftp.gettextfile('TEST.csv') do |line|
content << line
end
I'm using Selenium-Webdriver, OpenUri and Nokogiri to scrape a website. I want to download a particular image from said website to my Ubuntu computer. I tried a few different methods but each of them gives a different error message.
Here's my base code, which opens the website and gets the image url (everything after this I ran in my pry console):
require 'open-url'
require 'selenium-webdriver'
require 'nokogiri'
require 'uri'
url = "https://www.google.com/"
browser = Selenium::WebDriver.for :chrome
document = open(url).read
parsed_content = Nokogiri::HTML(content)
image = "https://www.google.com" + parsed_content.css('#hplogo').attr('src').value
binding.pry
1) Here's the first thing I tried to download the image:
download = open(image)
IO.copy_stream(download, '~/image.png')
For this, I got the following error:
Errno::ENOENT: No such file or directory # rb_sysopen - ~/image.png from (pry):44:in 'initialize'
As per this question, I tried adding a directory in the code:
FileUtils.mkdir_p(image) unless File.exist?(image)
But I got the same error.
2) Next I tried this:
open('image.png', 'wb') do |file|
file << open(image).read
end
and this returns
#<File:image.png (closed)
but the file isn't anywhere on my computer and I can't figure out what that message means.
3) Next I tried
IO.copy_stream(open(image), 'image.png')
which simply returned this:
5482
but again, I have no idea what that means and the file isn't anywhere.
4) Finally I tried
read_image = open(image).read
File.open(image, 'image.png') do |file|
file.puts read_image
end
which outputs
ArgumentError: invalid access mode image.png
from (pry):53:in 'initialize
What am I doing wrong? Was I close with any of my approaches?
File open second argument is mode for file openning.
read_image = open(image).read
File.open('image.png', 'w+') do |file|
file.write read_image
end
Your third variant works good.
5482 - length of file. File 'image.png' in same directory as your .rb file.
I have a problem with my attachment system on web page. I store them on amazon S3 using paperclip. I have an option to copy attachment to new file. Everything works fine until there are polish special characters in title, like: ŁĄKA.jpg. Then I get an error:
Saving error: Appendix Paperclip::Errors::NotIdentifiedByImageMagickError
/Users/michal/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/validations.rb:79:in `raise_record_invalid'
/Users/michal/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/validations.rb:43:in `save!'
My code:
instance.appendixes.select {|a| a.temporary? && !a.appendix.exists?}.each do |a|
a.appendix = S3File.new(a.s3path)
a.process = false
a.appendix_url = nil
puts "CREATING NEW FILE from (temporary?) appendix: #{a.id}, path: #{a.s3path}, is_public: #{a.is_public}, determine_is_public: #{a.determine_is_public}"
a.is_public = a.determine_is_public
logger.debug("CREATING NEW FILE from (temporary?) appendix: #{a.id}, path: #{a.s3path}, is_public: #{a.is_public}, determine_is_public: #{a.determine_is_public}")
a.save! # bo delayed_job
end
I'm getting error on a.save! when path is like: appendixes/appendixes/242/original/%25C5%2581A%25CC%25A8KA.jpg, but works like charm when it is: appendixes/appendixes/243/original/laka.jpg or another file name without polish letters. Anybody had this kind of problem or have suggestions how to fix it?
Ok, I found what was wrong. I had to replace in a.s3path, the last part with original name (łąka.jpg) and everything works fine. So when I have:
S3File.new(appendixes/appendixes/243/original/łąka.jpg) it works good and finds the correct file on s3 server.
I'm trying to load a library from a Controller and it is throwing me this error
cannot load such file -- /xsd
It is, in fact, being loaded in the first line of the library file
require 'xsd/qname'
# {http://www.f2b.com.br/soap/wsbillingaction.xsd}F2bAcaoCobranca
# mensagem - F2bAcaoCobranca::Mensagem
# cliente - F2bAcaoCobranca::Cliente
# acao_cobranca - F2bAcaoCobranca::Acao_cobranca
class F2bAcaoCobranca
#...
end
The library's file is in the following directory:
lib
--f2b
--acao_cobranca
--wsbillingaction.rb
And my controller's action has the following lines of code
def index
require 'f2b/acao_cobranca/wsbillingaction.rb'
end
What is giving me this error? Is it some old stuff from previous versions of Ruby/Rails? How can I fix it?
wsbillingaction depend on another lib (xsd/qname), you should include it in your project or install it as a gem
I'm using Rack::Directory.new to map a directory in the public folder directly to the website and serve static files.
My config.ru contains:
map '/pdfs/' do
run Rack::Directory.new('./public/resources/pdfs')
end
require './app/main.rb'
run MainSinatra
I'm using a Sinatra App to run the rest of the application and using cucumber-sinatra to pre-generate some files.
When I try to load the path to the pdfs in my paths file I get an error.
features/support/paths.rb:
def path_to(page_name)
case page_name
when /dias_all/
'/pdfs'
...
end
Then on my steps file I put
visit dias_all
and I get a 404 error.
I tried adding this to the features/support/env.rb file but still got an error:
class MainSinatraWorld
...
include Rack::Test::Methods
def app
Rack::Directory.new('./public/resources/pdfs')
end
end
and then changed the step file visit to a get, but still got an error.
get('/pdfs/dias')
error:
No response yet. Request a page first. (Rack::Test::Error)
How do I test the contents of the folder through Capybara or Cucumber?
I was able to figure out how to solve this problem by changing this line in env.rb from:
Capybara.app = MainSinatra
to:
Capybara.app = eval "Rack::Builder.new {( " + File.read(File.dirname(__FILE__) +
'/../../config.ru') + "\n )}"
And this runs the application from the rack up file (config.ru) and loads all the middleware that wasn't otherwise loading.
I found the answer in this blog.