restful-jsonapi 0.0.8

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 013af3832e22e46c18944a42cbde0eaf13ea23af
4
+ data.tar.gz: be9766c381db40bc2d14bcf3e2eff03253831f77
5
+ SHA512:
6
+ metadata.gz: d98c93fbd4669cba8d40649870496cad0a2884f30052047e94e51751649a46f4cccdad95161baaa83992507cb9b8d0759eb245219d119c4392bb662c74da6af9
7
+ data.tar.gz: 8dcbb7636b662c427bdc7c07f3d43c178b47462c674b04171c42f9d3fcf38c17b1607c781a4b3e936d66719bf5bb0d92aeedc0a1b04de5305a65f6b73a1fae72
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in restful-jsonapi.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Netflix, Inc.
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.
22
+
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Restful::Jsonapi
2
+
3
+ A temporary monkeypatch for JSONAPI support, both in request payload, and serializing the type without a namespace.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'restful-jsonapi'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install restful-jsonapi
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ class MovieTypesController < ApplicationController
25
+
26
+ ...
27
+
28
+ private
29
+
30
+ def movie_type_params
31
+ restify_param(:movie_type).require(:movie_type).permit(
32
+ :id,
33
+ :name,
34
+ created_user: [
35
+ :id,
36
+ :email
37
+ ]
38
+ end
39
+ end
40
+ ```
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( https://github.com/[my-github-username]/restful-jsonapi/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,11 @@
1
+ require "restful/jsonapi/version"
2
+ require 'restful/jsonapi/railtie' if defined?(Rails)
3
+ require 'restful/jsonapi/restify_param'
4
+ require 'restful/jsonapi/serializable_errors'
5
+ require 'restful/jsonapi/active_model_serializer'
6
+
7
+ module Restful
8
+ module Jsonapi
9
+ # Good job!
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Restful
2
+ module Jsonapi
3
+ module ActiveModelSerializer
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ def type
8
+ object.class.model_name.name.demodulize.pluralize.underscore
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ module Restful
2
+ module Jsonapi
3
+ class Railtie < Rails::Railtie
4
+ initializer "restful-jsonapi.action_controller" do
5
+ ActiveSupport.on_load(:action_controller) do
6
+ puts "Extending #{self} with Restful::Jsonapi::RestifyParam"
7
+ include Restful::Jsonapi::RestifyParam
8
+
9
+ puts "Extending #{self} with Restful::Jsonapi::SerializableErrors"
10
+ include Restful::Jsonapi::SerializableErrors
11
+ end
12
+ end
13
+
14
+ initializer "restful-jsonapi.active_model_serializer" do
15
+ ActiveModel::Serializer.class_eval do
16
+ puts "Extending #{self} with Restful::Jsonapi::ActiveModelSerializer"
17
+ include Restful::Jsonapi::ActiveModelSerializer
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,57 @@
1
+ module Restful
2
+ module Jsonapi
3
+ module RestifyParam
4
+ extend ActiveSupport::Concern
5
+
6
+ def restify_param(param_key)
7
+ ActionController::Parameters.new(param_key => restify_value(param_key))
8
+ end
9
+
10
+ private
11
+
12
+ def restify_value(param_key, value = params)
13
+ if value == params
14
+ value = params.clone[:data]
15
+ end
16
+ if value.delete(:type).underscore == param_key.to_s.pluralize
17
+ new_params = ActionController::Parameters.new
18
+ attributes = value.has_key?(:attributes) ? value[:attributes] : value
19
+ attributes.merge!(id: value[:id]) if value[:id]
20
+ attributes.transform_keys!(&:underscore)
21
+ new_params.merge!(attributes)
22
+ if value.has_key? :relationships
23
+ value[:relationships].each do |k,v|
24
+ if k.pluralize != k
25
+
26
+ # belongs to relationship
27
+ unless v[:data].nil?
28
+ new_params["#{k}_id"] = v[:data][:id]
29
+ else
30
+ new_params["#{k}_id"] = nil
31
+ end
32
+
33
+ # has many relationship
34
+ elsif v[:data].present?
35
+ if v[:data].reject{|data|data.has_key? :attributes}.blank?
36
+ relationship_key = k.to_s.underscore+"_attributes"
37
+ new_params[relationship_key] ||= []
38
+ v[:data].each do |vv|
39
+ new_params[relationship_key].push restify_value(k,vv)
40
+ end unless v[:data].nil?
41
+ else
42
+ relationship_key = k.to_s.singularize.underscore+"_ids"
43
+ new_params[relationship_key] ||= []
44
+ v[:data].each do |vv|
45
+ new_params[relationship_key].push vv[:id]
46
+ end unless v[:data].nil?
47
+ end
48
+ end
49
+ end
50
+ end
51
+ new_params
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,23 @@
1
+ module Restful
2
+ module Jsonapi
3
+ module SerializableErrors
4
+ extend ActiveSupport::Concern
5
+
6
+ def serializable_errors(object)
7
+ json = {}
8
+
9
+ errors = object.errors
10
+
11
+ new_hash = errors.to_hash(true).map do |k, v|
12
+ v.map do |msg|
13
+ { id: k, title: msg }
14
+ end
15
+ end.flatten
16
+
17
+ json[:errors] = new_hash
18
+
19
+ json
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module Restful
2
+ module Jsonapi
3
+ VERSION = "0.0.8"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'restful/jsonapi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "restful-jsonapi"
8
+ spec.version = Restful::Jsonapi::VERSION
9
+ spec.authors = ["Zach Wentz"]
10
+ spec.email = ["zwentz@netflix.com"]
11
+ spec.summary = %q{Nice type and params for temporary JSONAPI support.}
12
+ spec.description = %q{Nice type and params for temporary JSONAPI support.}
13
+ spec.homepage = "https://github.com/Netflix/restful-jsonapi"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: restful-jsonapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
+ platform: ruby
6
+ authors:
7
+ - Zach Wentz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Nice type and params for temporary JSONAPI support.
42
+ email:
43
+ - zwentz@netflix.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - lib/restful/jsonapi.rb
54
+ - lib/restful/jsonapi/active_model_serializer.rb
55
+ - lib/restful/jsonapi/railtie.rb
56
+ - lib/restful/jsonapi/restify_param.rb
57
+ - lib/restful/jsonapi/serializable_errors.rb
58
+ - lib/restful/jsonapi/version.rb
59
+ - restful-jsonapi.gemspec
60
+ homepage: https://github.com/Netflix/restful-jsonapi
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.2.2
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Nice type and params for temporary JSONAPI support.
84
+ test_files: []