Ruby on Rails: How to handle exceptions(rescue?) - ruby-on-rails

I'm fetching videos' data from youtube, including general video info, and likes number of them.
The problem is that sometimes, there is no corresponding data. Maybe the video is private, or the likes number is 0.
In either case, there will report NoMethodError: undefined method 'likes' for nil:NilClass
How can I handle this? I just want to ignore them and continue to the next youtube video.
I think I can use something like rescue, but I don't find many tutorial of it.
Here is my code:
client = YouTubeIt::Client.new(:dev_key => my_key)
Video.all.each do |video|
video_info = client.video_by(video.url)
like_number = video_info.rating.likes.to_i
video.likes = like_number
if video.save
puts "#{video.title} likes: #{video.likes}"
end
end

Rather that using the exception handling, which is slower, in this case just check for the presence of the value before calling the method.
Assuming the error is generated on the line
like_number = video_info.rating.likes.to_i
simply use
client = YouTubeIt::Client.new(:dev_key => my_key)
Video.all.each do |video|
video_info = client.video_by(video.url)
# here check for the presence.
if video_info.rating
like_number = video_info.rating.likes.to_i
video.likes = like_number
if video.save
puts "#{video.title} likes: #{video.likes}"
end
end
end

You should check for all possible nil values with, eg, video.nil? and corresponding conditionals. Rescuing should always be your last resort (it's slow and dirty), but, if there is no way to predict where the script will fail, you can always rescue some piece of code:
begin
#your error-prone code
rescue
#action you want to take in case your error-prone code raises error
end
Here is a good article on exceptions: http://www.tutorialspoint.com/ruby/ruby_exceptions.htm.

In this example, the method body acts as default begin block so need of begin block
def any_method
client = YouTubeIt::Client.new(:dev_key => my_key)
Video.all.each do |video|
video_info = client.video_by(video.url)
# try tries to find the method, if not found returns nil
like_number = video_info.try(:rating).try(:likes).to_i
video.likes = like_number
if video.save
puts "#{video.title} likes: #{video.likes}"
end
end
# For any type of standard errors, use rescue block
rescue => error_object
p 'Sorry, some error occured'
p error_object
end

Related

How to DRY a list of functions in ruby that are differ only by a single line of code?

I have a User model in a ROR application that has multiple methods like this
#getClient() returns an object that knows how to find certain info for a date
#processHeaders() is a function that processes output and updates some values in the database
#refreshToken() is function that is called when an error occurs when requesting data from the object returned by getClient()
def transactions_on_date(date)
if blocked?
# do something
else
begin
output = getClient().transactions(date)
processHeaders(output)
return output
rescue UnauthorizedError => ex
refresh_token()
output = getClient().transactions(date)
process_fitbit_rate_headers(output)
return output
end
end
end
def events_on_date(date)
if blocked?
# do something
else
begin
output = getClient().events(date)
processHeaders(output)
return output
rescue UnauthorizedError => ex
refresh_token()
output = getClient().events(date)
processHeaders(output)
return output
end
end
end
I have several functions in my User class that look exactly the same. The only difference among these functions is the line output = getClient().something(date). Is there a way that I can make this code look cleaner so that I do not have a repetitive list of functions.
The answer is usually passing in a block and doing it functional style:
def handle_blocking(date)
if blocked?
# do something
else
begin
output = yield(date)
processHeaders(output)
output
rescue UnauthorizedError => ex
refresh_token
output = yield(date)
process_fitbit_rate_headers(output)
output
end
end
end
Then you call it this way:
handle_blocking(date) do |date|
getClient.something(date)
end
That allows a lot of customization. The yield call executes the block of code you've supplied and passes in the date argument to it.
The process of DRYing up your code often involves looking for patterns and boiling them down to useful methods like this. Using a functional approach can keep things clean.
Yes, you can use Object#send: getClient().send(:method_name, date).
BTW, getClient is not a proper Ruby method name. It should be get_client.
How about a combination of both answers:
class User
def method_missing sym, *args
m_name = sym.to_s
if m_name.end_with? '_on_date'
prop = m_name.split('_').first.to_sym
handle_blocking(args.first) { getClient().send(prop, args.first) }
else
super(sym, *args)
end
end
def respond_to? sym, private=false
m_name.end_with?('_on_date') || super(sym, private)
end
def handle_blocking date
# see other answer
end
end
Then you can call "transaction_on_date", "events_on_date", "foo_on_date" and it would work.

Rails: delete a video object when its youtube source is deleted?

I use this code to get info of an youtube video(youtube_itgem)
video_info = Video.yt_session.video_by(self.source_id)
hits = video_info.view_count
if video_info.rating
self.rating = video_info.rating.average
self.likes = video_info.rating.likes.to_i
end
The problem is that sometimes, the original youtube video is deleted, like this: https://www.youtube.com/watch?v=lsZFkiimpKI.
So video_info = Video.yt_session.video_by(self.source_id) will reports OpenURI::HTTPError: 404, and the code cannot continue to get info of next videos.
I want to delete this non-exist video its source is not available anymore, how can I do that? Maybe by rescue?
I tried if Video.yt_session.video_by(self.source_id), but it doesn't work.
Did you try the following:
begin
video_info = Video.yt_session.video_by(self.source_id)
hits = video_info.view_count
if video_info.rating
self.rating = video_info.rating.average
self.likes = video_info.rating.likes.to_i
end
rescue OpenURI::HTTPError
# Delete the entry
end
Example with multiple cases of catching errors:
begin
# Do the thing
rescue OpenURI::HTTPError
# Delete the entry
rescue OpenURI::DifferentErrorType
# Do something different
resuce OpenURI::AnotherType, OpenURI::ASpecialError
# Do something radically different
end

Rails + Koala: Recover from exception and continue

Assuming the message argument is a string, I have the following snippet:
users.each do |user|
posted = Facebook.post_to_facebook(message,user.token)
end
Then, post_to_facebook is a method like this:
def post_to_facebook
facebook_graph = Koala::Facebook::GraphAPI.new(token)
object_from_koala = facebook_graph.put_wall_post(message)
end
For some users, when calling the put_wall_post I get an exception of this type: Koala::Facebook::APIError. I just want to skip posting to that user's wall, and go to the next user, but I fail to see how to do that managing the exception.
you could use rescue like this, and maybe handle your exceptions
def post_to_facebook
begin
facebook_graph = Koala::Facebook::GraphAPI.new(token)
object_from_koala = facebook_graph.put_wall_post(message)
rescue
do something else
end

How can I make this more object-oriented?

I'm a rails noob trying to follow the DRY methods of those who've come before me.
I know that I'm doing something wrong – I'm just not sure what that is or how to overcome it.
Basically, my question is how can I make this code more object-oriented?
I have a class Podcast, which at this point just contains a bunch of class methods that scrape various data from around the web.
So, for example, this class method tries to discover a podcast twitter or facebook feed from their website:
def self.social_discovery(options = {})
new_podcasts_only = options[:new_podcasts_only] || false
if new_podcasts_only
podcast = Podcast.find(:all, :select => 'id, siteurl, name', :conditions => ['created_at > ? and siteurl IS NOT ?', Time.now - 24.hours, nil])
Podcast.podcast_logger.info("#{podcast.count}")
else
podcast = Podcast.find(:all, :select => 'id, siteurl, name', :conditions => ['siteurl IS NOT ?', nil])
end
podcast.each do | pod |
puts "#{pod.name}"
begin
# Make sure the url is readable by open-uri
if pod.siteurl.include? 'http://'
pod_site = pod.siteurl
else
pod_site = pod.siteurl.insert 0, "http://"
end
# Skip all of this if we're dealing with a feed
unless pod_site.downcase =~ /.rss|.xml|libsyn/i
pod_doc = Nokogiri.HTML(open(pod_site))
pod_name_fragment = pod.name.split(" ")[0].to_s
if pod_name_fragment.downcase == "the"
pod_name_fragment = pod.name.split(" ")[1].to_s unless pod.name.split(" ")[1].to_s.nil?
end
doc_links = pod_doc.css('a')
# If a social url contains part of the podcast name, grab that
# If not, grab the first one you find within our conditions
# Give Nokogiri some room to breathe with pessimistic exception handling
begin
begin
twitter_url = doc_links.find {|link| link['href'] =~ /twitter.com\// and link['href'].match(/#{pod_name_fragment}/i).to_s != "" unless link['href'] =~ /share|status/i}.attribute('href').to_s
rescue Exception => ex
if doc_links.find {|link| link['href'] =~ /twitter.com\// unless link['href'] =~ /share|status/i}.nil?
twitter_url = nil
else
twitter_url = doc_links.find {|link| link['href'] =~ /twitter.com\// unless link['href'] =~ /share|status/i}.attribute('href').to_s
end
end
begin
facebook_url = doc_links.find {|link| link['href'] =~ /facebook.com\// and link['href'].match(/#{pod_name_fragment}/i).to_s != "" unless link['href'] =~ /share|.event/i}.attribute('href').to_s
rescue Exception => ex
if doc_links.find {|link| link['href'] =~ /facebook.com\// unless link['href'] =~ /share|.event/i}.nil?
facebook_url = nil
else
facebook_url = doc_links.find {|link| link['href'] =~ /facebook.com\// unless link['href'] =~ /share|.event/i}.attribute('href').to_s
end
end
rescue Exception => ex
puts "ANTISOCIAL"
# Ensure that the urls gets saved regardless of what else happens
ensure
pod.update_attributes(:twitter => twitter_url, :facebook => facebook_url)
end
puts "#{twitter_url}" + "#{facebook_url}"
Podcast.podcast_logger.info("#{twitter_url}" + "#{facebook_url}")
end
rescue Exception => ex
puts "FINAL EXCEPTION: #{ex.class} + #{ex.message}"
end
end
end
Again, I know this is bad code. Please help me understand why?
I will be forever in your debt.
Thanks,
Harris
The major thing I can see in your code is code duplication. If you look closely, the code for fetching the twitter url and the code for the facebook url is almost exactly the same, except for the 'twitter.com' and 'facebook.com' part. My suggestion is to pull that out into a separate method that takes the doc_links variable as a parameter as well as the regex for finding the link. Also, I'm not quite sure why you're doing the "unless ..." part here:
if pod_name_fragment.downcase == "the"
pod_name_fragment = pod.name.split(" ")[1].to_s unless pod.name.split(" ")[1].to_s.nil?
end
If you don't do the "unless ..." part of the line, pod_name_fragment will be defined but nil, but if you don't include it you will get an exception if you try to refer to pod_name_fragment.
Also, you should almost never rescue Exception. Use StandardError instead. Let's say your program is running and you try to cancel it with Ctrl-C. That throws a SystemExit (I'm not 100% sure about the name) exception, which is a subclass of the Exception exit. In most cases you would then want to exit right away. I know this isn't that much applicable for a Rails app, but I'm pretty sure there's other reasons to catch SystemError instead.
There might be more, one easy way to find "bad code" is to look at metrics. Ryan Bates made an excellent Railscast on metrics (http://railscasts.com/episodes/252-metrics-metrics-metrics), and I'd suggest you look at Reek in particular for finding "code smells". Check their wiki for information on what the different things mean.

save an active records array

I have an array like this
a = []
a << B.new(:name => "c")
a << B.new(:name => "s")
a << B.new(:name => "e")
a << B.new(:name => "t")
How i can save it at once?
B.transaction do
a.each(&:save!)
end
This will create a transaction that loops through each element of the array and calls element.save on it.
You can read about ActiveRecord Transactions and the each method in the Rails and Ruby APIs.
a.each(&:save)
This will call B#save on each item in the array.
So I think we need a middle ground to Alexey's raising exceptions and aborting the transaction and Jordan's one-liner solution. May I propose:
B.transaction do
success = a.map(&:save)
unless success.all?
errored = a.select{|b| !b.errors.blank?}
# do something with the errored values
raise ActiveRecord::Rollback
end
end
This will give you a bit of both worlds: a transaction with rollback, knowledge of which records failed and even gives you access to the validation errors therein.
Wrapping save in transaction will not be enough: if a validation is not passed, there will be no exception raised and no rollback triggered.
I can suggest this:
B.transaction do
a.each do |o|
raise ActiveRecord::Rollback unless o.save
end
end
Just doing B.transaction do a.each(&:save!) end is not an option either, because the transaction block will not rescue any exception other than ActiveRecord::Rollback, and the application would crash on failed validation.
I do not know how to check afterwards if the records have been saved.
Update. As someone has downrated my answer, i assume that the person was looking for a cut-and-paste solution :), so here is some (ugly :)) way to process fail/success value:
save_failed = nil
B.transaction do
a.each do |o|
unless o.save
save_failed = true
raise ActiveRecord::Rollback
end
end
end
if save_failed
# ...
else
# ...
end
I know this is an old question but I'm suprised no one thought of this:
B.transaction do
broken = a.reject { |o| o.save }
raise ActiveRecord::Rollback if broken.present?
end
if broken.present?
# error message
end
In case you're looking for more efficient solution than save each row in the loop please look my answer here Ruby on Rails - Import Data from a CSV file
I'm suggesting to use gem activerecord-import there.

Resources