APIs/Services for
  Mobile Apps
     (in Ruby)

   Andrei Navarro
Background
● Web development for Startups
  ○ Web Products (w/ APIs)
● .NET, PHP, Ruby (Rails, Ramaze)
● Currently:
  ○ Tech Guy at Scrambled Eggs
  ○ Faculty at Ateneo de Manila
● PHRUG, Startup Weekend
● Geekcamp
History
● 80 - 90s: Computers for "Business" use
● 90s - Early 00s: "dotcom" era; ecommerce
  ○ Web TV
● Mid 00s: "Web 2.0"
History
Recent Years
● Late 00s - 10s: Mobile Era
  ○   Faster mobile internet speed
  ○   Easier access to devices
  ○   Exponential growth in users
  ○   "App" Era
● People becoming App developers instead of
  going into Web
Mobile Apps need Web
● Web developers are still relevant
  ○ Need to provide data/interconnectedness to mobile
    apps
  ○ Moving from Application layer to
    Presentation/Session layer
Similarities with RIA
● Javascript/AJAX makes requests to your
  backend -- which is connected to your
  database
AJAX architecture
Quick Story (Amundsen vs Scott)
● The Antarctic Race
  ○ http://www.slideshare.net/apigee/amundsens-dogs-
    information-halos-and-apis
● Apps are the "South Pole"
● Native Wisdom: Your APIs support and
  strengthen your mobile apps (whether built
  by yourself or by a developer community)
Mobile App Data Architecture
Building APIs
● Just like any other web app
  ○ no view layer (HTML/CSS/JS)
  ○ apps "view" your API
● Transfer is via HTTP/HTTPS
● Plan your format
  ○ JSON, XML, SOAP etc.
  ○ REST/RPC?
  ○ Parsing capability on client end
● Scalability
● Sessions and Authentication
  ○ token-based
  ○ header-based
  ○ devise
Design-by-Contract
● Define your "interface" that will supply the
  mobile app with JSON/XML data
   ○ use mocks!
● When building your API, conform to that
  interface
● Allows parallelization of development
Testing
● Web apps are tested on a browser
● APIs ultimate test is on the target clients
● Development usually happens in parallel --
  and you need to validate your API works
  before app is completed
  ○ rest-client gem
  ○ rest-client chrome/firefox extensions
Building it with Rails
● Rails already supports RESTful architecture
● Rails already supports multiple formats
● Can use RSpec/Test::Unit to validate API
  requests
● Variety of existing gems already tested and
  well integrated with Rails
Issues with Rails
● Issues for APIs (not in general)
● Adds a lot of dependencies that aren't
  needed for your purpose
  ○ Wasted memory
● Cherry pick middleware and modules from
  the stack
  ○ hassle.
● Others
  ○ http://engineering.gomiso.com/2011/05/16/if-youre-
    using-to_json-youre-doing-it-wrong/
Some solutions
● RABL (Ruby API builder language)
  ○ https://github.com/nesquena/rabl
● Rails-API
  ○ https://github.com/rails-api/rails-api
  ○ a subset of rails specifically for building APIs
Microframeworks
● Much lighter than Rails
  ○   Sinatra/Padrino
  ○   Ramaze
  ○   Merb
  ○   Camping
● Build only what you need
  ○ smaller footprint
● You'll need to know Ruby well
  ○ that's actually a good thing
Grape Framework
● https://github.com/intridea/grape
● From Intridea, creators of OmniAuth
● "An opinionated microframework for creating
  REST-like APIs in Ruby"

Generalized
Rapid
API
Erector
Grape Framework
Grape Framework
● Rack-based
   ○ easy deployment over passenger
   ○ run via config.ru
   ○ can sit alongside your Rails app
     ■ mount My::API in config/routes.rb
● Different content-types supported
● Fun HTTP stuff (headers, verbs, status
  codes etc)
● Entities: grouping/presenting your data into
  "Models"
● Killer feature: API versioning
API Versioning
Traditional Web Development
API Versioning
● Support several versions of Mobile apps
  ○ up to client/user to update
● App store problem
  ○ Dev
  ○ Staging
    ■ App store staging
  ○ Production
    ■ App store production
API Versioning
Recommendations
● MVC-ize your application
  ○ It's just good design.
● Use Devise/Omniauth for authentication
  ○ token-based authentication
● Use oj gem for JSON
  ○ https://github.com/ohler55/oj
  ○ faster Json parser/encoder compared to native ruby
    JSON parsing
● Use ox gem for XML
Questions?
 @dreinavarro

APIs for mobile

  • 1.
    APIs/Services for Mobile Apps (in Ruby) Andrei Navarro
  • 2.
    Background ● Web developmentfor Startups ○ Web Products (w/ APIs) ● .NET, PHP, Ruby (Rails, Ramaze) ● Currently: ○ Tech Guy at Scrambled Eggs ○ Faculty at Ateneo de Manila ● PHRUG, Startup Weekend ● Geekcamp
  • 3.
    History ● 80 -90s: Computers for "Business" use ● 90s - Early 00s: "dotcom" era; ecommerce ○ Web TV ● Mid 00s: "Web 2.0"
  • 4.
  • 5.
    Recent Years ● Late00s - 10s: Mobile Era ○ Faster mobile internet speed ○ Easier access to devices ○ Exponential growth in users ○ "App" Era ● People becoming App developers instead of going into Web
  • 6.
    Mobile Apps needWeb ● Web developers are still relevant ○ Need to provide data/interconnectedness to mobile apps ○ Moving from Application layer to Presentation/Session layer
  • 7.
    Similarities with RIA ●Javascript/AJAX makes requests to your backend -- which is connected to your database
  • 8.
  • 9.
    Quick Story (Amundsenvs Scott) ● The Antarctic Race ○ http://www.slideshare.net/apigee/amundsens-dogs- information-halos-and-apis ● Apps are the "South Pole" ● Native Wisdom: Your APIs support and strengthen your mobile apps (whether built by yourself or by a developer community)
  • 10.
    Mobile App DataArchitecture
  • 11.
    Building APIs ● Justlike any other web app ○ no view layer (HTML/CSS/JS) ○ apps "view" your API ● Transfer is via HTTP/HTTPS ● Plan your format ○ JSON, XML, SOAP etc. ○ REST/RPC? ○ Parsing capability on client end ● Scalability ● Sessions and Authentication ○ token-based ○ header-based ○ devise
  • 12.
    Design-by-Contract ● Define your"interface" that will supply the mobile app with JSON/XML data ○ use mocks! ● When building your API, conform to that interface ● Allows parallelization of development
  • 13.
    Testing ● Web appsare tested on a browser ● APIs ultimate test is on the target clients ● Development usually happens in parallel -- and you need to validate your API works before app is completed ○ rest-client gem ○ rest-client chrome/firefox extensions
  • 14.
    Building it withRails ● Rails already supports RESTful architecture ● Rails already supports multiple formats ● Can use RSpec/Test::Unit to validate API requests ● Variety of existing gems already tested and well integrated with Rails
  • 15.
    Issues with Rails ●Issues for APIs (not in general) ● Adds a lot of dependencies that aren't needed for your purpose ○ Wasted memory ● Cherry pick middleware and modules from the stack ○ hassle. ● Others ○ http://engineering.gomiso.com/2011/05/16/if-youre- using-to_json-youre-doing-it-wrong/
  • 16.
    Some solutions ● RABL(Ruby API builder language) ○ https://github.com/nesquena/rabl ● Rails-API ○ https://github.com/rails-api/rails-api ○ a subset of rails specifically for building APIs
  • 17.
    Microframeworks ● Much lighterthan Rails ○ Sinatra/Padrino ○ Ramaze ○ Merb ○ Camping ● Build only what you need ○ smaller footprint ● You'll need to know Ruby well ○ that's actually a good thing
  • 18.
    Grape Framework ● https://github.com/intridea/grape ●From Intridea, creators of OmniAuth ● "An opinionated microframework for creating REST-like APIs in Ruby" Generalized Rapid API Erector
  • 19.
  • 20.
    Grape Framework ● Rack-based ○ easy deployment over passenger ○ run via config.ru ○ can sit alongside your Rails app ■ mount My::API in config/routes.rb ● Different content-types supported ● Fun HTTP stuff (headers, verbs, status codes etc) ● Entities: grouping/presenting your data into "Models" ● Killer feature: API versioning
  • 21.
  • 22.
    API Versioning ● Supportseveral versions of Mobile apps ○ up to client/user to update ● App store problem ○ Dev ○ Staging ■ App store staging ○ Production ■ App store production
  • 23.
  • 24.
    Recommendations ● MVC-ize yourapplication ○ It's just good design. ● Use Devise/Omniauth for authentication ○ token-based authentication ● Use oj gem for JSON ○ https://github.com/ohler55/oj ○ faster Json parser/encoder compared to native ruby JSON parsing ● Use ox gem for XML
  • 25.