Class: Lead::NuheatDealerScrapper
- Inherits:
-
BaseService
- Object
- BaseService
- Lead::NuheatDealerScrapper
- Defined in:
- app/services/lead/nuheat_dealer_scrapper.rb
Overview
Service object: nuheat dealer scrapper.
Instance Attribute Summary collapse
-
#source_id ⇒ Object
readonly
Returns the value of attribute source_id.
Attributes inherited from BaseService
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, #tagged_logger
Constructor Details
#initialize(options = {}) ⇒ NuheatDealerScrapper
Returns a new instance of NuheatDealerScrapper.
6 7 8 9 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 6 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.
4 5 6 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 4 def source_id @source_id end |
Instance Method Details
#append_match_data(dealer) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 101 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
140 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 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 140 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
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 116 def import_dealers(dealer_list) customers_created = [] error_msg = [] dealer_list.each do |dealer| 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 => e error_msg << "Could not import #{dealer[:dealer_name]} #{e} #{e.inspect}" end puts error_msg.join("\n") customers_created end |
#parse_dealer_node(li) ⇒ Object
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 99 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 29 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.delete('/') end uri.scheme ||= 'https' url = uri.to_s rescue StandardError 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).filter_map(&:presence) # 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).filter_map(&:presence) contact_elements = contact_elements.map do |ce| ce = ce.delete(' ') 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
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/services/lead/nuheat_dealer_scrapper.rb', line 11 def process dealers_html = Rails.root.join("data/nuheat-dealers.html").read(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) next if n[:dealer_name].blank? matched_dd = append_match_data(n) dealer_list << matched_dd new_dealer_count += 1 # if matched_dd[:party_id] end import_dealers(dealer_list) end |