dash_api 0.0.24 → 0.0.25

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
  SHA256:
3
- metadata.gz: 41b4f85f96294e8f796ac017f3d52452c2dc13b963f6af9056d614ac5ee6ddb4
4
- data.tar.gz: 0ceb837310db910dadd6c21937b9224832d5f498782ea9e964f68564f2d3cc28
3
+ metadata.gz: e3166c565503b2ccd94b3beb84b0aa75097de7cd746bcd62a29fca359b53b7d3
4
+ data.tar.gz: 14c183d50bbe19b1ca7082a542ee25a1190707e966a21ac85958139d800fb240
5
5
  SHA512:
6
- metadata.gz: 3de8da8ec097ac9416550900b89b539c90bce4791925957bf0299df328faa58ccce6ec6dec7178b432c9ee850ea1f8e9a21024de28fe7727b04dc1133b2995c1
7
- data.tar.gz: 1099ea57ccb8c4bc6e8770da6150f00b9459fac78d3a15fb8190f96147ae86b123e602be6c625e8f9ec145c766284142578e51c2997b4f3dd9f1fac09429ae77
6
+ metadata.gz: 9e8895ed01f5d98068301c2564a5528769929b65edd2ee7246647d4f5161b550185022814241d3e7613fe01a3b459e69045fbb9a699d31d435cd4e023e5f3b7e
7
+ data.tar.gz: 159f36163bd351307e91d4a11a3ad512d388445c495f4f65731326ba08021da67fd11a13327889509c3a65843c0afef9d7fc0e3372ca5c7b8d9ea6566053023c
data/README.md CHANGED
@@ -14,6 +14,7 @@ DashAPI supports several features out of the box with little or no configuration
14
14
  - Sorting
15
15
  - Selects
16
16
  - Associations
17
+ - Statistics
17
18
  - Pagination
18
19
  - JWT token authorization
19
20
  - Firebase authorization
@@ -218,11 +219,40 @@ GET /dash/api/books?includes=reviews
218
219
  ```
219
220
 
220
221
  To combine associations together comma seperate the included tables:
221
-
222
222
  ```
223
223
  GET /dash/api/books?includes=author,reviews
224
224
  ```
225
225
 
226
+ ### Statistics
227
+
228
+ You can perform calculations for the `minimum`, `maximum`, `average` or `count` of any field in a table.
229
+ You may also combine filters or other query parameters to refine results before performing the calculation.
230
+
231
+ Maxixum
232
+ ```
233
+ max=<field>
234
+ ```
235
+
236
+ Minimum
237
+ ```
238
+ min=<field>
239
+ ```
240
+
241
+ Average
242
+ ```
243
+ avg=<field>
244
+ ```
245
+
246
+ Count
247
+ ```
248
+ count=<field>
249
+ ```
250
+
251
+ Example:
252
+ ```
253
+ GET /dash/api/books?avg=ratings
254
+ ```
255
+
226
256
 
227
257
  ### Create
228
258
 
@@ -14,15 +14,21 @@ module DashApi
14
14
  @filters.each{|filter| resources = resources.where(filter) }
15
15
  resources = resources.pg_search(@keywords) if @keywords.present?
16
16
  resources = resources.order(@order) if @order.present?
17
- resources = resources.select(@select) if @select.present?
18
- resources = resources.page(@page).per(@per_page)
17
+ resources = resources.select(@select) if @select.present?
18
+
19
+ if @stats.present?
20
+ statistic, value = @stats.keys[0], @stats.values[0]&.to_sym
21
+ resources = resources.send(statistic, value)
22
+ else
23
+ resources = resources.page(@page).per(@per_page)
24
+ end
19
25
 
20
26
  render json: {
21
- data: DashApi::Serializer.to_json(resources, includes: @includes),
27
+ data: DashApi::Serializer.render(resources, includes: @includes),
22
28
  meta: {
23
29
  page: @page,
24
30
  per_page: @per_page,
25
- total_count: resources.total_count
31
+ total_count: resources.respond_to?(:total_count) ? resources.total_count : 1
26
32
  }
27
33
  }
28
34
  end
@@ -31,7 +37,7 @@ module DashApi
31
37
  resource = dash_scope.find(params[:id])
32
38
  authorize resource, :show?
33
39
  render json: {
34
- data: DashApi::Serializer.to_json(resource, includes: @includes)
40
+ data: DashApi::Serializer.render(resource, includes: @includes)
35
41
  }
36
42
  end
37
43
 
@@ -39,7 +45,7 @@ module DashApi
39
45
  resource = dash_scope.create!(dash_params)
40
46
  authorize resource, :create?
41
47
  render json: {
42
- data: DashApi::Serializer.to_json(resource)
48
+ data: DashApi::Serializer.render(resource)
43
49
  }
44
50
  end
45
51
 
@@ -48,7 +54,7 @@ module DashApi
48
54
  authorize resource, :update?
49
55
  if resource.update(dash_params)
50
56
  render json: {
51
- data: DashApi::Serializer.to_json(resource)
57
+ data: DashApi::Serializer.render(resource)
52
58
  }
53
59
  else
54
60
  render json: { error: resource.errors.full_messages }, status: 422
@@ -59,21 +65,21 @@ module DashApi
59
65
  resource = dash_scope.find(params[:id])
60
66
  authorize resource, :destroy?
61
67
  resource.destroy
62
- render json: { data: DashApi::Serializer.to_json(resource) }
68
+ render json: { data: DashApi::Serializer.render(resource) }
63
69
  end
64
70
 
65
71
  def update_many
66
72
  resources = dash_scope.where(id: params[:ids])
67
73
  authorize resources, :update?
68
74
  resources.update(dash_params)
69
- render json: { data: DashApi::Serializer.to_json(resources) }
75
+ render json: { data: DashApi::Serializer.render(resources) }
70
76
  end
71
77
 
72
78
  def delete_many
73
79
  resources = dash_scope.where(id: params[:ids])
74
80
  authorize resources, :destroy?
75
81
  resources.destroy_all
76
- render json: { data: DashApi::Serializer.to_json(resources) }
82
+ render json: { data: DashApi::Serializer.render(resources) }
77
83
  end
78
84
 
79
85
  private
@@ -85,8 +91,9 @@ module DashApi
85
91
  @per_page = query[:per_page]
86
92
  @order = query[:order]
87
93
  @filters = query[:filters]
94
+ @stats = query[:stats]
88
95
  @select = query[:select_fields]
89
- @includes = query[:associations]
96
+ @includes = query[:associations]
90
97
  end
91
98
 
92
99
  def load_dash_table
@@ -47,19 +47,38 @@
47
47
  end
48
48
  end
49
49
 
50
+ stats = nil
51
+ if params[:max]
52
+ stats = {maximum: params[:max]}
53
+ end
54
+
55
+ if params[:min]
56
+ stats = {minimum: params[:min]}
57
+ end
58
+
59
+ if params[:avg]
60
+ stats = {average: params[:avg]}
61
+ end
62
+
63
+ if params[:count]
64
+ stats = {count: params[:count]}
65
+ end
66
+
67
+
50
68
  page = params[:page]&.to_i || 1
51
69
  per_page = params[:per_page]&.to_i || PER_PAGE
52
70
 
53
71
  {
72
+ associations: associations,
73
+ filters: filters,
54
74
  keywords: keywords,
55
- select_fields: select_fields,
56
75
  order: order,
57
- sort_by: sort_by,
58
- sort_direction: sort_direction,
59
- filters: filters,
60
76
  page: page,
61
- associations: associations,
62
- per_page: per_page
77
+ per_page: per_page,
78
+ select_fields: select_fields,
79
+ sort_by: sort_by,
80
+ sort_direction: sort_direction,
81
+ stats: stats
63
82
  }
64
83
  end
65
84
 
@@ -1,7 +1,7 @@
1
1
  module DashApi
2
2
  module Serializer
3
3
 
4
- def self.to_json(current_scope, includes: nil)
4
+ def self.render(current_scope, includes: nil)
5
5
  opts = { except: DashApi.exclude_attributes }
6
6
  if includes
7
7
  include_hash = {}
@@ -1,3 +1,3 @@
1
1
  module DashApi
2
- VERSION = '0.0.24'
2
+ VERSION = '0.0.25'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dash_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.24
4
+ version: 0.0.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rami Bitar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-09 00:00:00.000000000 Z
11
+ date: 2021-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -184,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
184
  - !ruby/object:Gem::Version
185
185
  version: '0'
186
186
  requirements: []
187
- rubygems_version: 3.1.6
187
+ rubygems_version: 3.2.22
188
188
  signing_key:
189
189
  specification_version: 4
190
190
  summary: REST API for your postgres database.