Simple Web Apps With Sinatra

5,118 views

Published on

Published in: Technology
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total views
5,118
On SlideShare
0
From Embeds
0
Number of Embeds
1,258
Actions
Shares
0
Downloads
37
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Simple Web Apps With Sinatra

    1. 1. Simple Web Apps WithSinatra
    2. 2. Micro web apps withSinatra
    3. 3. RESTGETPOSTPUT
    4. 4. RESTGETPOSTPUT
    5. 5. Rubysudo gem install sinatra
    6. 6. Rubyrequire ‘rubygems’require ‘sinatra’
    7. 7. Rubyrequire ‘rubygems’require ‘sinatra’get ‘/’ do #do stuffendpost ‘/’ do #do stuffend
    8. 8. MVCModel Controller View
    9. 9. MVCModel Controller ViewData Presentation
    10. 10. MVCModel Controller ViewData Presentation{ "id": "2902652", "name": "Al Shaw", "first_name": "Al", "last_name": "Shaw", "link": "https://www.facebook.com/ashaw", "username": "ashaw", "gender": "male", "locale": "en_US"}
    11. 11. MVCModel Controller ViewData Presentation{ "id": "2902652", "name": "Al Shaw", "first_name": "Al", "last_name": "Shaw", "link": "https://www.facebook.com/ashaw", "username": "ashaw", "gender": "male", "locale": "en_US"}
    12. 12. MVC Model Controller View Data Presentation get ‘/profile/:id’ do # request data for :id from FB API # pass to view end{ "id": "2902652", "name": "Al Shaw",
    13. 13. MVC Model Controller View Data Presentation post ‘/profile’ do # pass new values from user input # to Model end{ "id": "2902652", "name": "Al Shaw",
    14. 14. Let’s do it!require ‘rubygems’require ‘sinatra’require ‘open-uri’require ‘json’get ‘/profile/:id’ do user = params[:id] profile = open(“http://graph.facebook.com/#{user}”).read profile = JSON.parse(profile) “<h2>” + profile[‘name’] + “</h2>”end
    15. 15. Let’s do it!ashaw@Al-Shaws-MacBook-Pro rails $ ruby fb.rb
    16. 16. What just happened?
    17. 17. 1. Require Librariesrequire ‘rubygems’ # lets you use librariesrequire ‘sinatra‘ # our micro web frameworkrequire ‘open-uri’ # for requesting URLsrequire ‘json‘ # for parsing JSON
    18. 18. 2. A get blockget ‘/profile/:id’ do # do stuffend URL parameter http://localhost:4567/pro le/ashaw params[:id]
    19. 19. 3. Capture parametersget ‘/profile/:id’ do user = params[:id]end
    20. 20. 4. Call & Parse FB APIget ‘/profile/:id’ do user = params[:id] profile = open(“http://graph.facebook.com/#{user}”).read profile = JSON.parse(profile)endhttp://graph.facebook.com/ashaw{ {"name"=>"Al Shaw", "id": "2902652", "username"=>"ashaw", "name": "Al Shaw", "first_name": "Al", "gender"=>"male", "last_name": "Shaw", "id"=>"2902652", "link": "https://www.facebook.com/ashaw", "last_name"=>"Shaw", "username": "ashaw", "locale"=>"en_US", "gender": "male", "locale": "en_US" "link"=>"http://www.facebook.com/ashaw",} "first_name"=>"Al"}
    21. 21. 5. Show what you want get ‘/profile/:id’ do user = params[:id] profile = open(“http://graph.facebook.com/#{user}”).read profile = JSON.parse(profile) “<h2>” + profile[‘name’] + “</h2>” end By default, last line in a block is an implicit view“<h2>” + profile[‘name’] + “</h2>”
    22. 22. And Now For SomethingCompletely Different
    23. 23. Does x follow you onTwitter?
    24. 24. Let’s build this:
    25. 25. Actions‘/‘ Collect user data‘/follows‘ Show following status
    26. 26. Collect user namesget ‘/‘ do #show formend
    27. 27. *Short interlude regarding views get ‘/‘ do get ‘/‘ do “Hello” erb :hello end end
    28. 28. *Short interlude regarding views get ‘/‘ do get ‘/‘ do “Hello” erb :hello end end Implicit view View Template
    29. 29. *Short interlude regarding views get ‘/‘ do get ‘/‘ do “Hello” erb :hello end end Implicit view View Template
    30. 30. View Templatesfollowapp.rb views/index.erbget ‘/‘ do <html> erb :index <head>end ....
    31. 31. View Templatesfollowapp.rb views/index.erbget ‘/‘ do <%= @foo %> @foo = ‘bar’ erb :indexend
    32. 32. </Short interlude regarding views>
    33. 33. Actions‘/‘ Collect user datafollowapp.rbget ‘/‘ do erb :indexend
    34. 34. Actions‘/‘ Collect user dataviews/index.erb<h1>Enter two users</h1><form method="get" action="/follows">Does <input type="text" name="user1"> follow<input type="text" name="user2">?<input type="submit" value="Go"></form>
    35. 35. Actions‘/‘ Collect user dataviews/index.erb<h1>Enter two users</h1><form method="get" action="/follows">Does <input type="text" name="user1"> follow<input type="text" name="user2">?<input type="submit" value="Go"></form>
    36. 36. Actions‘/follows‘ Show following status
    37. 37. Actions‘/follows‘ Show following statusget /follows do @user1 = params[:user1] @user2 = params[:user2] #not implemented yet @following = is_following?(@user1, @user2) erb :followsend
    38. 38. is_following?@following = is_following?(@user1, @user2)To Implement:• Get user IDs for usernames• Check if user ID 1 is in user 2’s follower array
    39. 39. is_following?• Get user IDs for usernamesdef twitter_id(screen_name) user = open(“http://api.twitter.com/1/users/show.json?screen_name=#{screen_name}”).read user = JSON.parse(user) user[id]end
    40. 40. is_following?• Get user IDs for usernamesdef twitter_id(screen_name) user = open(“http://api.twitter.com/1/users/show.json?screen_name=#{screen_name}”).read user = JSON.parse(user) user[id]endtwitter_id(‘a_l’) #=> 7865282
    41. 41. is_following?• Check if user ID 1 is in user 2’s follower arraydef twitter_id(screen_name) user = open(“http://api.twitter.com/1/users/show.json?screen_name=#{screen_name}”).read user = JSON.parse(user) user[id]enddef is_following?(a,b) followers = open("http://api.twitter.com/1/followers/ids.json?screen_name=#{b}").read followers = JSON.parse(followers) followers.include?(twitter_id(a))end
    42. 42. is_following?• Check if user ID 1 is in user 2’s follower arraydef twitter_id(screen_name) user = open(“http://api.twitter.com/1/users/show.json?screen_name=#{screen_name}”).read user = JSON.parse(user) user[id]enddef is_following?(a,b) followers = open("http://api.twitter.com/1/followers/ids.json?screen_name=#{b}").read followers = JSON.parse(followers) followers.include?(twitter_id(a))endis_following?(barackobama, a_l)#=> true
    43. 43. Putting it togetherdef twitter_id(screen_name) user = open(“http://api.twitter.com/1/users/show.json?screen_name=#{screen_name}”).read user = JSON.parse(user) user[id]enddef is_following?(a,b) followers = open("http://api.twitter.com/1/followers/ids.json?screen_name=#{b}").read followers = JSON.parse(followers) followers.include?(twitter_id(a))endget /follows do @user1 = params[:user1] @user2 = params[:user2]@following = is_following?(@user1, @user2) erb :followsend
    44. 44. Putting it togetherviews/follows.erb<h2><%= @user1 %><%= @following ? "follows" : "does not follow" %><%= @user2 %></h2>
    45. 45. More.http://www.alistapart.com/articles/rapid-prototyping-with-sinatra/https://github.com/ashaw/ALA-Sample-Sinatra-App
    46. 46. Thank you. Questions?http://shaw.al@A_L

    ×