SlideShare a Scribd company logo
1 of 26
http://www.rails.in.th

Developing API with Rails
Sponsors




http://www.artellectual.com   http://www.oozou.com
Who Am I

Sakchai (Zack) Siripanyawuth
   twitter: @artellectual
  developer at Artellectual
Options

•   LightRail - https://github.com/lightness/lightrail

•   RocketPants (i am not joking) - https://github.com/
    filtersquad/rocket_pants

•   Sinatra Mounted into a Rails App - http://
    www.sinatrarb.com/

•   Good Ol’ - ActionController::Base
Why Rails Metal
• Because its thinner
• Benefits of rails is a few lines of code away
• Less context switching (ie same
  conventions)
• Easy to do Hybrid Apps
• Modular
• No need to write Boilerplate Code
Something to keep in mind
VS
MODULES = [
      AbstractController::Layouts,
      AbstractController::Translation,
      AbstractController::AssetPaths,
      Helpers,
      HideActions,
      UrlFor,
      Redirecting,
      Rendering,
      Renderers::All,
      ConditionalGet,
      RackDelegation,
      Caching,
      MimeResponds,
      ImplicitRender,
      Cookies,
      Flash,
      RequestForgeryProtection,
      ForceSSL,
      Streaming,
                                                       VS
      DataStreaming,
      RecordIdentifier,
      HttpAuthentication::Basic::ControllerMethods,
      HttpAuthentication::Digest::ControllerMethods,
      HttpAuthentication::Token::ControllerMethods,
      AbstractController::Callbacks,
      Rescue,
      Instrumentation,
      ParamsWrapper
    ]
MODULES = [                                                 include   ActionController::Helpers
      AbstractController::Layouts,                          include   ActionController::Redirecting
      AbstractController::Translation,                      include   ActionController::Rendering
      AbstractController::AssetPaths,                       include   ActionController::Renderers::All
      Helpers,                                              include   ActionController::ConditionalGet
      HideActions,                                          include   ActionController::MimeResponds
      UrlFor,                                               include   ActionController::RequestForgeryProtection
      Redirecting,                                          include   ActionController::ForceSSL
      Rendering,                                            include   AbstractController::Callbacks
      Renderers::All,                                       include   ActionController::Instrumentation
      ConditionalGet,                                       include   ActionController::ParamsWrapper
      RackDelegation,                                       include   Devise::Controllers::Helpers
      Caching,
      MimeResponds,
      ImplicitRender,
      Cookies,
      Flash,
      RequestForgeryProtection,
      ForceSSL,
      Streaming,
                                                       VS
      DataStreaming,
      RecordIdentifier,
      HttpAuthentication::Basic::ControllerMethods,
      HttpAuthentication::Digest::ControllerMethods,
      HttpAuthentication::Token::ControllerMethods,
      AbstractController::Callbacks,
      Rescue,
      Instrumentation,
      ParamsWrapper
    ]
MODULES = [                                                 include   ActionController::Helpers
      AbstractController::Layouts,                          include   ActionController::Redirecting
      AbstractController::Translation,                      include   ActionController::Rendering
      AbstractController::AssetPaths,                       include   ActionController::Renderers::All
      Helpers,                                              include   ActionController::ConditionalGet
      HideActions,                                          include   ActionController::MimeResponds
      UrlFor,                                               include   ActionController::RequestForgeryProtection
      Redirecting,                                          include   ActionController::ForceSSL
      Rendering,                                            include   AbstractController::Callbacks
      Renderers::All,                                       include   ActionController::Instrumentation
      ConditionalGet,                                       include   ActionController::ParamsWrapper
      RackDelegation,                                       include   Devise::Controllers::Helpers
      Caching,
      MimeResponds,
      ImplicitRender,
      Cookies,
      Flash,
      RequestForgeryProtection,
      ForceSSL,
      Streaming,
                                                       VS
      DataStreaming,
      RecordIdentifier,
      HttpAuthentication::Basic::ControllerMethods,
      HttpAuthentication::Digest::ControllerMethods,
      HttpAuthentication::Token::ControllerMethods,
      AbstractController::Callbacks,
      Rescue,
      Instrumentation,
      ParamsWrapper
    ]




              28 Modules
MODULES = [                                                 include   ActionController::Helpers
      AbstractController::Layouts,                          include   ActionController::Redirecting
      AbstractController::Translation,                      include   ActionController::Rendering
      AbstractController::AssetPaths,                       include   ActionController::Renderers::All
      Helpers,                                              include   ActionController::ConditionalGet
      HideActions,                                          include   ActionController::MimeResponds
      UrlFor,                                               include   ActionController::RequestForgeryProtection
      Redirecting,                                          include   ActionController::ForceSSL
      Rendering,                                            include   AbstractController::Callbacks
      Renderers::All,                                       include   ActionController::Instrumentation
      ConditionalGet,                                       include   ActionController::ParamsWrapper
      RackDelegation,                                       include   Devise::Controllers::Helpers
      Caching,
      MimeResponds,
      ImplicitRender,
      Cookies,
      Flash,
      RequestForgeryProtection,
      ForceSSL,
      Streaming,
                                                       VS
      DataStreaming,
      RecordIdentifier,
      HttpAuthentication::Basic::ControllerMethods,
      HttpAuthentication::Digest::ControllerMethods,
      HttpAuthentication::Token::ControllerMethods,
      AbstractController::Callbacks,
      Rescue,
      Instrumentation,
      ParamsWrapper
    ]




              28 Modules                                              12 Modules
How much faster?
                    ActionController::Base
                    ActionController::Metal

             1200
                                     1200
              900

              600

              300
                      400
                0
                             Rails


                     not so fine print:
*take these numbers with a grain of salt / benchmark yourself
Let’s Code!
/app/controllers/application_controller.rb
/app/controllers/application_controller.rb


class ApplicationController < ActionController::Base
  # All kinds of magic
end
/app/controllers/api_controller.rb
/app/controllers/api_controller.rb
class ApiController < ActionController::Metal # inherit from Metal instead of Base
  include ActionController::Helpers
  include ActionController::Redirecting
  include ActionController::Rendering
  include ActionController::Renderers::All
  include ActionController::ConditionalGet

  # need this for responding to different types .json .xml etc...
  include ActionController::MimeResponds
  include ActionController::RequestForgeryProtection
  # need this if your using SSL
  include ActionController::ForceSSL
  include AbstractController::Callbacks
  # need this to build 'params'
  include ActionController::Instrumentation
  # need this for wrap_parameters
  include ActionController::ParamsWrapper
  include Devise::Controllers::Helpers

  # need make your ApiController aware of your routes
  include Rails.application.routes.url_helpers

  # tell the controller where to look for templates
  append_view_path "#{Rails.root}/app/views"

  # you need this to wrap the parameters correctly eg
  # { "person": { "name": "Zack", "email": "sakchai@artellectual.com", "twitter": "@artellectual" }}
  wrap_parameters format: [:json]

  # might not need this (depends on your client) rails bypasses automatically if client is not browser
  protect_from_forgery
end
config/routes.rb
config/routes.rb


namespace "api" do
  resources :contacts
  resources :comments
  resources :photos
end
config/routes.rb


namespace "api" do
  resources :contacts
  resources :comments
  resources :photos
end


       example:
config/routes.rb


   namespace "api" do
     resources :contacts
     resources :comments
     resources :photos
   end


           example:
www.greatapp.com/api/contacts
/app/controllers/api/contacts_controller.rb
/app/controllers/api/contacts_controller.rb




class Api::ContactsController < ApiController
  # more magic goes here
end
Lets look at Views

• RABL - https://github.com/nesquena/rabl
• jbuilder - https://github.com/rails/jbuilder
• jsonify - https://github.com/bsiggelkow/
  jsonify
Testing with json_spec
https://github.com/collectiveidea/json_spec
   describe "/contacts/:id.json" do
     before(:each) do
       get :show, id: @client.id, format: :json
     end

     it "should render the correct template for SHOW" do
       response.should render_template("api/contacts/show")
     end

     it "should render the correct attributes for contact" do
       response.body.should have_json_path("id")
       response.body.should have_json_path("first_name")
       response.body.should have_json_path("last_name")
       response.body.should have_json_path("type")
       response.body.should have_json_path("updated_at")
     end
   end
Thank You !

More Related Content

What's hot

Ruby HTTP clients comparison
Ruby HTTP clients comparisonRuby HTTP clients comparison
Ruby HTTP clients comparison
Hiroshi Nakamura
 
Android webservices
Android webservicesAndroid webservices
Android webservices
Krazy Koder
 
Vulnerability intelligence with vulners.com / Кирилл Ермаков, Игорь Булатенко...
Vulnerability intelligence with vulners.com / Кирилл Ермаков, Игорь Булатенко...Vulnerability intelligence with vulners.com / Кирилл Ермаков, Игорь Булатенко...
Vulnerability intelligence with vulners.com / Кирилл Ермаков, Игорь Булатенко...
Ontico
 

What's hot (20)

Ruby HTTP clients comparison
Ruby HTTP clients comparisonRuby HTTP clients comparison
Ruby HTTP clients comparison
 
Java Web Programming [9/9] : Web Application Security
Java Web Programming [9/9] : Web Application SecurityJava Web Programming [9/9] : Web Application Security
Java Web Programming [9/9] : Web Application Security
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic Analysis
 
HTTP For the Good or the Bad
HTTP For the Good or the BadHTTP For the Good or the Bad
HTTP For the Good or the Bad
 
Managing user's data with Spring Session
Managing user's data with Spring SessionManaging user's data with Spring Session
Managing user's data with Spring Session
 
Cracking JWT tokens: a tale of magic, Node.js and parallel computing - Code E...
Cracking JWT tokens: a tale of magic, Node.js and parallel computing - Code E...Cracking JWT tokens: a tale of magic, Node.js and parallel computing - Code E...
Cracking JWT tokens: a tale of magic, Node.js and parallel computing - Code E...
 
Security Checkpoints in Agile SDLC
Security Checkpoints in Agile SDLCSecurity Checkpoints in Agile SDLC
Security Checkpoints in Agile SDLC
 
Testing NodeJS Security
Testing NodeJS SecurityTesting NodeJS Security
Testing NodeJS Security
 
Plack at OSCON 2010
Plack at OSCON 2010Plack at OSCON 2010
Plack at OSCON 2010
 
Browser Serving Your We Application Security - ZendCon 2017
Browser Serving Your We Application Security - ZendCon 2017Browser Serving Your We Application Security - ZendCon 2017
Browser Serving Your We Application Security - ZendCon 2017
 
Servlet30 20081218
Servlet30 20081218Servlet30 20081218
Servlet30 20081218
 
Client side
Client sideClient side
Client side
 
Android webservices
Android webservicesAndroid webservices
Android webservices
 
10 Excellent Ways to Secure Your Spring Boot Application - Devoxx Morocco 2019
10 Excellent Ways to Secure Your Spring Boot Application - Devoxx Morocco 201910 Excellent Ways to Secure Your Spring Boot Application - Devoxx Morocco 2019
10 Excellent Ways to Secure Your Spring Boot Application - Devoxx Morocco 2019
 
Ws security with opensource platform
Ws security with opensource platformWs security with opensource platform
Ws security with opensource platform
 
Vulnerability intelligence with vulners.com / Кирилл Ермаков, Игорь Булатенко...
Vulnerability intelligence with vulners.com / Кирилл Ермаков, Игорь Булатенко...Vulnerability intelligence with vulners.com / Кирилл Ермаков, Игорь Булатенко...
Vulnerability intelligence with vulners.com / Кирилл Ермаков, Игорь Булатенко...
 
There and back again: A story of a s
There and back again: A story of a sThere and back again: A story of a s
There and back again: A story of a s
 
Dynamic Database Credentials: Security Contingency Planning
Dynamic Database Credentials: Security Contingency PlanningDynamic Database Credentials: Security Contingency Planning
Dynamic Database Credentials: Security Contingency Planning
 
Automated malware analysis
Automated malware analysisAutomated malware analysis
Automated malware analysis
 
Make your application expressive
Make your application expressiveMake your application expressive
Make your application expressive
 

Similar to Developing api with rails metal

Using Apache as an Application Server
Using Apache as an Application ServerUsing Apache as an Application Server
Using Apache as an Application Server
Phil Windley
 
Dynatrace: DevOps, shift-left &amp; self-healing a performance clinic with andi
Dynatrace: DevOps, shift-left &amp; self-healing a performance clinic with andiDynatrace: DevOps, shift-left &amp; self-healing a performance clinic with andi
Dynatrace: DevOps, shift-left &amp; self-healing a performance clinic with andi
Dynatrace
 

Similar to Developing api with rails metal (20)

Intro to Ruby on Rails
Intro to Ruby on RailsIntro to Ruby on Rails
Intro to Ruby on Rails
 
Null Mumbai 14th May Lesser Known Webapp attacks by Ninad Sarang
Null Mumbai 14th May Lesser Known Webapp attacks by Ninad SarangNull Mumbai 14th May Lesser Known Webapp attacks by Ninad Sarang
Null Mumbai 14th May Lesser Known Webapp attacks by Ninad Sarang
 
.NET Architects Day - DNAD 2011
.NET Architects Day - DNAD 2011.NET Architects Day - DNAD 2011
.NET Architects Day - DNAD 2011
 
09 - Fábio Akita - Além do rails
09 - Fábio Akita - Além do rails09 - Fábio Akita - Além do rails
09 - Fábio Akita - Além do rails
 
Using Apache as an Application Server
Using Apache as an Application ServerUsing Apache as an Application Server
Using Apache as an Application Server
 
Developing ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller PatternDeveloping ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller Pattern
 
Asp.Net MVC Framework Design Pattern
Asp.Net MVC Framework Design PatternAsp.Net MVC Framework Design Pattern
Asp.Net MVC Framework Design Pattern
 
Nginx: Accelerate Rails, HTTP Tricks
Nginx: Accelerate Rails, HTTP TricksNginx: Accelerate Rails, HTTP Tricks
Nginx: Accelerate Rails, HTTP Tricks
 
Rohit yadav cloud stack internals
Rohit yadav   cloud stack internalsRohit yadav   cloud stack internals
Rohit yadav cloud stack internals
 
Struts2 - 101
Struts2 - 101Struts2 - 101
Struts2 - 101
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010
 
Foomo / Zugspitze Presentation
Foomo / Zugspitze PresentationFoomo / Zugspitze Presentation
Foomo / Zugspitze Presentation
 
Rails 2.3 and Rack - NHRuby Feb 2009
Rails 2.3 and Rack - NHRuby Feb 2009Rails 2.3 and Rack - NHRuby Feb 2009
Rails 2.3 and Rack - NHRuby Feb 2009
 
Clojure and the Web
Clojure and the WebClojure and the Web
Clojure and the Web
 
Operator SDK for K8s using Go
Operator SDK for K8s using GoOperator SDK for K8s using Go
Operator SDK for K8s using Go
 
Advanced iOS Debbuging (Reloaded)
Advanced iOS Debbuging (Reloaded)Advanced iOS Debbuging (Reloaded)
Advanced iOS Debbuging (Reloaded)
 
11-DWR-and-JQuery
11-DWR-and-JQuery11-DWR-and-JQuery
11-DWR-and-JQuery
 
11-DWR-and-JQuery
11-DWR-and-JQuery11-DWR-and-JQuery
11-DWR-and-JQuery
 
Rack
RackRack
Rack
 
Dynatrace: DevOps, shift-left &amp; self-healing a performance clinic with andi
Dynatrace: DevOps, shift-left &amp; self-healing a performance clinic with andiDynatrace: DevOps, shift-left &amp; self-healing a performance clinic with andi
Dynatrace: DevOps, shift-left &amp; self-healing a performance clinic with andi
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

Developing api with rails metal

  • 3. Who Am I Sakchai (Zack) Siripanyawuth twitter: @artellectual developer at Artellectual
  • 4. Options • LightRail - https://github.com/lightness/lightrail • RocketPants (i am not joking) - https://github.com/ filtersquad/rocket_pants • Sinatra Mounted into a Rails App - http:// www.sinatrarb.com/ • Good Ol’ - ActionController::Base
  • 5. Why Rails Metal • Because its thinner • Benefits of rails is a few lines of code away • Less context switching (ie same conventions) • Easy to do Hybrid Apps • Modular • No need to write Boilerplate Code
  • 7. VS
  • 8. MODULES = [ AbstractController::Layouts, AbstractController::Translation, AbstractController::AssetPaths, Helpers, HideActions, UrlFor, Redirecting, Rendering, Renderers::All, ConditionalGet, RackDelegation, Caching, MimeResponds, ImplicitRender, Cookies, Flash, RequestForgeryProtection, ForceSSL, Streaming, VS DataStreaming, RecordIdentifier, HttpAuthentication::Basic::ControllerMethods, HttpAuthentication::Digest::ControllerMethods, HttpAuthentication::Token::ControllerMethods, AbstractController::Callbacks, Rescue, Instrumentation, ParamsWrapper ]
  • 9. MODULES = [ include ActionController::Helpers AbstractController::Layouts, include ActionController::Redirecting AbstractController::Translation, include ActionController::Rendering AbstractController::AssetPaths, include ActionController::Renderers::All Helpers, include ActionController::ConditionalGet HideActions, include ActionController::MimeResponds UrlFor, include ActionController::RequestForgeryProtection Redirecting, include ActionController::ForceSSL Rendering, include AbstractController::Callbacks Renderers::All, include ActionController::Instrumentation ConditionalGet, include ActionController::ParamsWrapper RackDelegation, include Devise::Controllers::Helpers Caching, MimeResponds, ImplicitRender, Cookies, Flash, RequestForgeryProtection, ForceSSL, Streaming, VS DataStreaming, RecordIdentifier, HttpAuthentication::Basic::ControllerMethods, HttpAuthentication::Digest::ControllerMethods, HttpAuthentication::Token::ControllerMethods, AbstractController::Callbacks, Rescue, Instrumentation, ParamsWrapper ]
  • 10. MODULES = [ include ActionController::Helpers AbstractController::Layouts, include ActionController::Redirecting AbstractController::Translation, include ActionController::Rendering AbstractController::AssetPaths, include ActionController::Renderers::All Helpers, include ActionController::ConditionalGet HideActions, include ActionController::MimeResponds UrlFor, include ActionController::RequestForgeryProtection Redirecting, include ActionController::ForceSSL Rendering, include AbstractController::Callbacks Renderers::All, include ActionController::Instrumentation ConditionalGet, include ActionController::ParamsWrapper RackDelegation, include Devise::Controllers::Helpers Caching, MimeResponds, ImplicitRender, Cookies, Flash, RequestForgeryProtection, ForceSSL, Streaming, VS DataStreaming, RecordIdentifier, HttpAuthentication::Basic::ControllerMethods, HttpAuthentication::Digest::ControllerMethods, HttpAuthentication::Token::ControllerMethods, AbstractController::Callbacks, Rescue, Instrumentation, ParamsWrapper ] 28 Modules
  • 11. MODULES = [ include ActionController::Helpers AbstractController::Layouts, include ActionController::Redirecting AbstractController::Translation, include ActionController::Rendering AbstractController::AssetPaths, include ActionController::Renderers::All Helpers, include ActionController::ConditionalGet HideActions, include ActionController::MimeResponds UrlFor, include ActionController::RequestForgeryProtection Redirecting, include ActionController::ForceSSL Rendering, include AbstractController::Callbacks Renderers::All, include ActionController::Instrumentation ConditionalGet, include ActionController::ParamsWrapper RackDelegation, include Devise::Controllers::Helpers Caching, MimeResponds, ImplicitRender, Cookies, Flash, RequestForgeryProtection, ForceSSL, Streaming, VS DataStreaming, RecordIdentifier, HttpAuthentication::Basic::ControllerMethods, HttpAuthentication::Digest::ControllerMethods, HttpAuthentication::Token::ControllerMethods, AbstractController::Callbacks, Rescue, Instrumentation, ParamsWrapper ] 28 Modules 12 Modules
  • 12. How much faster? ActionController::Base ActionController::Metal 1200 1200 900 600 300 400 0 Rails not so fine print: *take these numbers with a grain of salt / benchmark yourself
  • 15. /app/controllers/application_controller.rb class ApplicationController < ActionController::Base # All kinds of magic end
  • 17. /app/controllers/api_controller.rb class ApiController < ActionController::Metal # inherit from Metal instead of Base include ActionController::Helpers include ActionController::Redirecting include ActionController::Rendering include ActionController::Renderers::All include ActionController::ConditionalGet # need this for responding to different types .json .xml etc... include ActionController::MimeResponds include ActionController::RequestForgeryProtection # need this if your using SSL include ActionController::ForceSSL include AbstractController::Callbacks # need this to build 'params' include ActionController::Instrumentation # need this for wrap_parameters include ActionController::ParamsWrapper include Devise::Controllers::Helpers # need make your ApiController aware of your routes include Rails.application.routes.url_helpers # tell the controller where to look for templates append_view_path "#{Rails.root}/app/views" # you need this to wrap the parameters correctly eg # { "person": { "name": "Zack", "email": "sakchai@artellectual.com", "twitter": "@artellectual" }} wrap_parameters format: [:json] # might not need this (depends on your client) rails bypasses automatically if client is not browser protect_from_forgery end
  • 19. config/routes.rb namespace "api" do resources :contacts resources :comments resources :photos end
  • 20. config/routes.rb namespace "api" do resources :contacts resources :comments resources :photos end example:
  • 21. config/routes.rb namespace "api" do resources :contacts resources :comments resources :photos end example: www.greatapp.com/api/contacts
  • 24. Lets look at Views • RABL - https://github.com/nesquena/rabl • jbuilder - https://github.com/rails/jbuilder • jsonify - https://github.com/bsiggelkow/ jsonify
  • 25. Testing with json_spec https://github.com/collectiveidea/json_spec describe "/contacts/:id.json" do before(:each) do get :show, id: @client.id, format: :json end it "should render the correct template for SHOW" do response.should render_template("api/contacts/show") end it "should render the correct attributes for contact" do response.body.should have_json_path("id") response.body.should have_json_path("first_name") response.body.should have_json_path("last_name") response.body.should have_json_path("type") response.body.should have_json_path("updated_at") end end

Editor's Notes

  1. - Thanks everyone for coming\n- Goals for our Group\n- Web Development is shifting to Rich Client Apps\n- Rise of Javascript Libraries / Frameworks\n- Pattern for developing Javascript MVC is very similar to iOS / Android apps\n- Rails is evolving\n- Active Discussions in rails community on how to handle this &amp;#x2018;API World&amp;#x2019;\n
  2. - Thanks to our sponsors for providing us with the space\n
  3. - Introduce yourself\n
  4. - many options out there to choose from\n- We&amp;#x2019;re going to focus on feature of rails not many people know about\n
  5. - Convenient\n- Focus on your code rather than building a framework\n
  6. - People forget that security is important\n- Do you really want to implement security features yourself?\n- with things like Node / Sinatra its up to you to handle the security\n
  7. - Might need to add / remove a few depending on your needs\n
  8. - Might need to add / remove a few depending on your needs\n
  9. - Might need to add / remove a few depending on your needs\n
  10. - Might need to add / remove a few depending on your needs\n
  11. \n
  12. \n
  13. \n
  14. - explain the code\n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n