-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_apartment_saver.rb
40 lines (31 loc) · 994 Bytes
/
new_apartment_saver.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class NewApartmentSaver
def initialize(url:, area:)
@scrapper = ApartmentScrapper.new(url)
@area = area
end
def call
new_apartments.each { |new_apartment| new_apartment.save! }
return if new_apartments.count > 10 # Probably the initialization process
new_apartments.each do |new_apartment|
NewApartmentMailer.sendout(
apartment: new_apartment,
recipients: MySettings.recipients,
area: @area
).deliver_now
end
end
def apartments
@apartments ||= @scrapper.apartment_elements.map do |apartment_element|
apartment_parser = ApartmentParser.new(apartment_element)
apartment = Apartment.find_or_initialize_by_njuskalo_id(apartment_parser.njuskalo_id)
unless apartment.persisted?
apartment.name = apartment_parser.name
apartment.link = apartment_parser.link
end
apartment
end
end
def new_apartments
@new_apartments ||= apartments.reject(&:persisted?)
end
end