Skip to content

Instantly share code, notes, and snippets.

@alvir
Created July 4, 2012 13:08
Show Gist options
  • Save alvir/3047252 to your computer and use it in GitHub Desktop.
Save alvir/3047252 to your computer and use it in GitHub Desktop.
Smells of our city
def create_listing_by_url(url, who_creates_listing)
return { } if url.match(UrlsProcessor::URL_WITH_DOMAIN_ONLY)
initial_url = url
res = create_listing_matched_to_feed_by_url(url, who_creates_listing, initial_url)
return res if res
res = Listing.create_empty_result_hash
source_info = Scraper::Manager.detect_top_level_source(url)
unless source_info[:source]
res[:not_found] << '' # it's needed to make res[:not_found] not empty array
SomeProject.action_log.warn "There is no Scraper for: #{source_info[:requested_url]}"
return res
end
listing_was_received = lambda do |listing|
main_source = listing.main_source
address = address_of_listing(main_source)
web_id = main_source[:web_id]
res[:received] << web_id
res[:addresses][web_id] = address
res[:sources][web_id] = main_source[:source]
res[:listing] = listing
SomeProject.action_log.info I18n.t('flash.listing.added', :address => "'#{address}'")
listing.add_event(:adding, :actor => who_creates_listing)
end
listing_was_not_found = lambda do |listing_src_id|
res[:not_found] << listing_src_id
SomeProject.action_log.warn I18n.t('flash.listing.not_found')
end
report_about_existed_listing = lambda do |existed_listing|
main_src = existed_listing.main_source
listing_src_id = main_src[:web_id]
if existed_listing.deleted?
res[:earlier_deleted] << listing_src_id
SomeProject.action_log.info I18n.t('flash.listing.deleted', :details => "'#{existed_listing.address}'")
else
res[:already_exist] << listing_src_id
SomeProject.action_log.info I18n.t('flash.listing.already_exists')
end
res[:addresses][listing_src_id] = address_of_listing(main_src)
end
after_create = lambda do |listing, requested_listing_id|
unless listing.new_record?
listing_was_received.call(listing)
else
res[:error_occurred] << requested_listing_id
res[:addresses][requested_listing_id] = listing.address if listing.address
SomeProject.action_log.error "Listing was not added due to error: #{listing.errors.full_messages.inspect}"
end
end
create_new_listing = lambda do |requested_listing_id, attrs|
listing = self.add_listing(attrs)
after_create.call(listing, requested_listing_id)
end
add_new_listing = lambda do |requested_listing_id, newly_received_listing|
newly_received_listing.url_chain.compact.each do |u|
feed_match_res = create_listing_matched_to_feed_by_url(u, who_creates_listing, initial_url)
return feed_match_res if feed_match_res
end
if newly_received_listing.valid?
criteria = newly_received_listing.sources.map { |s| [s[0], s[1], newly_received_listing.address, newly_received_listing.sqft] }
feed = Feed.searchable_by_sources(criteria).first
if feed
res = create_listing_by_feed(feed, who_creates_listing, :url => initial_url)
else
Listing.log_open_house(newly_received_listing)
create_new_listing.call(requested_listing_id, newly_received_listing.attributes)
end
else
listing_was_not_found.call(requested_listing_id)
end
end
source_site = source_info[:source]
listing_src_id = source_info[:web_id]
# Logging is performed in this method (but not in the caller),
# because logging is the same for any caller (unlike messages for client)
# and logging from here is especially convenient in the case of adding many listings per request
SomeProject.action_log.info "Requested URL: #{source_info[:requested_url]}, top scraper is #{source_site.short_class_name}."
begin
existed_listing = self.find_listing(:source => source_site.short_class_name, :web_id => listing_src_id) # now we don't have neither address nor sqft to pass
unless existed_listing
newly_received_listing = source_site.find(listing_src_id, source_info[:requested_url])
if newly_received_listing.valid?
find_criteria = newly_received_listing.sources.map do |k, v|
{ :source => k, :web_id => v, :address => newly_received_listing.address, :sqft => newly_received_listing.sqft }
end
existed_listing = self.find_listing(find_criteria)
end
end
if existed_listing
report_about_existed_listing.call(existed_listing)
else
add_new_listing.call(listing_src_id, newly_received_listing)
end
rescue => e
res[:error_occurred] << listing_src_id # it's needed for caller
# it's needed to log exception
SomeProject.action_log.perform_around_logged_block "Error on #{listing_src_id}" do
raise e
end
end
res
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment