sinatra-active-model-serializers 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/Gemfile +13 -0
- data/LICENSE +21 -0
- data/README.md +89 -0
- data/Rakefile +7 -0
- data/db/migrate/00_create_tests.rb +7 -0
- data/lib/sinatra_active_model_serializers.rb +2 -0
- data/lib/sinatra_active_model_serializers/json.rb +32 -0
- data/lib/sinatra_active_model_serializers/version.rb +6 -0
- data/sinatra-active-model-serializers.gemspec +45 -0
- data/spec/app/application.rb +33 -0
- data/spec/app/models/test.rb +2 -0
- data/spec/app/serializers/test_serializer.rb +7 -0
- data/spec/json_spec.rb +13 -0
- data/spec/schemas/test_with_root.json +19 -0
- data/spec/schemas/test_without_root.json +11 -0
- data/spec/spec_helper.rb +28 -0
- data/spec/support/request_support.rb +14 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9de09c12a25df8fa91796a5574b5c532c320b6ee
|
4
|
+
data.tar.gz: b2e44cb5e2297efe91436422c846de165f004696
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c33258af4814d0037f66deb4f7f22685c07363e1b2af47f6251469b04dff85ab4af038772371c02fab62f2cb192a95277a8d05f94e77453c069579d5f73e5244
|
7
|
+
data.tar.gz: b065396ae32be06bbacbc80315cd7d5a486fc7c7c6c23d349d4d02cf99477712c9dac53cd555806808e3a257d967b2d1282c715589e8f44dffb8fccf762fba2f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
gemspec
|
3
|
+
|
4
|
+
group :test do
|
5
|
+
gem 'sinatra', '1.4.5'
|
6
|
+
gem 'sinatra-contrib', '1.4.2'
|
7
|
+
gem 'rake', '10.4.2'
|
8
|
+
gem 'json-schema', '2.5.0'
|
9
|
+
gem 'rspec', '3.1.0'
|
10
|
+
gem 'sinatra-activerecord', '2.0.4'
|
11
|
+
gem 'sqlite3', '1.3.10'
|
12
|
+
gem 'database_cleaner', '1.4.0'
|
13
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Saulo da Silva Santiago
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
[ ](https://codeship.com/projects/60665)
|
2
|
+
|
3
|
+
# Sinatra::ActiveModelSerializers
|
4
|
+
|
5
|
+
This gem has the function of adapt the Active Model Serializers to work in Sinatra from a more practical way with models.
|
6
|
+
If do you use everything at the default, just require `sinatra_active_model_serializers` for serializers work correctly the a json response.
|
7
|
+
|
8
|
+
## Requirements
|
9
|
+
|
10
|
+
Ruby 1.9.2 or greater, Sinatra 1.4.5 or greater and Sinatra Contrib 1.4.2 or greater.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
`gem install sinatra-active-model-serializers`
|
15
|
+
|
16
|
+
or with bundler
|
17
|
+
|
18
|
+
```
|
19
|
+
# Gemfile
|
20
|
+
source :rubygems
|
21
|
+
|
22
|
+
gem 'sinatra'
|
23
|
+
gem 'sinatra-contrib'
|
24
|
+
gem 'sinatra-active-record'
|
25
|
+
gem 'sinatra-active-model-serializers'
|
26
|
+
```
|
27
|
+
|
28
|
+
## Configure
|
29
|
+
|
30
|
+
First you have to being with the active record configured in your system environment correctly. If the environment is not set up, see their [documentation](https://github.com/janko-m/sinatra-activerecord#sinatra-activerecord-extension).
|
31
|
+
After is simply do require from our library on your `application.rb`, eg.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require 'rubygems'
|
35
|
+
require 'sinatra-active-model-serializers'
|
36
|
+
|
37
|
+
class App < Sinatra::Base
|
38
|
+
get '/' do
|
39
|
+
json Test.first
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
## Options
|
45
|
+
|
46
|
+
**active_model_serializers**
|
47
|
+
|
48
|
+
This attribute is an object, all inserted configuration this object it will be passed on to Active Model Serializers, eg.
|
49
|
+
|
50
|
+
```
|
51
|
+
set :active_model_serializers, { root: false }
|
52
|
+
```
|
53
|
+
|
54
|
+
**serializers_path**
|
55
|
+
|
56
|
+
By default this attribute is set up to look for the serializers from your project in "* app / serializers *". Whether you have a different environment you can set up by inserting the path of the string, eg.
|
57
|
+
|
58
|
+
```
|
59
|
+
set :serializers_path, './whatever_path/serializers'
|
60
|
+
```
|
61
|
+
|
62
|
+
or not to automatically requires
|
63
|
+
|
64
|
+
```
|
65
|
+
set :serializers_path, false
|
66
|
+
```
|
67
|
+
|
68
|
+
**json**
|
69
|
+
|
70
|
+
When you return a json, you can send a the second parameter.
|
71
|
+
This the second parameter is an object. This object may contain new configurations to assign to the Active Model Serializers or to rewrite any already set by default, eg.
|
72
|
+
|
73
|
+
```
|
74
|
+
get '/' do
|
75
|
+
json Resource.first, { root: false }
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
or
|
80
|
+
|
81
|
+
```
|
82
|
+
get '/' do
|
83
|
+
json Resource.first, { scope: self }
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
[MIT](https://github.com/SauloSilva/sinatra-active-model-serializers/blob/master/LICENSE)
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Sinatra
|
2
|
+
SERIALIZERS_DEFAULT_PATH = './app/serializers'
|
3
|
+
|
4
|
+
module JSON
|
5
|
+
def initialize
|
6
|
+
files_required
|
7
|
+
super
|
8
|
+
end
|
9
|
+
|
10
|
+
def json(object, options={})
|
11
|
+
options = options.merge(settings.active_model_serializers)
|
12
|
+
|
13
|
+
serializer = ActiveModel::Serializer.serializer_for(object, options)
|
14
|
+
|
15
|
+
if serializer
|
16
|
+
serializer.new(object, options).to_json
|
17
|
+
else
|
18
|
+
object.to_json(options)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def files_required
|
23
|
+
return unless settings.serializers_path
|
24
|
+
Dir["#{ settings.serializers_path }/**/*.rb"].flatten.sort.each do |file|
|
25
|
+
require file
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
Base.set :active_model_serializers, {}
|
31
|
+
Base.set :serializers_path, SERIALIZERS_DEFAULT_PATH
|
32
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
# Run `rake sinatra-contrib.gemspec` to update the gemspec.
|
4
|
+
require File.expand_path('../lib/sinatra_active_model_serializers/version', __FILE__)
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'sinatra-active-model-serializers'
|
8
|
+
s.version = SinatraActiveModelSerializers::VERSION
|
9
|
+
s.description = 'Adapter from Active Model Serializers, to work in Sinatra from a more practical way with models'
|
10
|
+
s.homepage = 'https://github.com/SauloSilva/sinatra-active-model-serializers'
|
11
|
+
s.summary = 'Adapter from Active Model Serializers'
|
12
|
+
|
13
|
+
s.license = 'MIT'
|
14
|
+
|
15
|
+
# generated from git shortlog -sn
|
16
|
+
s.authors = ['saulosantiago']
|
17
|
+
|
18
|
+
# generated from git shortlog -sne
|
19
|
+
s.email = ['saulodasilvasantiago@gmail.com']
|
20
|
+
|
21
|
+
# generated from git ls-files
|
22
|
+
s.files = [
|
23
|
+
'.gitignore',
|
24
|
+
'Gemfile',
|
25
|
+
'LICENSE',
|
26
|
+
'README.md',
|
27
|
+
'Rakefile',
|
28
|
+
'db/migrate/00_create_tests.rb',
|
29
|
+
'db/schema.rb',
|
30
|
+
'lib/sinatra_active_model_serializers.rb',
|
31
|
+
'lib/sinatra_active_model_serializers/json.rb',
|
32
|
+
'lib/sinatra_active_model_serializers/version.rb',
|
33
|
+
'sinatra-active-model-serializers.gemspec',
|
34
|
+
'spec/app/application.rb',
|
35
|
+
'spec/app/models/test.rb',
|
36
|
+
'spec/app/serializers/test_serializer.rb',
|
37
|
+
'spec/json_spec.rb',
|
38
|
+
'spec/schemas/test_with_root.json',
|
39
|
+
'spec/schemas/test_without_root.json',
|
40
|
+
'spec/spec_helper.rb',
|
41
|
+
'spec/support/request_support.rb'
|
42
|
+
]
|
43
|
+
|
44
|
+
s.add_runtime_dependency 'active_model_serializers', '0.9.3'
|
45
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.require(:defaut, :test)
|
4
|
+
|
5
|
+
require './lib/sinatra_active_model_serializers/'
|
6
|
+
Dir['./spec/app/models/**/*.rb'].flatten.sort.each { |file| require file}
|
7
|
+
|
8
|
+
module App
|
9
|
+
class Base < Sinatra::Base
|
10
|
+
register Sinatra::ActiveRecordExtension
|
11
|
+
|
12
|
+
set :serializers_path, './spec/app/serializers'
|
13
|
+
|
14
|
+
configure :test do
|
15
|
+
ActiveRecord::Base.logger = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
set :database, {
|
19
|
+
adapter: 'sqlite3',
|
20
|
+
database: 'db/sinatra_active_model_serializers.sqlite3'
|
21
|
+
}
|
22
|
+
|
23
|
+
get '/with-root/' do
|
24
|
+
json Test.create(foo: 'bar'), { root: true }
|
25
|
+
end
|
26
|
+
|
27
|
+
get '/without-root/' do
|
28
|
+
json Test.create(foo: 'bar'), { root: false }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
App::Base.run
|
data/spec/json_spec.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe App::Base do
|
4
|
+
it 'should response with root' do
|
5
|
+
get '/with-root/'
|
6
|
+
match_response_schema(:test_with_root)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should response without root' do
|
10
|
+
get '/without-root/'
|
11
|
+
match_response_schema(:test_without_root)
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
{
|
2
|
+
"type": "object",
|
3
|
+
"required": [
|
4
|
+
"test"
|
5
|
+
],
|
6
|
+
"properties": {
|
7
|
+
"test": {
|
8
|
+
"type": "object",
|
9
|
+
"required": [
|
10
|
+
"foo",
|
11
|
+
"bar"
|
12
|
+
],
|
13
|
+
"properties": {
|
14
|
+
"foo": { "type": "string" },
|
15
|
+
"bar": { "type": "string" }
|
16
|
+
}
|
17
|
+
}
|
18
|
+
}
|
19
|
+
}
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
ENV['RACK_ENV'] = 'test'
|
2
|
+
|
3
|
+
require 'sinatra'
|
4
|
+
require 'sinatra/contrib'
|
5
|
+
require 'app/application'
|
6
|
+
require 'rspec'
|
7
|
+
require 'rack/test'
|
8
|
+
Dir[File.expand_path '../support/**/*.rb', __FILE__].each { |file| require file }
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.expect_with :rspec
|
12
|
+
include Rack::Test::Methods
|
13
|
+
include RequestSupport
|
14
|
+
DatabaseCleaner.clean_with :truncation
|
15
|
+
DatabaseCleaner.strategy = :transaction
|
16
|
+
|
17
|
+
before :all do
|
18
|
+
DatabaseCleaner.start
|
19
|
+
end
|
20
|
+
|
21
|
+
after :all do
|
22
|
+
DatabaseCleaner.clean
|
23
|
+
end
|
24
|
+
|
25
|
+
def app
|
26
|
+
App::Base
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module RequestSupport
|
2
|
+
def response_body
|
3
|
+
JSON.parse last_response.body
|
4
|
+
end
|
5
|
+
|
6
|
+
def match_response_schema(schema, options={})
|
7
|
+
default_optitons = { strict: true, validate_schema: true }
|
8
|
+
schema_directory = "#{ Dir.pwd }/spec/schemas"
|
9
|
+
schema_path = "#{ schema_directory }/#{ schema }.json"
|
10
|
+
|
11
|
+
JSON::Validator.clear_cache
|
12
|
+
JSON::Validator.validate!(schema_path, response_body, default_optitons.merge(options))
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-active-model-serializers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- saulosantiago
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: active_model_serializers
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.9.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.9.3
|
27
|
+
description: Adapter from Active Model Serializers, to work in Sinatra from a more
|
28
|
+
practical way with models
|
29
|
+
email:
|
30
|
+
- saulodasilvasantiago@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".gitignore"
|
36
|
+
- Gemfile
|
37
|
+
- LICENSE
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- db/migrate/00_create_tests.rb
|
41
|
+
- db/schema.rb
|
42
|
+
- lib/sinatra_active_model_serializers.rb
|
43
|
+
- lib/sinatra_active_model_serializers/json.rb
|
44
|
+
- lib/sinatra_active_model_serializers/version.rb
|
45
|
+
- sinatra-active-model-serializers.gemspec
|
46
|
+
- spec/app/application.rb
|
47
|
+
- spec/app/models/test.rb
|
48
|
+
- spec/app/serializers/test_serializer.rb
|
49
|
+
- spec/json_spec.rb
|
50
|
+
- spec/schemas/test_with_root.json
|
51
|
+
- spec/schemas/test_without_root.json
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
- spec/support/request_support.rb
|
54
|
+
homepage: https://github.com/SauloSilva/sinatra-active-model-serializers
|
55
|
+
licenses:
|
56
|
+
- MIT
|
57
|
+
metadata: {}
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 2.2.2
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: Adapter from Active Model Serializers
|
78
|
+
test_files: []
|