grape-active_model_serializers-revibe 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 16b7e78d2c0e96bc530e07fbc785c6b70b23ab67
4
+ data.tar.gz: cee9baec078b19f2c32ddac3cd89dd172e2d1978
5
+ SHA512:
6
+ metadata.gz: 1015db0c24722dedb393adf0c8216eb14de6bb2c7ceadf298777e4142390690185c9d9e89058e8f331174d8fbd60a159e85d0692661bb4e512d5ef9740772ef9
7
+ data.tar.gz: 4df7dc96104e0478328757f2580e9455730073f5b542d4adeabaeee2ff3653b231f36bb61a52d32098003cb638a52392c2ab925fd0b392dec2d1ec2c9c9d1d22
@@ -0,0 +1,18 @@
1
+ *.rbc
2
+ *.sassc
3
+ .sass-cache
4
+ capybara-*.html
5
+ .rspec
6
+ /.bundle
7
+ /vendor/bundle
8
+ /log/*
9
+ /tmp/*
10
+ /db/*.sqlite3
11
+ /public/system/*
12
+ /coverage/
13
+ /spec/tmp/*
14
+ **.orig
15
+ rerun.txt
16
+ pickle-email-*.html
17
+ Gemfile.lock
18
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'jazz_hands', git: 'https://github.com/jrhe/jazz_hands.git', branch: 'without_rails'
7
+ end
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jonathan Evans
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,192 @@
1
+ # Grape::ActiveModelSerializers
2
+
3
+ Use [active_model_serializers](https://github.com/rails-api/active_model_serializers) with [Grape](https://github.com/intridea/grape)!
4
+
5
+ [![Build Status](https://api.travis-ci.org/jrhe/grape-active_model_serializers.png)](http://travis-ci.org/jrhe/grape-active_model_serializers) [![Dependency Status](https://gemnasium.com/jrhe/grape-active_model_serializers.png)](https://gemnasium.com/jrhe/grape-active_model_serializers) [![Code Climate](https://codeclimate.com/github/jrhe/grape-active_model_serializers.png)](https://codeclimate.com/github/jrhe/grape-active_model_serializers)
6
+
7
+ ## Breaking Changes
8
+ #### v1.0.0
9
+ * *BREAKING* Changes behaviour of root keys when serialising arrays. See [Array roots](https://github.com/jrhe/grape-active_model_serializers#array-roots)
10
+
11
+ ## Installation
12
+
13
+ Add the `grape` and `grape-active_model_serializers` gems to Gemfile.
14
+
15
+ ```ruby
16
+ gem 'grape'
17
+ gem 'grape-active_model_serializers'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ bundle
23
+
24
+ ## Usage
25
+
26
+ ### Require grape-active_model_serializers
27
+
28
+ ```ruby
29
+ # config.ru
30
+ require 'grape-active_model_serializers'
31
+ ```
32
+
33
+
34
+ ### Tell your API to use Grape::Formatter::ActiveModelSerializers
35
+
36
+ ```ruby
37
+ class API < Grape::API
38
+ format :json
39
+ formatter :json, Grape::Formatter::ActiveModelSerializers
40
+ end
41
+ ```
42
+
43
+
44
+ ### Writing serializers
45
+
46
+ See [active_model_serializers](https://github.com/rails-api/active_model_serializers)
47
+
48
+
49
+ ### Serializers are inferred by active_record model names
50
+
51
+ `grape-active_model_serializers` will search for serializers for the objects returned by your grape API.
52
+
53
+ ```ruby
54
+ namespace :users do
55
+ get ":id" do
56
+ @user = User.find(params[:id])
57
+ end
58
+ end
59
+ ```
60
+ In this case, as User objects are being returned, grape-active_model_serializers will look for a serializer named UserSerializer.
61
+
62
+ ### Array roots
63
+ When serializing an array, the array root is set to the innermost namespace name if there is one, otherwise it is set to the route name (e.g. get 'name').
64
+
65
+ ```ruby
66
+ namespace :users do
67
+ get ":id" do
68
+ @user = User.find(params[:id])
69
+ end
70
+ end
71
+ # root = users
72
+ ```
73
+
74
+ ```ruby
75
+ get "people" do
76
+ @user = User.all
77
+ end
78
+ # root = people
79
+ ```
80
+
81
+ ### Manually specifying serializer options
82
+
83
+ ```ruby
84
+ # Serializer options can be specified on routes or namespaces.
85
+ namespace 'foo', :serializer => :bar do
86
+ get "/" do
87
+ # will use "bar" serializer
88
+ end
89
+
90
+ # Options specified on a route or namespace override those of the containing namespace.
91
+ get "/home", :serializer => :home do
92
+ # will use "home" serializer
93
+ end
94
+
95
+ # All standard options for `ActiveModel::Serializers` are supported.
96
+ get "/fancy_homes", :root => 'world', :each_serializer => :fancy_homes
97
+ ...
98
+ end
99
+ end
100
+ ```
101
+
102
+ ### Custom metadata along with the resources
103
+
104
+ ```ruby
105
+ # Control any additional metadata using meta and meta_key
106
+ get "/homes"
107
+ collection = Home.all
108
+ render collection, { meta: { page: 5, current_page: 3 }, meta_key: :pagination_info }
109
+ end
110
+ ```
111
+
112
+ ### current_user
113
+
114
+ One of the nice features of ActiveModel::Serializers is that it
115
+ provides access to the authorization context via the `current_user`.
116
+
117
+ In Grape, you can get the same behavior by defining a `current_user`
118
+ helper method:
119
+
120
+ ```ruby
121
+ helpers do
122
+ def current_user
123
+ @current_user ||= User.where( :access_token => params[:token]).first
124
+ end
125
+
126
+ def authenticate!
127
+ error!('401 Unauthenticated', 401) unless current_user
128
+ end
129
+ end
130
+ ```
131
+
132
+ Then, in your serializer, you could show or hide some elements
133
+ based on the current user's permissions:
134
+
135
+ ```ruby
136
+ class PostSerializer < ActiveModel::Serializer
137
+ ...
138
+ def include_admin_comments?
139
+ current_user.roles.member? :admin
140
+ end
141
+ end
142
+ ```
143
+
144
+ ### Full Example
145
+
146
+ ```ruby
147
+ class User < ActiveRecord::Base
148
+ attr_accessor :first_name, :last_name, :password, :email
149
+ end
150
+
151
+ class UserSerializer < ActiveModel::Serializer
152
+ attributes :first_name, :last_name
153
+ end
154
+
155
+ class API < Grape::API
156
+ get("/home") do
157
+ User.new({first_name: 'JR', last_name: 'HE', email: 'contact@jrhe.co.uk'})
158
+ end
159
+ end
160
+
161
+ API.new.get "/home" # => '{:user=>{:first_name=>"JR", :last_name=>"HE"}}'
162
+ ```
163
+
164
+
165
+ ## RSpec
166
+
167
+ See "Writing Tests" in https://github.com/intridea/grape.
168
+
169
+ Enjoy :)
170
+
171
+ ## Changelog
172
+
173
+ #### v1.0.0
174
+ * Released on rubygems.org
175
+ * *BREAKING* Changes behaviour of root keys when serialising arrays. See [Array roots](https://github.com/jrhe/grape-active_model_serializers#array-roots)
176
+
177
+
178
+ ## Contributing
179
+
180
+ 1. Fork it
181
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
182
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
183
+ 4. Push to the branch (`git push origin my-new-feature`)
184
+ 5. Create new Pull Request
185
+
186
+
187
+ ## Thanks to
188
+ The developers and maintainers of:
189
+ [active_model_serializers](https://github.com/rails-api/active_model_serializers)
190
+ [Grape](https://github.com/intridea/grape)!
191
+
192
+ Structured and based upon [grape-rabl](https://github.com/LTe/grape-rabl).
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.rspec_opts = ['-fd -c']
8
+ spec.pattern = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/grape-active_model_serializers/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jonathan Richard Henry Evans"]
6
+ gem.email = ["contact@jrhe.co.uk"]
7
+ gem.summary = %q{Use active_model_serializer in grape}
8
+ gem.description = %q{Provides a Formatter for the Grape API DSL to emit objects serialized with active_model_serializers.}
9
+ gem.homepage = "https://github.com/jrhe/grape-active_model_serializers"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "grape-active_model_serializers-revibe"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Grape::ActiveModelSerializers::VERSION
17
+ gem.licenses = ['MIT']
18
+
19
+ gem.add_dependency "grape", "~> 0.3"
20
+ gem.add_dependency "active_model_serializers", ">= 0.8.1"
21
+
22
+ gem.add_development_dependency "rspec"
23
+ gem.add_development_dependency "rack-test"
24
+ gem.add_development_dependency "rake"
25
+ gem.add_development_dependency "jazz_hands"
26
+ gem.add_development_dependency 'guard-rspec'
27
+ end
@@ -0,0 +1,8 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - ruby-head
5
+ - rbx-19mode
6
+
7
+ env:
8
+ - JRUBY_OPTS="--1.9"
@@ -0,0 +1,5 @@
1
+ require 'active_model_serializers'
2
+ require 'grape'
3
+ require 'grape-active_model_serializers/endpoint_extension'
4
+ require 'grape-active_model_serializers/formatter'
5
+ require 'grape-active_model_serializers/version'
@@ -0,0 +1,52 @@
1
+ #
2
+ # Make the Grape::Endpoint quack like a ActionController
3
+ #
4
+ # This allows us to rely on the ActiveModel::Serializer#build_json method
5
+ # to lookup the approriate serializer.
6
+ #
7
+ module Grape
8
+ module EndpointExtension
9
+ attr_accessor :controller_name
10
+
11
+ def namespace_options
12
+ settings[:namespace] ? settings[:namespace].options : {}
13
+ end
14
+
15
+ def route_options
16
+ options[:route_options]
17
+ end
18
+
19
+ def self.included(base)
20
+ mattr_accessor :_serialization_scope
21
+ self._serialization_scope = :current_user
22
+
23
+ base.class_eval do
24
+ def serialization_scope
25
+ send(_serialization_scope) if _serialization_scope && respond_to?(_serialization_scope, true)
26
+ end
27
+ end
28
+ end
29
+
30
+ def render(resources, meta={})
31
+ set_meta_and_meta_key(meta)
32
+ resources
33
+ end
34
+
35
+ def default_serializer_options; end
36
+
37
+ def url_options; end
38
+
39
+ private
40
+
41
+ def set_meta_and_meta_key(meta)
42
+ if meta.has_key?(:meta)
43
+ Formatter::ActiveModelSerializers.meta = meta[:meta]
44
+ if meta.has_key?(:meta_key)
45
+ Formatter::ActiveModelSerializers.meta_key = meta[:meta_key]
46
+ end
47
+ end
48
+ end
49
+
50
+ end
51
+ Endpoint.send(:include, EndpointExtension)
52
+ end
@@ -0,0 +1,79 @@
1
+ module Grape
2
+ module Formatter
3
+ module ActiveModelSerializers
4
+ class << self
5
+ def call(resource, env)
6
+ serializer = fetch_serializer(resource, env)
7
+
8
+ if serializer
9
+ serializer.to_json
10
+ else
11
+ Grape::Formatter::Json.call resource, env
12
+ end
13
+ end
14
+
15
+ def fetch_serializer(resource, env)
16
+ endpoint = env['api.endpoint']
17
+ options = build_options_from_endpoint(endpoint)
18
+
19
+ if resource.respond_to?(:to_ary) && !resource.empty?
20
+ # ensure we have an root to fallback on
21
+ endpoint.controller_name = default_root(endpoint)
22
+ end
23
+
24
+ ::ActiveModel::Serializer.build_json(endpoint,
25
+ resource,
26
+ options.merge(other_options)
27
+ )
28
+ end
29
+
30
+ def other_options
31
+ options = {}
32
+ if @meta_content_items
33
+ meta_option = @meta_content_items[:meta]
34
+ @meta_content_items.delete(:meta)
35
+ options[:meta] = meta_option if meta_option
36
+ if @meta_key
37
+ key_option = @meta_key[:meta_key]
38
+ @meta_key.delete(:meta_key)
39
+ options[:meta_key] = key_option if key_option
40
+ end
41
+ end
42
+ options
43
+ end
44
+
45
+ def meta
46
+ @meta_content_items || {}
47
+ end
48
+
49
+ def meta=(meta_content)
50
+ @meta_content_items = { meta: meta_content } if meta_content
51
+ end
52
+
53
+ def meta_key
54
+ @meta_key || {}
55
+ end
56
+
57
+ def meta_key=(key)
58
+ @meta_key = { meta_key: key } if key
59
+ end
60
+
61
+ def build_options_from_endpoint(endpoint)
62
+ endpoint.namespace_options.merge(endpoint.route_options)
63
+ end
64
+
65
+ # array root is the innermost namespace name ('space') if there is one,
66
+ # otherwise the route name (e.g. get 'name')
67
+ def default_root(endpoint)
68
+ innermost_scope = endpoint.settings.peek
69
+
70
+ if innermost_scope[:namespace]
71
+ innermost_scope[:namespace].space
72
+ else
73
+ endpoint.options[:path][0].to_s.split('/')[-1]
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,5 @@
1
+ module Grape
2
+ module ActiveModelSerializers
3
+ VERSION = '1.0.1'
4
+ end
5
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+ require 'support/models/user'
3
+ require 'support/serializers/user_serializer'
4
+ require 'grape-active_model_serializers'
5
+ require 'securerandom'
6
+
7
+ describe '#render' do
8
+ let(:app) { Class.new(Grape::API) }
9
+
10
+ before do
11
+ app.format :json
12
+ app.formatter :json, Grape::Formatter::ActiveModelSerializers
13
+ end
14
+
15
+ def get_resource_with(meta)
16
+ url = "/#{SecureRandom.hex}"
17
+ app.get(url) do
18
+ render User.new(first_name: 'Jeff'), meta
19
+ end
20
+ get url
21
+ JSON.parse last_response.body
22
+ end
23
+
24
+ context 'with meta key' do
25
+ it 'includes meta key and content' do
26
+ result = get_resource_with({ meta: { total: 2 }})
27
+ expect(result).to have_key('meta')
28
+ expect(result.fetch('meta')).to eq({ 'total' => 2 })
29
+ end
30
+ end
31
+
32
+ context 'with a custom meta_key' do
33
+
34
+ it 'includes the custom meta key name' do
35
+ result = get_resource_with({ meta: { total: 2 }, meta_key: :custom_key_name })
36
+ expect(result).to have_key('custom_key_name')
37
+ expect(result.fetch('custom_key_name')).to eq({ 'total' => 2 })
38
+ end
39
+
40
+ it 'ignores a lonely meta_key' do
41
+ result = get_resource_with({ meta_key: :custom_key_name })
42
+ expect(result).not_to have_key('meta')
43
+ expect(result).not_to have_key('custom_key_name')
44
+ end
45
+ end
46
+
47
+ context 'junk keys' do
48
+
49
+ it 'ignores junk keys' do
50
+ result = get_resource_with({ junk_key: { total: 2 } })
51
+ expect(result).not_to have_key('junk_key')
52
+ end
53
+
54
+ it 'ignores empty meta_key' do
55
+ result = get_resource_with({ meta: { total: 2 }, meta_key: nil })
56
+ expect(result).to have_key('meta')
57
+ end
58
+
59
+ it 'ignores empty meta' do
60
+ result = get_resource_with({ meta: nil })
61
+ expect(result).not_to have_key('meta')
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Grape::EndpointExtension' do
4
+
5
+ subject { Grape::Endpoint.new(nil, {path: '/', method: 'foo'}) }
6
+
7
+ let(:serializer) { Grape::Formatter::ActiveModelSerializers }
8
+
9
+ let(:user) do
10
+ Object.new do
11
+ def name
12
+ 'sven'
13
+ end
14
+ end
15
+ end
16
+
17
+ let(:users) { [user, user] }
18
+
19
+ describe "#render" do
20
+ it { should respond_to(:render) }
21
+ let (:meta_content) { { total: 2 } }
22
+ let (:meta_full) { { meta: meta_content } }
23
+ context 'supplying meta' do
24
+ it 'passes through the Resource and uses given meta settings' do
25
+ expect(serializer).to receive(:meta=).with(meta_content)
26
+ expect(subject.render(users, meta_full)).to eq(users)
27
+ end
28
+ end
29
+ context 'supplying meta and key' do
30
+ let (:meta_key) { { meta_key: :custom_key_name } }
31
+ it 'passes through the Resource and uses given meta settings' do
32
+ expect(serializer).to receive(:meta=).with(meta_content)
33
+ expect(serializer).to receive(:meta_key=).with(meta_key[:meta_key])
34
+ expect(subject.render(users, meta_full.merge(meta_key))).to eq(users)
35
+ end
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require 'grape-active_model_serializers/formatter'
3
+
4
+ describe Grape::Formatter::ActiveModelSerializers do
5
+ subject { Grape::Formatter::ActiveModelSerializers }
6
+ it { should respond_to(:meta) }
7
+ it { should respond_to(:meta=) }
8
+ it { should respond_to(:meta_key) }
9
+ it { should respond_to(:meta_key=) }
10
+
11
+ context '#meta' do
12
+ it 'will silently accept falsy input but return empty Hash' do
13
+ subject.meta = nil
14
+ expect(subject.meta).to eq({})
15
+ end
16
+
17
+ it 'will wrap valid input in the meta: {} wrapper' do
18
+ subject.meta = { total: 2 }
19
+ expect(subject.meta).to eq({ meta: { total: 2 } })
20
+ end
21
+ end
22
+
23
+ context '#meta_key' do
24
+ it 'will silently accept falsy input but return empty Hash' do
25
+ subject.meta_key = nil
26
+ expect(subject.meta_key).to eq({})
27
+ end
28
+
29
+ it 'will wrap valid input in the meta_key: {} wrapper' do
30
+ subject.meta_key = :custom_key_name
31
+ expect(subject.meta_key).to eq({ meta_key: :custom_key_name })
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'grape-active_model_serializers' do
4
+
5
+ end
@@ -0,0 +1,118 @@
1
+ require 'spec_helper'
2
+ require 'support/models/user'
3
+ require 'support/models/blog_post'
4
+ require 'support/serializers/user_serializer'
5
+ require 'support/serializers/blog_post_serializer'
6
+ require "grape-active_model_serializers"
7
+
8
+ describe Grape::ActiveModelSerializers do
9
+ let(:app) { Class.new(Grape::API) }
10
+
11
+ before do
12
+ app.format :json
13
+ app.formatter :json, Grape::Formatter::ActiveModelSerializers
14
+ end
15
+
16
+
17
+ it "should respond with proper content-type" do
18
+ app.get("/home/users", :serializer => UserSerializer) do
19
+ User.new
20
+ end
21
+ get("/home/users")
22
+ last_response.headers["Content-Type"].should == "application/json"
23
+ end
24
+
25
+ context 'serializer is set to nil' do
26
+ before do
27
+ app.get("/home", serializer: nil) do
28
+ {user: {first_name: "JR", last_name: "HE"}}
29
+ end
30
+ end
31
+ it 'uses the built in grape serializer' do
32
+ get("/home")
33
+ last_response.body.should == "{\"user\":{\"first_name\":\"JR\",\"last_name\":\"HE\"}}"
34
+ end
35
+ end
36
+
37
+ context "serializer isn't set" do
38
+ before do
39
+ app.get("/home") do
40
+ User.new({first_name: 'JR', last_name: 'HE', email: 'contact@jrhe.co.uk'})
41
+ end
42
+ end
43
+
44
+ it 'infers the serializer' do
45
+ get "/home"
46
+ last_response.body.should == "{\"user\":{\"first_name\":\"JR\",\"last_name\":\"HE\"}}"
47
+ end
48
+ end
49
+
50
+ it "serializes arrays of objects" do
51
+ app.get("/users") do
52
+ user = User.new({first_name: 'JR', last_name: 'HE', email: 'contact@jrhe.co.uk'})
53
+ [user, user]
54
+ end
55
+
56
+ get "/users"
57
+ last_response.body.should == "{\"users\":[{\"first_name\":\"JR\",\"last_name\":\"HE\"},{\"first_name\":\"JR\",\"last_name\":\"HE\"}]}"
58
+ end
59
+
60
+ context "models with compound names" do
61
+ it "generates the proper 'root' node for individual objects" do
62
+ app.get("/home") do
63
+ BlogPost.new({title: 'Grape AM::S Rocks!', body: 'Really, it does.'})
64
+ end
65
+
66
+ get "/home"
67
+ last_response.body.should == "{\"blog_post\":{\"title\":\"Grape AM::S Rocks!\",\"body\":\"Really, it does.\"}}"
68
+ end
69
+
70
+ it "generates the proper 'root' node for serialized arrays" do
71
+ app.get("/blog_posts") do
72
+ blog_post = BlogPost.new({title: 'Grape AM::S Rocks!', body: 'Really, it does.'})
73
+ [blog_post, blog_post]
74
+ end
75
+
76
+ get "/blog_posts"
77
+ last_response.body.should == "{\"blog_posts\":[{\"title\":\"Grape AM::S Rocks!\",\"body\":\"Really, it does.\"},{\"title\":\"Grape AM::S Rocks!\",\"body\":\"Really, it does.\"}]}"
78
+ end
79
+ end
80
+
81
+ it "uses namespace options when provided" do
82
+ app.namespace :admin, :serializer => UserSerializer do
83
+ get('/jeff') do
84
+ User.new(first_name: 'Jeff')
85
+ end
86
+ end
87
+
88
+ get "/admin/jeff"
89
+ last_response.body.should == "{\"user\":{\"first_name\":\"Jeff\",\"last_name\":null}}"
90
+ end
91
+
92
+ context 'route is in a namespace' do
93
+ it 'uses the name of the closest namespace for the root' do
94
+ app.namespace :admin do
95
+ get('/jeff') do
96
+ user = User.new(first_name: 'Jeff')
97
+ [user, user]
98
+ end
99
+ end
100
+
101
+ get "/admin/jeff"
102
+ last_response.body.should == "{\"admin\":[{\"first_name\":\"Jeff\",\"last_name\":null},{\"first_name\":\"Jeff\",\"last_name\":null}]}"
103
+ end
104
+ end
105
+
106
+ context 'route is not in a namespace' do
107
+ it 'uses the of the route for the root' do
108
+ app.get('/people') do
109
+ user = User.new(first_name: 'Jeff')
110
+ [user, user]
111
+ end
112
+
113
+ get "/people"
114
+ last_response.body.should == "{\"people\":[{\"first_name\":\"Jeff\",\"last_name\":null},{\"first_name\":\"Jeff\",\"last_name\":null}]}"
115
+ end
116
+ end
117
+ end
118
+
@@ -0,0 +1,19 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'bundler'
5
+ Bundler.setup :default, :test
6
+
7
+ require "active_model_serializers"
8
+ require 'active_support/core_ext/hash/conversions'
9
+ require "active_support/json"
10
+ require 'rspec'
11
+ require 'rack/test'
12
+
13
+ require 'jazz_hands'
14
+
15
+ RSpec.configure do |config|
16
+ config.include Rack::Test::Methods
17
+ end
18
+
19
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
@@ -0,0 +1,10 @@
1
+ class BlogPost
2
+ include ActiveModel::SerializerSupport
3
+ attr_accessor :title, :body
4
+
5
+ def initialize(params={})
6
+ params.each do |k,v|
7
+ instance_variable_set("@#{k}", v) unless v.nil?
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class User
2
+ include ActiveModel::SerializerSupport
3
+ attr_accessor :first_name, :last_name, :password, :email
4
+
5
+ def initialize(params={})
6
+ params.each do |k,v|
7
+ instance_variable_set("@#{k}", v) unless v.nil?
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ class BlogPostSerializer < ActiveModel::Serializer
2
+ attributes :title, :body
3
+ end
@@ -0,0 +1,3 @@
1
+ class UserSerializer < ActiveModel::Serializer
2
+ attributes :first_name, :last_name
3
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-active_model_serializers-revibe
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Richard Henry Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_model_serializers
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: jazz_hands
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Provides a Formatter for the Grape API DSL to emit objects serialized
112
+ with active_model_serializers.
113
+ email:
114
+ - contact@jrhe.co.uk
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - Gemfile
121
+ - Guardfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - grape-active_model_serializers.gemspec
126
+ - lib/.rspec
127
+ - lib/.travis.yml
128
+ - lib/grape-active_model_serializers.rb
129
+ - lib/grape-active_model_serializers/endpoint_extension.rb
130
+ - lib/grape-active_model_serializers/formatter.rb
131
+ - lib/grape-active_model_serializers/version.rb
132
+ - spec/features/grape-active_model_serializers/render_spec.rb
133
+ - spec/grape-active_model_serializers/endpoint_extension_spec.rb
134
+ - spec/grape-active_model_serializers/formatter_spec.rb
135
+ - spec/grape-active_model_serializers_spec.rb
136
+ - spec/old_grape_ams_spec.rb
137
+ - spec/spec_helper.rb
138
+ - spec/support/models/blog_post.rb
139
+ - spec/support/models/user.rb
140
+ - spec/support/serializers/blog_post_serializer.rb
141
+ - spec/support/serializers/user_serializer.rb
142
+ homepage: https://github.com/jrhe/grape-active_model_serializers
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.0.3
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: Use active_model_serializer in grape
166
+ test_files:
167
+ - spec/features/grape-active_model_serializers/render_spec.rb
168
+ - spec/grape-active_model_serializers/endpoint_extension_spec.rb
169
+ - spec/grape-active_model_serializers/formatter_spec.rb
170
+ - spec/grape-active_model_serializers_spec.rb
171
+ - spec/old_grape_ams_spec.rb
172
+ - spec/spec_helper.rb
173
+ - spec/support/models/blog_post.rb
174
+ - spec/support/models/user.rb
175
+ - spec/support/serializers/blog_post_serializer.rb
176
+ - spec/support/serializers/user_serializer.rb