Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
@happynoff
AndletHertakecareoftheREST
RESTwithHer
bySimonCourtois
@happynoff
Rails+RESTAPI
@happynoff
ActiveResource?
@happynoff
RemovedinRails4
@happynoff
ActiveModel+Net::HTTP?
@happynoff
Faraday?
@happynoff
Her
@happynoff
gem‘her’
@happynoff
ontopofFaraday
Middlewaresforfree!w00t!
@happynoff
# config/initializers/her.rb
Her::API.setup url: 'http://api.expl.com' do |co|
co.use Faraday::Request::UrlEnco...
@happynoff
class User
include Her::Model
end
@happynoff
User.all
# GET https://api.expl.com/users
# => [#<User ...>, #<User ...>, ...]
@happynoff
User.find(1)
# GET https://api.expl.com/users/1
# => #<User ...>
@happynoff
User.create(login: “parisrb”)
# POST https://api.expl.com/users
# => #<User ...>
@happynoff
user = User.create(login: “parisrb”)
user.active = true
user.save
# POST https://api.expl.com/users
# => #<User...
@happynoff
user = User.find(1)
user.active = true
user.save
# PUT https://api.expl.com/users/1
# => #<User ...>
@happynoff
user = User.find(1)
user.destroy
# DELETE https://api.expl.com/users/1
# => #<User ...>
@happynoff
✔BasicCRUD
@happynoff
User.where(moderator: 1).all
# GET /users?moderator=1
# => [#<User ...>, ...]
@happynoff
class User
include Her::Model
custom_get :popular
end
User.popular
# GET /users/popular
# => [#<User ...>, #<Us...
@happynoff
class User
include Her::Model
collection_path ‘/published-users/:id’
end
User.find(1)
# GET /published-users/1
...
@happynoff
✔Customfinders
@happynoff
canIhazmanycomments?
@happynoff
class User
include Her::Model
has_many :comments
end
class Comment
include Her::Model
end
@happynoff
user = User.find(1)
# GET /users/1
# { "id": 1, "login": "parisrb" }
user.comments
# GET /users/1/comments
# =>...
@happynoff
user = User.find(1)
# GET /users/1
# {
# "id": 1,
# "login": "parisrb",
# “comments”: [
# { "id": 1, "text": "F...
@happynoff
✔associations
@happynoff
Middlewares!
@happynoff
gem‘faraday_middleware’
@happynoff
# config/initializers/her.rb
Her::API.setup url: 'http://api.expl.com' do |co|
co.use FaradayMiddleware::Cachin...
@happynoff
customMiddlewares
@happynoff
✔awesomeness
@happynoff
http://her-rb.org
https://github.com/remiprev/her
Her
Faraday
https://github.com/lostisland/faraday
https://git...
@happynoff
Thankyou!
Upcoming SlideShare
Loading in …5
×

REST with Her (and let Her take care of the REST)

2,950 views

Published on

My talk about the Her gem for Paris.rb on july 2, 2013.

Published in: Technology
  • Be the first to comment

REST with Her (and let Her take care of the REST)

  1. 1. @happynoff AndletHertakecareoftheREST RESTwithHer bySimonCourtois
  2. 2. @happynoff Rails+RESTAPI
  3. 3. @happynoff ActiveResource?
  4. 4. @happynoff RemovedinRails4
  5. 5. @happynoff ActiveModel+Net::HTTP?
  6. 6. @happynoff Faraday?
  7. 7. @happynoff Her
  8. 8. @happynoff gem‘her’
  9. 9. @happynoff ontopofFaraday Middlewaresforfree!w00t!
  10. 10. @happynoff # config/initializers/her.rb Her::API.setup url: 'http://api.expl.com' do |co| co.use Faraday::Request::UrlEncoded co.use Her::Middleware::DefaultParseJSON co.use Faraday::Adapter::NetHttp end
  11. 11. @happynoff class User include Her::Model end
  12. 12. @happynoff User.all # GET https://api.expl.com/users # => [#<User ...>, #<User ...>, ...]
  13. 13. @happynoff User.find(1) # GET https://api.expl.com/users/1 # => #<User ...>
  14. 14. @happynoff User.create(login: “parisrb”) # POST https://api.expl.com/users # => #<User ...>
  15. 15. @happynoff user = User.create(login: “parisrb”) user.active = true user.save # POST https://api.expl.com/users # => #<User ...>
  16. 16. @happynoff user = User.find(1) user.active = true user.save # PUT https://api.expl.com/users/1 # => #<User ...>
  17. 17. @happynoff user = User.find(1) user.destroy # DELETE https://api.expl.com/users/1 # => #<User ...>
  18. 18. @happynoff ✔BasicCRUD
  19. 19. @happynoff User.where(moderator: 1).all # GET /users?moderator=1 # => [#<User ...>, ...]
  20. 20. @happynoff class User include Her::Model custom_get :popular end User.popular # GET /users/popular # => [#<User ...>, #<User ...>]
  21. 21. @happynoff class User include Her::Model collection_path ‘/published-users/:id’ end User.find(1) # GET /published-users/1 # => #<User ...>
  22. 22. @happynoff ✔Customfinders
  23. 23. @happynoff canIhazmanycomments?
  24. 24. @happynoff class User include Her::Model has_many :comments end class Comment include Her::Model end
  25. 25. @happynoff user = User.find(1) # GET /users/1 # { "id": 1, "login": "parisrb" } user.comments # GET /users/1/comments # => [#<Comment ...>, #<Comment ...>]
  26. 26. @happynoff user = User.find(1) # GET /users/1 # { # "id": 1, # "login": "parisrb", # “comments”: [ # { "id": 1, "text": "Foo" }, # { "id": 2, "text": "Bar" } # ] # } user.comments # => [#<Comment ...>, #<Comment ...>]
  27. 27. @happynoff ✔associations
  28. 28. @happynoff Middlewares!
  29. 29. @happynoff gem‘faraday_middleware’
  30. 30. @happynoff # config/initializers/her.rb Her::API.setup url: 'http://api.expl.com' do |co| co.use FaradayMiddleware::Caching, Memcached::Rails.new(...) # ... end
  31. 31. @happynoff customMiddlewares
  32. 32. @happynoff ✔awesomeness
  33. 33. @happynoff http://her-rb.org https://github.com/remiprev/her Her Faraday https://github.com/lostisland/faraday https://github.com/lostisland/faraday_middleware
  34. 34. @happynoff Thankyou!

×