api_helper 0.0.3 → 0.0.5

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: 326e1e9e2c1b9509990e1103a0d8f95da5be83a9
4
- data.tar.gz: 4c9fd145828a49a586d8a4b7dee67fa177ea957c
3
+ metadata.gz: 1adf4979040c0208985d4361607511531fb29f52
4
+ data.tar.gz: ceca06658f81cb44773d226dbda4f6961c7c864d
5
5
  SHA512:
6
- metadata.gz: d14e69fb29488afe354b941fc4501ba92620936731673a2bc41274cf37f77bd4f2fbcf7e368e9dd8cc52e473a4b9b8ce1fd1b49391d59f6e638df0fac6452eda
7
- data.tar.gz: f44a72e00cc934ad01194d0c20fa799f0fc6e232d26abced40f6a60b120b6942b66db0d3e21bfa90496ecc10e0b51b84833ad77727ebb2b85363dd1a5c7e5177
6
+ metadata.gz: 5dcc39614587391601e8537c9589377d00aea011b10d77dbd0b0aedc42235cef97fe85904a6d2a772a87f7775239a92b4168007c2ba25e4ded5aad56abcc473c
7
+ data.tar.gz: 69634679a2a32c1338e6bcd616f73e907cc33d02235adfc0ab34ea61f9c3979a9a01a4e013f030a9768ef88502194bbd42e54aeb99e0e3ad401af671cb4d1497
@@ -162,7 +162,7 @@ require 'active_support'
162
162
  module APIHelper::Includable
163
163
  extend ActiveSupport::Concern
164
164
 
165
- # Gets the include parameters, organize them into a +@inclusion+ hash.
165
+ # Gets the include parameters, organize them into a +@inclusion+ hash
166
166
  #
167
167
  # Params:
168
168
  #
@@ -226,7 +226,7 @@ module APIHelper::Includable
226
226
  end
227
227
  end
228
228
 
229
- # Getter for the inclusion data.
229
+ # Getter for the inclusion data
230
230
  #
231
231
  # This method will act as a traditional getter of the inclusion data and
232
232
  # returns a hash containing fields for each resource if no parameter is
@@ -1,16 +1,22 @@
1
1
  require 'active_support'
2
2
 
3
- # = Helper To Make Resource APIs Multigettable
3
+ # = Multigettable
4
4
  #
5
- # A normal resource API can let clients retrieve one specified data at a time:
5
+ # A normal RESTful API can let clients get one specified resource at a time:
6
6
  #
7
7
  # GET /posts/3
8
8
  #
9
- # If it's declared to be multigettable, then clients can retrieve multiple
10
- # specified data like this:
9
+ # If it's declared to be multigettable, then clients can get multiple
10
+ # specified resource like this:
11
11
  #
12
12
  # GET /posts/3,4,8,9
13
13
  #
14
+ # An API may also support applying operations on multiple resource sith a
15
+ # single request using this approach
16
+ #
17
+ # PATCH /posts/3,4,8,9
18
+ # DELETE /posts/3,4,8,9
19
+ #
14
20
  # == Usage
15
21
  #
16
22
  # Include this +Concern+ in your Action Controller:
@@ -22,50 +28,43 @@ require 'active_support'
22
28
  # or in your Grape API class:
23
29
  #
24
30
  # class SampleAPI < Grape::API
25
- # include APIHelper::Multigettable
31
+ # helpers APIHelper::Multigettable
26
32
  # end
27
33
  #
28
34
  # then use the +multiget+ method like this:
29
35
  #
30
- # resources :posts do
31
- # # ...
32
- # get :id do
33
- # @post = multiget(Post, find_by: :id, max: 12)
34
- # # ...
35
- # end
36
- # end
36
+ # @post = multiget(Post, find_by: :id, param: :id, max: 12)
37
37
  #
38
- # <em>The +multiget+ method returns a array of or a single model, based
38
+ # <em>The +multiget+ method returns an collection of or a single model, based
39
39
  # directly from the requested URL.</em>
40
40
  #
41
41
  # There is also another helper method to determine whether the request is
42
42
  # multigeting or not:
43
43
  #
44
- # multiget?(find_by: id) #=> true of false
44
+ # multiget?(param: id) # => true of false
45
45
  #
46
- # It can be used to interact with other condition and functionalities,
47
- # like this:
48
- #
49
- # inclusion_for :post, root: true,
50
- # default_includes: (multiget?(find_by: :id) ? [] : [:author])
51
46
  module APIHelper::Multigettable
52
47
  extend ActiveSupport::Concern
53
48
 
54
- # Get multiple resources from a resource URL by specifing ids split by ','
49
+ # Get multiple resources from the request by specifing ids split by ','
55
50
  #
56
51
  # Params:
57
52
  #
58
53
  # +resource+::
59
- # +ActiveRecord::Base+ or +ActiveRecord::Relation+ resource collection
60
- # to find data from
54
+ # +ActiveRecord::Relation+ resource collection to find resources from
61
55
  #
62
56
  # +find_by+::
63
- # +Symbol+ the attribute that is used to find data
57
+ # +Symbol+ the attribute that is used to find resources, defaults to :id
58
+ #
59
+ # +param+::
60
+ # +Symbol+ the request parameter name used to find resources,
61
+ # defaults to :id
64
62
  #
65
63
  # +max+::
66
64
  # +Integer+ maxium count of returning results
67
- def multiget(resource, find_by: :id, max: 10)
68
- ids = params[find_by].split(',')
65
+ #
66
+ def multiget(resource, find_by: :id, param: :id, max: 10)
67
+ ids = params[param].split(',')
69
68
  ids = ids[0..(max - 1)]
70
69
 
71
70
  if ids.count > 1
@@ -76,7 +75,7 @@ module APIHelper::Multigettable
76
75
  end
77
76
 
78
77
  # Is the a multiget request?
79
- def multiget?(find_by: :id)
80
- params[find_by].include?(',')
78
+ def multiget?(param: :id)
79
+ params[param].present? && params[param].include?(',')
81
80
  end
82
81
  end
@@ -1,21 +1,21 @@
1
1
  require 'active_support'
2
2
 
3
- # = Helper To Make Resource APIs Paginatable
3
+ # = Paginatable
4
4
  #
5
- # Paginating the requested items can avoid returning too much information
6
- # in a single response. API callers can iterate over the results using
7
- # pagination instead of rerteving all the data in one time, ruining the
8
- # database connection or network.
5
+ # Paginating the requested items can avoid returning too much data in a single
6
+ # response. API clients can iterate over the results with pagination instead of
7
+ # rerteving all the data in one time, ruining the database connection or
8
+ # network.
9
9
  #
10
- # There are two parameters clients can use: +per_page+ and +page+. The former
11
- # is used for setting how many data will be returned in each page, there will
10
+ # There are two available URL parameters: +per_page+ and +page+. The former is
11
+ # used for setting how many resources will be returned in each page, there will
12
12
  # be a maxium limit and default value for each API:
13
13
  #
14
14
  # GET /posts?per_page=10
15
15
  #
16
- # <em>The server will respond 10 items at a time.</em>
16
+ # <em>The server will respond 10 resources in a request.</em>
17
17
  #
18
- # Use the +page+ parameter to specify which to retrieve:
18
+ # Use the +page+ parameter to specify which to page get:
19
19
  #
20
20
  # GET /posts?page=5
21
21
  #
@@ -30,6 +30,9 @@ require 'active_support'
30
30
  #
31
31
  # Which follows the proposed RFC 5988 standard.
32
32
  #
33
+ # An aditional header, +X-Items-Count+, will also be set to the total pages
34
+ # count.
35
+ #
33
36
  # == Usage
34
37
  #
35
38
  # Include this +Concern+ in your Action Controller:
@@ -41,85 +44,104 @@ require 'active_support'
41
44
  # or in your Grape API class:
42
45
  #
43
46
  # class SampleAPI < Grape::API
44
- # include APIHelper::Paginatable
47
+ # helpers APIHelper::Paginatable
45
48
  # end
46
49
  #
47
- # then set the options for pagination in the grape method:
50
+ # then set the options for pagination in the grape method, as the following as
51
+ # an example:
48
52
  #
49
53
  # resources :posts do
50
54
  # get do
51
- # pagination User.count, default_per_page: 25, maxium_per_page: 100
55
+ # collection = current_user.posts
56
+ # pagination collection.count, default_per_page: 25, maxium_per_page: 100
52
57
  #
53
58
  # # ...
54
59
  # end
55
60
  # end
56
61
  #
57
- # Then use the helper methods, like this:
62
+ # Then use the helper methods like this:
58
63
  #
64
+ # # this example uses kaminari
59
65
  # User.page(page).per(per_page)
60
66
  #
61
- # HTTP Link header will be automatically set.
67
+ # HTTP Link header will be automatically set by the way.
62
68
  module APIHelper::Paginatable
63
69
  extend ActiveSupport::Concern
64
70
 
65
- def pagination(items_count, default_per_page: 20, maxium_per_page: 100, set_header: true)
71
+ # Set pagination for the request
72
+ #
73
+ # Params:
74
+ #
75
+ # +items_count+::
76
+ # +Symbol+ name of resource to receive the inclusion
77
+ #
78
+ # +default_per_page+::
79
+ # +Integer+ default per_page
80
+ #
81
+ # +maxium_per_page+::
82
+ # +Integer+ maximum results do return on a single page
83
+ #
84
+ def pagination(items_count, default_per_page: 20,
85
+ maxium_per_page: 100,
86
+ set_header: true)
66
87
  items_count = items_count.count if items_count.respond_to? :count
67
88
 
68
- @per_page = (params[:per_page] || default_per_page).to_i
69
- @per_page = maxium_per_page if @per_page > maxium_per_page
70
- @per_page = 1 if @per_page < 1
89
+ @pagination_per_page = (params[:per_page] || default_per_page).to_i
90
+ @pagination_per_page = maxium_per_page if @pagination_per_page > maxium_per_page
91
+ @pagination_per_page = 1 if @pagination_per_page < 1
71
92
 
72
93
  items_count = 0 if items_count < 0
73
- pages_count = (items_count.to_f / @per_page).ceil
94
+ pages_count = (items_count.to_f / @pagination_per_page).ceil
74
95
  pages_count = 1 if pages_count < 1
75
96
 
76
- @page = (params[:page] || 1).to_i
77
- @page = pages_count if @page > pages_count
78
- @page = 1 if @page < 1
97
+ @pagination_page = (params[:page] || 1).to_i
98
+ @pagination_page = pages_count if @pagination_page > pages_count
99
+ @pagination_page = 1 if @pagination_page < 1
79
100
 
80
- link_headers ||= []
101
+ if set_header
102
+ link_headers ||= []
81
103
 
82
- if current_page < pages_count
83
- link_headers << "<#{add_or_replace_uri_param(request.url, :page, current_page + 1)}>; rel=\"next\""
84
- link_headers << "<#{add_or_replace_uri_param(request.url, :page, pages_count)}>; rel=\"last\""
85
- end
86
- if current_page > 1
87
- link_headers << "<#{add_or_replace_uri_param(request.url, :page, (current_page > pages_count ? pages_count : current_page - 1))}>; rel=\"prev\""
88
- link_headers << "<#{add_or_replace_uri_param(request.url, :page, 1)}>; rel=\"first\""
89
- end
104
+ if current_page > 1
105
+ link_headers << "<#{add_or_replace_uri_param(request.url, :page, 1)}>; rel=\"first\""
106
+ link_headers << "<#{add_or_replace_uri_param(request.url, :page, (current_page > pages_count ? pages_count : current_page - 1))}>; rel=\"prev\""
107
+ end
90
108
 
91
- link_header = link_headers.join(', ')
109
+ if current_page < pages_count
110
+ link_headers << "<#{add_or_replace_uri_param(request.url, :page, current_page + 1)}>; rel=\"next\""
111
+ link_headers << "<#{add_or_replace_uri_param(request.url, :page, pages_count)}>; rel=\"last\""
112
+ end
113
+
114
+ link_header = link_headers.join(', ')
92
115
 
93
- if set_header
94
116
  if self.respond_to?(:header)
95
117
  self.header('Link', link_header)
96
118
  self.header('X-Items-Count', items_count.to_s)
119
+ self.header('X-Pages-Count', pages_count.to_s)
97
120
  end
98
121
 
99
122
  if defined?(response) && response.respond_to?(:headers)
100
123
  response.headers['Link'] = link_header
101
124
  response.headers['X-Items-Count'] = items_count.to_s
125
+ response.headers['X-Pages-Count'] = pages_count.to_s
102
126
  end
103
127
  end
104
-
105
- link_header
106
128
  end
107
129
 
108
130
  # Getter for the current page
109
- def page
110
- @page
131
+ def pagination_page
132
+ @pagination_page
111
133
  end
112
134
 
113
- alias_method :current_page, :page
135
+ alias_method :current_page, :pagination_page
114
136
 
115
137
  # Getter for per_page
116
- def per_page
117
- @per_page
138
+ def pagination_per_page
139
+ @pagination_per_page
118
140
  end
119
141
 
120
- alias_method :page_with, :per_page
142
+ alias_method :paginate_with, :pagination_per_page
121
143
 
122
- def add_or_replace_uri_param(url, param_name, param_value)
144
+ def add_or_replace_uri_param(url, param_name, param_value) # :nodoc:
123
145
  uri = URI(url)
124
146
  params = URI.decode_www_form(uri.query || '')
125
147
  params.delete_if { |param| param[0].to_s == param_name.to_s }
@@ -1,3 +1,3 @@
1
1
  module APIHelper
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
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.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neson