website.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. class Website < ActiveRecord::Base
  2. validates_uniqueness_of :domain, scope: :user_id
  3. belongs_to :user
  4. has_many :email_leads #, :foreign_key => :domain, :primary_key => :domain
  5. has_many :phone_leads #, :foreign_key => :domain, :primary_key => :domain
  6. has_many :social_leads #, :foreign_key => :domain, :primary_key => :domain
  7. has_many :companies, :foreign_key => :domain, :primary_key => :domain
  8. scope :by_user, lambda {|user| where(:user_id => user) }
  9. scope :by_domain, lambda {|domain| where(:domain => domain) }
  10. scope :is_active, lambda { where(:is_active => true) }
  11. scope :is_inactive, lambda { where(:is_active => false) }
  12. searchkick callbacks: :async
  13. # after_create :whois_me
  14. def locate_me
  15. ip = IPSocket::getaddress(self.domain)
  16. result = Curl.get("http://#{ENV['API_HOST']}:8882/locate?ip=#{ip}")
  17. if result
  18. self.update(:location => result.body_str)
  19. end
  20. end
  21. def self.import(file)
  22. spreadsheet = open_spreadsheet(file)
  23. header = spreadsheet.row(1)
  24. (2..spreadsheet.last_row).each do |i|
  25. row = Hash[[header, spreadsheet.row(i)].transpose]
  26. website = find_by_id(row["id"]) || new
  27. website.attributes = row.to_hash.slice(*accessible_attributes)
  28. website.save!
  29. end
  30. end
  31. def self.open_spreadsheet(file)
  32. case File.extname(file.original_filename)
  33. when ".csv" then Csv.new(file.path, nil, :ignore)
  34. when ".xls" then Excel.new(file.path, nil, :ignore)
  35. when ".xlsx" then Excelx.new(file.path, nil, :ignore)
  36. else raise "Unknown file type: #{file.original_filename}"
  37. end
  38. end
  39. def whois_me
  40. WhoisWorker.perform_async(self.domain)
  41. end
  42. end