14
créer son API Raphaël Audet Pierre Renaudin Apéro Ruby Bordeaux, 10 Janvier 2011 mardi 10 janvier 12

Apéro ruby bordeaux créer son api avec ruby - 9 01 2012

Embed Size (px)

Citation preview

Page 1: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

créer son APIRaphaël AudetPierre RenaudinApéro Ruby Bordeaux, 10 Janvier 2011

mardi 10 janvier 12

Page 2: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

créer son API

• Pourquoi créer son API ?

• Quelles sont les solutions qui existent ?

• Démo d’une API réalisée avec RABL

mardi 10 janvier 12

Page 3: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

Pourquoi une API ? 1/2

• Pour ouvrir son application à des acteurs externes

api.twitter.com

site de recherche sur twitter blog app facebook

mardi 10 janvier 12

Page 4: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

Pourquoi une API ? 2/2

• Pour séparer les clients ( web, mobile ...) du serveur et améliorer les performances

api.twitter.com

client web client mobile client iphone

mardi 10 janvier 12

Page 5: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

Solution 1 : utiliser rails

Texteclass User < ActiveRecord::Base field :first_name field :last_name field :address

def name self.first_name + ' ' + self.last_name endend

class UsersController < ApplicationController def show render :json => @user end

end

{ id: ‘28ds84dsd’,first_name: ‘Ted’,last_name: ‘Penn’,address: ‘3 rue Anchor...’}

models/users.rb

controllers/users_controller.rbmardi 10 janvier 12

Page 6: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

Solution 2 : utiliser acts_as_apiclass User < ActiveRecord::Base acts_as_api

api_accessible :public do |t| t.add :id t.add :name end

def name self.first_name + ' ' + self.last_name endend

class UsersController < ApplicationController def show render_for_api :public, :json => @user end

end

{ id: ‘28ds84dsd’,name: ‘Ted Penn’}

controllers/users_controller.rb

models/users.rb

mardi 10 janvier 12

Page 7: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

Solution 3 : utiliser RABL

class UsersController < ApplicationController def show render @user end

end

attributes :id, :name

child :pets do extends ‘pets/base’end

controllers/users_controller.rb

views/users/show.json.rabl

{ id: ‘28ds84dsd’,name: ‘Ted Penn’,pets: [ {name: ‘choupette’}, {name: ‘choupy’}]}

mardi 10 janvier 12

Page 8: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

Démo - 1

mardi 10 janvier 12

Page 9: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

Démo - 2

mardi 10 janvier 12

Page 10: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

Démo - 3

controllers/messages_controller.rb

mardi 10 janvier 12

Page 11: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

Démo : RABL WAY ! 4

Gemfile

views/messages/index.rabl messages_controller.rb

mardi 10 janvier 12

Page 12: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

Démo - 5

mardi 10 janvier 12

Page 13: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

Démo : autre exemple 6

views/users/base.json.rabl

mardi 10 janvier 12

Page 14: Apéro ruby bordeaux   créer son api avec ruby - 9 01 2012

Références

•Rabl : https://github.com/nesquena/rabl

•acts_as_api : https://github.com/fabrik42/acts_as_api

mardi 10 janvier 12