grape-roar 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5827a1b2d9d4d8b3add2a301aa5fae68074c5d22
4
- data.tar.gz: a60fedc73b8d76bdbb608e5331448a910476a7b2
3
+ metadata.gz: 0d20b4c91d2e6c8ddf666534169e08e979d1f0b4
4
+ data.tar.gz: 80f25794c1901dc9dcbeb5d137dfef37ab634121
5
5
  SHA512:
6
- metadata.gz: 343baa16ae114e19e69f365f4109a4e1f8a269a03e3cb1623302377d567cea1e020f24cd0d9726c8ee951d535f0872012378c84e37cd9f6e505bd094b081144f
7
- data.tar.gz: aa5f1ed03620b635defe792007768d8f7afd8b5b11ff2969b99306db70767091937ffc943b73aa03a8105d96f85c55847e4251cda658a10c79bcaaab1c40cf5a
6
+ metadata.gz: 268ee883eafd778ae6fdaf2a68392426f152532d8354e75e5620c33b869f115a4a4fe5382ad222b70720e95eedf05e612fdebdb12d23542af8942685a393acbf
7
+ data.tar.gz: f9c92f7d2a629d6d23937ac1b7017530d467e74bf53be5ab2651b6d3c7991d59fb5c31be6204f95d04b3df4057a65e4f7cd8f3b54d427cb90c9b0375c3af6c88
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  Gemfile.lock
2
2
  doc
3
3
  pkg
4
+ .bundle
@@ -3,27 +3,4 @@ AllCops:
3
3
  - vendor/**/*
4
4
  - bin/**/*
5
5
 
6
- LineLength:
7
- Enabled: false
8
-
9
- MethodLength:
10
- Enabled: false
11
-
12
- ClassLength:
13
- Enabled: false
14
-
15
- Documentation:
16
- # don't require classes to be documented
17
- Enabled: false
18
-
19
- CollectionMethods:
20
- # don't prefer map to collect, recuce to inject
21
- Enabled: false
22
-
23
- Encoding:
24
- # no need to always specify encoding
25
- Enabled: false
26
-
27
- FileName:
28
- # allow grape-roar.rb
29
- Enabled: false
6
+ inherit_from: .rubocop_todo.yml
@@ -0,0 +1,20 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2014-12-18 10:52:56 -0500 using RuboCop version 0.28.0.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 8
9
+ # Configuration parameters: AllowURI, URISchemes.
10
+ Metrics/LineLength:
11
+ Max: 304
12
+
13
+ # Offense count: 13
14
+ Style/Documentation:
15
+ Enabled: false
16
+
17
+ # Offense count: 1
18
+ # Configuration parameters: Exclude.
19
+ Style/FileName:
20
+ Enabled: false
@@ -1,3 +1,5 @@
1
+ sudo: false
2
+
1
3
  language: ruby
2
4
 
3
5
  cache: bundler
@@ -8,4 +10,4 @@ rvm:
8
10
  - 2.0.0
9
11
  - 1.9.3
10
12
  - jruby-19mode
11
- - rbx-2
13
+ - rbx-2.2.10
@@ -1,4 +1,9 @@
1
- 0.1.0 (18/07/2014)
1
+ 0.2.0 (12/18/2014)
2
2
  ------------------
3
3
 
4
+ * [#10](https://github.com/dblock/grape-roar/pull/10): Support for Roar decorator - [@sdbondi](https://github.com/sdbondi).
5
+
6
+ 0.1.0 (7/17/2014)
7
+ -----------------
8
+
4
9
  * Initial public release, with support for Grape `present` - [@dblock](https://github.com/dblock).
data/Gemfile CHANGED
@@ -3,14 +3,14 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  group :development do
6
- gem "rake"
6
+ gem 'rake'
7
7
  end
8
8
 
9
9
  group :test do
10
- gem "rspec", "~> 3.0"
11
- gem "rack-test"
10
+ gem 'rspec', '~> 3.0'
11
+ gem 'rack-test'
12
12
  end
13
13
 
14
14
  group :development, :test do
15
- gem 'rubocop', '0.24.1'
15
+ gem 'rubocop', '0.28.0'
16
16
  end
data/README.md CHANGED
@@ -5,6 +5,11 @@ Use [Roar](https://github.com/apotonick/roar) with [Grape](https://github.com/in
5
5
 
6
6
  [![Build Status](https://secure.travis-ci.org/dblock/grape-roar.png)](http://travis-ci.org/dblock/grape-roar)
7
7
 
8
+ Demo
9
+ ----
10
+
11
+ The [grape-with-roar](https://github.com/dblock/grape-with-roar) project deployed [here on heroku](http://grape-with-roar.herokuapp.com).
12
+
8
13
  Installation
9
14
  ------------
10
15
 
@@ -30,7 +35,7 @@ end
30
35
 
31
36
  ### Use Grape's Present
32
37
 
33
- You can use Grape's `present` keyword after including Grape::Roar::Presenter into a representer module.
38
+ You can use Grape's `present` keyword after including Grape::Roar::Representer into a representer module.
34
39
 
35
40
  ```ruby
36
41
  module ProductRepresenter
@@ -49,9 +54,27 @@ get 'product/:id' do
49
54
  end
50
55
  ```
51
56
 
52
- ### Accessing the Request Inside a Presenter
57
+ Presenting collections works the same way. The following example returns an embedded set of products in the HAL Hypermedia format.
58
+
59
+ ```ruby
60
+ module ProductsRepresenter
61
+ include Roar::Representer::JSON::HAL
62
+ include Roar::Representer::Feature::Hypermedia
63
+ include Grape::Roar::Representer
64
+
65
+ collection :entries, extend: ProductPresenter, as: :products, embedded: true
66
+ end
67
+ ```
68
+
69
+ ```ruby
70
+ get 'products' do
71
+ present Product.all, with: ProductsRepresenter
72
+ end
73
+ ```
74
+
75
+ ### Accessing the Request Inside a Representer
53
76
 
54
- The formatter invokes `to_json` on presented objects and provides access to the requesting environment via the `env` option. The following example renders a full request URL in a presenter.
77
+ The formatter invokes `to_json` on presented objects and provides access to the requesting environment via the `env` option. The following example renders a full request URL in a representer.
55
78
 
56
79
  ```ruby
57
80
  module ProductRepresenter
@@ -66,6 +89,33 @@ module ProductRepresenter
66
89
  end
67
90
  ```
68
91
 
92
+ ### Decorators
93
+
94
+ If you prefer to use a decorator class instead of modules.
95
+
96
+ ```ruby
97
+ class ProductRepresenter < Grape::Roar::Decorator
98
+ include Roar::Representer::JSON
99
+ include Roar::Representer::Feature::Hypermedia
100
+
101
+ link :self do |opts|
102
+ "#{request(opts).url}/#{represented.id}"
103
+ end
104
+
105
+ private
106
+
107
+ def request(opts)
108
+ Grape::Request.new(opts[:env])
109
+ end
110
+ end
111
+ ```
112
+
113
+ ```ruby
114
+ get 'products' do
115
+ present Product.all, with: ProductsRepresenter
116
+ end
117
+ ```
118
+
69
119
  Contributing
70
120
  ------------
71
121
 
@@ -74,6 +124,6 @@ See [CONTRIBUTING](CONTRIBUTING.md).
74
124
  Copyright and License
75
125
  ---------------------
76
126
 
77
- MIT License, see [LICENSE](http://github.com/dblock/grape-roar/raw/master/LICENSE) for details.
127
+ MIT License, see [LICENSE](LICENSE) for details.
78
128
 
79
- (c) 2012-2014 [Daniel Doubrovkine](http://github.com/dblock), [Art.sy](http://artsy.github.com)
129
+ (c) 2012-2014 [Daniel Doubrovkine](https://github.com/dblock) & Contributors, [Artsy](https://artsy.net)
@@ -1,3 +1,4 @@
1
1
  require 'grape/roar/version'
2
2
  require 'grape/roar/formatter'
3
3
  require 'grape/roar/representer'
4
+ require 'grape/roar/decorator'
@@ -0,0 +1,11 @@
1
+ require 'roar/decorator'
2
+
3
+ module Grape
4
+ module Roar
5
+ class Decorator < ::Roar::Decorator
6
+ def self.represent(object, _options = {})
7
+ new(object)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -3,8 +3,7 @@ module Grape
3
3
  module Roar
4
4
  class << self
5
5
  def call(object, env)
6
- return object.to_json(env: env) if object.respond_to?(:to_json)
7
- MultiJson.dump(object)
6
+ object.respond_to?(:to_json) ? object.to_json(env: env) : MultiJson.dump(object)
8
7
  end
9
8
  end
10
9
  end
@@ -1,5 +1,5 @@
1
1
  module Grape
2
2
  module Roar
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Roar::Decorator do
4
+ subject do
5
+ Class.new(Grape::API)
6
+ end
7
+
8
+ before do
9
+ subject.format :json
10
+ subject.formatter :json, Grape::Formatter::Roar
11
+ end
12
+
13
+ def app
14
+ subject
15
+ end
16
+
17
+ context 'decorator' do
18
+ before do
19
+ subject.get('/user/:id') do
20
+ present User.new(name: 'Lonestar', id: params[:id]), with: UserRepresenter
21
+ end
22
+ end
23
+
24
+ it 'returns a hypermedia representation' do
25
+ get '/user/666'
26
+ expect(last_response.body).to eq '{"name":"Lonestar","id":"666","links":[{"rel":"self","href":"/user/666"}]}'
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ class User
2
+ attr_accessor :id, :name
3
+
4
+ def initialize(attrs = {})
5
+ attrs.each_pair do |k, v|
6
+ send("#{k}=", v)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ class UserRepresenter < Grape::Roar::Decorator
2
+ include Roar::Representer::JSON
3
+ include Roar::Representer::Feature::Hypermedia
4
+
5
+ property :name
6
+ property :id
7
+
8
+ link :self do
9
+ "/user/#{represented.id}"
10
+ end
11
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-roar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Doubrovkine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-18 00:00:00.000000000 Z
11
+ date: 2014-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
@@ -48,6 +48,7 @@ files:
48
48
  - .gitignore
49
49
  - .rspec
50
50
  - .rubocop.yml
51
+ - .rubocop_todo.yml
51
52
  - .travis.yml
52
53
  - CHANGELOG.md
53
54
  - CONTRIBUTING.md
@@ -58,9 +59,11 @@ files:
58
59
  - grape-roar.gemspec
59
60
  - lib/grape-roar.rb
60
61
  - lib/grape/roar.rb
62
+ - lib/grape/roar/decorator.rb
61
63
  - lib/grape/roar/formatter.rb
62
64
  - lib/grape/roar/representer.rb
63
65
  - lib/grape/roar/version.rb
66
+ - spec/decorator_spec.rb
64
67
  - spec/nested_representer_spec.rb
65
68
  - spec/present_with_spec.rb
66
69
  - spec/representer_spec.rb
@@ -71,6 +74,8 @@ files:
71
74
  - spec/support/order_representer.rb
72
75
  - spec/support/product.rb
73
76
  - spec/support/product_representer.rb
77
+ - spec/support/user.rb
78
+ - spec/support/user_representer.rb
74
79
  homepage: http://github.com/dblock/grape-roar
75
80
  licenses: []
76
81
  metadata: {}
@@ -90,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
95
  version: '0'
91
96
  requirements: []
92
97
  rubyforge_project:
93
- rubygems_version: 2.0.14
98
+ rubygems_version: 2.1.11
94
99
  signing_key:
95
100
  specification_version: 4
96
101
  summary: Enable Resource-Oriented Architectures in Grape API DSL