-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Roar decorator support #10
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e483401
Added decorator support
95648ff
Readd formatter
4381d32
Update readme for decorators
0abf3ba
Require roar decorator
70d0db5
Unused options param (rubocop)
728a7ee
Changelog entry (0.1.1)
0801c36
Remove note about block context issue
cf9b169
Refactor example for clarity
4724c97
"Next" instead of non-existent version
6532f98
Capital Roar
f3af2c6
Refactor request example
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require 'grape/roar/version' | ||
require 'grape/roar/formatter' | ||
require 'grape/roar/representer' | ||
require 'grape/roar/decorator' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'roar/decorator' | ||
|
||
module Grape | ||
module Roar | ||
class Decorator < ::Roar::Decorator | ||
def self.represent(object, _options = {}) | ||
new(object) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'spec_helper' | ||
|
||
describe Grape::Roar::Decorator do | ||
subject do | ||
Class.new(Grape::API) | ||
end | ||
|
||
before do | ||
subject.format :json | ||
subject.formatter :json, Grape::Formatter::Roar | ||
end | ||
|
||
def app | ||
subject | ||
end | ||
|
||
context 'decorator' do | ||
before do | ||
subject.get('/user/:id') do | ||
present User.new(name: 'Lonestar', id: params[:id]), with: UserRepresenter | ||
end | ||
end | ||
|
||
it 'returns a hypermedia representation' do | ||
get '/user/666' | ||
expect(last_response.body).to eq '{"name":"Lonestar","id":"666","links":[{"rel":"self","href":"/user/666"}]}' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class User | ||
attr_accessor :id, :name | ||
|
||
def initialize(attrs = {}) | ||
attrs.each_pair do |k, v| | ||
send("#{k}=", v) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class UserRepresenter < Grape::Roar::Decorator | ||
include Roar::Representer::JSON | ||
include Roar::Representer::Feature::Hypermedia | ||
|
||
property :name | ||
property :id | ||
|
||
link :self do | ||
"/user/#{represented.id}" | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I am nitpicky I would make this
request
and then dorequest.url
because URL is not the first or the last thing you might need inside the link, sorequest
should be similar torepresented
.