api_queries 0.1.3 → 0.1.6

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
  SHA256:
3
- metadata.gz: 157e3ef3d1ccb494f1f857f7638e6f487ca095ce95b630fcb62cda9fa0cf9c40
4
- data.tar.gz: f03b084df3c6fd7299d2a4a2a0a21c15b287568c313c0405134f679943df09b5
3
+ metadata.gz: 0e2ffc36bc14ef3fa1695201119ddb8039709924ae1a8d5ad08e0ff7ce518d95
4
+ data.tar.gz: d926f83065802c9b70b06773ea1b568adbf6e16f68b10857dfbca38dca48d424
5
5
  SHA512:
6
- metadata.gz: a481ae1c8aac4c6e8c94a4537c258c51f941a28877ed6ee2d4004153120ce2599b2d25b07619072cefd09336eb9c1f3a5e3ca0f42801f547f14c2b32a18d457b
7
- data.tar.gz: 6efd0a6e9963d03f84b1bfb9888e69dbaa747cfbcbf1fe384f92c20f8eabbe95c225126a63946a8c1d4763569c81a57541e4e0b0152b4d67dc8d83de3bb44e72
6
+ metadata.gz: 9b201d964f73f04127be2000eaad2aba229202aebfb6ffd240d9ae28ed3e7a2c972417c176d4509f6e6a7c51d78aa1f944adb271000429465775bced1a6e0b69
7
+ data.tar.gz: 83e7eeb5a9f5a8482960e6b86536117f306c3e1a2f702fd724973560cf6178382020a1ca10e5a298127d5447e40297378182be696922bdf3e81420078f9f9ad9
data/.rspec_status CHANGED
@@ -1,10 +1,11 @@
1
1
  example_id | status | run_time |
2
2
  --------------------------------- | ------ | --------------- |
3
- ./spec/api_queries_spec.rb[1:1:1] | passed | 0.00603 seconds |
4
- ./spec/api_queries_spec.rb[1:1:2] | passed | 0.01774 seconds |
5
- ./spec/api_queries_spec.rb[1:1:3] | passed | 0.00072 seconds |
6
- ./spec/api_queries_spec.rb[1:1:4] | passed | 0.00949 seconds |
7
- ./spec/api_queries_spec.rb[1:1:5] | passed | 0.00301 seconds |
8
- ./spec/api_queries_spec.rb[1:1:6] | passed | 0.00538 seconds |
9
- ./spec/api_queries_spec.rb[1:1:7] | passed | 0.00325 seconds |
10
- ./spec/api_queries_spec.rb[1:1:8] | passed | 0.00575 seconds |
3
+ ./spec/api_queries_spec.rb[1:1:1] | passed | 0.00588 seconds |
4
+ ./spec/api_queries_spec.rb[1:1:2] | passed | 0.0209 seconds |
5
+ ./spec/api_queries_spec.rb[1:1:3] | passed | 0.00068 seconds |
6
+ ./spec/api_queries_spec.rb[1:1:4] | passed | 0.01339 seconds |
7
+ ./spec/api_queries_spec.rb[1:1:5] | passed | 0.00339 seconds |
8
+ ./spec/api_queries_spec.rb[1:1:6] | passed | 0.00318 seconds |
9
+ ./spec/api_queries_spec.rb[1:1:7] | passed | 0.00644 seconds |
10
+ ./spec/api_queries_spec.rb[1:1:8] | passed | 0.00293 seconds |
11
+ ./spec/api_queries_spec.rb[1:1:9] | passed | 0.00166 seconds |
data/.rubocop_todo.yml CHANGED
@@ -1,48 +1,11 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2018-07-21 23:51:29 +0800 using RuboCop version 0.58.1.
3
+ # on 2018-08-06 14:56:48 +0800 using RuboCop version 0.58.2.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 1
10
- # Cop supports --auto-correct.
11
- # Configuration parameters: Include, TreatCommentsAsGroupSeparators.
12
- # Include: **/*.gemspec
13
- Gemspec/OrderedDependencies:
14
- Exclude:
15
-
16
-
17
- # Offense count: 1
18
- # Cop supports --auto-correct.
19
- # Configuration parameters: EnforcedStyle, IndentationWidth.
20
- # SupportedStyles: outdent, indent
21
- Layout/AccessModifierIndentation:
22
- Exclude:
23
-
24
-
25
- # Offense count: 1
26
- # Cop supports --auto-correct.
27
- Layout/EmptyLineAfterMagicComment:
28
- Exclude:
29
-
30
- # Offense count: 2
31
- # Cop supports --auto-correct.
32
- # Configuration parameters: EnforcedStyle.
33
- # SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines
34
- Layout/EmptyLinesAroundModuleBody:
35
- Exclude:
36
-
37
-
38
- # Offense count: 31
39
- # Cop supports --auto-correct.
40
- # Configuration parameters: Width, IgnoredPatterns.
41
- Layout/IndentationWidth:
42
- Exclude:
43
-
44
-
45
-
46
9
  # Offense count: 1
47
10
  # Cop supports --auto-correct.
48
11
  # Configuration parameters: .
@@ -50,152 +13,47 @@ Layout/IndentationWidth:
50
13
  Layout/SpaceAroundEqualsInParameterDefault:
51
14
  EnforcedStyle: no_space
52
15
 
53
- # Offense count: 57
54
- # Cop supports --auto-correct.
55
- # Configuration parameters: IndentationWidth.
56
- Layout/Tab:
57
- Exclude:
58
-
59
-
60
- # Offense count: 4
61
- # Cop supports --auto-correct.
62
- # Configuration parameters: EnforcedStyle.
63
- # SupportedStyles: final_newline, final_blank_line
64
- Layout/TrailingBlankLines:
65
- Exclude:
66
-
67
- # Offense count: 8
68
- # Cop supports --auto-correct.
69
- # Configuration parameters: AllowInHeredoc.
70
- Layout/TrailingWhitespace:
71
- Exclude:
72
-
73
16
  # Offense count: 1
74
- Lint/UselessAssignment:
17
+ Lint/UnreachableCode:
75
18
  Exclude:
19
+ # - 'lib/api_queries.rb'
76
20
 
77
21
  # Offense count: 1
78
22
  Metrics/AbcSize:
79
- Max: 53
23
+ Max: 61
80
24
 
81
25
  # Offense count: 2
82
26
  # Configuration parameters: CountComments, ExcludedMethods.
83
27
  # ExcludedMethods: refine
84
28
  Metrics/BlockLength:
85
- Max: 55
29
+ Max: 58
86
30
 
87
31
  # Offense count: 1
88
32
  Metrics/CyclomaticComplexity:
89
- Max: 14
33
+ Max: 16
90
34
 
91
35
  # Offense count: 1
92
36
  # Configuration parameters: CountComments.
93
37
  Metrics/MethodLength:
94
- Max: 31
38
+ Max: 32
95
39
 
96
40
  # Offense count: 1
97
41
  Metrics/PerceivedComplexity:
98
- Max: 17
99
-
100
- # Offense count: 2
101
- # Cop supports --auto-correct.
102
- # Configuration parameters: MaxKeyValuePairs.
103
- Performance/RedundantMerge:
104
- Exclude:
105
-
106
- # Offense count: 2
107
- # Cop supports --auto-correct.
108
- # Configuration parameters: EnforcedStyle.
109
- # SupportedStyles: is_a?, kind_of?
110
- Style/ClassCheck:
111
- Exclude:
112
-
113
- # Offense count: 1
114
- Style/DateTime:
115
- Exclude:
116
-
117
- # Offense count: 2
118
- Style/Documentation:
119
- Exclude:
120
-
121
- # Offense count: 1
122
- # Cop supports --auto-correct.
123
- Style/Encoding:
124
- Exclude:
125
-
126
- # Offense count: 1
127
- # Cop supports --auto-correct.
128
- Style/ExpandPathArguments:
129
- Exclude:
130
-
131
- # Offense count: 2
132
- # Configuration parameters: MinBodyLength.
133
- Style/GuardClause:
134
- Exclude:
135
- # - 'lib/api_queries.rb'
136
-
137
- # Offense count: 1
138
- # Cop supports --auto-correct.
139
- # Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
140
- # SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
141
- Style/HashSyntax:
142
- Exclude:
143
-
144
- # Offense count: 2
145
- # Cop supports --auto-correct.
146
- Style/IfUnlessModifier:
147
- Exclude:
148
-
149
- # Offense count: 2
150
- # Cop supports --auto-correct.
151
- # Configuration parameters: EnforcedStyle.
152
- # SupportedStyles: require_parentheses, require_no_parentheses, require_no_parentheses_except_multiline
153
- Style/MethodDefParentheses:
154
- Exclude:
42
+ Max: 18
155
43
 
156
44
  # Offense count: 1
157
45
  Style/MixinUsage:
158
46
  Exclude:
159
47
  - 'spec/db/helper.rb'
160
48
 
161
- # Offense count: 1
162
- # Cop supports --auto-correct.
163
- Style/MutableConstant:
164
- Exclude:
165
-
166
- # Offense count: 2
167
- # Cop supports --auto-correct.
168
- # Configuration parameters: PreferredDelimiters.
169
- Style/PercentLiteralDelimiters:
170
- Exclude:
171
-
172
- # Offense count: 3
173
- # Cop supports --auto-correct.
174
- Style/RedundantSelf:
175
- Exclude:
176
-
177
49
  # Offense count: 1
178
50
  # Cop supports --auto-correct.
179
51
  Style/RescueModifier:
180
52
  Exclude:
181
53
  - 'spec/db/helper.rb'
182
54
 
183
- # Offense count: 30
184
- # Cop supports --auto-correct.
185
- # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
186
- # SupportedStyles: single_quotes, double_quotes
187
- Style/StringLiterals:
188
- Exclude:
189
-
190
-
191
- # Offense count: 2
192
- # Cop supports --auto-correct.
193
- Style/UnneededPercentQ:
194
- Exclude:
195
-
196
-
197
- # Offense count: 24
55
+ # Offense count: 26
198
56
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
199
57
  # URISchemes: http, https
200
58
  Metrics/LineLength:
201
- Max: 129
59
+ Max: 129
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- api_queries (0.1.3)
4
+ api_queries (0.1.6)
5
5
  will_paginate (>= 3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -30,6 +30,7 @@ include ApiQueries
30
30
  Optional Parameters:
31
31
  - `active_only [1, 0]`
32
32
  - `page [1 ~ n]`
33
+ - `column_date`(Default is `updated_at`)
33
34
 
34
35
  Get data filtered by date:
35
36
  - `after`
@@ -63,7 +64,17 @@ Product.api_q(q: 'count')
63
64
 
64
65
  # Get last updated_at
65
66
  Product.api_q(q: 'last_updated_at')
67
+ ```
68
+
69
+ You can catch exception `ApiQueries::Errors::UnknownColumn` in your `api_controller.rb` if the value of `column_date` is invalid or does not exist. Example below:
66
70
 
71
+ ```ruby
72
+ # api_controller.rb
73
+ rescue_from ApiQueries::Errors::UnknownColumn, with: render_unknown_column_error
74
+
75
+ def render_unknown_column_error
76
+ render json: { errors: 'Your error message' }, status: 422
77
+ end
67
78
  ```
68
79
 
69
80
  ## Contributing
Binary file
Binary file
@@ -0,0 +1,5 @@
1
+ module ApiQueries
2
+ module Errors
3
+ class UnknownColumn < StandardError; end
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module ApiQueries
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.1.6'.freeze
3
3
  end
data/lib/api_queries.rb CHANGED
@@ -1,15 +1,23 @@
1
1
  require 'active_support/core_ext/string/conversions'
2
2
  require 'api_queries/version'
3
+ require 'api_queries/errors/unknown_column'
4
+
3
5
  # ApiQueries
4
6
  module ApiQueries
5
7
  extend ActiveSupport::Concern
6
8
  # class method
7
9
  module ClassMethods
8
10
  def api_q(opts={})
11
+ # add default value
12
+ opts[:column_date] = 'updated_at' unless opts[:column_date].present?
13
+
14
+ # check if specified column exists
15
+ raise Errors::UnknownColumn, 'Invalid value for column_date.' if column_names.exclude?(opts[:column_date])
16
+
9
17
  # last updated at q
10
18
  if opts[:q] == 'last_updated_at'
11
19
  return { last_updated_at: (begin
12
- order('updated_at DESC').limit(1).first.updated_at.strftime('%Y-%m-%dT%H:%M:%SZ')
20
+ order(opts[:column_date] => :desc).limit(1).first.updated_at.strftime('%Y-%m-%dT%H:%M:%SZ')
13
21
  rescue StandardError
14
22
  nil
15
23
  end) }
@@ -20,19 +28,19 @@ module ApiQueries
20
28
 
21
29
  # AFTER: updated_at > given_date
22
30
  if opts[:after].present?
23
- conditions = ['updated_at > ?', fdate(opts[:after])]
31
+ conditions = ["#{opts[:column_date]} > ?", fdate(opts[:after])]
24
32
  # BEFORE: updated_at < given_date
25
33
  elsif opts[:before].present?
26
- conditions = ['updated_at < ?', fdate(opts[:before])]
34
+ conditions = ["#{opts[:column_date]} < ?", fdate(opts[:before])]
27
35
  # FROM & TO: between "from date" to "to date"
28
36
  elsif opts[:from].present? && opts[:to].present?
29
- conditions[:updated_at] = (fdate(opts[:from])..fdate(opts[:to]))
37
+ conditions[opts[:column_date].to_sym] = (fdate(opts[:from])..fdate(opts[:to]))
30
38
  # FROM: updated_at >= given_date
31
39
  elsif opts[:from].present?
32
- conditions = ['updated_at >= ?', fdate(opts[:from])]
40
+ conditions = ["#{opts[:column_date]} >= ?", fdate(opts[:from])]
33
41
  # TO: updated_at <= given_date
34
42
  elsif opts[:to].present?
35
- conditions = ['updated_at <= ?', fdate(opts[:to])]
43
+ conditions = ["#{opts[:column_date]} <= ?", fdate(opts[:to])]
36
44
  end
37
45
 
38
46
  # get by status
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_queries
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nujian Den Mark Meralpis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-31 00:00:00.000000000 Z
11
+ date: 2018-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: will_paginate
@@ -103,7 +103,11 @@ files:
103
103
  - bin/console
104
104
  - bin/setup
105
105
  - builds/api_queries-0.1.2.gem
106
+ - builds/api_queries-0.1.3.gem
107
+ - builds/api_queries-0.1.4.gem.bad_release
108
+ - builds/api_queries-0.1.5.gem
106
109
  - lib/api_queries.rb
110
+ - lib/api_queries/errors/unknown_column.rb
107
111
  - lib/api_queries/version.rb
108
112
  homepage: https://github.com/denmarkmeralpis/api_queries
109
113
  licenses: