standardapi 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/standard_api.rb +4 -40
- data/lib/standard_api/includes.rb +5 -5
- data/lib/standard_api/orders.rb +85 -0
- data/lib/standard_api/test_case.rb +2 -2
- data/lib/standard_api/test_case/index_tests.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb2bcb0df75b9410634d0fec29e4391a6830ba3e
|
4
|
+
data.tar.gz: 9ef3bc3fd6e4b4d7d5d704ae09255f20182bb04d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3ef2868b977624aa84a784020647292d90baa82fc98bf4c34a25fc86c32138b6c6d4c7e902f339f2b54532513ebb9ca4f10c385add0a42a5dfda4031fc02040
|
7
|
+
data.tar.gz: dbfd5941419c1a41fb27d96407a7e98855e8f790a6132fbefe7f54b27ad68de6f70369ff5c471ef0ea49505b6529fad78aafbf93dc885a3ce1fb4b039f73b806
|
data/lib/standard_api.rb
CHANGED
@@ -6,11 +6,13 @@ require 'jbuilder'
|
|
6
6
|
require 'jbuilder/railtie'
|
7
7
|
require 'active_record/filter'
|
8
8
|
require 'active_record/sort'
|
9
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
9
10
|
|
10
11
|
if !ActionView::Template.registered_template_handler(:jbuilder)
|
11
12
|
ActionView::Template.register_template_handler :jbuilder, JbuilderHandler
|
12
13
|
end
|
13
14
|
|
15
|
+
require 'standard_api/orders'
|
14
16
|
require 'standard_api/includes'
|
15
17
|
|
16
18
|
module StandardAPI
|
@@ -88,49 +90,11 @@ module StandardAPI
|
|
88
90
|
end
|
89
91
|
|
90
92
|
def includes
|
91
|
-
@includes ||= StandardAPI::Includes.normalize(params[:include]
|
93
|
+
@includes ||= StandardAPI::Includes.normalize(params[:include])
|
92
94
|
end
|
93
95
|
|
94
|
-
# TODO: sanitize orders
|
95
96
|
def orders
|
96
|
-
|
97
|
-
end
|
98
|
-
|
99
|
-
def normalized_order(orderings)
|
100
|
-
return nil if orderings.nil?
|
101
|
-
|
102
|
-
orderings = Array(orderings)
|
103
|
-
|
104
|
-
orderings.map! do |order|
|
105
|
-
if order.is_a?(Symbol) || order.is_a?(String)
|
106
|
-
order = order.to_s
|
107
|
-
if order.index(".")
|
108
|
-
relation, column = order.split('.').map(&:to_sym)
|
109
|
-
{ relation => [column] }
|
110
|
-
else
|
111
|
-
order.to_sym
|
112
|
-
end
|
113
|
-
elsif order.is_a?(Hash)
|
114
|
-
normalized_order = {}
|
115
|
-
order.each do |key, value|
|
116
|
-
key = key.to_s
|
117
|
-
|
118
|
-
if key.index(".")
|
119
|
-
relation, column = key.split('.').map(&:to_sym)
|
120
|
-
normalized_order[relation] ||= []
|
121
|
-
normalized_order[relation] << { column => value }
|
122
|
-
elsif value.is_a?(Hash) && value.keys.first.to_s != 'desc' && value.keys.first.to_s != 'asc'
|
123
|
-
normalized_order[key.to_sym] ||= []
|
124
|
-
normalized_order[key.to_sym] << value
|
125
|
-
else
|
126
|
-
normalized_order[key.to_sym] = value
|
127
|
-
end
|
128
|
-
end
|
129
|
-
normalized_order
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
orderings
|
97
|
+
@orders ||= params[:order]
|
134
98
|
end
|
135
99
|
|
136
100
|
# Used in #calculate
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'active_support/core_ext/hash/indifferent_access'
|
2
|
-
# require 'active_support/core_ext/hash'
|
3
1
|
module StandardAPI
|
4
2
|
module Includes
|
5
3
|
|
@@ -17,6 +15,8 @@ module StandardAPI
|
|
17
15
|
includes.flatten.compact.each { |v| normalized.merge!(normalize(v)) }
|
18
16
|
when Hash
|
19
17
|
includes.each_pair { |k, v| normalized[k] = normalize(v) }
|
18
|
+
when nil
|
19
|
+
{}
|
20
20
|
else
|
21
21
|
if ![true, 'true'].include?(includes)
|
22
22
|
normalized[includes] = {}
|
@@ -33,8 +33,8 @@ module StandardAPI
|
|
33
33
|
# sanitize({:key => {:value => {}}}, {:key => [:value]}) # => {:key => {:value => {}}}
|
34
34
|
# sanitize({:key => {:value => {}}}, {:key => {:value => true}}) # => {:key => {:value => {}}}
|
35
35
|
# sanitize({:key => {:value => {}}}, [:key]) => # Raises ParseError
|
36
|
-
def self.sanitize(includes, permit)
|
37
|
-
includes = normalize(includes)
|
36
|
+
def self.sanitize(includes, permit, normalized=false)
|
37
|
+
includes = normalize(includes) if !normalized
|
38
38
|
permitted = ActiveSupport::HashWithIndifferentAccess.new
|
39
39
|
|
40
40
|
if permit.is_a?(Array)
|
@@ -44,7 +44,7 @@ module StandardAPI
|
|
44
44
|
permit = normalize(permit.with_indifferent_access)
|
45
45
|
includes.each do |k, v|
|
46
46
|
if permit.has_key?(k) || ['where', 'order'].include?(k.to_s)
|
47
|
-
permitted[k] = sanitize(v, permit[k] || {})
|
47
|
+
permitted[k] = sanitize(v, permit[k] || {}, true)
|
48
48
|
else
|
49
49
|
if [:raise, nil].include?(Rails.configuration.try(:action_on_unpermitted_includes))
|
50
50
|
raise(ActionDispatch::ParamsParser::ParseError.new(<<-ERR.squish, nil))
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module StandardAPI
|
2
|
+
module Orders
|
3
|
+
|
4
|
+
# # :size => [:size]
|
5
|
+
# # { :size => :asc } => [{:size => :asc}]
|
6
|
+
# # { :size => :desc } => [{:size => :desc}]
|
7
|
+
# # {:size => {:desc => :nulls_first}} => [{:size => {:desc => :nulls_first}}]
|
8
|
+
# # [:size, :id] => [:size, :id]
|
9
|
+
# # { :size => :asc, :id => :desc } => [{ :size => :asc, :id => :desc }]
|
10
|
+
# # 'listings.size' => [{:listings => [:size]}]
|
11
|
+
# # { 'listings.size' => :asc } => [{:listings => [{:size => :asc}]}]
|
12
|
+
# # { 'listings.size' => :desc } => [{:listings => [{:size => :desc}]}]
|
13
|
+
# # ['listings.size', 'property.id'] => [{:listings => [:size]}, {:property => [:id]}]
|
14
|
+
# # { 'size' => :asc, 'property.id' => :desc } => [{:size => :asc}, {:property => [{:id => :desc}]}]
|
15
|
+
# # { :size => {:asc => :nulls_first}, 'property.id' => {:desc => :null_last} } => [{:size => {:asc => :nulls_first}}, {:property => [{:id => {:desc => :nulls_first}}]}]
|
16
|
+
# def self.normalize(orderings)
|
17
|
+
# return nil if orderings.nil?
|
18
|
+
# orderings = orderings.is_a?(Array) ? orderings : [orderings]
|
19
|
+
#
|
20
|
+
# orderings.map! do |order|
|
21
|
+
# case order
|
22
|
+
# when Hash
|
23
|
+
# normalized = ActiveSupport::HashWithIndifferentAccess.new
|
24
|
+
# order.each do |key, value|
|
25
|
+
# key = key.to_s
|
26
|
+
# if key.index(".")
|
27
|
+
# relation, column = key.split('.').map(&:to_sym)
|
28
|
+
# normalized[relation] ||= []
|
29
|
+
# normalized[relation] << { column => value }
|
30
|
+
# elsif value.is_a?(Hash) && value.keys.first.to_s != 'desc' && value.keys.first.to_s != 'asc'
|
31
|
+
# normalized[key.to_sym] ||= []
|
32
|
+
# normalized[key.to_sym] << value
|
33
|
+
# else
|
34
|
+
# normalized[key.to_sym] = value
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
# normalized
|
38
|
+
# else
|
39
|
+
# order = order.to_s
|
40
|
+
# if order.index(".")
|
41
|
+
# relation, column = order.split('.').map(&:to_sym)
|
42
|
+
# { relation => [column] }
|
43
|
+
# else
|
44
|
+
# order.to_sym
|
45
|
+
# end
|
46
|
+
# end
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
|
50
|
+
def self.sanitize(orders, permit, normalized=false)
|
51
|
+
return nil if orders.nil?
|
52
|
+
|
53
|
+
permit = [permit] if !permit.is_a?(Array)
|
54
|
+
permit = permit.flatten.map { |x| x.is_a?(Hash) ? x.with_indifferent_access : x.to_s }
|
55
|
+
permitted = []
|
56
|
+
|
57
|
+
case orders
|
58
|
+
when Hash
|
59
|
+
orders.each do |key, value|
|
60
|
+
if permit.include?(key.to_s)
|
61
|
+
value = value.symbolize_keys if value.is_a?(Hash)
|
62
|
+
permitted = { key.to_sym => value }
|
63
|
+
elsif permit.find { |x| x.is_a?(Hash) && x.has_key?(key.to_s) }
|
64
|
+
subpermit = permit.find { |x| x.is_a?(Hash) && x.has_key?(key.to_s) }[key.to_s]
|
65
|
+
sanitized_value = sanitize(value, subpermit, true)
|
66
|
+
permitted = { key.to_sym => sanitized_value }
|
67
|
+
else
|
68
|
+
raise(ActionDispatch::ParamsParser::ParseError.new("Invalid Ordering #{orders.inspect}", nil))
|
69
|
+
end
|
70
|
+
end
|
71
|
+
when Array
|
72
|
+
orders.each { |order| permitted << sanitize(order, permit, true); }
|
73
|
+
else
|
74
|
+
if permit.include?(orders.to_s)
|
75
|
+
permitted = orders
|
76
|
+
else
|
77
|
+
raise(ActionDispatch::ParamsParser::ParseError.new("Invalid Ordering #{orders.inspect}", nil))
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
permitted
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
@@ -24,6 +24,7 @@ module StandardAPI
|
|
24
24
|
|
25
25
|
test '#index.json params[:order]' do
|
26
26
|
orders.each do |order|
|
27
|
+
@controller.instance_variable_set('@orders', nil) # Hack for dealing with caching / multiple request per controller life
|
27
28
|
if order.is_a?(Hash)
|
28
29
|
order.values.last.each do |o|
|
29
30
|
get :index, order: {order.keys.first => o}, format: 'json'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: standardapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Bracy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord-sort
|
@@ -190,6 +190,7 @@ files:
|
|
190
190
|
- README.md
|
191
191
|
- lib/standard_api.rb
|
192
192
|
- lib/standard_api/includes.rb
|
193
|
+
- lib/standard_api/orders.rb
|
193
194
|
- lib/standard_api/test_case.rb
|
194
195
|
- lib/standard_api/test_case/calculate_tests.rb
|
195
196
|
- lib/standard_api/test_case/create_tests.rb
|