Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
shahwaiznq committed Jun 30, 2020
2 parents 35f24fa + 64ec4c8 commit 8920ea1
Show file tree
Hide file tree
Showing 717 changed files with 13,068 additions and 1 deletion.
49 changes: 49 additions & 0 deletions alex_maunder/week_04_ruby/MTA_ruby/mta.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "pry"

# $lines.map { |line, stop| stop }.inject &:& # gets the intersection (Union Square) - would be useful if multiple intersecting stations
# $lines.map { |_, n| n.index "Union Square" } # find index of Union Square at each line

$lines = {
"n_line"=>['Times Square', '34th', '28th', '23rd', 'Union Square', '8th'],
"six_line"=>['Grand Central', '33rd', '28th', '23rd', 'Union Square', 'Astor Place'],
"l_line"=>['8th', '6th', 'Union Square', '3rd', '1st']
}

$total_stops_first = 0
$total_stops_second = 0

def plan_trip (start_line, start_station, end_line, end_station)
index_start = $lines["#{start_line}"].index "#{start_station}"
index_union_start = $lines["#{start_line}"].index "Union Square"
index_end = $lines["#{end_line}"].index "#{end_station}"
index_union_end = $lines["#{end_line}"].index "Union Square"

#change at Union Square
if start_line != end_line
$total_stops_first += index_start - $lines["#{start_line}"].index("Union Square")
$total_stops_second += index_end - $lines["#{end_line}"].index("Union Square")
trip1 = $lines[start_line][index_union_start..index_start].reverse.join(", ") if $total_stops_first.positive? # r to l
trip1 = $lines[start_line][index_start..(index_union_start -1)].join(", ") if $total_stops_first.negative? # l to r
trip2 = $lines[end_line][index_union_end..index_end].join(", ") if $total_stops_second.positive?
trip2 = $lines[end_line][index_end..(index_union_end - 1)].reverse.join(", ") if $total_stops_second.negative?

puts "You must travel through the following stops on the #{start_line}: #{trip1}"
puts "Change at Union Square"
puts "You must travel through the following stops on the #{end_line}: #{trip2}"
puts "#{$total_stops_first.abs + $total_stops_second.abs} stops in total."

$total_stops_first = 0 # reset the count
$total_stops_second = 0 # reset the count

else #stay on same line
$total_stops_first += (index_start - index_end) # if negative, left to right, if positive, right to left
trip = $lines[start_line][index_end, index_start].reverse.join(", ") if $total_stops_first.positive? # the predicate here is more explicit, but not needed twice
trip = $lines[start_line][(index_start + 1), index_end].join(", ") if $total_stops_first.negative?
puts "You must travel through the following stops on the #{start_line}: #{trip}"
puts "#{$total_stops_first.abs} stops in total." # abs converts total_stops_first to positive int (ie in the case you're going from left to right)
$total_stops_first = 0 # reset the count
end

end # this will return 0 as it is the last line of code executed ie $total_stops_first = 0

binding.pry
15 changes: 15 additions & 0 deletions alex_maunder/week_04_ruby/book_finder/book_finder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Books I

## Specification

Build a search form that lets the user enter a book title. The Sinatra app will use HTTParty to fetch the data for the chosen book from Google Books and display it on the page. Display the cover, as a bare minimum.

## Sample URL

https://www.googleapis.com/books/v1/volumes?q=title:Ulysses

### Additional Resources

- [Dynamic URLs in Sinatra](http://blog.teamtreehouse.com/ruby-sinatra-dynamic-urls-tutorial)
- [Google Books Search API](https://developers.google.com/books/docs/v1/reference/volumes/list)
- [HTTParty Tutorial](http://blog.teamtreehouse.com/its-time-to-httparty)
22 changes: 22 additions & 0 deletions alex_maunder/week_04_ruby/book_finder/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'sinatra'
require 'sinatra/reloader'
require 'pry'
require 'httparty'


get '/' do
erb :home
end

get '/bookinfo' do
@book = params[:book].to_s
book_url = "https://www.googleapis.com/books/v1/volumes?q=title:" + @book
info = HTTParty.get book_url
@title = info["items"][0]["volumeInfo"]["title"]
@thumbnail_url = info["items"][0]["volumeInfo"]["imageLinks"]["thumbnail"]
@authors = info["items"][0]["volumeInfo"]["authors"][0]
@description = info["items"][0]["volumeInfo"]["description"]
@book_review = info["items"][0]["searchInfo"]["textSnippet"]
@book_link = info["items"][0]["volumeInfo"]["canonicalVolumeLink"]
erb :bookinfo
end
7 changes: 7 additions & 0 deletions alex_maunder/week_04_ruby/book_finder/public/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* body {
background-color: grey;
}
.container {
background-color: white;
} */
18 changes: 18 additions & 0 deletions alex_maunder/week_04_ruby/book_finder/views/bookinfo.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h1>Book Info</h1>

<img src=<%= @thumbnail_url %> alt="cover image">

<p>
<a href=<%= @book_link %>><%= @title %></a>
By <%= @authors %>
</p>

<p>
Description:
<%= @description %>
</p>

<p>
Review:
<%= @book_review %>
</p>
8 changes: 8 additions & 0 deletions alex_maunder/week_04_ruby/book_finder/views/home.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>Welcome to Book Finder</h1>
<form action="/bookinfo">
<label>
Please enter the book you want info on:
<input type="text" placeholder="TheHobbit" name="book" required>
</label>
<button>Get Info!</button>
</form>
19 changes: 19 additions & 0 deletions alex_maunder/week_04_ruby/book_finder/views/layout.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
<link rel="stylesheet" href="/css/style.css">

<title>Book Finder</title>
</head>
<body>

<div class="container">
<%= yield %>
</div>

</body>
</html>
Binary file not shown.
21 changes: 21 additions & 0 deletions alex_maunder/week_04_ruby/sinatra_sql_crud/key_switches.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- id name brand image (urls)
-- integer text text text -- you could store this as a string but strings have a limit of 256 characters, URL may be longer than that.
--
-- 1 Blue Cherry http://....
-- 2 Green Gateron http://....

CREATE TABLE keyswitches (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
brand TEXT,
image TEXT
);

-- seed data
INSERT INTO keyswitches (name, brand) VALUES ('Blue', 'Cherry');
INSERT INTO keyswitches (name, brand) VALUES ('Green', 'Gateron');

-- -- next steps
-- 1. apply this to the db
-- 2. test: .schema, SELECT *
-- 3. connect to the db from RUBY
65 changes: 65 additions & 0 deletions alex_maunder/week_04_ruby/sinatra_sql_crud/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'sinatra'
require 'sinatra/reloader'
require 'sqlite3'


get '/' do
erb :home
end

# Show all entries
get '/keyswitches' do
@switches = query_db('SELECT * FROM keyswitches')
erb :keyswitches
end

# NEW -- show the form to create a new keyswitch
get '/keyswitch/new' do
erb :newswitch
end

# POST -- CREATE new keyswitch
post '/keyswitches' do
query = "INSERT INTO keyswitches (name, brand, image) VALUES ('#{params[:name]}', '#{params[:brand]}', '#{params[:image]}')"
query_db(query)
redirect to('/keyswitches')
end

#SHOW -- show the switch once clicked
get '/keyswitches/:id' do
@switch = query_db("SELECT * FROM keyswitches WHERE id=#{params[:id]}")
@switch = @switch.first # pull the switch from the array
erb :show_switch
end


#EDIT -- Shows a form which allows you to edit a single switch: SQL READ operation
get '/keyswitches/:id/edit' do
@switch = query_db("SELECT * FROM keyswitches WHERE id=#{params[:id]}")
@switch = @switch.first
erb :edit_switch
end

# UPDATE -- Update the db with new information for an existing butterfly
post '/keyswitches/:id' do
query = "UPDATE keyswitches SET name='#{params[:name]}', brand='#{params[:brand]}', image='#{params[:image]}' WHERE id=#{params[:id]}"
query_db(query)
redirect to("/keyswitches/#{params[:id]}") # GET
end

# DELETE -- Delete a single butterfly from the db
get '/keyswitches/:id/delete' do
query_db("DELETE FROM keyswitches WHERE id=#{params[:id]}")
redirect to('/keyswitches')
end


#Connect to db
def query_db(sql_statement) # connect to the db and pull results
puts sql_statement # for debugging in the server, shows the SQL query being executed
db = SQLite3::Database.new 'database.sqlite3'
db.results_as_hash = true
results = db.execute sql_statement
db.close # limit of 1024 connections to db at any one time. we need to close it so the limits aren't maxed out
return results
end
Empty file.
20 changes: 20 additions & 0 deletions alex_maunder/week_04_ruby/sinatra_sql_crud/views/edit_switch.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h1>Edit Keyswitch</h1>

<form action="/keyswitches/<%= @switch["id"] %>" method="post">
<label>
Name:
<input name="name" placeholder="Black" required value="<%= @switch["name"] %>">
</label>

<label>
Brand:
<input name="brand" placeholder="Cherry MX" value="<%= @switch["brand"] %>">
</label>

<label>
Image:
<input type="url" name="image" placeholder="http://..." value="<%= @switch["image"] %>">
</label>

<button>Update KeySwitch</button>
</form>
5 changes: 5 additions & 0 deletions alex_maunder/week_04_ruby/sinatra_sql_crud/views/home.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Keyboard KeySwitch Repo</h1>

<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa provident alias nam enim, vero voluptates, eligendi dolor, eveniet nesciunt voluptatibus consequuntur distinctio. Esse maxime sunt laboriosam deleniti fugit. Iste, praesentium!
</p>
12 changes: 12 additions & 0 deletions alex_maunder/week_04_ruby/sinatra_sql_crud/views/keyswitches.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h2>Key Switches</h2>

<!-- @switches are an array, switch is a string...-->
<!-- <%= @switches[0]["name"] %> -->

<% @switches.each do |switch| %>
<p>
<a href="/keyswitches/<%= switch["id"] %>">
<%= "#{switch["brand"]} - #{switch["name"]}" %>
</a>
</p>
<% end %>
23 changes: 23 additions & 0 deletions alex_maunder/week_04_ruby/sinatra_sql_crud/views/layout.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
<title>Keyboard Switches</title>
</head>
<body>

<div class="container">
<nav>
<a href="/">Home</a>
<a href="/keyswitches">Switches</a>
<a href="/keyswitch/new">New Switch</a>
</nav>
<%= yield %>

</div>

</body>
</html>
20 changes: 20 additions & 0 deletions alex_maunder/week_04_ruby/sinatra_sql_crud/views/newswitch.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h1>New Keyswitch</h1>

<form action="/keyswitches" method="post">
<label>
Name:
<input name="name" placeholder="Black" required>
</label>

<label>
Brand:
<input name="brand" placeholder="Cherry MX">
</label>

<label>
Image:
<input type="url" name="image" placeholder="http://...">
</label>

<button>Create KeySwitch</button>
</form>
11 changes: 11 additions & 0 deletions alex_maunder/week_04_ruby/sinatra_sql_crud/views/show_switch.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h2><%= @switch["name"] %></h2>
<h3>Brand: <em><%= @switch["brand"] %></em></h3>

<% unless @switch["image"].nil? || @switch["image"].empty? %>
<img class="feature" src="<%= @switch["image"] %>" alt="<%= @switch["name"] %>">
<% end %>

<div class="user_controls">
<a href="/keyswitches/<%= @switch["id"] %>/edit">Edit Switch</a>
<a href="/keyswitches/<%= @switch["id"] %>/delete">Delete Switch</a>
</div>
3 changes: 3 additions & 0 deletions behdad_setoodegan/week_04/Book/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"git.ignoreLimitWarning": true
}
17 changes: 17 additions & 0 deletions behdad_setoodegan/week_04/Book/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'sinatra'
require 'sinatra/reloader'
require 'stock_quote'
require 'HTTParty'

get '/' do
erb :home
end

get '/pics' do
@title = params[:title]
book_url = "https://www.googleapis.com/books/v1/volumes?q=title#{@title}"
info = HTTParty.get book_url;
@cover = info["items"][0]["volumeInfo"]["imageLinks"]["thumbnail"]
erb :pics
end

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions behdad_setoodegan/week_04/Book/public/css/book.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
body{
background-color: aquamarine;
background-image: url("Books.jpg");

}

div{
margin: 0 auto;
width: 960px;
background-color: white;
min-height: 80vh;
}

a{
margin: 40px;
}

img{
margin: 0 auto;
padding: 50px;
}
9 changes: 9 additions & 0 deletions behdad_setoodegan/week_04/Book/views/home.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Books</h1>
<form action = "/pics">
<lable>
Stock symbol:
<input placeholder = "AAPL" name = "title">
</lable>

<button >See book covers</button>
</form>
Loading

0 comments on commit 8920ea1

Please sign in to comment.