grape-jsonapi-resources 0.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: 66ed30fd8d9408e26b35c52db9035123f83efa94
4
+ data.tar.gz: fe03036472c71e70ab7b287dc5696a29f71d7ef1
5
+ SHA512:
6
+ metadata.gz: eec5d985fa7a1d518f24ec3f5646f44cb891fbcb7a531d7d3035fc861bfe6de422f005540e0a46ef27ecdc9a7762d37360098b1a7cb419866c16576ff9d3ea29
7
+ data.tar.gz: 4d0a59553e96d9e29333aa4be6a50cdfb6d5f48e371bbfba71e4b1bc2643ec6b1767edcee91142e602f86052a93ec2214d26785bd0643243146798463bc84739
@@ -0,0 +1,19 @@
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
19
+ *.gem
@@ -0,0 +1,2 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
@@ -0,0 +1,24 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2015-01-13 18:47:14 -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: 25
9
+ # Configuration parameters: AllowURI, URISchemes.
10
+ Metrics/LineLength:
11
+ Max: 179
12
+
13
+ # Offense count: 7
14
+ Style/Documentation:
15
+ Enabled: false
16
+
17
+ # Offense count: 2
18
+ # Configuration parameters: Exclude.
19
+ Style/FileName:
20
+ Enabled: false
21
+
22
+ # Offense count: 4
23
+ Style/RegexpLiteral:
24
+ MaxSlashes: 0
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+
3
+ sudo: false
4
+
5
+ rvm:
6
+ - 2.1.1
7
+ - 1.9.3
8
+ - rbx-2.2.10
9
+ - jruby-19mode
10
+
11
+ env:
12
+ - GRAPE_VERSION=0.8.0
13
+ - GRAPE_VERSION=0.9.0
14
+ - GRAPE_VERSION=0.10.1
15
+ - GRAPE_VERSION=HEAD
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ case version = ENV['GRAPE_VERSION'] || '~> 0.10.0'
6
+ when 'HEAD'
7
+ gem 'grape', github: 'intridea/grape'
8
+ else
9
+ gem 'grape', version
10
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Cary Dunn
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,38 @@
1
+ # Grape::JSONAPIResources
2
+
3
+ Use [jsonapi-resources](https://github.com/cerebris/jsonapi-resources) with [Grape](https://github.com/intridea/grape)!
4
+
5
+ ## Installation
6
+
7
+ Add the `grape` and `grape-jsonapi-resources` gems to Gemfile.
8
+
9
+ ```ruby
10
+ gem 'grape'
11
+ gem 'grape-jsonapi-resources'
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ### Require grape-jsonapi-resources
17
+
18
+ ### Tell your API to use Grape::Formatter::JSONAPIResources
19
+
20
+ ```ruby
21
+ class API < Grape::API
22
+ format :json
23
+ formatter :json, Grape::Formatter::JSONAPIResources
24
+ end
25
+ ```
26
+
27
+ ### Use `render` to specify JSONAPI options
28
+
29
+ ```ruby
30
+ get "/" do
31
+ user = User.find("123")
32
+ render user, include: ["account"], context: { something: :important }
33
+ end
34
+ ```
35
+
36
+ ## Credit
37
+
38
+ Code adapted from [grape-active_model_serializers](https://github.com/jrhe/grape-active_model_serializers)
@@ -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,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/grape-jsonapi-resources/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['Cary Dunn']
6
+ gem.email = ['cary.dunn@gmail.com']
7
+ gem.summary = 'Use jsonapi-resources in grape'
8
+ gem.description = 'Provides a Formatter for the Grape API DSL to emit objects serialized with jsonapi-resources.'
9
+ gem.homepage = 'https://github.com/cdunn/grape-jsonapi-resources'
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}/*`.split("\n")
14
+ gem.name = 'grape-jsonapi-resources'
15
+ gem.require_paths = ['lib']
16
+ gem.version = Grape::JSONAPIResources::VERSION
17
+ gem.licenses = ['MIT']
18
+
19
+ gem.add_dependency 'grape'
20
+ gem.add_dependency 'jsonapi-resources', '>= 0.5.0'
21
+
22
+ gem.add_development_dependency 'rails-api', '>= 0.4.0'
23
+ gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'rack-test'
25
+ gem.add_development_dependency 'rake'
26
+ end
@@ -0,0 +1,10 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.2
6
+ - ruby-head
7
+ - rbx-19mode
8
+
9
+ env:
10
+ - JRUBY_OPTS="--1.9"
@@ -0,0 +1,6 @@
1
+ require 'jsonapi-resources'
2
+ require 'grape'
3
+ require 'grape-jsonapi-resources/endpoint_extension'
4
+ require 'grape-jsonapi-resources/api_options_extension'
5
+ require 'grape-jsonapi-resources/formatter'
6
+ require 'grape-jsonapi-resources/version'
@@ -0,0 +1,9 @@
1
+ module Grape
2
+ class API
3
+ class << self
4
+ def jsonapi_base_url(url = nil)
5
+ namespace_inheritable(:jsonapi_base_url, url)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module Grape
2
+ module EndpointExtension
3
+ def render(resources, options = {})
4
+ env['jsonapi_options'] = options
5
+ resources
6
+ end
7
+ end
8
+
9
+ Endpoint.send(:include, EndpointExtension)
10
+ end
@@ -0,0 +1,74 @@
1
+ module Grape
2
+ module Formatter
3
+ module JSONAPIResources
4
+ class << self
5
+ def call(resource, env)
6
+ serialize_resource(resource, env) || Grape::Formatter::Json.call(resource, env)
7
+ end
8
+
9
+ def serialize_resource(resource, env)
10
+ endpoint = env['api.endpoint']
11
+
12
+ jsonapi_options = build_options_from_endpoint(endpoint)
13
+ jsonapi_options.merge!(env['jsonapi_options'] || {})
14
+
15
+ context = {}
16
+ context.merge!(current_user: endpoint.current_user) if endpoint.respond_to?(:current_user)
17
+ context.merge!(jsonapi_options.delete(:context)) if jsonapi_options[:context]
18
+
19
+ resource_class = resource_class_for(resource)
20
+ return nil unless resource_class
21
+ resource_instances = nil
22
+ if resource.respond_to?(:to_ary)
23
+ resource_instances = resource.to_ary.collect do |each_resource|
24
+ resource_class.new(each_resource, context)
25
+ end
26
+ else
27
+ resource_instances = resource_class.new(resource, context)
28
+ end
29
+
30
+ resource_serialzer = JSONAPI::ResourceSerializer.new(resource_class, jsonapi_options).serialize_to_hash(resource_instances)
31
+ if jsonapi_options[:meta]
32
+ # Add option to merge top level meta tag as jsonapi-resources does not appear to support this
33
+ resource_serialzer.as_json.merge(meta: jsonapi_options[:meta]).to_json if jsonapi_options[:meta]
34
+ else
35
+ resource_serialzer.to_json
36
+ end
37
+ end
38
+
39
+ def build_options_from_endpoint(endpoint)
40
+ options = {}
41
+ options[:base_url] = endpoint.namespace_inheritable(:jsonapi_base_url) if endpoint.namespace_inheritable(:jsonapi_base_url)
42
+ options
43
+ end
44
+
45
+ def resource_class_for(resource)
46
+ if resource.class.respond_to?(:jsonapi_resource_class)
47
+ resource.class.jsonapi_resource_class
48
+ elsif resource.respond_to?(:to_ary)
49
+ resource_class_for(resource.to_ary.first)
50
+ else
51
+ get_resource_for(resource.class)
52
+ end
53
+ end
54
+
55
+ def resources_cache
56
+ @resources_cache ||= ThreadSafe::Cache.new
57
+ end
58
+
59
+ def get_resource_for(klass)
60
+ resources_cache.fetch_or_store(klass) do
61
+ resource_class_name = "#{klass.name}Resource"
62
+ resource_class = resource_class_name.safe_constantize
63
+
64
+ if resource_class
65
+ resource_class
66
+ elsif klass.superclass
67
+ get_resource_for(klass.superclass)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,5 @@
1
+ module Grape
2
+ module JSONAPIResources
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Grape::EndpointExtension' do
4
+ if Grape::Util.const_defined?('InheritableSetting')
5
+ subject { Grape::Endpoint.new(Grape::Util::InheritableSetting.new, path: '/', method: 'foo') }
6
+ else
7
+ subject { Grape::Endpoint.new({}, path: '/', method: 'foo') }
8
+ end
9
+
10
+ let(:serializer) { Grape::Formatter::JSONAPIResources }
11
+
12
+ let(:user) do
13
+ User.new(name: "yasiel")
14
+ end
15
+
16
+ let(:users) { [user, user] }
17
+
18
+ describe '#render' do
19
+ before do
20
+ allow(subject).to receive(:env).and_return({})
21
+ end
22
+
23
+ it { should respond_to(:render) }
24
+
25
+ context 'settings options' do
26
+ it 'sets the jsonapi options on the environment' do
27
+ expect(subject.render(users, {include: ["included_resource"]})).to eq(users)
28
+ expect(subject.env).to include({"jsonapi_options"=>{:include=>["included_resource"]}})
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe Grape::Formatter::JSONAPIResources do
4
+ subject { Grape::Formatter::JSONAPIResources }
5
+
6
+ describe 'serializer options from namespace' do
7
+ let(:app) { Class.new(Grape::API) }
8
+
9
+ before do
10
+ app.format :json
11
+ app.formatter :json, Grape::Formatter::JSONAPIResources
12
+
13
+ app.namespace('space') do |ns|
14
+ ns.jsonapi_base_url "/api/v1"
15
+ ns.get('/') do
16
+ { user: { first_name: 'Cary', last_name: 'D' } }
17
+ end
18
+ end
19
+ end
20
+
21
+ it 'should read serializer options like "jsonapi_base_url"' do
22
+ expect(described_class.build_options_from_endpoint(app.endpoints.first)).to include({base_url: "/api/v1"})
23
+ end
24
+ end
25
+
26
+ describe '.serialize_resource' do
27
+ let(:user) { User.new(first_name: 'John', id: 123) }
28
+
29
+ if Grape::Util.const_defined?('InheritableSetting')
30
+ let(:endpoint) { Grape::Endpoint.new(Grape::Util::InheritableSetting.new, path: '/', method: 'foo', root: false) }
31
+ else
32
+ let(:endpoint) { Grape::Endpoint.new({}, path: '/', method: 'foo', root: false) }
33
+ end
34
+
35
+ let(:env) { { 'api.endpoint' => endpoint } }
36
+
37
+ before do
38
+ def endpoint.current_user
39
+ @current_user ||= User.new(first_name: 'Current user')
40
+ end
41
+ end
42
+
43
+ subject { described_class.serialize_resource(user, env) }
44
+
45
+ it { should be_a String }
46
+
47
+ it 'should have used the correct resource' do
48
+ expect(UserResource).to receive(:new).with(user, {current_user: endpoint.current_user}).once.and_call_original
49
+ subject
50
+ end
51
+
52
+ it 'should map a collect of resources' do
53
+ expect(UserResource).to receive(:new).with(user, {current_user: endpoint.current_user}).twice.and_call_original
54
+ described_class.serialize_resource([ user, user ], env)
55
+ end
56
+ # array
57
+
58
+ it 'should specify serializer options like "base_url" and "include"' do
59
+ allow(described_class).to receive(:build_options_from_endpoint).with(endpoint).and_return({base_url: "/api/v1"})
60
+ expect(JSONAPI::ResourceSerializer).to receive(:new).with(UserResource, {base_url: "/api/v1"}).and_call_original
61
+ subject
62
+ end
63
+
64
+ it 'should allow custom meta tag' do
65
+ meta = {total_pages: 123}
66
+ allow(described_class).to receive(:build_options_from_endpoint).with(endpoint).and_return({meta: meta})
67
+ expect(subject).to match meta.to_json
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'grape-jsonapi-resources' do
4
+ end
@@ -0,0 +1,21 @@
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 'rails'
8
+ require 'action_controller'
9
+ require 'active_model'
10
+ require 'jsonapi-resources'
11
+ require 'active_support/core_ext/hash/conversions'
12
+ require 'active_support/json'
13
+ require 'rspec'
14
+ require 'rack/test'
15
+ require 'grape-jsonapi-resources'
16
+
17
+ RSpec.configure do |config|
18
+ config.include Rack::Test::Methods
19
+ end
20
+
21
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@@ -0,0 +1,13 @@
1
+ class BlogPost
2
+ include ActiveModel::Model
3
+ include ActiveModel::Serialization
4
+
5
+ attr_accessor :id, :title, :body
6
+
7
+ def initialize(params = {})
8
+ @id = 1
9
+ params.each do |k, v|
10
+ instance_variable_set("@#{k}", v) unless v.nil?
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class User
2
+ include ActiveModel::Model
3
+ include ActiveModel::Serialization
4
+
5
+ attr_accessor :id, :first_name, :last_name, :password, :email
6
+
7
+ def initialize(params = {})
8
+ @id = 1
9
+ params.each do |k, v|
10
+ instance_variable_set("@#{k}", v) unless v.nil?
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ class BlogPostResource < JSONAPI::Resource
2
+ attributes :title, :body
3
+ end
@@ -0,0 +1,3 @@
1
+ class UserResource < JSONAPI::Resource
2
+ attributes :first_name, :last_name
3
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-jsonapi-resources
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cary Dunn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-30 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jsonapi-resources
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails-api
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.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.4.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
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: rack-test
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: rake
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
+ description: Provides a Formatter for the Grape API DSL to emit objects serialized
98
+ with jsonapi-resources.
99
+ email:
100
+ - cary.dunn@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rubocop.yml"
107
+ - ".rubocop_todo.yml"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - grape-jsonapi-resources.gemspec
114
+ - lib/.travis.yml
115
+ - lib/grape-jsonapi-resources.rb
116
+ - lib/grape-jsonapi-resources/api_options_extension.rb
117
+ - lib/grape-jsonapi-resources/endpoint_extension.rb
118
+ - lib/grape-jsonapi-resources/formatter.rb
119
+ - lib/grape-jsonapi-resources/version.rb
120
+ - spec/grape-jsonapi-resources/endpoint_extension_spec.rb
121
+ - spec/grape-jsonapi-resources/formatter_spec.rb
122
+ - spec/grape-jsonapi-resources_spec.rb
123
+ - spec/spec_helper.rb
124
+ - spec/support/models/blog_post.rb
125
+ - spec/support/models/user.rb
126
+ - spec/support/resources/blog_post_resource.rb
127
+ - spec/support/resources/user_resource.rb
128
+ homepage: https://github.com/cdunn/grape-jsonapi-resources
129
+ licenses:
130
+ - MIT
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.4.5
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Use jsonapi-resources in grape
152
+ test_files:
153
+ - spec/grape-jsonapi-resources/endpoint_extension_spec.rb
154
+ - spec/grape-jsonapi-resources/formatter_spec.rb
155
+ - spec/grape-jsonapi-resources_spec.rb
156
+ - spec/spec_helper.rb
157
+ - spec/support/models/blog_post.rb
158
+ - spec/support/models/user.rb
159
+ - spec/support/resources/blog_post_resource.rb
160
+ - spec/support/resources/user_resource.rb