Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Creating a RESTful API for mobile applications
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Paul McMahon
March 25, 2013
Technology
310
6
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Creating a RESTful API for mobile applications
Presented at
http://www.tokyorubyistmeetup.org/events/2814
Paul McMahon
March 25, 2013
More Decks by Paul McMahon
See All by Paul McMahon
Why Ember.js?
pwim
0
1.1k
JSON API
pwim
0
140
A developer's perspective on entrepreneurship
pwim
1
400
Using GitHub to get a better job
pwim
11
2.3k
Using Analytics to Improve UX
pwim
0
3.4k
Event Organizers Co-Edo edition
pwim
0
2.9k
勉強会を開催する大まかな流れ
pwim
2
10k
Creating International Communities in Japan
pwim
0
230
英語でコミットを書こう
pwim
52
28k
Other Decks in Technology
See All in Technology
AIは実装を速くする。では、私たちは何を今作るべきか?-立場を越えてリリースに向き合ったチーム開発の実践 / 20260801 Hiromi Nakaya and Naoki Takahashi
shift_evolve
PRO
3
450
認知負荷をGemini で溶かす — GKE 基盤「Orbit」における AI エージェントの実践
sansantech
PRO
1
270
組織にどうSREを根付かせるか?〜IVRyの場合〜
abnoumaru
0
330
モダンフロントエンド 開発研修
recruitengineers
PRO
4
570
システム思考で問題に対処する
yussak
0
210
Forza Horizon 6 のテレメトリ機能で 自動運転に使えそうな学習データを集める話
henjin0
0
150
OSPN.JPバージョンアップ作業進捗のご報告 / 20260801-osc26kyoto
akkiesoft
0
420
取引先から届く 「セキュリティチェックシート」の読み解き方
kamadamakoto
0
140
グローバル基準のSREは、運用現場でどう機能したか:成熟度アセスメントの実践 / SRE NEXT 2026
sorawatanabe
0
140
ボトムアップ文化が強い組織で セキュリティをどう根付かせていくかの現在進行形の話 / Making Security Stick in a Bottom-Up Organization
yamaguchitk333
0
210
【CEDEC2026】ゲームシナリオライターを支援するAIツール開発の実践 ― 設計とプロンプトの工夫 ―
cygames
PRO
1
770
メルカリのグローバルアプリで挑んだ AlloyDB 運用と課題解決の実践記
hatappi
0
240
Featured
See All Featured
First, design no harm
axbom
PRO
2
1.2k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
67k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
Deep Space Network (abreviated)
tonyrice
0
250
Optimizing for Happiness
mojombo
378
71k
AI: The stuff that nobody shows you
jnunemaker
PRO
9
890
Joys of Absence: A Defence of Solitary Play
codingconduct
1
430
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
380
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
Thoughts on Productivity
jonyablonski
76
5.3k
Transcript
Creating a RESTful API for mobile applications Paul McMahon @pwim
My Company: My Product:
REST provides a convention for client/ server communication
The core idea in REST: everything is a resource
Example: https://community-board.herokuapp.com/communities/1 or /communities/1
Four methods: GET, POST, PUT, DELETE
5 standard actions in APIs GET /communities List communities POST
/communities Create a community GET /communities/1 Get a community PUT /communities/1 Update a community DELETE /communities/1 Delete a community
Nesting Example: GET /communities/1/posts
These actions are all you need!
The resources in your API are not the same as
in your application models
Example: Archive a community
POST /communities/1/archive
Example: Unarchive a community
DELETE /communities/1/archive
Significance of pluralization: Many communities, but only one archive per
community
Format of resource is independent of representation
So the body of a request / response could be
html, json, xml, image, etc
Practically speaking, we use json
Example { “community”: { “name”: “Tokyo iOS Meetup”, “post_count”: 5,
“members”: [ { “name”: “Paul” }, { “name”: “Matt” } ], “public”: true, }
Use HTTP Status to Indicate Status of Request
Important status codes 200 OK 201 Created 401 Not Authorized
404 Not Found 406 Not Acceptable 422 Unprocessable Entity
Authentication: Use OAuth 2.0
http://openam.forgerock.org/openam-documentation/openam-doc-source/doc/admin-guide/index/chap-oauth2.html#openam-oauth2-authz-server
API Practicalities
Version your api: /api/v1/communities
Kill Switch: Force clients to upgrade
Control Endpoint Domain: i.e, don’t use community-board.herokuapp.com in production
Don’t handcraft your json
Return complete URLs
Build your API to minimize requests for mobile client
So, what about Rails?
Anatomy of an API Controller class Api::V1::CommunitiesController respond_to :json def
index @communities = Community.all respond_with @communities end end
Generating JSON user.as_json(include: { posts: { include: { comments: {
only: :body } }, only: :title } })
RABL # app/views/posts/index.rabl collection @posts attributes :id, :title, :subject child(:user)
{ attributes :full_name } node(:read) { |post| post.read_by?(@user) } [{ "post" : { "id" : 5, title: "...", subject: "...", "user" : { full_name : "..." }, "read" : true } }]
ActiveModel Serializers class PostSerializer < ActiveModel::Serializer attributes :id, :title, :body
has_many :comments end class CommentSerializer < ActiveModel::Serializer attributes :id, :text end # /posts/1 { “post” : { “id”: 1, “title”: “Sample”, “body”: “Sample Body”, “comments”: [ {“id”: 1, “text”: “comment 1”}}, {“id”: 2, “text”: “comment 2”}] }
OAuth2 with Doorkeeper class Api::V1::CommunitiesController respond_to :json doorkeeper_for :index def
index @communities = Community.all respond_with @communities end end