[vagrant@localhost ruby3]$ bundle install Fetching gem metadata from https://rubygems.org/........... Fetching version metadata from https://rubygems.org/. Using json 1.8.3 Installing multi_xml 0.5.5 Installing rack 1.6.4 Installing tilt 2.0.2 Using bundler 1.13.6 Installing httparty 0.13.7 Using rack-protection 1.5.3 Using sinatra 1.4.7 Bundle complete! 3 Gemfile dependencies, 8 gems now installed. Use `bundle show [gemname]` to see where a bundled gem is installed. Post-install message from httparty: When you HTTParty, you must party hard!
get '/product' do
...
@products = []
LOCATIONS.each do |location|
@product.push DATA.select { |product| product['location'] == location }.sample
end
<% @product.each do |product| %> <a href='/products/location<%= product[location] %>'> <div class='product'> <div class='thumb'> <img src='<%= product['url'] %>'> </div> <div class='caption'> <%= product['location'] != 'us' ? product['location'].capitalize : product['location'].upcase %> </div> </div> </a> <% end %>
get '/products/location/:location' do
DATA = HTTParty.get('https://fomotograph-api.udacity.com/products.json')['photos']
@products = DATA.select{ |product| product['location'] == params[:location]}
erb "<!DOCTYPE html>..."
end
get '/products/:id' do
DATA = HTTPartyp.get('https://hogehoge/products.json')['photos']
@product = DATA.select{ |prod| prod['id'] == params[:id].to_i }.first
erb "<!DOCTYPE html> ..."
end
class Product url = 'https://fomotograph-api.com/product.json' DATA = HTTParty.get(url)['photos'] end
require 'HTTParty'
require 'json'
class Product
url = 'https://fomotograph-api.com/product.json'
DATA = HTTParty.get(url)['photos']
def initialize(product_data = {})
@id = product_data['id']
@title = product_data['title']
@location = product_data['location']
@summary = product_data['summary']
@url = product_data['url']
@price = product_data['price']
end
def self.all
DATA.map { |product| new(product) }
end
def self.sample_locations
@products = []
LOCATIONS.each do |location|
@products.push self.all.select { |product| product.location == location }.sample_locations
end
return @product
end
def self.find_by_location(location)
self.all.select { |product| product.location == location }
end
end
{
"id"=>27, "title"=>"worlds end", "summary"=>"travel tothe end ...",
"location"=>"scotland", "price"=>37, "url"=>"/images/scotland/worlds-end.jpg"
}
controller
get '/products' do @products = Product.all erb :products end