json_api_toolbox 0.5.3 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 710c3ff50ec8bf19092d3c77dfc61873879e9c82
4
- data.tar.gz: dbbefba1e47f36704523c847496cd9c78f7be687
3
+ metadata.gz: 9e89034246f639df9db8ed22e59a9c36c6106d6e
4
+ data.tar.gz: 3b6aa85aeb7d1c5b637f2dc1f23591ec0524bfd4
5
5
  SHA512:
6
- metadata.gz: 425af0b5b6f1c0a360516a3942c324767e77040ef0fad696024abcffef60efdaee174fe6b7bb9c92e79d64a70df944a9fc0c0a30c14cad94c41e1e78b508e340
7
- data.tar.gz: 8b6178f6b286d0f694dd8c6a8e9eb83f63e54904146b690193488bc7123f8b6adf3b8c6746b3fd118de13d678a01174f879ce5e1a01f514d5564e1eff1f3f938
6
+ metadata.gz: 419b604717ac0006a07d65054f74449930858dca63d7ee3b2826d8dc0506bc3a5df62a9d7779d37d0bbc009ba743fbab3260cbc9d796ae738efe223d190c91e4
7
+ data.tar.gz: be81ed014c3e6a760ef749637ccf4c88c6b81d557de0ac0ae4640867f3620e2446e31c9c5da8b5e68b38fa6435b544c8a5ddec802dd4df4659491f105b777d3a
data/README.md CHANGED
@@ -36,6 +36,21 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/abacha
36
36
 
37
37
  ## Examples
38
38
 
39
+ #Renderizable
40
+
41
+ Render common
42
+ ```ruby
43
+ render_object(Quota.all)
44
+ ```
45
+
46
+ To render an object with a custom serializable, you will need to send a Hash
47
+ that contains a key called 'class' and its values must be another Hash. In this
48
+ new Hash, the key must be 'Hash' and the value must be your Serializable.
49
+
50
+ ```ruby
51
+ render_object(QuotaGap.all, class: { Hash: SerializableQuotaGap })
52
+ ```
53
+
39
54
  ##ShareExamplesForControllers
40
55
 
41
56
  it_behaves_like 'a http method'
@@ -0,0 +1,20 @@
1
+ image: ruby:2.4.1
2
+
3
+ pipelines:
4
+ default:
5
+ - step:
6
+ script:
7
+ - git archive --remote=git@bitbucket.org:guideinvestimentos/rails_defaults.git HEAD .rubocop.yml | tar -x
8
+ - bundle install
9
+ - gem install rubocop
10
+ - rspec -fdoc
11
+ - rubocop .
12
+
13
+ branches:
14
+ master:
15
+ - step:
16
+ script:
17
+ - gem build json_api_toolbox.gemspec
18
+ - curl -u $RUBYGEMS_USERNAME:$RUBYGEMS_PASSWORD https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials
19
+ - chmod 0600 ~/.gem/credentials
20
+ - gem push $(ls *.gem)
@@ -1,29 +1,31 @@
1
+ # frozen_string_literal: true
1
2
  # coding: utf-8
2
- lib = File.expand_path("../lib", __FILE__)
3
+
4
+ lib = File.expand_path('../lib', __FILE__)
3
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "json_api_toolbox/version"
6
+ require 'json_api_toolbox/version'
5
7
 
6
8
  Gem::Specification.new do |spec|
7
- spec.name = "json_api_toolbox"
9
+ spec.name = 'json_api_toolbox'
8
10
  spec.version = JsonApiToolbox::VERSION
9
- spec.authors = ["Adriano Bacha"]
10
- spec.email = ["abacha@gmail.com"]
11
+ spec.authors = ['Adriano Bacha']
12
+ spec.email = ['abacha@gmail.com']
11
13
 
12
- spec.summary = %q{Json API usefull tools}
13
- spec.homepage = "http://bitbucket.org/guideinvestimentos/json_api_toolbox"
14
+ spec.summary = 'Json API usefull tools'
15
+ spec.homepage = 'http://bitbucket.org/guideinvestimentos/json_api_toolbox'
14
16
 
15
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
18
  f.match(%r{^(test|spec|features)/})
17
19
  end
18
- spec.require_paths = ["lib"]
20
+ spec.require_paths = ['lib']
19
21
 
20
- spec.add_dependency "activesupport", ">= 4.0"
21
- spec.add_dependency "jsonapi-rails", ">= 0.2"
22
- spec.add_dependency "rspec", "~> 3.0"
22
+ spec.add_dependency 'activesupport', '>= 4.0'
23
+ spec.add_dependency 'jsonapi-rails', '>= 0.3.1'
24
+ spec.add_dependency 'rspec', '~> 3.0'
23
25
 
24
- spec.add_development_dependency "bundler", "~> 1.15"
25
- spec.add_development_dependency "rake", "~> 10.0"
26
- spec.add_development_dependency "pry-byebug"
27
- spec.add_development_dependency "json"
28
- spec.add_development_dependency "pry"
26
+ spec.add_development_dependency 'bundler', '~> 1.15'
27
+ spec.add_development_dependency 'json'
28
+ spec.add_development_dependency 'pry'
29
+ spec.add_development_dependency 'pry-byebug'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
29
31
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'rspec'
3
4
 
4
5
  module JsonApiToolbox
@@ -30,7 +31,8 @@ module JsonApiToolbox
30
31
  end
31
32
  end
32
33
 
33
- RSpec.shared_examples 'a get with jsonapi with default value of' do |model|
34
+ RSpec.
35
+ shared_examples 'a get with jsonapi with default value of' do |model|
34
36
  let(:attributes) { data['attributes'].keys }
35
37
  let(:model_keys) do
36
38
  enums = []
@@ -51,7 +53,8 @@ module JsonApiToolbox
51
53
  end
52
54
  end
53
55
 
54
- RSpec.shared_examples 'a json api response with all relations of' do |model|
56
+ RSpec.
57
+ shared_examples 'a json api response with all relations of' do |model|
55
58
  let(:attributes) { data['relationships'].keys }
56
59
  let(:relation_keys) { model.reflections.keys }
57
60
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module JsonApiToolbox
2
- VERSION = "0.5.3"
4
+ VERSION = '0.6.1'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/inflector'
2
4
  require_relative 'json_api_toolbox/spec_support/shared_examples_for_controllers'
3
5
  require 'renderizable'
data/lib/postable.rb CHANGED
@@ -2,33 +2,35 @@
2
2
 
3
3
  module JsonApiToolbox
4
4
  module Postable
5
- def self.normalize_post(hash, model)
6
- associations = model.reflect_on_all_associations.map(&:name)
7
- normalized_hash = hash.map do |key, value|
8
- child_model = self.association_class(model, key) if value.is_a? Hash
9
- key = associations.include?(key.to_sym) ? "#{key}_attributes" : key
10
- hash_value = check_child_association(value, key, model, child_model)
11
- [key, hash_value]
5
+ class << self
6
+ def normalize_post(hash, model)
7
+ associations = model.reflect_on_all_associations.map(&:name)
8
+ normalized_hash = hash.map do |key, value|
9
+ child_model = association_class(model, key) if value.is_a? Hash
10
+ key = associations.include?(key.to_sym) ? "#{key}_attributes" : key
11
+ hash_value = check_child_association(value, key, model, child_model)
12
+ [key, hash_value]
13
+ end
14
+ Hash[normalized_hash]
12
15
  end
13
- Hash[normalized_hash]
14
- end
15
16
 
16
- private
17
+ private_class_method
17
18
 
18
- def self.association_class(model, key)
19
- association = model.reflect_on_all_associations.find do |association|
20
- association.name.to_s == key.to_s
19
+ def association_class(model, key)
20
+ association = model.reflect_on_all_associations.find do |association_i|
21
+ association_i.name.to_s == key.to_s
22
+ end
23
+ return false unless association
24
+ class_name = (association.options.symbolize_keys[:class_name] || key)
25
+ ActiveSupport::Inflector.singularize(class_name).camelize.constantize
21
26
  end
22
- return false unless association
23
- class_name = (association.options.symbolize_keys[:class_name] || key)
24
- ActiveSupport::Inflector.singularize(class_name).camelize.constantize
25
- end
26
27
 
27
- def self.check_child_association(value, key, model, child_model)
28
- if value.is_a?(Hash) && !model.has_attribute?(key)
29
- self.normalize_post(value, child_model)
30
- else
31
- value
28
+ def check_child_association(value, key, model, child_model)
29
+ if value.is_a?(Hash) && !model.has_attribute?(key)
30
+ normalize_post(value, child_model)
31
+ else
32
+ value
33
+ end
32
34
  end
33
35
  end
34
36
  end
data/lib/renderizable.rb CHANGED
@@ -2,21 +2,11 @@
2
2
 
3
3
  module JsonApiToolbox
4
4
  module Renderizable
5
- def render_with_errors(object_errors)
6
- errors = object_errors.details.map do |field, error_details|
7
- error_details.each_with_index.map do |item, error_i|
8
- build_line(item[:error], field, object_errors.messages[field][error_i])
9
- end
10
- end
11
-
12
- render json: { errors: errors }, status: 422
13
- end
14
-
15
- def render_object(object)
5
+ def render_object(object, options = {})
16
6
  if object.respond_to?(:errors) && object.errors.any?
17
- render_with_errors(object.errors)
7
+ render({ jsonapi_errors: object.errors }.merge(options))
18
8
  else
19
- render jsonapi: object, include: includes_params
9
+ render({ jsonapi: object, include: includes_params }.merge(options))
20
10
  end
21
11
  end
22
12
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_toolbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adriano Bacha
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-13 00:00:00.000000000 Z
11
+ date: 2017-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0.2'
33
+ version: 0.3.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0.2'
40
+ version: 0.3.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -67,21 +67,21 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.15'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rake
70
+ name: json
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '10.0'
75
+ version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '10.0'
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: pry-byebug
84
+ name: pry
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: json
98
+ name: pry-byebug
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -109,19 +109,19 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: pry
112
+ name: rake
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - ">="
115
+ - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '0'
117
+ version: '10.0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - ">="
122
+ - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '0'
124
+ version: '10.0'
125
125
  description:
126
126
  email:
127
127
  - abacha@gmail.com
@@ -136,6 +136,7 @@ files:
136
136
  - Gemfile
137
137
  - README.md
138
138
  - Rakefile
139
+ - bitbucket-pipelines.yml
139
140
  - json_api_toolbox.gemspec
140
141
  - lib/enums.rb
141
142
  - lib/json_api_toolbox.rb
@@ -162,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
163
  version: '0'
163
164
  requirements: []
164
165
  rubyforge_project:
165
- rubygems_version: 2.6.11
166
+ rubygems_version: 2.6.13
166
167
  signing_key:
167
168
  specification_version: 4
168
169
  summary: Json API usefull tools