Currency fetch does not work anymore - ruby-on-rails

So I have this code in my applicaton which is used to get a xml list of current rates from web and save them for future use in the app.
def get_rates
today_path = Rails.root.join 'rates', "#{Date.today.to_s}.xml"
Hash[Hash.from_xml(if File.exists? today_path
File.read today_path
else
xml = Net::HTTP.get URI 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
File.write today_path, xml
xml
end)["Envelope"]["Cube"]["Cube"]["Cube"].map &:values]
end
This was written like half a year ago.
Now since today, it does not work anymore. I get this error:
NameError in FormController#converter
uninitialized constant FormController::Net
What has gone wrong?

It looks like the net/http library is not being required. It may have been required somewhere else in your application and that line was deleted or a gem could have been removed that loaded the library which allowed it to work. Try adding the following line at the top of your file before the class definition and see if it works again.
require "net/http"

It looks like you need to do require 'net/http'. Add that line in your form_controller.rb file and try to run that method again to check if it works.

Related

Changing YAML file without restarting Rails 4

I want to be able to edit a YAML file and reload it in a Rails 4 app. Right now I am loading the YAML file via an initializer, and I know that this will load the file only once and a restart will be required after changing it.
How could I accomplish a YAML reloading/refreshing as it is accomplished via i18n YAML files in Rails?
You can try something in the lines of checking for file's change time, for example:
module MyFileReader
def self.my_yaml_contents
if #my_yaml_file_ctime != File.ctime(file_name)
#my_yaml_contents = YAML.load(File.open(file_name))
#my_yaml_file_ctime = File.ctime(file_name)
end
#my_yaml_contents
end
end
MyFileReader.my_yaml_contents method will load and parse the file only on startup and change and serve the already parsed data in the meantime,
see http://www.ruby-doc.org/core-2.0.0/File.html#method-c-ctime
When you load the file, I assume you assign it to some variable or constant. If instead you don't assign it, then the load will be performed every time.
Instead of:
CONTENT = Yaml.load_file('your_file.yml')
create a simple class or function:
module YourFileReader
def self.load
Yaml.load_file('your_file.yml')
end
end
and use the defined method to read the file in your app
YourFileReader.load
or even simpler, use
Yaml.load_file('your_file.yml')
directly in your app where you need to read the file.
Instead of require use load to load the file.
require will load files only once. But load will load when it is called.
See more about this here http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/

Problems parsing a web-based XML file

I coded a little Ruby script that would parse a remote XML file and extract some data from it using Nokogiri. Now I'm trying to code a more advanced version as a Rails application.
I have my code inside of a controller. It's similar to the code that I used in my Ruby script, however it's not working. I believe the error is because it's trying to load the XML locally rather then externally.
Here is the error that Rails is giving me:
No such file or directory - http://mal-api.com/anime/10?format=xml
Here is a sample of the code in my controller: (I can provide the whole thing if needed, but it's mainly just the default Rails scaffold code.)
def create
require 'nokogiri'
#anime = Anime.new(params[:anime])
doc = Nokogiri::XML(open("http://mal-api.com/anime/#{#anime.mal_id}?format=xml"))
#Title
title = doc.css("anime english_title").inner_html
#Snipped rails scaffold code
end
mal_id is passed in through a form. Nokogiri is added in my Gemfile.
Is there something I'm missing or that I've done wrong?
Any help is appreciated.
By default the open method in ruby is used to open files. If you want to directly open an URL you need to require 'open-uri'. More information can be found in the docs: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/OpenURI.html

Rails 3 application/octet-stream

I am working on an application where I use paperclip for uploading images, then the image is manipulated in a flash app and returned to my application using application/octet-stream. The problem is that the parameters from flash are not available using params. I have seen examples where something like
File.open(..,..) {|f| f.write(request.body) }
but when I do this, the file is damaged some how.
How can I handle this in rails 3?
After you make sure that the request parameters have hit the Rails application, you may want to ensure that there were no parsing problems. Try to add these lines in you controller's action:
def update # (or whatever)
logger.debug "params: #{params.inspect}"
# I hope you do not test this using very large files ;)
logger.debug "request.raw_post: #{request.raw_post.inspect}"
# ...
end
Maybe the variable names got changed somehow? Maybe something escaped the parameter string one time too much?
Also, you have said that the file into which you want to save the request body is damaged. How exactly?
The request.body object does not need to be String. It may be a StringIO, for example, so you may want to type this:
File.open(..,..) {|f| f.write(request.body.read) }

rails3, confused about using params[:filename].tempfile.path vs params[:filename][:tempfile].path

On a Rails 3 app hosted at Heroku, where a multipart file is POSTed to my app, I'm trying to use some sample code that says :
File.open(params['filename'][:tempfile].path)
however, my logs show the error NoMethodErr no such method as tempfile.
I also tried
File.open(params[:filename].tempfile.path)
got the same error.
I added require 'tempfile' to my Controller, made no difference.
When a file is posted to your application, the object in the params should already be a Tempfile so calling [:tempfile] or .tempfile should not be necessary. Try something like this:
File.open params[:filename].path

Uninitialized Constant MyClass::Playback

I'm attempting to use the ruby-alsa gem to provide audio playback on the server. Unfortunately, I keep getting an uninitialized constant MyClass::Playback exception when attempting to do so.
I'm very new to Ruby and Rails, so I'm not sure how to resolve this issue. The following has been added to my Gemfile and I have run a bundle install:
gem 'ruby-alsa'
My controller code looks like this (though I can't even begin to guarantee the validity of the code):
# Test audio playback
file = File.open("sample.wav")
#ALSA::PCM::Playback.open do |playback| # This line is commented out because it didn't work
Playback.open do |playback|
playback.write do |length|
file.read length
end
end
file.close
Update: If I uncomment the following line, I get the same exception (except ALSA is the uninitialized constant):
ALSA::PCM::Playback.open do |playback|
Looking at this briefly, it looks like your Gemfile needs to be:
gem 'ruby-alsa', :require => 'alsa'
It looks as if your commented-out code is correct; you should be using ALSA::PCM::Playback.
Your next problem is that write is an instance method of that class. As shown on the page you linked to, it appears that correct usage might be more like:
ALSA::PCM::Playback.open do |playback|
playback.write do |length|
file.read length
end
end
(Caveat: I know nothing about ALSA or this gem, so I have no idea what the above should do.)
Your code is inside a class so you need to do this:
::ALSA::PCM::Playback.open do |playback|
Note the preceding double colon.
Did you try to require it ?
require 'ruby-alsa'
edit:
Try to require rubygems first
require 'rubygems'
require 'ruby-alsa'

Resources