onsi 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +1 -0
- data/.rubocop.yml +53 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +168 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/onsi.rb +9 -0
- data/lib/onsi/controller.rb +27 -0
- data/lib/onsi/errors.rb +18 -0
- data/lib/onsi/model.rb +116 -0
- data/lib/onsi/params.rb +72 -0
- data/lib/onsi/resource.rb +40 -0
- data/lib/onsi/version.rb +3 -0
- data/onsi.gemspec +36 -0
- metadata +195 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: efa13befa06b8a9e8c45e5364b9e77c14e261fa5898dc4c7a2fd7e881234b69c
|
4
|
+
data.tar.gz: c3ca075945d5d8cb2d9c8f2e144039dab47338b23812674ac4cbd31639c51ae0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2cb3264cd255c4a38803b352ef173f97f5e1a84ee58a7747cf1bb5951a083b708ad5963b8ac2625941f11cf33dac960a101199e001aa5e73e57c54d409ddd781
|
7
|
+
data.tar.gz: 538a6e4abb95c6eba3525a03816b4db692b1948bae8fdbe43c85ce2a1baed0b209c89cbd96c018a322166dc6749b4d4f429fb53a583378cd417d8d5380a4ef15
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
Exclude:
|
4
|
+
- Rakefile
|
5
|
+
- spec/**/*
|
6
|
+
|
7
|
+
Metrics/AbcSize:
|
8
|
+
Max: 30
|
9
|
+
|
10
|
+
Metrics/LineLength:
|
11
|
+
Max: 160
|
12
|
+
|
13
|
+
Metrics/MethodLength:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
Metrics/ParameterLists:
|
17
|
+
Max: 10
|
18
|
+
|
19
|
+
Metrics/CyclomaticComplexity:
|
20
|
+
Max: 10
|
21
|
+
|
22
|
+
DotPosition:
|
23
|
+
EnforcedStyle: leading
|
24
|
+
|
25
|
+
StringLiterals:
|
26
|
+
EnforcedStyle: single_quotes
|
27
|
+
|
28
|
+
Layout/AlignParameters:
|
29
|
+
EnforcedStyle: with_fixed_indentation
|
30
|
+
|
31
|
+
Layout/MultilineOperationIndentation:
|
32
|
+
EnforcedStyle: indented
|
33
|
+
|
34
|
+
Layout/MultilineMethodCallIndentation:
|
35
|
+
EnforcedStyle: indented
|
36
|
+
|
37
|
+
Style/AndOr:
|
38
|
+
EnforcedStyle: conditionals
|
39
|
+
|
40
|
+
Style/ClassAndModuleChildren:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
Style/ConditionalAssignment:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
Style/GuardClause:
|
47
|
+
Enabled: false
|
48
|
+
|
49
|
+
Style/RaiseArgs:
|
50
|
+
Enabled: false
|
51
|
+
|
52
|
+
Style/FrozenStringLiteralComment:
|
53
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
onsi (0.1.0)
|
5
|
+
rails (>= 5.0, < 6.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actioncable (5.2.0)
|
11
|
+
actionpack (= 5.2.0)
|
12
|
+
nio4r (~> 2.0)
|
13
|
+
websocket-driver (>= 0.6.1)
|
14
|
+
actionmailer (5.2.0)
|
15
|
+
actionpack (= 5.2.0)
|
16
|
+
actionview (= 5.2.0)
|
17
|
+
activejob (= 5.2.0)
|
18
|
+
mail (~> 2.5, >= 2.5.4)
|
19
|
+
rails-dom-testing (~> 2.0)
|
20
|
+
actionpack (5.2.0)
|
21
|
+
actionview (= 5.2.0)
|
22
|
+
activesupport (= 5.2.0)
|
23
|
+
rack (~> 2.0)
|
24
|
+
rack-test (>= 0.6.3)
|
25
|
+
rails-dom-testing (~> 2.0)
|
26
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
27
|
+
actionview (5.2.0)
|
28
|
+
activesupport (= 5.2.0)
|
29
|
+
builder (~> 3.1)
|
30
|
+
erubi (~> 1.4)
|
31
|
+
rails-dom-testing (~> 2.0)
|
32
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
33
|
+
activejob (5.2.0)
|
34
|
+
activesupport (= 5.2.0)
|
35
|
+
globalid (>= 0.3.6)
|
36
|
+
activemodel (5.2.0)
|
37
|
+
activesupport (= 5.2.0)
|
38
|
+
activerecord (5.2.0)
|
39
|
+
activemodel (= 5.2.0)
|
40
|
+
activesupport (= 5.2.0)
|
41
|
+
arel (>= 9.0)
|
42
|
+
activestorage (5.2.0)
|
43
|
+
actionpack (= 5.2.0)
|
44
|
+
activerecord (= 5.2.0)
|
45
|
+
marcel (~> 0.3.1)
|
46
|
+
activesupport (5.2.0)
|
47
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
48
|
+
i18n (>= 0.7, < 2)
|
49
|
+
minitest (~> 5.1)
|
50
|
+
tzinfo (~> 1.1)
|
51
|
+
appraisal (2.1.0)
|
52
|
+
bundler
|
53
|
+
rake
|
54
|
+
thor (>= 0.14.0)
|
55
|
+
arel (9.0.0)
|
56
|
+
builder (3.2.3)
|
57
|
+
coderay (1.1.2)
|
58
|
+
concurrent-ruby (1.0.5)
|
59
|
+
crass (1.0.4)
|
60
|
+
database_cleaner (1.7.0)
|
61
|
+
diff-lcs (1.3)
|
62
|
+
docile (1.3.0)
|
63
|
+
erubi (1.7.1)
|
64
|
+
globalid (0.4.1)
|
65
|
+
activesupport (>= 4.2.0)
|
66
|
+
i18n (1.0.1)
|
67
|
+
concurrent-ruby (~> 1.0)
|
68
|
+
json (2.1.0)
|
69
|
+
loofah (2.2.2)
|
70
|
+
crass (~> 1.0.2)
|
71
|
+
nokogiri (>= 1.5.9)
|
72
|
+
mail (2.7.0)
|
73
|
+
mini_mime (>= 0.1.1)
|
74
|
+
marcel (0.3.2)
|
75
|
+
mimemagic (~> 0.3.2)
|
76
|
+
method_source (0.9.0)
|
77
|
+
mimemagic (0.3.2)
|
78
|
+
mini_mime (1.0.0)
|
79
|
+
mini_portile2 (2.3.0)
|
80
|
+
minitest (5.11.3)
|
81
|
+
nio4r (2.3.1)
|
82
|
+
nokogiri (1.8.2)
|
83
|
+
mini_portile2 (~> 2.3.0)
|
84
|
+
pry (0.11.3)
|
85
|
+
coderay (~> 1.1.0)
|
86
|
+
method_source (~> 0.9.0)
|
87
|
+
rack (2.0.5)
|
88
|
+
rack-test (1.0.0)
|
89
|
+
rack (>= 1.0, < 3)
|
90
|
+
rails (5.2.0)
|
91
|
+
actioncable (= 5.2.0)
|
92
|
+
actionmailer (= 5.2.0)
|
93
|
+
actionpack (= 5.2.0)
|
94
|
+
actionview (= 5.2.0)
|
95
|
+
activejob (= 5.2.0)
|
96
|
+
activemodel (= 5.2.0)
|
97
|
+
activerecord (= 5.2.0)
|
98
|
+
activestorage (= 5.2.0)
|
99
|
+
activesupport (= 5.2.0)
|
100
|
+
bundler (>= 1.3.0)
|
101
|
+
railties (= 5.2.0)
|
102
|
+
sprockets-rails (>= 2.0.0)
|
103
|
+
rails-dom-testing (2.0.3)
|
104
|
+
activesupport (>= 4.2.0)
|
105
|
+
nokogiri (>= 1.6)
|
106
|
+
rails-html-sanitizer (1.0.4)
|
107
|
+
loofah (~> 2.2, >= 2.2.2)
|
108
|
+
railties (5.2.0)
|
109
|
+
actionpack (= 5.2.0)
|
110
|
+
activesupport (= 5.2.0)
|
111
|
+
method_source
|
112
|
+
rake (>= 0.8.7)
|
113
|
+
thor (>= 0.18.1, < 2.0)
|
114
|
+
rake (10.5.0)
|
115
|
+
rspec-core (3.7.1)
|
116
|
+
rspec-support (~> 3.7.0)
|
117
|
+
rspec-expectations (3.7.0)
|
118
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
119
|
+
rspec-support (~> 3.7.0)
|
120
|
+
rspec-mocks (3.7.0)
|
121
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
122
|
+
rspec-support (~> 3.7.0)
|
123
|
+
rspec-rails (3.7.2)
|
124
|
+
actionpack (>= 3.0)
|
125
|
+
activesupport (>= 3.0)
|
126
|
+
railties (>= 3.0)
|
127
|
+
rspec-core (~> 3.7.0)
|
128
|
+
rspec-expectations (~> 3.7.0)
|
129
|
+
rspec-mocks (~> 3.7.0)
|
130
|
+
rspec-support (~> 3.7.0)
|
131
|
+
rspec-support (3.7.1)
|
132
|
+
simplecov (0.16.1)
|
133
|
+
docile (~> 1.1)
|
134
|
+
json (>= 1.8, < 3)
|
135
|
+
simplecov-html (~> 0.10.0)
|
136
|
+
simplecov-html (0.10.2)
|
137
|
+
sprockets (3.7.1)
|
138
|
+
concurrent-ruby (~> 1.0)
|
139
|
+
rack (> 1, < 3)
|
140
|
+
sprockets-rails (3.2.1)
|
141
|
+
actionpack (>= 4.0)
|
142
|
+
activesupport (>= 4.0)
|
143
|
+
sprockets (>= 3.0.0)
|
144
|
+
sqlite3 (1.3.13)
|
145
|
+
thor (0.20.0)
|
146
|
+
thread_safe (0.3.6)
|
147
|
+
tzinfo (1.2.5)
|
148
|
+
thread_safe (~> 0.1)
|
149
|
+
websocket-driver (0.7.0)
|
150
|
+
websocket-extensions (>= 0.1.0)
|
151
|
+
websocket-extensions (0.1.3)
|
152
|
+
|
153
|
+
PLATFORMS
|
154
|
+
ruby
|
155
|
+
|
156
|
+
DEPENDENCIES
|
157
|
+
appraisal (~> 2.1.0)
|
158
|
+
bundler (~> 1.16)
|
159
|
+
database_cleaner (~> 1.7.0)
|
160
|
+
onsi!
|
161
|
+
pry (~> 0.11.3)
|
162
|
+
rake (~> 10.0)
|
163
|
+
rspec-rails (~> 3.7.2)
|
164
|
+
simplecov (~> 0.15)
|
165
|
+
sqlite3 (~> 1.3.10)
|
166
|
+
|
167
|
+
BUNDLED WITH
|
168
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Skylar Schipper
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Onsi
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/onsi`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'onsi'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install onsi
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/onsi.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'onsi'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/onsi.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Onsi
|
4
|
+
module Controller
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def render_resource(resource, opts = {})
|
8
|
+
version = opts.delete(:version) || Model::DEFAULT_API_VERSION
|
9
|
+
payload = format_resource(resource, version)
|
10
|
+
render_options = {}
|
11
|
+
render_options[:json] = { data: payload }
|
12
|
+
render_options.merge!(opts)
|
13
|
+
render(render_options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def format_resource(resource, version)
|
17
|
+
case resource
|
18
|
+
when Onsi::Resource
|
19
|
+
resource
|
20
|
+
when Enumerable
|
21
|
+
resource.map { |res| format_resource(res, version) }
|
22
|
+
else
|
23
|
+
Onsi::Resource.new(resource, version)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/onsi/errors.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Onsi
|
2
|
+
module Errors
|
3
|
+
class BaseError < StandardError; end
|
4
|
+
|
5
|
+
class UnknownVersionError < BaseError
|
6
|
+
attr_reader :klass, :version
|
7
|
+
|
8
|
+
def initialize(klass, version)
|
9
|
+
@klass = klass
|
10
|
+
@version = version
|
11
|
+
end
|
12
|
+
|
13
|
+
def message
|
14
|
+
"Unsupported version #{version} for #{klass.name}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/onsi/model.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Onsi
|
4
|
+
module Model
|
5
|
+
DEFAULT_API_VERSION = :v1
|
6
|
+
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def api_render(version = DEFAULT_API_VERSION, &block)
|
11
|
+
api_renderer(version).instance_exec(&block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def api_renderer(version = DEFAULT_API_VERSION, for_render: false)
|
15
|
+
@api_renderer ||= {}
|
16
|
+
if for_render
|
17
|
+
raise Errors::UnknownVersionError.new(self, version) if @api_renderer[version].nil?
|
18
|
+
else
|
19
|
+
@api_renderer[version] ||= ModelRenderer.new
|
20
|
+
end
|
21
|
+
@api_renderer[version]
|
22
|
+
end
|
23
|
+
|
24
|
+
class ModelRenderer
|
25
|
+
DATE_FORMAT = '%Y-%m-%d'.freeze
|
26
|
+
DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'.freeze
|
27
|
+
|
28
|
+
def initialize
|
29
|
+
@attributes = {}
|
30
|
+
@relationships = {}
|
31
|
+
@metadata = {}
|
32
|
+
end
|
33
|
+
|
34
|
+
def type(name = nil)
|
35
|
+
@type = name if name
|
36
|
+
@type
|
37
|
+
end
|
38
|
+
|
39
|
+
def attribute(name, &block)
|
40
|
+
@attributes[name.to_sym] = block || name
|
41
|
+
end
|
42
|
+
|
43
|
+
def relationship(name, type, &block)
|
44
|
+
@relationships[name.to_sym] = { type: type, attr: block || name }
|
45
|
+
end
|
46
|
+
|
47
|
+
def meta(name, &block)
|
48
|
+
@metadata[name.to_sym] = block
|
49
|
+
end
|
50
|
+
|
51
|
+
def render_attributes(object)
|
52
|
+
@attributes.each_with_object({}) do |(key, value), attrs|
|
53
|
+
val = value.respond_to?(:call) ? object.instance_exec(&value) : object.send(value)
|
54
|
+
attrs[key.to_s] = format_attribute(val)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def render_relationships(object)
|
59
|
+
@relationships.each_with_object({}) do |(key, value), rels|
|
60
|
+
render_relationship_entry(object, key, value, rels)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def render_metadata(object)
|
65
|
+
@metadata.each_with_object({}) do |(key, block), meta|
|
66
|
+
meta[key.to_s] = object.instance_exec(&block)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def render_relationship_entry(object, key, value, rels)
|
73
|
+
attr = value[:attr]
|
74
|
+
relationship = get_relationship_value(attr, object)
|
75
|
+
data = format_relationship(relationship, value)
|
76
|
+
rels[key.to_s] = {
|
77
|
+
'data' => data
|
78
|
+
}
|
79
|
+
end
|
80
|
+
|
81
|
+
def get_relationship_value(attr, object)
|
82
|
+
if attr.respond_to?(:call)
|
83
|
+
object.instance_exec(&attr)
|
84
|
+
else
|
85
|
+
object.send("#{attr}_id")
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def format_relationship(relationship, value)
|
90
|
+
case relationship
|
91
|
+
when Array
|
92
|
+
relationship.map { |v| { 'type' => value[:type].to_s, 'id' => v.to_s } }
|
93
|
+
else
|
94
|
+
{
|
95
|
+
'type' => value[:type].to_s,
|
96
|
+
'id' => relationship.to_s
|
97
|
+
}
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def format_attribute(value)
|
102
|
+
case value
|
103
|
+
when Date
|
104
|
+
value.strftime(DATE_FORMAT)
|
105
|
+
when DateTime, Time
|
106
|
+
value.utc.strftime(DATETIME_FORMAT)
|
107
|
+
when String
|
108
|
+
value.presence
|
109
|
+
else
|
110
|
+
value
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
data/lib/onsi/params.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
module Onsi
|
2
|
+
class Params
|
3
|
+
class RelationshipNotFound < StandardError
|
4
|
+
attr_reader :key
|
5
|
+
|
6
|
+
def initialize(message, key)
|
7
|
+
super(message)
|
8
|
+
@key = key
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def parse(params, attributes = [], relationships = [])
|
14
|
+
data = params.require(:data)
|
15
|
+
data.require(:type)
|
16
|
+
attrs = permit_attributes(data, attributes)
|
17
|
+
relas = permit_relationships(data, relationships)
|
18
|
+
new(attrs, relas)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def permit_attributes(data, attributes)
|
24
|
+
return {} if Array(attributes).empty?
|
25
|
+
data.require(:attributes).permit(*attributes)
|
26
|
+
end
|
27
|
+
|
28
|
+
def permit_relationships(data, relationships)
|
29
|
+
return {} if Array(relationships).empty?
|
30
|
+
rels = data.require(:relationships)
|
31
|
+
{}.tap do |obj|
|
32
|
+
relationships.each do |name|
|
33
|
+
next if rels[name].nil?
|
34
|
+
resource = rels.require(name).require(:data)
|
35
|
+
case resource
|
36
|
+
when Array
|
37
|
+
ids = resource.map { |r| parse_relationship(r).last }
|
38
|
+
obj["#{name.to_s.singularize}_ids".to_sym] = ids
|
39
|
+
else
|
40
|
+
_type, id = parse_relationship(resource)
|
41
|
+
obj["#{name}_id".to_sym] = id
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def parse_relationship(data)
|
48
|
+
[
|
49
|
+
data.require(:type),
|
50
|
+
data.require(:id)
|
51
|
+
]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
attr_reader :attributes, :relationships
|
56
|
+
|
57
|
+
def initialize(attributes, relationships)
|
58
|
+
@attributes = attributes
|
59
|
+
@relationships = relationships
|
60
|
+
end
|
61
|
+
|
62
|
+
def flatten
|
63
|
+
attributes.to_h.merge(relationships.to_h).with_indifferent_access
|
64
|
+
end
|
65
|
+
|
66
|
+
def safe_fetch(key)
|
67
|
+
yield(@relationships[key])
|
68
|
+
rescue ActiveRecord::RecordNotFound
|
69
|
+
raise RelationshipNotFound.new("Can't find relationship #{key}", key)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Onsi
|
2
|
+
class Resource
|
3
|
+
attr_reader :object, :version
|
4
|
+
|
5
|
+
def initialize(object, version = Model::DEFAULT_API_VERSION)
|
6
|
+
@object = object
|
7
|
+
@version = version
|
8
|
+
end
|
9
|
+
|
10
|
+
def as_json(_opts = {})
|
11
|
+
{}.tap do |root|
|
12
|
+
root['type'] = type
|
13
|
+
root['id'] = object.id.to_s
|
14
|
+
root['attributes'] = generate_attributes
|
15
|
+
rela = generate_relationships
|
16
|
+
root['relationships'] = rela if rela.any?
|
17
|
+
meta = generate_metadata
|
18
|
+
root['meta'] = meta if meta.any?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def type
|
25
|
+
object.class.api_renderer(version, for_render: true).type || object.class.name.underscore
|
26
|
+
end
|
27
|
+
|
28
|
+
def generate_attributes
|
29
|
+
object.class.api_renderer(version, for_render: true).render_attributes(object)
|
30
|
+
end
|
31
|
+
|
32
|
+
def generate_relationships
|
33
|
+
object.class.api_renderer(version, for_render: true).render_relationships(object)
|
34
|
+
end
|
35
|
+
|
36
|
+
def generate_metadata
|
37
|
+
object.class.api_renderer(version, for_render: true).render_metadata(object)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/onsi/version.rb
ADDED
data/onsi.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path('lib', __dir__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'onsi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'onsi'
|
8
|
+
spec.version = Onsi::VERSION
|
9
|
+
spec.authors = ['Skylar Schipper']
|
10
|
+
spec.email = ['me@skylarsch.com']
|
11
|
+
|
12
|
+
spec.summary = 'Format JSON API Responses'
|
13
|
+
spec.description = 'Format JSON API responses and parse inbound requests.'
|
14
|
+
spec.homepage = 'https://github.com/skylarsch/onsi'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.required_ruby_version = '>= 2.3'
|
25
|
+
|
26
|
+
spec.add_dependency 'rails', '>= 5.0', '< 6.0'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'appraisal', '~> 2.1.0'
|
29
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
30
|
+
spec.add_development_dependency 'database_cleaner', '~> 1.7.0'
|
31
|
+
spec.add_development_dependency 'pry', '~> 0.11.3'
|
32
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
33
|
+
spec.add_development_dependency 'rspec-rails', '~> 3.7.2'
|
34
|
+
spec.add_development_dependency 'simplecov', '~> 0.15'
|
35
|
+
spec.add_development_dependency 'sqlite3', '~> 1.3.10'
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: onsi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Skylar Schipper
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '5.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: appraisal
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 2.1.0
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.1.0
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.16'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.16'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: database_cleaner
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 1.7.0
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 1.7.0
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: pry
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 0.11.3
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 0.11.3
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rake
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '10.0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '10.0'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: rspec-rails
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 3.7.2
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 3.7.2
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: simplecov
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - "~>"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0.15'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - "~>"
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0.15'
|
131
|
+
- !ruby/object:Gem::Dependency
|
132
|
+
name: sqlite3
|
133
|
+
requirement: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - "~>"
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: 1.3.10
|
138
|
+
type: :development
|
139
|
+
prerelease: false
|
140
|
+
version_requirements: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - "~>"
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: 1.3.10
|
145
|
+
description: Format JSON API responses and parse inbound requests.
|
146
|
+
email:
|
147
|
+
- me@skylarsch.com
|
148
|
+
executables: []
|
149
|
+
extensions: []
|
150
|
+
extra_rdoc_files: []
|
151
|
+
files:
|
152
|
+
- ".gitignore"
|
153
|
+
- ".rspec"
|
154
|
+
- ".rubocop.yml"
|
155
|
+
- ".travis.yml"
|
156
|
+
- Gemfile
|
157
|
+
- Gemfile.lock
|
158
|
+
- LICENSE.txt
|
159
|
+
- README.md
|
160
|
+
- Rakefile
|
161
|
+
- bin/console
|
162
|
+
- bin/setup
|
163
|
+
- lib/onsi.rb
|
164
|
+
- lib/onsi/controller.rb
|
165
|
+
- lib/onsi/errors.rb
|
166
|
+
- lib/onsi/model.rb
|
167
|
+
- lib/onsi/params.rb
|
168
|
+
- lib/onsi/resource.rb
|
169
|
+
- lib/onsi/version.rb
|
170
|
+
- onsi.gemspec
|
171
|
+
homepage: https://github.com/skylarsch/onsi
|
172
|
+
licenses:
|
173
|
+
- MIT
|
174
|
+
metadata: {}
|
175
|
+
post_install_message:
|
176
|
+
rdoc_options: []
|
177
|
+
require_paths:
|
178
|
+
- lib
|
179
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
180
|
+
requirements:
|
181
|
+
- - ">="
|
182
|
+
- !ruby/object:Gem::Version
|
183
|
+
version: '2.3'
|
184
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
185
|
+
requirements:
|
186
|
+
- - ">="
|
187
|
+
- !ruby/object:Gem::Version
|
188
|
+
version: '0'
|
189
|
+
requirements: []
|
190
|
+
rubyforge_project:
|
191
|
+
rubygems_version: 2.7.6
|
192
|
+
signing_key:
|
193
|
+
specification_version: 4
|
194
|
+
summary: Format JSON API Responses
|
195
|
+
test_files: []
|