jsonapi-serializers 0.16.1 → 0.16.2

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: 7f9584848d438371a8e74a93af4cd1e2ef0dbe2f
4
- data.tar.gz: 997e23a37237cd66ef6eaa5776c0512041178be3
3
+ metadata.gz: 9cb70fe5c20fe83fc3fffd76988b1f1ef2501182
4
+ data.tar.gz: dfc3d9775ef13c60d94caee9ee43b1671899030d
5
5
  SHA512:
6
- metadata.gz: ac2d3b371c2c94bdbacd2d77f2fb63d3b9e0b5f5e29daca35bffaf8152a2dbc3c8b68081c2eb3c4981ebb79f7af5c85f119b769cf68028d17de3f71d57515854
7
- data.tar.gz: 3a5789b22a8508cd3ee6641fe2753490321087592852f670a7824ed59d6f3a67b6d6a2b3ed4d1a0d55380bf3948a3b0b9cb4ea502d18972a2aee522d7f39ceb2
6
+ metadata.gz: d4a5e0debc18ca03b11b2269c7ee15b60f2b23596ac5152dc839c445a0edee99ad9a787a6cc13487664ae0b3b923621703875d562a17671b3c40c0d8a35ff6b7
7
+ data.tar.gz: e93e5223aa956c33c5a4d26f942b4022b501f71ab1aaffbbdeca0f9683fcbdccf2e76d465142605e6c74e73829ced9217b7bd558723114c9c561ae115f94e94b
data/README.md CHANGED
@@ -193,7 +193,7 @@ Many other formatting and customizations are possible by overriding any of the f
193
193
  # Override this to customize the JSON:API "id" for this object.
194
194
  # Always return a string from this method to conform with the JSON:API spec.
195
195
  def id
196
- object.id.to_s
196
+ object.slug.to_s
197
197
  end
198
198
  ```
199
199
  ```ruby
@@ -202,7 +202,7 @@ end
202
202
  # per the spec naming recommendations: http://jsonapi.org/recommendations/#naming
203
203
  # For example, 'MyApp::LongCommment' will become the 'long-comments' type.
204
204
  def type
205
- object.class.name.demodulize.tableize.dasherize
205
+ 'long-comments'
206
206
  end
207
207
  ```
208
208
  ```ruby
@@ -699,9 +699,11 @@ ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime::Type.lookup(JSONAPI::MIMETYP
699
699
  end
700
700
 
701
701
  # Rails 5 moved DEFAULT_PARSERS
702
- ActionDispatch::Http::Parameters::DEFAULT_PARSERS[Mime::Type.lookup(JSONAPI::MIMETYPE)] = lambda do |body|
702
+ ActionDispatch::Http::Parameters::DEFAULT_PARSERS[:api_json] = lambda do |body|
703
703
  JSON.parse(body)
704
704
  end
705
+ ActionDispatch::Request.parameter_parsers = ActionDispatch::Request::DEFAULT_PARSERS
706
+
705
707
  ```
706
708
 
707
709
  ## Sinatra example
@@ -723,7 +725,7 @@ The important takeaways here are that:
723
725
  ```ruby
724
726
  require 'sequel'
725
727
  require 'sinatra/base'
726
- require 'json/ext'
728
+ require 'json'
727
729
  require 'jsonapi-serializers'
728
730
 
729
731
  class Post < Sequel::Model
@@ -759,20 +761,21 @@ end
759
761
  module Api
760
762
  class V1 < Sinatra::Base
761
763
  configure do
762
- mime_type :jsonapi, 'application/vnd.api+json'
764
+ mime_type :api_json, 'application/vnd.api+json'
763
765
 
764
766
  set :database, Sequel.connect
765
767
  end
766
768
 
767
769
  helpers do
768
- # Parse the body of the request depending on its content-type:
769
770
  def parse_request_body
770
- request.body.rewind
771
+ return unless request.body.respond_to?(:size) &&
772
+ request.body.size > 0
773
+
774
+ halt 415 unless request.content_type &&
775
+ request.content_type[/^[^;]+/] == mime_type(:api_json)
771
776
 
772
- case request.content_type && request.content_type[/^([^;]+)/]
773
- when /json$/, /javascript$/
774
- JSON.parse(request.body.read, symbolize_names: true)
775
- end
777
+ request.body.rewind
778
+ JSON.parse(request.body.read, symbolize_names: true)
776
779
  end
777
780
 
778
781
  # Convenience methods for serializing models:
@@ -789,16 +792,17 @@ module Api
789
792
  end
790
793
 
791
794
  before do
792
- @data = parse_request_body if request.body.size > 0
795
+ halt 406 unless request.preferred_type.entry == mime_type(:api_json)
796
+ @data = parse_request_body
797
+ content_type :api_json
793
798
  end
794
799
 
795
- get '/posts', provides: :jsonapi do
800
+ get '/posts' do
796
801
  posts = Post.all
797
- not_found if posts.empty?
798
802
  serialize_models(posts).to_json
799
803
  end
800
804
 
801
- get '/posts/:id', provides: :jsonapi do
805
+ get '/posts/:id' do
802
806
  post = Post[params[:id].to_i]
803
807
  not_found if post.nil?
804
808
  serialize_model(post, include: 'comments').to_json
@@ -807,6 +811,9 @@ module Api
807
811
  end
808
812
  ```
809
813
 
814
+ See also: [Sinja](https://github.com/mwpastore/sinja), which extends Sinatra
815
+ and leverages jsonapi-serializers to provide a JSON:API framework.
816
+
810
817
  ## Changelog
811
818
 
812
819
  See [Releases](https://github.com/fotinakis/jsonapi-serializers/releases).
@@ -22,6 +22,10 @@ module JSONAPI
22
22
  end
23
23
 
24
24
  module InstanceMethods
25
+ @@class_names = {}
26
+ @@formatted_attribute_names = {}
27
+ @@unformatted_attribute_names = {}
28
+
25
29
  attr_accessor :object
26
30
  attr_accessor :context
27
31
  attr_accessor :base_url
@@ -48,19 +52,22 @@ module JSONAPI
48
52
  # per the spec naming recommendations: http://jsonapi.org/recommendations/#naming
49
53
  # For example, 'MyApp::LongCommment' will become the 'long-comments' type.
50
54
  def type
51
- object.class.name.demodulize.tableize.dasherize
55
+ class_name = object.class.name
56
+ @@class_names[class_name] ||= class_name.demodulize.tableize.dasherize.freeze
52
57
  end
53
58
 
54
59
  # Override this to customize how attribute names are formatted.
55
60
  # By default, attribute names are dasherized per the spec naming recommendations:
56
61
  # http://jsonapi.org/recommendations/#naming
57
62
  def format_name(attribute_name)
58
- attribute_name.to_s.dasherize
63
+ attr_name = attribute_name.to_s
64
+ @@formatted_attribute_names[attr_name] ||= attr_name.dasherize.freeze
59
65
  end
60
66
 
61
67
  # The opposite of format_name. Override this if you override format_name.
62
68
  def unformat_name(attribute_name)
63
- attribute_name.to_s.underscore
69
+ attr_name = attribute_name.to_s
70
+ @@unformatted_attribute_names[attr_name] ||= attr_name.underscore.freeze
64
71
  end
65
72
 
66
73
  # Override this to provide resource-object jsonapi object containing the version in use.
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Serializer
3
- VERSION = '0.16.1'
3
+ VERSION = '0.16.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-serializers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.1
4
+ version: 0.16.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Fotinakis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-18 00:00:00.000000000 Z
11
+ date: 2017-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -137,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
137
  version: '0'
138
138
  requirements: []
139
139
  rubyforge_project:
140
- rubygems_version: 2.6.7
140
+ rubygems_version: 2.4.8
141
141
  signing_key:
142
142
  specification_version: 4
143
143
  summary: Pure Ruby readonly serializers for the JSON:API spec.