@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::UrlEncoded
co.use Her::Middleware::DefaultParseJSON
co.use Faraday::Adapter::NetHttp
end
@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 ...>, #<User ...>]
@happynoff
class User
include Her::Model
collection_path ‘/published-users/:id’
end
User.find(1)
# GET /published-users/1
# => #<User ...>
@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
# => [#<Comment ...>, #<Comment ...>]
@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 ...>]
@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::Caching,
Memcached::Rails.new(...)
# ...
end
@happynoff
customMiddlewares
@happynoff
✔awesomeness
@happynoff
http://her-rb.org
https://github.com/remiprev/her
Her
Faraday
https://github.com/lostisland/faraday
https://github.com/lostisland/faraday_middleware
@happynoff
Thankyou!

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