Skip to content

Instantly share code, notes, and snippets.

@jpotts18
Last active September 17, 2015 05:05
Show Gist options
  • Save jpotts18/5dfbeedec398cbf4f143 to your computer and use it in GitHub Desktop.
Save jpotts18/5dfbeedec398cbf4f143 to your computer and use it in GitHub Desktop.
Explaining the Template Method design pattern with a WebScraper
class WebScraper
def execute
create_folders
download_data
parse_data
write_output
end
def create_folder
# Make directory
end
def download_data
# Initialize Http client
end
def parse_data
# Find all rows in table and put them in a hash
end
def write_output
# Write hash to a CSV on filesystem
end
end
class HTMLWebScraper < WebScraper
def parse_data
# customize how to extract the data into a hash and
# let the superclass take care of the rest :)
end
end
class JSONWebScraper < WebScraper
def download_data
# initialize JSON client
# save data to filesystem
end
def parse_data
# read from file system
# custom JSON parsing code
# store results in hash
# let the super class take care of the rest :)
end
end
html_scraper = HTMLWebScraper.new
html_scraper.execute
json_scraper = JSONWebScraper.new
json_scraper.excute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment