Class: Lead::NuheatDealerScrapper
- Inherits:
-
BaseService
- Object
- BaseService
- Lead::NuheatDealerScrapper
- Defined in:
- app/services/lead/nuheat_dealer_scrapper.rb
Instance Attribute Summary collapse
-
#source_id ⇒ Object
readonly
Returns the value of attribute source_id.
Instance Method Summary collapse
- #append_match_data(dealer) ⇒ Object
- #import_dealer(dealer) ⇒ Object
- #import_dealers(dealer_list) ⇒ Object
-
#initialize(options = {}) ⇒ NuheatDealerScrapper
constructor
A new instance of NuheatDealerScrapper.
- #parse_dealer_node(li) ⇒ Object
- #process ⇒ Object
Methods inherited from BaseService
#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger
Constructor Details
#initialize(options = {}) ⇒ NuheatDealerScrapper
Returns a new instance of NuheatDealerScrapper.
5 6 7 8 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 5 def initialize(={}) @source_id = Source.find_by(referral_code: 'HH6W5I').id super end |
Instance Attribute Details
#source_id ⇒ Object (readonly)
Returns the value of attribute source_id.
3 4 5 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 3 def source_id @source_id end |
Instance Method Details
#append_match_data(dealer) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 100 def append_match_data(dealer) #phone number is easiest party = nil dealer[:contact_elements].each do |ce| cp = ContactPoint.joins(:party).find_phones(ce[1]).first party = cp&.party break if party end if party dealer[:party_id] = party.id dealer[:party_full_name] = party.full_name end dealer end |
#import_dealer(dealer) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 141 def import_dealer(dealer) Customer.transaction do country_iso3 = case dealer[:country] when 'UNITED STATES' 'USA' when 'CANADA' 'CAN' else raise "unrecognized country #{dealer[:country]}" end catalog_id = country_iso3 == 'CAN' ? 2 : 1 cu = Customer.create!( full_name: dealer[:dealer_name], source_id: source_id, catalog_id: catalog_id, creation_method: 'crm', profile_id: 20, # TP - Other state: 'lead_qualify' ) cu.create_activity('SOLICIT', description: 'Existing Nuheat Dealer') dealer[:contact_elements].each do |ce| cu.contact_points.create!(category: ce[0], detail: ce[1]) end cu.addresses.create!( street1: dealer[:address1], street2: dealer[:address2], city: dealer[:city], state_code: dealer[:state_code], zip: dealer[:zip_code], country_iso3: country_iso3, skip_carrier_validation: true ) cu end end |
#import_dealers(dealer_list) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 115 def import_dealers(dealer_list) customers_created = [] error_msg = [] dealer_list.each do |dealer| begin if dealer[:party_id] && dealer[:url] # append url to contact points cu = Party.find(dealer[:party_id]).customer puts "Adding #{dealer[:url]} to customer id #{cu.reference_number}" cu.contact_points.where(category: ContactPoint::WEBSITE).first_or_create!(detail: dealer[:url]) end #if dealer[:party_id].nil? #customers_created << import_dealer(dealer) #puts "#{cu.id} customer created #{cu.full_name} #{cu.reference_number}" #else # puts "Existing dealer" # do something else #end rescue StandardError => exc error_msg << "Could not import #{dealer[:dealer_name]} #{exc.to_s} #{exc.inspect}" end end puts error_msg.join("\n") customers_created end |
#parse_dealer_node(li) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 28 def parse_dealer_node(li) dealer_name = li.css('div.col1 strong').text&.squish original_url = li.css('div.col1 a').text&.squish&.presence url = nil if original_url.present? begin uri = Addressable::URI.parse(original_url) if uri.host.nil? && uri.path.present? p = uri.path uri.path = nil uri.host = p.gsub(/\//,'') end uri.scheme ||= 'https' url = uri.to_s rescue StandardError => exc puts "Unparsable url: #{original_url}" end end # looks like we have to clean this up address_raw = li.css('div.col1').text address_elements = address_raw.split("\n").map(&:squish).map(&:presence).compact # Reject those elements already processed original_address_elements = address_elements.reject{|e| e == dealer_name || e == original_url } if original_address_elements.present? address_elements = original_address_elements.dup # So we preserve it # start from the bottom country = address_elements.pop # then its the postal code/zip zip_code = address_elements.pop # then it's a city/state city, state_code = address_elements.pop.split(', ') # Then it's address1 and 2 address1, address2 = address_elements end # if state code is longer than two characters, map it to code if state_code.present? && state_code.size > 2 if s = State.where("name ILIKE ?", state_code).first state_code = s.code end end contact_raw = li.css('div.col2').text contact_elements = contact_raw.split("\n").map(&:squish).map(&:presence).compact contact_elements = contact_elements.map do |ce| ce = ce.gsub(/ /,'') category,detail = ce.split(':') case category when 'Phone','Fax' cp_category = category.downcase detail = PhoneNumber.parse_and_format(detail) else #Email most likely cp_category = 'email' detail = category end [cp_category, detail] end { dealer_name: dealer_name, url: url, #address_elements: original_address_elements, address1: address1, address2: address2, city: city, state_code: state_code, zip_code: zip_code, country: country, contact_elements: contact_elements } end |
#process ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 10 def process dealers_html = IO.read(Rails.root.join('data','nuheat-dealers.html'), chomp: true) parsed_data = Nokogiri::HTML.parse(dealers_html) list = parsed_data.xpath("//li") dealer_list = [] new_dealer_count = 0 list.each_with_index do |li, index| n = parse_dealer_node(li) if n[:dealer_name].present? matched_dd = append_match_data(n) dealer_list << matched_dd new_dealer_count += 1 #if matched_dd[:party_id] end end import_dealers(dealer_list) end |