phone_lead.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. class PhoneLead < ActiveRecord::Base
  2. validates_uniqueness_of :number, scope: :user_id
  3. belongs_to :user
  4. belongs_to :website #, :foreign_key => :domain, :primary_key => :domain
  5. alias_attribute :display_name, :better_number
  6. alias_attribute :location, :state
  7. alias_attribute :website, :original_url
  8. after_validation :geotag
  9. scope :from_state, lambda {|state| where(:state => state) }
  10. scope :from_country, lambda {|country| where(:country => country) }
  11. scope :by_user, lambda {|user| where(:user_id => user) }
  12. scope :by_domain, lambda {|domain| where(:domain => domain) }
  13. # after_create :get_page_text
  14. searchkick callbacks: :async
  15. def get_page_text
  16. ExtractWorker.perform_async("PhoneLead", self.id) # unless Rails.env.development?
  17. end
  18. def event_name
  19. ["matched from ", self.domain].join
  20. end
  21. def tags
  22. self.keywords.delete("[").delete("]").delete('"')
  23. end
  24. def icon
  25. "phone"
  26. end
  27. def ux_color
  28. "pink"
  29. end
  30. def fa_icon
  31. "phone"
  32. end
  33. def display_name
  34. self.better_number
  35. end
  36. def index_path
  37. "/phone_leads"
  38. end
  39. def area_code
  40. if self.number[0].to_s == "1"
  41. area = self.number.to_s[1..3]
  42. else
  43. area = self.number.to_s[0..2]
  44. end
  45. area
  46. end
  47. def geotag
  48. identifier = Phonelib.parse (self.number)
  49. begin
  50. state = Integer(self.area_code).to_region
  51. self.location = state if state
  52. rescue
  53. # Ignore
  54. end
  55. if identifier
  56. self.number_type = identifier.human_type
  57. self.country = identifier.country
  58. self.location ||= identifier.geo_name
  59. else
  60. self.destroy
  61. end
  62. end
  63. def self.to_csv(options = {})
  64. csv_string = CSV.generate(options) do |csv|
  65. csv << ["Phone", "Tags", "Source", "Date"]
  66. all.each do |record|
  67. csv << [record.number, record.keywords, record.domain, record.created_at.to_date]
  68. end
  69. end
  70. csv_string
  71. end
  72. def color
  73. "orange"
  74. end
  75. def score
  76. missing = self.attributes.values.select(&:nil?).count
  77. total = self.attributes.count
  78. (((total.to_f - missing.to_f) / total.to_f) * 100).round(1)
  79. end
  80. def better_number
  81. self.number.phony_formatted(:normalize => :US, :spaces => '-')
  82. end
  83. end