jsonapi-utils 0.1.3 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 061fed9fa47dbb57c871087a1dc759c62432e08b
4
- data.tar.gz: ccd60a2cb0094254c59beb832725a88189b0d826
3
+ metadata.gz: fbc9435ea77e7a938e5c3cd34b701e48509294aa
4
+ data.tar.gz: af2c51ee9c5bb6f5baffa1aa8bec5706befd546f
5
5
  SHA512:
6
- metadata.gz: e9c65099d501f11f4559c84623dbcf27b36ae357c5563885e559ba584ea7f21ed692d0fb91bf851b1028d103e7977a2654f379635c99f62f51f96f83857cda34
7
- data.tar.gz: f6585b1f810f2728525f0d9b7b8b57c87381c8bf45a6d5a413fc4434c81ff565233b53e80aefacb2c971f5e4d0dccbe5a612ed6185d3f3980429300f81abd722
6
+ metadata.gz: f60cd55bdc532063292bdddb6fca175f25d9b82d8fc3d1359481f053499b13eae60174884fd77847131ec3996f5f7ec4e9ca50f5f20c04d51cd0dc036de9aaed
7
+ data.tar.gz: c37add6ab91aa7a08c2c49131eea9ce478f403250a43cafaed81d3d39273de1db5bf330aadafe5028a502c07e78eeef84cf417583bc7fb7d83b6db2d050b99b3
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["tiagopog@gmail.com", "douglas@beautydate.com.br"]
11
11
 
12
12
  spec.summary = %q{Full-featured JSON API serialization in a simple way}
13
- spec.description = %q{A Rails-style way to get your API's data serialized, following the standards set by JSON API's specs (http://jsosapi.org)}
13
+ spec.description = %q{A Rails-way to get your API's data serialized following the JSON API's specs (http://jsosapi.org)}
14
14
  spec.homepage = "https://github.com/b2beauty/jsonapi-utils"
15
15
  spec.license = "MIT"
16
16
 
data/lib/jsonapi/utils.rb CHANGED
@@ -10,23 +10,40 @@ module JSONAPI
10
10
 
11
11
  def jsonapi_render(options)
12
12
  if options.has_key?(:json)
13
- response_body = jsonapi_serialize(options[:json], options[:options] || {})
14
- render json: response_body, status: (options[:status] || :ok)
13
+ response = build_response(options)
14
+ render json: response[:body], status: options[:status] || response[:status] || :ok
15
15
  end
16
16
  end
17
17
 
18
+ def jsonapi_render_not_found
19
+ jsonapi_render json: nil
20
+ end
21
+
22
+ def jsonapi_render_not_found_with_null
23
+ jsonapi_render json: nil, options: { allow_null: true }
24
+ end
25
+
18
26
  def jsonapi_serialize(records, options = {})
19
- fix_request_options(params, records)
27
+ return build_nil if records.nil? && options[:allow_null]
20
28
  results = JSONAPI::OperationResults.new
21
29
 
22
- if records.respond_to?(:to_ary)
23
- records = fix_when_hash(records, options) if records.all? { |e| e.is_a?(Hash) }
24
- @resources = build_collection(records, options)
25
- results.add_result(JSONAPI::ResourcesOperationResult.new(:ok, @resources, result_options(options)))
30
+ if records.nil?
31
+ id = extract_ids(@request.params)
32
+ record_not_found = JSONAPI::Exceptions::RecordNotFound.new(id)
33
+ results.add_result(JSONAPI::ErrorsOperationResult.new(record_not_found.errors[0].code, record_not_found.errors))
26
34
  else
27
- @resource = turn_into_resource(records, options)
28
- results.add_result(JSONAPI::ResourceOperationResult.new(:ok, @resource))
35
+ fix_request_options(params, records)
36
+
37
+ if records.respond_to?(:to_ary)
38
+ records = fix_when_hash(records, options) if records.all? { |e| e.is_a?(Hash) }
39
+ @resources = build_collection(records, options)
40
+ results.add_result(JSONAPI::ResourcesOperationResult.new(:ok, @resources, result_options(options)))
41
+ else
42
+ @resource = turn_into_resource(records, options)
43
+ results.add_result(JSONAPI::ResourceOperationResult.new(:ok, @resource))
44
+ end
29
45
  end
46
+
30
47
  create_response_document(results).contents
31
48
  end
32
49
 
@@ -36,6 +53,22 @@ module JSONAPI
36
53
 
37
54
  private
38
55
 
56
+ def build_response(options)
57
+ {
58
+ body: jsonapi_serialize(options[:json], options[:options] || {}),
59
+ status: options[:json].nil? && !options[:allow_null] ? :not_found : :ok
60
+ }
61
+ end
62
+
63
+ def build_nil
64
+ { data: nil }
65
+ end
66
+
67
+ def extract_ids(hash)
68
+ ids = hash.keys.select { |e| e =~ /id$/i }.map { |e| hash[e] }
69
+ ids.first rescue '(id not identified)'
70
+ end
71
+
39
72
  def fix_request_options(params, records)
40
73
  return if request.method !~ /get/i ||
41
74
  params.nil? ||
@@ -62,31 +95,38 @@ module JSONAPI
62
95
  @paginator ||= paginator(params)
63
96
  if @paginator && JSONAPI.configuration.top_level_links_include_pagination
64
97
  options = {}
65
- options[:record_count] = count_records(@resources, options) if @paginator.class.requires_record_count
98
+ @paginator.class.requires_record_count &&
99
+ options[:record_count] = count_records(@resources, options)
66
100
  return @paginator.links_page_params(options)
67
101
  else
68
102
  return {}
69
103
  end
70
104
  end
71
105
 
72
-
73
106
  def build_collection(records, options = {})
74
- return [] unless records.present?
75
- paginator(@request.params).apply(records, nil).map do |record|
76
- turn_into_resource(record, options)
77
- end
107
+ return [] if records.nil? || records.empty?
108
+ JSONAPI.configuration.default_paginator == :none ||
109
+ records = paginator(@request.params).apply(records, nil)
110
+ records.map { |record| turn_into_resource(record, options) }
78
111
  end
79
112
 
80
113
  def turn_into_resource(record, options = {})
81
114
  if options[:resource]
82
- options[:resource].new(record)
115
+ options[:resource].to_s.constantize.new(record)
83
116
  else
84
117
  @request.resource_klass.new(record)
85
118
  end
86
119
  end
87
120
 
88
121
  def paginator(params)
89
- PagedPaginator.new(ActionController::Parameters.new(params[:page]))
122
+ page_params = ActionController::Parameters.new(params[:page])
123
+
124
+ @paginator ||=
125
+ if JSONAPI.configuration.default_paginator == :paged
126
+ PagedPaginator.new(page_params)
127
+ elsif JSONAPI.configuration.default_paginator == :offset
128
+ OffsetPaginator.new(page_params)
129
+ end
90
130
  end
91
131
 
92
132
  def fix_when_hash(records, options)
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Utils
3
- VERSION = "0.1.3"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tiago Guedes
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2015-09-12 00:00:00.000000000 Z
12
+ date: 2015-09-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -53,8 +53,8 @@ dependencies:
53
53
  - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
- description: A Rails-style way to get your API's data serialized, following the standards
57
- set by JSON API's specs (http://jsosapi.org)
56
+ description: A Rails-way to get your API's data serialized following the JSON API's
57
+ specs (http://jsosapi.org)
58
58
  email:
59
59
  - tiagopog@gmail.com
60
60
  - douglas@beautydate.com.br