http://www.rails.in.thDeveloping API with Rails
Sponsorshttp://www.artellectual.com   http://www.oozou.com
Who Am ISakchai (Zack) Siripanyawuth   twitter: @artellectual  developer at Artellectual
Options•   LightRail - https://github.com/lightness/lightrail•   RocketPants (i am not joking) - https://github.com/    fil...
Why Rails Metal• Because its thinner• Benefits of rails is a few lines of code away• Less context switching (ie same  conve...
Something to keep in mind
VS
MODULES = [      AbstractController::Layouts,      AbstractController::Translation,      AbstractController::AssetPaths,  ...
MODULES = [                                                 include   ActionController::Helpers      AbstractController::L...
MODULES = [                                                 include   ActionController::Helpers      AbstractController::L...
MODULES = [                                                 include   ActionController::Helpers      AbstractController::L...
How much faster?                    ActionController::Base                    ActionController::Metal             1200    ...
Let’s Code!
/app/controllers/application_controller.rb
/app/controllers/application_controller.rbclass ApplicationController < ActionController::Base  # All kinds of magicend
/app/controllers/api_controller.rb
/app/controllers/api_controller.rbclass ApiController < ActionController::Metal # inherit from Metal instead of Base  incl...
config/routes.rb
config/routes.rbnamespace "api" do  resources :contacts  resources :comments  resources :photosend
config/routes.rbnamespace "api" do  resources :contacts  resources :comments  resources :photosend       example:
config/routes.rb   namespace "api" do     resources :contacts     resources :comments     resources :photos   end          ...
/app/controllers/api/contacts_controller.rb
/app/controllers/api/contacts_controller.rbclass Api::ContactsController < ApiController  # more magic goes hereend
Lets look at Views• RABL - https://github.com/nesquena/rabl• jbuilder - https://github.com/rails/jbuilder• jsonify - https...
Testing with json_spechttps://github.com/collectiveidea/json_spec   describe "/contacts/:id.json" do     before(:each) do ...
Thank You !
Upcoming SlideShare
Loading in …5
×

Developing api with rails metal

8,332 views

Published on

Published in: Technology, Education
0 Comments
10 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,332
On SlideShare
0
From Embeds
0
Number of Embeds
83
Actions
Shares
0
Downloads
58
Comments
0
Likes
10
Embeds 0
No embeds

No notes for slide
  • - 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
  • - Thanks to our sponsors for providing us with the space\n
  • - Introduce yourself\n
  • - 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
  • - Convenient\n- Focus on your code rather than building a framework\n
  • - 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
  • - Might need to add / remove a few depending on your needs\n
  • - Might need to add / remove a few depending on your needs\n
  • - Might need to add / remove a few depending on your needs\n
  • - Might need to add / remove a few depending on your needs\n
  • \n
  • \n
  • \n
  • - explain the code\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Developing api with rails metal

    1. 1. http://www.rails.in.thDeveloping API with Rails
    2. 2. Sponsorshttp://www.artellectual.com http://www.oozou.com
    3. 3. Who Am ISakchai (Zack) Siripanyawuth twitter: @artellectual developer at Artellectual
    4. 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. 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
    6. 6. Something to keep in mind
    7. 7. VS
    8. 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. 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. 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. 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. 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
    13. 13. Let’s Code!
    14. 14. /app/controllers/application_controller.rb
    15. 15. /app/controllers/application_controller.rbclass ApplicationController < ActionController::Base # All kinds of magicend
    16. 16. /app/controllers/api_controller.rb
    17. 17. /app/controllers/api_controller.rbclass 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_forgeryend
    18. 18. config/routes.rb
    19. 19. config/routes.rbnamespace "api" do resources :contacts resources :comments resources :photosend
    20. 20. config/routes.rbnamespace "api" do resources :contacts resources :comments resources :photosend example:
    21. 21. config/routes.rb namespace "api" do resources :contacts resources :comments resources :photos end example:www.greatapp.com/api/contacts
    22. 22. /app/controllers/api/contacts_controller.rb
    23. 23. /app/controllers/api/contacts_controller.rbclass Api::ContactsController < ApiController # more magic goes hereend
    24. 24. Lets look at Views• RABL - https://github.com/nesquena/rabl• jbuilder - https://github.com/rails/jbuilder• jsonify - https://github.com/bsiggelkow/ jsonify
    25. 25. Testing with json_spechttps://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
    26. 26. Thank You !

    ×