Created
July 6, 2012 12:59
-
-
Save elentok/3060041 to your computer and use it in GitHub Desktop.
refactoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#====================================== | |
# BEFORE: | |
#====================================== | |
class FetchApartments | |
def fetch_query_page(query) | |
stream = download(query.to_url, "http://www.yad2.co.il/Nadlan/rent.php") | |
doc = Nokogiri::HTML(stream) | |
total_text = doc.css('li.ads').first.text | |
@total = /\d+/.match(total_text)[0].to_i | |
table = doc.css('.ads_list')[1] | |
@current_page = table.css('tr[class^=ActiveLink]').map do |tr| | |
apartment_attributes_from_tr(tr) | |
end | |
end | |
#... | |
end | |
#====================================== | |
# AFTER: | |
#====================================== | |
class FetchesApartments | |
def fetch_query_page(query) | |
stream = download(query.to_url, "...") | |
doc = Nokogiri::HTML(stream) | |
@total = find_total(doc) | |
@apartments = find_apartments(doc) | |
end | |
def calculate_total(doc) | |
total_text = doc.css('li.ads').first.text | |
@total = /\d+/.match(total_text)[0].to_i | |
end | |
def find_apartments(doc) | |
table = doc.css('.ads_list')[1] | |
table.css('tr[class^=ActiveLink]').map do |tr| | |
apartment_attributes_from_tr(tr) | |
end | |
end | |
#... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment