Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.05 KB | None | 0 0
  1. desc "Inserts all the link data from the link building spreadsheet in Google Drive"
  2. task :fetch_actual_links => :environment do
  3.   Client.all.each do |client|
  4.     worksheet = GoogleDriveAPI.get_link_worksheet(client.client_name)
  5.     puts "checking for new links for " + client.client_name
  6.  
  7.     # A worksheet for this client was found
  8.     if(worksheet != nil)
  9.         (14..worksheet.num_rows).each do |i|
  10.               # Read the data from the columns
  11.               link_directory_url = worksheet[i, 1].downcase
  12.               listing_url = worksheet[i, 3]
  13.               status = worksheet[i, 5]
  14.                 submission_date = worksheet[i, 6]
  15.  
  16.                 # Fetch the correct link directory to attach this actual link to
  17.                
  18.               # If the link directory does not exist - create it
  19.                 if(LinkDirectory.where("url = ?", link_directory_url).blank?)
  20.                 puts link_directory_url + " does not exist in the db, adding it..."
  21.                     LinkDirectory.create(url: link_directory_url)
  22.                 link_directory = LinkDirectory.where("url = ?", link_directory_url).first
  23.               else
  24.                 puts link_directory_url + " already exists in the db"
  25.                 link_directory = LinkDirectory.where("url = ?", link_directory_url).first
  26.                 end
  27.  
  28.               # If the link does not already exist - create it
  29.               if(ActualLink.where("link_directory_id = ? AND client_id = ?", link_directory.id, client.id).count == 0 && status != '')
  30.                 puts "creating a link..."
  31.                 ActualLink.create(link_directory_id: link_directory.id, client_id: client.id, status: status, listing_url: listing_url)    
  32.               elsif(ActualLink.where("link_directory_id = ? AND client_id = ?", link_directory.id, client.id).count > 0 && status != '')
  33.                 actual_link = ActualLink.where("link_directory_id = ? AND client_id = ?", link_directory.id, client.id).first
  34.                 actual_link.status = status
  35.                 actual_link.save
  36.               end
  37.             end
  38.     end
  39.   end
  40. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement