api_helper 0.0.5 → 0.0.6

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: 1adf4979040c0208985d4361607511531fb29f52
4
- data.tar.gz: ceca06658f81cb44773d226dbda4f6961c7c864d
3
+ metadata.gz: 4c9251a106ef4147f1bdeafd3c63c6bb772bcefe
4
+ data.tar.gz: 3f4b4a7493bfe73371c3d9f0879263b367d8b14d
5
5
  SHA512:
6
- metadata.gz: 5dcc39614587391601e8537c9589377d00aea011b10d77dbd0b0aedc42235cef97fe85904a6d2a772a87f7775239a92b4168007c2ba25e4ded5aad56abcc473c
7
- data.tar.gz: 69634679a2a32c1338e6bcd616f73e907cc33d02235adfc0ab34ea61f9c3979a9a01a4e013f030a9768ef88502194bbd42e54aeb99e0e3ad401af671cb4d1497
6
+ metadata.gz: 994a9811ac0a139ceff2cc1c80af5e2503d41fb30d39ad2c9b3d32a1f1e37759133ee422dc1ce8e40e6c027c2fc82c68b877bbc2ec4b9a2f39813841075eed48
7
+ data.tar.gz: 1eac3f4b577c4107ca7031d060f4390509d544f8a51ee13c27c5d2a2fbc2799ebef267bbd8582f853d770709a14b8773f971d5164bd6a4a16739ef7e480428d7
@@ -1,6 +1,6 @@
1
1
  require 'active_support'
2
2
 
3
- # = Helper To Make Resource APIs Sortable
3
+ # = Sortable
4
4
  #
5
5
  # A Sortable Resource API gives the flexibility to change how the returned data
6
6
  # is sorted to the client. Clients can use the +sort+ URL parameter to control
@@ -22,7 +22,7 @@ require 'active_support'
22
22
  # or in your Grape API class:
23
23
  #
24
24
  # class SampleAPI < Grape::API
25
- # include APIHelper::Sortable
25
+ # helpers APIHelper::Sortable
26
26
  # end
27
27
  #
28
28
  # then use the +sortable+ method like this:
@@ -30,15 +30,16 @@ require 'active_support'
30
30
  # resources :posts do
31
31
  # get do
32
32
  # sortable default_order: { created_at: :desc }
33
- # # ...
34
- # @posts = Post.order(sort)#...
33
+ # @posts = Post.order(sortable_sort)
34
+ #
35
35
  # # ...
36
36
  # end
37
37
  # end
38
+ #
38
39
  module APIHelper::Sortable
39
40
  extend ActiveSupport::Concern
40
41
 
41
- # Gets the `sort` parameter with the format 'resourses?sort=-created_at,name',
42
+ # Gets the +sort+ parameter with the format 'resourses?sort=-created_at,name',
42
43
  # verify and converts it into an safe Hash that can be passed into the .order
43
44
  # method.
44
45
  #
@@ -46,31 +47,32 @@ module APIHelper::Sortable
46
47
  #
47
48
  # +default_order+::
48
49
  # +Hash+ the default value to return if the sort parameter is not provided
50
+ #
49
51
  def sortable(default_order: {})
50
52
  # get the parameter
51
53
  sort_by = params[:sort] || params[:sort_by]
52
54
 
53
- if sort_by.is_a? String
55
+ if sort_by.is_a?(String)
54
56
  # split it
55
57
  sort_by_attrs = sort_by.gsub(/[^a-zA-Z0-9\-_,]/, '').split(',')
56
58
 
57
59
  # save it
58
- @sort = {}
60
+ @sortable_sort = {}
59
61
  sort_by_attrs.each do |attrb|
60
62
  if attrb.match(/^-/)
61
- @sort[attrb.gsub(/^-/, '')] = :desc
63
+ @sortable_sort[attrb.gsub(/^-/, '')] = :desc
62
64
  else
63
- @sort[attrb] = :asc
65
+ @sortable_sort[attrb] = :asc
64
66
  end
65
67
  end
66
68
  else
67
- @sort = default_order
69
+ @sortable_sort = default_order
68
70
  end
69
71
  end
70
72
 
71
73
  # Helper to get the sort data
72
- def sort
73
- @sort
74
+ def sortable_sort
75
+ @sortable_sort
74
76
  end
75
77
 
76
78
  # Return the 'sort' param description
@@ -1,3 +1,3 @@
1
1
  module APIHelper
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neson