Created
July 10, 2014 21:03
-
-
Save bomatson/ab448e2da46abd02c635 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Class 07 Homework | |
| # 7.3.2014 | |
| # By Josh Esguia | |
| # ASSIGNMENT | |
| # Using http://mashable.com/stories.json and sinatra, show | |
| # a webpage listing all the articles with images next to them. | |
| # BONUS | |
| # ----- | |
| # * Filter by category | |
| # * Filter by author | |
| # * Sorting by social shares | |
| ################################## | |
| # Acquire and pars article JSON | |
| ################################## | |
| require 'sinatra' | |
| require 'httparty' | |
| response = HTTParty.get('http://www.mashable.com/stories.json') | |
| # Turn the JSON string into arrays and hashes | |
| # From looking at the JSON, data is formatted as: | |
| # main hash (keys: category, values: article array) -> | |
| # article array -> article details hash | |
| # Provide sorting options | |
| get '/' do | |
| erb :home | |
| end | |
| ################################################## | |
| # Sort by Category | |
| ################################################## | |
| # Pass parsed response to .erb file | |
| get '/by_category' do | |
| @mashable_hash = response.parsed_response | |
| erb :by_category | |
| end | |
| ################################################## | |
| # Sort by Author | |
| ################################################## | |
| get '/by_author' do | |
| mashable_hash = response.parsed_response | |
| all_articles_array = [] | |
| # Extract all articles from category hashes | |
| # and shovel them into all_articles_array | |
| # there's a handy way you could achieve the same result as below by using map: | |
| # all_articles_array = mashable_hash.map { |category, array| array } | |
| # this creates a new array, iterating over each key value pair in your hash. | |
| # by specifying 'array' in the block, I'm saying that I want to return the | |
| # value (in this case, array) of each key pair in my new array | |
| mashable_hash.each do |category, article_array| | |
| # I'd stay away from using != nil and move towards just enforcing the presence of your article_array: | |
| # if article_array | |
| # .. do stuff | |
| # end | |
| if article_array != nil | |
| article_array.each do |one_article_hash| | |
| all_articles_array << one_article_hash | |
| end | |
| end | |
| end | |
| # Now you can sort by author | |
| @articles_by_author_hash = {} | |
| all_articles_array.each do |article_hash| | |
| # Grab all necessary data from each article | |
| # within the main article array | |
| author_name = article_hash["author"] | |
| # Create a new hash (k: author name, v: array of | |
| # hashes of his or her articles) | |
| # GREAT! really nice usage of unless, similar to how we did it in class | |
| unless @articles_by_author_hash.has_key?(author_name) | |
| @articles_by_author_hash[author_name] = [] | |
| end | |
| @articles_by_author_hash[author_name] << article_hash | |
| end | |
| # articles_by_author_hash is now all set to pass | |
| # to the .erb file | |
| erb :by_author | |
| end | |
| ################################################## | |
| # Sort by Shares | |
| ################################################## | |
| get '/by_shares' do | |
| mashable_hash = response.parsed_response | |
| all_articles_array = [] | |
| # Extract all articles from category hashes | |
| # and shovel them into all_articles_array | |
| mashable_hash.each do |category, article_array| | |
| # again, try just checking for the presence: "if article_array" | |
| # I saw this in your category.erb as well | |
| if article_array != nil | |
| article_array.each do |one_article_hash| | |
| all_articles_array << one_article_hash | |
| end | |
| end | |
| end | |
| # Now you can sort by shares | |
| # nice use of sort_by below! | |
| @all_articles_array = all_articles_array.sort_by{|k| k["shares"]["total"]}.reverse | |
| # And once sorted, pass the array to the .erb | |
| # for display | |
| erb :by_shares | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment