ruby on rails - How can I make this more object-oriented? -


i'm rails noob trying follow dry methods of who've come before me. know i'm doing wrong – i'm not sure or how overcome it.

basically, question how can make code more object-oriented?

i have class podcast, @ point contains bunch of class methods scrape various data around web.

so, example, class method tries discover podcast twitter or facebook feed 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 > ? , siteurl not ?', time.now - 24.hours, nil])     podcast.podcast_logger.info("#{podcast.count}")   else     podcast = podcast.find(:all, :select => 'id, siteurl, name', :conditions => ['siteurl not ?', nil])   end    podcast.each | pod |     puts "#{pod.name}"     begin        # make sure url readable open-uri       if pod.siteurl.include? 'http://'         pod_site = pod.siteurl       else         pod_site = pod.siteurl.insert 0, "http://"       end        # skip of if we're dealing 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 social url contains part of podcast name, grab         # if not, grab first 1 find within our conditions         # give nokogiri room breathe pessimistic exception handling         begin           begin                      twitter_url = doc_links.find {|link| link['href'] =~ /twitter.com\// , 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\// , 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 urls gets saved regardless of 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, know bad code. please me understand why? forever in debt.

thanks,

harris

the major thing can see in code code duplication. if closely, code fetching twitter url , code facebook url same, except 'twitter.com' , 'facebook.com' part. suggestion pull out separate method takes doc_links variable parameter regex finding link. also, i'm not quite sure why you're doing "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 don't "unless ..." part of line, pod_name_fragment defined nil, if don't include exception if try refer pod_name_fragment.

also, should never rescue exception. use standarderror instead. let's program running , try cancel ctrl-c. throws systemexit (i'm not 100% sure name) exception, subclass of exception exit. in cases want exit right away. know isn't applicable rails app, i'm pretty sure there's other reasons catch systemerror instead.

there might more, 1 easy way find "bad code" @ metrics. ryan bates made excellent railscast on metrics (http://railscasts.com/episodes/252-metrics-metrics-metrics), , i'd suggest @ reek in particular finding "code smells". check wiki information on different things mean.


Comments

Post a Comment

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

java - where to store the user credentials in an enterprise application(EAI)? -

java - netbeans "Please wait - classpath scanning in progress..." -