Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dblock/grape-with-roar-walkthrough
Live coding of a rudimentary Grape+Roar HAL API.
https://github.com/dblock/grape-with-roar-walkthrough
Last synced: 16 days ago
JSON representation
Live coding of a rudimentary Grape+Roar HAL API.
- Host: GitHub
- URL: https://github.com/dblock/grape-with-roar-walkthrough
- Owner: dblock
- License: mit
- Created: 2014-11-04T15:25:33.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2024-02-29T01:14:11.000Z (9 months ago)
- Last Synced: 2024-10-09T23:10:14.619Z (about 1 month ago)
- Language: Ruby
- Size: 6.84 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A live coding exercise for creating a Hypermedia API w/ Grape and Roar for [this talk](http://www.meetup.com/API-Craft-NYC/events/209294892).
#### Create Gemfile
```ruby
source 'http://rubygems.org'gem 'grape'
gem 'grape-roar'
```Run `bundle install`.
#### Create an API Root
Create `api.rb`.
```ruby
class Api < Grape::API
format :jsondesc 'Root of the Hypermedia API.'
get do
{ foo: 'bar' }
end
end
```Create `config.ru`.
```ruby
$LOAD_PATH.unshift(File.dirname(__FILE__))require 'rubygems'
require 'bundler/setup'Bundler.require :default
require 'api'
run Api
```Run `rackup`, browse to http://localhost:9292, see the API.
#### Create a Model
Add `activemodel` to Gemfile.
```ruby
gem 'activemodel', require: 'active_model'
```Create `models/spline.rb`.
```ruby
class Spline
include ActiveModel::Modelattr_accessor :uuid
attr_accessor :reticulateddef initialize(attrs = { reticulated: [true, false].sample })
super(attrs)
@uuid ||= SecureRandom.uuid
@reticulated = !!attrs[:reticulated]
end
end
```#### Return a Spline in the API
Add to `api.rb`.
```ruby
resource :splines do
desc 'Return a spline.'
get ':uuid' do
Spline.new(uuid: params[:uuid])
end
end
```You can navigate to http://localhost:9292/splines/1 and see this `spline.to_s`.
Make a presenter in `presenters/spline_presenter.rb`.
```ruby
module SplinePresenter
include Roar::JSON::HAL
include Roar::Hypermedia
include Grape::Roar::Representerproperty :uuid
property :reticulatedlink :self do
"http://localhost:9292/splines/#{uuid}"
end
end
```Require JSON+HAL in `config.ru`.
```ruby
require 'roar/representer'
require 'roar/json'
require 'roar/json/hal'require 'presenters/spline_presenter'
```Present the spline in `api.rb`.
```ruby
desc 'Return a spline.'
get ':uuid' do
present Spline.new(uuid: params[:uuid]), with: SplinePresenter
end
```See it at http://localhost:9292/splines/123.
#### Return a Collection of Splines
Create a `presenters/splines_presenter.rb`.
``` ruby
module SplinesPresenter
include Roar::JSON::HAL
include Roar::Hypermedia
include Grape::Roar::Representercollection :to_a, extend: SplinePresenter, as: :splines, embedded: true
end
```Require it.
```ruby
require 'presenters/splines_presenter'
```Present splines.
```ruby
desc 'Return a few splines.'
get do
present 5.times.map { Spline.new }, with: SplinesPresenter
end
```#### Add a Root Presenter
Create `presenters/root_presenter.rb`.
```ruby
module RootPresenter
include Roar::JSON::HAL
include Roar::Hypermedia
include Grape::Roar::Representerlink :self do
"http://localhost:9292"
endlink :splines do
"http://localhost:9292/splines"
endlink :spline do |opts|
{
href: "http://localhost:9292/splines/{uuid}",
templated: true
}
end
end
```Require it.
```ruby
require 'presenters/root_presenter'
```Present it.
```ruby
desc 'Root of the Hypermedia API.'
get do
present self, with: RootPresenter
end
```#### Try It
``` ruby
require 'hyperclient'client = Hyperclient.new('http://localhost:9292')
client.splines.count
client.splines.to_a.each do |spline|
puts "Spline #{spline.uuid} is #{spline.reticulated ? 'reticulated' : 'not reticulated'}."
end
```### LICENSE
[MIT License](LICENSE)