Skip to content

Instantly share code, notes, and snippets.

@elentok
Created July 6, 2012 12:59
Show Gist options
  • Save elentok/3060041 to your computer and use it in GitHub Desktop.
Save elentok/3060041 to your computer and use it in GitHub Desktop.
refactoring
#======================================
# 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