Class: Lead::MidAtlanticScrapper

Inherits:
BaseService show all
Defined in:
app/services/lead/mid_atlantic_scrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

#initialize(options = {}) ⇒ MidAtlanticScrapper

Returns a new instance of MidAtlanticScrapper.



5
6
7
8
9
10
# File 'app/services/lead/mid_atlantic_scrapper.rb', line 5

def initialize(options={})
  @source_id =  4647 # Association > Professional Remodeling Organization (PRO) Mid Atlantic
  @buying_group_id = 123 # PRO ATLANTIC
  @profile_id = 1 # DL - Generic
  super
end

Instance Attribute Details

#buying_group_idObject (readonly)

Returns the value of attribute buying_group_id.



3
4
5
# File 'app/services/lead/mid_atlantic_scrapper.rb', line 3

def buying_group_id
  @buying_group_id
end

#dealers_jsonObject (readonly)

Returns the value of attribute dealers_json.



3
4
5
# File 'app/services/lead/mid_atlantic_scrapper.rb', line 3

def dealers_json
  @dealers_json
end

#profile_idObject (readonly)

Returns the value of attribute profile_id.



3
4
5
# File 'app/services/lead/mid_atlantic_scrapper.rb', line 3

def profile_id
  @profile_id
end

#source_idObject (readonly)

Returns the value of attribute source_id.



3
4
5
# File 'app/services/lead/mid_atlantic_scrapper.rb', line 3

def source_id
  @source_id
end

Instance Method Details

#append_match_data(dealer) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'app/services/lead/mid_atlantic_scrapper.rb', line 90

def append_match_data(dealer)
  #phone number is easiest
  party = nil
  cp = ContactPoint.joins(:party).find_phones(dealer[:phone1]).first
  party = cp&.party
  if party
    dealer[:party_id] = party.id
    dealer[:party_full_name] = party.full_name
  end
  dealer
end

#dump_to_csv(file_path = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'app/services/lead/mid_atlantic_scrapper.rb', line 79

def dump_to_csv(file_path = nil)
  file_path ||= Rails.root.join(Rails.application.config.x.temp_storage_path.to_s, 'mid_atlantic.csv')
  CSV.open(file_path, "wb") do |csv|
    csv << dealers_json.first.keys
    dealers_json.each do |hash|
      csv << hash.values
    end
  end
  file_path
end

#import_dealer(dealer) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/services/lead/mid_atlantic_scrapper.rb', line 124

def import_dealer(dealer)
  Customer.transaction do
    country_iso3 = dealer[:country]
    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: 'imported',
      profile_id: profile_id,
      buying_group_id: buying_group_id,
      state: 'lead_qualify'
    )
    #cu.create_activity('SOLICIT', description: 'Existing Nuheat Dealer')
    cu.contact_points.create!(category: ContactPoint::PHONE, detail: dealer[:phone1])

    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
    )
    dealer[:contacts].each do |contact_name|
      cu.contacts.create!(full_name: contact_name)
    end
    cu
  end
end

#import_dealers(dealer_list) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/services/lead/mid_atlantic_scrapper.rb', line 102

def import_dealers(dealer_list)
  customers_created = []
  error_msg = []
  dealer_list.each do |dealer|
    begin
      if dealer[:party_id]
        if dealer[:phone1]
          cu = Party.find(dealer[:party_id]).customer
          cu.contact_points.where(category: ContactPoint::PHONE).first_or_create!(detail: dealer[:phone1])
        end
      else #New
        customers_created << import_dealer(dealer)
        puts "#{cu.id} customer created #{cu.full_name} #{cu.reference_number}"
      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



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
# File 'app/services/lead/mid_atlantic_scrapper.rb', line 36

def parse_dealer_node(li)
  dealer_name = li.css('.mn-searchlisting-title a.mn-list-item-link').text&.squish
  dealer_name ||= li.css('.mn-searchlisting-title span').text&.squish
  url = li.css('.mn-searchlisting-title a.mn-list-item-link').attr('href')&.text&.squish&.presence
  # looks like we have to clean this up
  address1 = li.css('.mn-address1')&.text&.squish.presence
  address2 = li.css('.mn-address2')&.text&.squish.presence
  city = li.css('.mn-cityspan')&.text&.squish.presence
  state_code = li.css('.mn-stspan')&.text&.squish.presence
  state_code = case state_code
  when 'Virginia'
    'VA'
  when 'Utah'
    'UT'
  when 'District of Columbia'
    'DC'
  when 'Maryland'
    'MD'
  else
    state_code
  end
  zip_code = li.css('.mn-zipspan')&.text&.squish.presence
  country = 'USA'
  description = li.css('.mn-searchlisting-description')&.text&.squish.presence
  contacts = li.css('a.mn-list-item-link').map(&:text)[1..-1] || []
  contacts = contacts.compact.uniq.map(&:squish)
  phone1 = li.css('.mn-phone1')&.text&.squish.presence
  phone1 = PhoneNumber.parse_and_format(phone1) if phone1.present?
  {
    dealer_name: dealer_name,
    url: url,
    address1: address1,
    address2: address2,
    city: city,
    state_code: state_code,
    zip_code: zip_code,
    country: country,
    contacts: contacts,
    phone1: phone1,
    description: description
  }
end

#process(limit: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/lead/mid_atlantic_scrapper.rb', line 12

def process(limit: nil)
  dealers_html = IO.read(Rails.root.join('data','mid-atlantic.html'), chomp: true)
  parsed_data = Nokogiri::HTML.parse(dealers_html)
  list = parsed_data.xpath("//div[contains(@class, 'mn-member-listing')]")
  list = list.first(limit) if limit
  dealer_list = []
  new_dealer_count = 0
  dealer_to_import = []
  list.each_with_index do |li, index|
    n = parse_dealer_node(li)
    if n[:dealer_name].present? && n[:phone1].present?
      matched_dd = append_match_data(n)
      dealer_list << matched_dd
      unless matched_dd[:party_id]
        dealer_to_import << matched_dd
        new_dealer_count += 1
      end
    end
  end
  import_dealers(dealer_to_import)
  @dealers_json = dealer_list
  dump_to_csv
end