SlideShare une entreprise Scribd logo
1  sur  48
Simple Web Apps With
Sinatra
Micro web apps with
Sinatra
REST
GET
POST
PUT
REST
GET
POST
PUT
Ruby
sudo gem install sinatra
Ruby
require ‘rubygems’
require ‘sinatra’
Ruby
require ‘rubygems’
require ‘sinatra’

get ‘/’ do
 #do stuff
end

post ‘/’ do
 #do stuff
end
MVC

Model   Controller   View
MVC

Model   Controller   View
Data                 Presentation
MVC

Model                             Controller    View
Data                                            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"
}
MVC

Model                             Controller    View
Data                                            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"
}
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",
MVC

    Model                Controller     View
    Data                                Presentation


                         post ‘/profile’ do
                         # pass new values from user input
                         # to Model
                         end



{
    "id": "2902652",
    "name": "Al Shaw",
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
Let’s do it!
ashaw@Al-Shaws-MacBook-Pro rails $ ruby fb.rb
What just happened?
1. Require Libraries
require   ‘rubygems’ # lets you use libraries
require   ‘sinatra‘ # our micro web framework
require   ‘open-uri’ # for requesting URLs
require   ‘json‘    # for parsing JSON
2. A get block
get ‘/profile/:id’ do
 # do stuff
end

                       URL parameter
                       http://localhost:4567/pro le/ashaw
                        params[:id]
3. Capture parameters
get ‘/profile/:id’ do
 user = params[:id]
end
4. Call & Parse FB API
get ‘/profile/:id’ do
 user = params[:id]
 profile = open(“http://graph.facebook.com/#{user}”).read
 profile = JSON.parse(profile)
end


http://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"}
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>”
And Now For Something
Completely Different
Does x follow you on
Twitter?
Let’s build this:
Actions
‘/‘      Collect user data
‘/follows‘ Show following status
Collect user names
get ‘/‘ do
 #show form
end
*Short interlude
              regarding views

 get ‘/‘ do      get ‘/‘ do
  “Hello”         erb :hello
 end             end
*Short interlude
                 regarding views

 get ‘/‘ do         get ‘/‘ do
  “Hello”            erb :hello
 end                end

 Implicit view      View Template
*Short interlude
                 regarding views
 get ‘/‘ do         get ‘/‘ do
  “Hello”            erb :hello
 end                end

 Implicit view      View Template
View Templates
followapp.rb   views/index.erb

get ‘/‘ do     <html>
 erb :index    <head>
end             ....
View Templates
followapp.rb    views/index.erb

get ‘/‘ do      <%= @foo %>
 @foo = ‘bar’
 erb :index
end
</Short interlude
        regarding views>
Actions
‘/‘            Collect user data

followapp.rb

get ‘/‘ do
 erb :index
end
Actions
‘/‘            Collect user data

views/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>
Actions
‘/‘            Collect user data

views/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>
Actions
‘/follows‘ Show following status
Actions
‘/follows‘ Show following status
get '/follows' do
 @user1 = params[:user1]
 @user2 = params[:user2]

  #not implemented yet
 @following = is_following?(@user1, @user2)

 erb :follows
end
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
is_following?
• Get user IDs for usernames
def 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
is_following?
• Get user IDs for usernames
def 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



twitter_id(‘a_l’) #=> 7865282
is_following?
• Check if user ID 1 is in user 2’s follower array
def 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

def 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
is_following?
• Check if user ID 1 is in user 2’s follower array
def 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

def 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




is_following?('barackobama', 'a_l')
#=> true
Putting it together
def 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

def 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


get '/follows' do
 @user1 = params[:user1]
 @user2 = params[:user2]

@following = is_following?(@user1, @user2)

 erb :follows
end
Putting it together
views/follows.erb
<h2>
<%= @user1 %>
<%= @following ? "follows" : "does not follow" %>
<%= @user2 %>
</h2>
More.
http://www.alistapart.com/articles/rapid-prototyping-with-sinatra/
https://github.com/ashaw/ALA-Sample-Sinatra-App
Thank you. Questions?
http://shaw.al
@A_L

Contenu connexe

Tendances

BOSS around the web
BOSS around the webBOSS around the web
BOSS around the webJai Santhosh
 
Mojolicious, real-time web framework
Mojolicious, real-time web frameworkMojolicious, real-time web framework
Mojolicious, real-time web frameworktaggg
 
Rails for Beginners - Le Wagon
Rails for Beginners - Le WagonRails for Beginners - Le Wagon
Rails for Beginners - Le WagonAlex Benoit
 
Context with Yahoo! BOSS
Context with Yahoo! BOSSContext with Yahoo! BOSS
Context with Yahoo! BOSSJai Santhosh
 
jQtouch, Building Awesome Webapps
jQtouch, Building Awesome WebappsjQtouch, Building Awesome Webapps
jQtouch, Building Awesome WebappsHome
 
Mojolicious: what works and what doesn't
Mojolicious: what works and what doesn'tMojolicious: what works and what doesn't
Mojolicious: what works and what doesn'tCosimo Streppone
 
The Art of AngularJS in 2015 - Angular Summit 2015
The Art of AngularJS in 2015 - Angular Summit 2015The Art of AngularJS in 2015 - Angular Summit 2015
The Art of AngularJS in 2015 - Angular Summit 2015Matt Raible
 
Why You Should be Using Web Components Right Now. And How. ForwardJS July 2015
Why You Should be Using Web Components Right Now. And How. ForwardJS July 2015Why You Should be Using Web Components Right Now. And How. ForwardJS July 2015
Why You Should be Using Web Components Right Now. And How. ForwardJS July 2015Phil Leggetter
 
Moving from Django Apps to Services
Moving from Django Apps to ServicesMoving from Django Apps to Services
Moving from Django Apps to ServicesCraig Kerstiens
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Anatoly Sharifulin
 
Cheap frontend tricks
Cheap frontend tricksCheap frontend tricks
Cheap frontend tricksambiescent
 
Web Development with Sinatra
Web Development with SinatraWeb Development with Sinatra
Web Development with SinatraBob Nadler, Jr.
 
Cheap tricks for startups
Cheap tricks for startupsCheap tricks for startups
Cheap tricks for startupsSimon Willison
 
Game Development Using HTML 5
Game Development Using HTML 5Game Development Using HTML 5
Game Development Using HTML 5osa_ora
 
Hooked on WordPress: WordCamp Columbus
Hooked on WordPress: WordCamp ColumbusHooked on WordPress: WordCamp Columbus
Hooked on WordPress: WordCamp ColumbusShawn Hooper
 
YQL: Hacking on steroids - Yahoo! Open Hack Day 2012
YQL: Hacking on steroids - Yahoo! Open Hack Day 2012YQL: Hacking on steroids - Yahoo! Open Hack Day 2012
YQL: Hacking on steroids - Yahoo! Open Hack Day 2012Saurabh Sahni
 
JavaScript on Rails 튜토리얼
JavaScript on Rails 튜토리얼JavaScript on Rails 튜토리얼
JavaScript on Rails 튜토리얼Sukjoon Kim
 
The effective use of Django ORM
The effective use of Django ORMThe effective use of Django ORM
The effective use of Django ORMYaroslav Muravskyi
 
iPhone Appleless Apps
iPhone Appleless AppsiPhone Appleless Apps
iPhone Appleless AppsRemy Sharp
 

Tendances (20)

BOSS around the web
BOSS around the webBOSS around the web
BOSS around the web
 
Mojolicious, real-time web framework
Mojolicious, real-time web frameworkMojolicious, real-time web framework
Mojolicious, real-time web framework
 
Rails for Beginners - Le Wagon
Rails for Beginners - Le WagonRails for Beginners - Le Wagon
Rails for Beginners - Le Wagon
 
Context with Yahoo! BOSS
Context with Yahoo! BOSSContext with Yahoo! BOSS
Context with Yahoo! BOSS
 
jQtouch, Building Awesome Webapps
jQtouch, Building Awesome WebappsjQtouch, Building Awesome Webapps
jQtouch, Building Awesome Webapps
 
Mojolicious: what works and what doesn't
Mojolicious: what works and what doesn'tMojolicious: what works and what doesn't
Mojolicious: what works and what doesn't
 
The Art of AngularJS in 2015 - Angular Summit 2015
The Art of AngularJS in 2015 - Angular Summit 2015The Art of AngularJS in 2015 - Angular Summit 2015
The Art of AngularJS in 2015 - Angular Summit 2015
 
AWS Elastic Beanstalk
AWS Elastic BeanstalkAWS Elastic Beanstalk
AWS Elastic Beanstalk
 
Why You Should be Using Web Components Right Now. And How. ForwardJS July 2015
Why You Should be Using Web Components Right Now. And How. ForwardJS July 2015Why You Should be Using Web Components Right Now. And How. ForwardJS July 2015
Why You Should be Using Web Components Right Now. And How. ForwardJS July 2015
 
Moving from Django Apps to Services
Moving from Django Apps to ServicesMoving from Django Apps to Services
Moving from Django Apps to Services
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!
 
Cheap frontend tricks
Cheap frontend tricksCheap frontend tricks
Cheap frontend tricks
 
Web Development with Sinatra
Web Development with SinatraWeb Development with Sinatra
Web Development with Sinatra
 
Cheap tricks for startups
Cheap tricks for startupsCheap tricks for startups
Cheap tricks for startups
 
Game Development Using HTML 5
Game Development Using HTML 5Game Development Using HTML 5
Game Development Using HTML 5
 
Hooked on WordPress: WordCamp Columbus
Hooked on WordPress: WordCamp ColumbusHooked on WordPress: WordCamp Columbus
Hooked on WordPress: WordCamp Columbus
 
YQL: Hacking on steroids - Yahoo! Open Hack Day 2012
YQL: Hacking on steroids - Yahoo! Open Hack Day 2012YQL: Hacking on steroids - Yahoo! Open Hack Day 2012
YQL: Hacking on steroids - Yahoo! Open Hack Day 2012
 
JavaScript on Rails 튜토리얼
JavaScript on Rails 튜토리얼JavaScript on Rails 튜토리얼
JavaScript on Rails 튜토리얼
 
The effective use of Django ORM
The effective use of Django ORMThe effective use of Django ORM
The effective use of Django ORM
 
iPhone Appleless Apps
iPhone Appleless AppsiPhone Appleless Apps
iPhone Appleless Apps
 

En vedette

TimelineSetter
TimelineSetterTimelineSetter
TimelineSettera_l
 
Learning to `chmod` the news
Learning to `chmod` the newsLearning to `chmod` the news
Learning to `chmod` the newsa_l
 
Einstiegsgehalt in der Lebensmittelbranche 2015 - foodjobs.de
Einstiegsgehalt in der Lebensmittelbranche 2015 - foodjobs.deEinstiegsgehalt in der Lebensmittelbranche 2015 - foodjobs.de
Einstiegsgehalt in der Lebensmittelbranche 2015 - foodjobs.deBianca Burmester
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika AldabaLightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldabaux singapore
 

En vedette (6)

TimelineSetter
TimelineSetterTimelineSetter
TimelineSetter
 
Learning to `chmod` the news
Learning to `chmod` the newsLearning to `chmod` the news
Learning to `chmod` the news
 
Einstiegsgehalt in der Lebensmittelbranche 2015 - foodjobs.de
Einstiegsgehalt in der Lebensmittelbranche 2015 - foodjobs.deEinstiegsgehalt in der Lebensmittelbranche 2015 - foodjobs.de
Einstiegsgehalt in der Lebensmittelbranche 2015 - foodjobs.de
 
Intervalos aparentes de estadistica
Intervalos aparentes de estadistica Intervalos aparentes de estadistica
Intervalos aparentes de estadistica
 
Tipos de graficos de control
Tipos de graficos de controlTipos de graficos de control
Tipos de graficos de control
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika AldabaLightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
 

Similaire à Simple Web Apps With Sinatra

Effectively Testing Services on Rails - Railsconf 2014
Effectively Testing Services on Rails - Railsconf 2014Effectively Testing Services on Rails - Railsconf 2014
Effectively Testing Services on Rails - Railsconf 2014neal_kemp
 
Building a real life application in node js
Building a real life application in node jsBuilding a real life application in node js
Building a real life application in node jsfakedarren
 
Transformando os pepinos do cliente no código de testes da sua aplicação
Transformando os pepinos do cliente no código de testes da sua aplicaçãoTransformando os pepinos do cliente no código de testes da sua aplicação
Transformando os pepinos do cliente no código de testes da sua aplicaçãoRodrigo Urubatan
 
REST with Eve and Python
REST with Eve and PythonREST with Eve and Python
REST with Eve and PythonPiXeL16
 
Be happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuBe happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuLucas Renan
 
MIKE Stack Introduction - MongoDB, io.js, KendoUI, and Express
MIKE Stack Introduction - MongoDB, io.js, KendoUI, and ExpressMIKE Stack Introduction - MongoDB, io.js, KendoUI, and Express
MIKE Stack Introduction - MongoDB, io.js, KendoUI, and ExpressCharlie Key
 
Effectively Testing Services - Burlington Ruby Conf
Effectively Testing Services - Burlington Ruby ConfEffectively Testing Services - Burlington Ruby Conf
Effectively Testing Services - Burlington Ruby Confneal_kemp
 
FamilySearch Reference Client
FamilySearch Reference ClientFamilySearch Reference Client
FamilySearch Reference ClientDallan Quass
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVCAlive Kuo
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overviewYehuda Katz
 
Backbone js
Backbone jsBackbone js
Backbone jsrstankov
 
Associations & JavaScript
Associations & JavaScriptAssociations & JavaScript
Associations & JavaScriptJoost Elfering
 
Authenticating and Securing Node.js APIs
Authenticating and Securing Node.js APIsAuthenticating and Securing Node.js APIs
Authenticating and Securing Node.js APIsJimmy Guerrero
 
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史Shengyou Fan
 
以Vue開發電子商務網站
架構與眉角
以Vue開發電子商務網站
架構與眉角以Vue開發電子商務網站
架構與眉角
以Vue開發電子商務網站
架構與眉角Mei-yu Chen
 
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK - Nicola Iarocci - Co...
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK -  Nicola Iarocci - Co...RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK -  Nicola Iarocci - Co...
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK - Nicola Iarocci - Co...Codemotion
 
Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4DEVCON
 
Spca2014 hillier 3rd party_javascript_libraries
Spca2014 hillier 3rd party_javascript_librariesSpca2014 hillier 3rd party_javascript_libraries
Spca2014 hillier 3rd party_javascript_librariesNCCOMMS
 
SINATRA + HAML + TWITTER
SINATRA + HAML + TWITTERSINATRA + HAML + TWITTER
SINATRA + HAML + TWITTERElber Ribeiro
 

Similaire à Simple Web Apps With Sinatra (20)

Effectively Testing Services on Rails - Railsconf 2014
Effectively Testing Services on Rails - Railsconf 2014Effectively Testing Services on Rails - Railsconf 2014
Effectively Testing Services on Rails - Railsconf 2014
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Building a real life application in node js
Building a real life application in node jsBuilding a real life application in node js
Building a real life application in node js
 
Transformando os pepinos do cliente no código de testes da sua aplicação
Transformando os pepinos do cliente no código de testes da sua aplicaçãoTransformando os pepinos do cliente no código de testes da sua aplicação
Transformando os pepinos do cliente no código de testes da sua aplicação
 
REST with Eve and Python
REST with Eve and PythonREST with Eve and Python
REST with Eve and Python
 
Be happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuBe happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP Itu
 
MIKE Stack Introduction - MongoDB, io.js, KendoUI, and Express
MIKE Stack Introduction - MongoDB, io.js, KendoUI, and ExpressMIKE Stack Introduction - MongoDB, io.js, KendoUI, and Express
MIKE Stack Introduction - MongoDB, io.js, KendoUI, and Express
 
Effectively Testing Services - Burlington Ruby Conf
Effectively Testing Services - Burlington Ruby ConfEffectively Testing Services - Burlington Ruby Conf
Effectively Testing Services - Burlington Ruby Conf
 
FamilySearch Reference Client
FamilySearch Reference ClientFamilySearch Reference Client
FamilySearch Reference Client
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Backbone js
Backbone jsBackbone js
Backbone js
 
Associations & JavaScript
Associations & JavaScriptAssociations & JavaScript
Associations & JavaScript
 
Authenticating and Securing Node.js APIs
Authenticating and Securing Node.js APIsAuthenticating and Securing Node.js APIs
Authenticating and Securing Node.js APIs
 
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
 
以Vue開發電子商務網站
架構與眉角
以Vue開發電子商務網站
架構與眉角以Vue開發電子商務網站
架構與眉角
以Vue開發電子商務網站
架構與眉角
 
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK - Nicola Iarocci - Co...
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK -  Nicola Iarocci - Co...RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK -  Nicola Iarocci - Co...
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK - Nicola Iarocci - Co...
 
Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4
 
Spca2014 hillier 3rd party_javascript_libraries
Spca2014 hillier 3rd party_javascript_librariesSpca2014 hillier 3rd party_javascript_libraries
Spca2014 hillier 3rd party_javascript_libraries
 
SINATRA + HAML + TWITTER
SINATRA + HAML + TWITTERSINATRA + HAML + TWITTER
SINATRA + HAML + TWITTER
 

Dernier

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 

Dernier (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 

Simple Web Apps With Sinatra

  • 1. Simple Web Apps With Sinatra
  • 2. Micro web apps with Sinatra
  • 3.
  • 4.
  • 9. Ruby require ‘rubygems’ require ‘sinatra’ get ‘/’ do #do stuff end post ‘/’ do #do stuff end
  • 10. MVC Model Controller View
  • 11. MVC Model Controller View Data Presentation
  • 12. MVC Model Controller View Data 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" }
  • 13. MVC Model Controller View Data 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" }
  • 14. 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",
  • 15. MVC Model Controller View Data Presentation post ‘/profile’ do # pass new values from user input # to Model end { "id": "2902652", "name": "Al Shaw",
  • 16. 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
  • 19. 1. Require Libraries require ‘rubygems’ # lets you use libraries require ‘sinatra‘ # our micro web framework require ‘open-uri’ # for requesting URLs require ‘json‘ # for parsing JSON
  • 20. 2. A get block get ‘/profile/:id’ do # do stuff end URL parameter http://localhost:4567/pro le/ashaw params[:id]
  • 21. 3. Capture parameters get ‘/profile/:id’ do user = params[:id] end
  • 22. 4. Call & Parse FB API get ‘/profile/:id’ do user = params[:id] profile = open(“http://graph.facebook.com/#{user}”).read profile = JSON.parse(profile) end http://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"}
  • 23. 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>”
  • 24. And Now For Something Completely Different
  • 25. Does x follow you on Twitter?
  • 27. Actions ‘/‘ Collect user data ‘/follows‘ Show following status
  • 28. Collect user names get ‘/‘ do #show form end
  • 29. *Short interlude regarding views get ‘/‘ do get ‘/‘ do “Hello” erb :hello end end
  • 30. *Short interlude regarding views get ‘/‘ do get ‘/‘ do “Hello” erb :hello end end Implicit view View Template
  • 31. *Short interlude regarding views get ‘/‘ do get ‘/‘ do “Hello” erb :hello end end Implicit view View Template
  • 32. View Templates followapp.rb views/index.erb get ‘/‘ do <html> erb :index <head> end ....
  • 33. View Templates followapp.rb views/index.erb get ‘/‘ do <%= @foo %> @foo = ‘bar’ erb :index end
  • 34. </Short interlude regarding views>
  • 35. Actions ‘/‘ Collect user data followapp.rb get ‘/‘ do erb :index end
  • 36. Actions ‘/‘ Collect user data views/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>
  • 37. Actions ‘/‘ Collect user data views/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>
  • 39. Actions ‘/follows‘ Show following status get '/follows' do @user1 = params[:user1] @user2 = params[:user2] #not implemented yet @following = is_following?(@user1, @user2) erb :follows end
  • 40. 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
  • 41. is_following? • Get user IDs for usernames def 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
  • 42. is_following? • Get user IDs for usernames def 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 twitter_id(‘a_l’) #=> 7865282
  • 43. is_following? • Check if user ID 1 is in user 2’s follower array def 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 def 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
  • 44. is_following? • Check if user ID 1 is in user 2’s follower array def 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 def 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 is_following?('barackobama', 'a_l') #=> true
  • 45. Putting it together def 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 def 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 get '/follows' do @user1 = params[:user1] @user2 = params[:user2] @following = is_following?(@user1, @user2) erb :follows end
  • 46. Putting it together views/follows.erb <h2> <%= @user1 %> <%= @following ? "follows" : "does not follow" %> <%= @user2 %> </h2>

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n