apiphobic-resource 1.0.0
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 +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/LICENSE.txt +19 -0
- data/README.md +35 -0
- data/lib/apiphobic-resource.rb +3 -0
- data/lib/apiphobic/parameters/filter.rb +85 -0
- data/lib/apiphobic/parameters/include.rb +57 -0
- data/lib/apiphobic/parameters/index.rb +32 -0
- data/lib/apiphobic/parameters/page.rb +34 -0
- data/lib/apiphobic/parameters/sort.rb +33 -0
- data/lib/apiphobic/resource/collection.rb +11 -0
- data/lib/apiphobic/resource/model.rb +43 -0
- data/lib/apiphobic/resource/processors/filtering.rb +69 -0
- data/lib/apiphobic/resource/processors/including.rb +41 -0
- data/lib/apiphobic/resource/processors/indexing.rb +41 -0
- data/lib/apiphobic/resource/processors/paging.rb +48 -0
- data/lib/apiphobic/resource/processors/sorting.rb +45 -0
- data/lib/apiphobic/resource/version.rb +7 -0
- metadata +112 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aad272bf8a8f332e0fedc698fede4e0aa85808d7c416bd6475e0ac70795a1039
|
4
|
+
data.tar.gz: b9aedf6d02180664731f177c9adf2b16a42229523e45d002480623e4f6d4724f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 19fac65555022fe9c108a278c5687056233d7a8a4715486bbd14b7fe2ad91f99fcc2b5644eb62714072686328baf2db2ce878c691aa87c7068447dae2cb33000
|
7
|
+
data.tar.gz: 03b6fe9c87cbbbce13dcdd63b0a0500a05f2effe5213b33b7b0524b959c3d7010689bc6c8f625db6527dc2d426023840ea519ddfb35c48831550c0fe312d58c3
|
checksums.yaml.gz.sig
ADDED
Binary file
|
data.tar.gz.sig
ADDED
Binary file
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010-2016 The Kompanee, Ltd
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Resource
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/resource`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'resource'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install resource
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jfelchner/resource.
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Apiphobic
|
4
|
+
module Parameters
|
5
|
+
class Filter
|
6
|
+
FLOAT = /\A[\-+]?[_\d]+\.[_\d]+\z/
|
7
|
+
INTEGER = /\A[\-+]?[_\d]+\z/
|
8
|
+
ISO8601 = /\d{4}\-\d{2}\-\d{2}T\d{2}\:\d{2}\:\d{2}(?:[+\-]\d{2}\:\d{2}|Z)/
|
9
|
+
NUMERICAL = /[\-+]?[_\d]+(?:\.\d+)?/
|
10
|
+
|
11
|
+
DATE_RANGE = /\A(#{ISO8601})\.\.\.?(#{ISO8601})\z/
|
12
|
+
NUMERICAL_RANGE = /\A(#{NUMERICAL})\.\.\.?(#{NUMERICAL}|Infinity)\z/
|
13
|
+
|
14
|
+
attr_accessor :raw_parameters
|
15
|
+
|
16
|
+
def initialize(raw_parameters)
|
17
|
+
self.raw_parameters = raw_parameters || {}
|
18
|
+
end
|
19
|
+
|
20
|
+
def present?
|
21
|
+
compacted_parameters.any?
|
22
|
+
end
|
23
|
+
|
24
|
+
def each_with_object(memoized)
|
25
|
+
compacted_parameters.each do |name, value|
|
26
|
+
yield [name, format_value(value)], memoized
|
27
|
+
end
|
28
|
+
|
29
|
+
memoized
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def compacted_parameters
|
35
|
+
@compacted_parameters ||= raw_parameters.reject do |name, value|
|
36
|
+
name == 'query' ||
|
37
|
+
value == '' ||
|
38
|
+
value.nil?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# rubocop:disable Lint/AssignmentInCondition, Metrics/AbcSize,
|
43
|
+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
44
|
+
def format_value(value)
|
45
|
+
return value unless value.is_a?(::String)
|
46
|
+
|
47
|
+
if range_points = value.match(DATE_RANGE)
|
48
|
+
exclusive = value.include?('...')
|
49
|
+
starting_point = ::Time.iso8601(range_points[1])
|
50
|
+
ending_point = ::Time.iso8601(range_points[2])
|
51
|
+
|
52
|
+
::Range.new(starting_point, ending_point, exclusive)
|
53
|
+
elsif range_points = value.match(NUMERICAL_RANGE)
|
54
|
+
exclusive = value.include?('...')
|
55
|
+
starting_point = string_to_number(range_points[1])
|
56
|
+
ending_point = string_to_number(range_points[2])
|
57
|
+
|
58
|
+
::Range.new(starting_point, ending_point, exclusive)
|
59
|
+
elsif value.match?(FLOAT)
|
60
|
+
Float(value.delete('_'))
|
61
|
+
elsif value.match?(INTEGER)
|
62
|
+
Integer(value.delete('_'))
|
63
|
+
elsif value.match?(ISO8601)
|
64
|
+
::Time.iso8601(value)
|
65
|
+
else
|
66
|
+
value
|
67
|
+
end
|
68
|
+
end
|
69
|
+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
70
|
+
# rubocop:enable Lint/AssignmentInCondition, Metrics/AbcSize,
|
71
|
+
|
72
|
+
def string_to_number(other)
|
73
|
+
other = other.delete('_')
|
74
|
+
|
75
|
+
if other == 'Infinity'
|
76
|
+
9_999_999
|
77
|
+
elsif other.match?(FLOAT)
|
78
|
+
other.to_f
|
79
|
+
elsif other.match?(INTEGER)
|
80
|
+
other.to_i
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Apiphobic
|
4
|
+
module Parameters
|
5
|
+
class Include
|
6
|
+
attr_accessor :raw_parameters
|
7
|
+
|
8
|
+
def initialize(raw_parameters)
|
9
|
+
self.raw_parameters = raw_parameters ? raw_parameters.split(',') : []
|
10
|
+
end
|
11
|
+
|
12
|
+
def present?
|
13
|
+
raw_parameters.any?
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_a
|
17
|
+
@to_a ||= map_inclusions(
|
18
|
+
raw_parameters
|
19
|
+
.reject(&:empty?)
|
20
|
+
.compact,
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# rubocop:disable Metrics/PerceivedComplexity,
|
27
|
+
# rubocop:disable Metrics/BlockNesting
|
28
|
+
def map_inclusions(array)
|
29
|
+
mapped = array.each_with_object([{}]) do |inclusion, memo|
|
30
|
+
if inclusion.is_a?(::Hash)
|
31
|
+
memo[0] = inclusion
|
32
|
+
else
|
33
|
+
first, _match, rest = inclusion.partition('.')
|
34
|
+
|
35
|
+
if rest == ''
|
36
|
+
memo << first
|
37
|
+
else
|
38
|
+
memo.delete(first)
|
39
|
+
|
40
|
+
memo[0][first] = if memo[0][first].nil?
|
41
|
+
rest
|
42
|
+
else
|
43
|
+
map_inclusions(Array(memo[0][first]) << rest)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
mapped.shift if mapped[0] == {}
|
50
|
+
|
51
|
+
mapped
|
52
|
+
end
|
53
|
+
# rubocop:enable Metrics/BlockNesting
|
54
|
+
# rubocop:enable Metrics/PerceivedComplexity,
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Apiphobic
|
4
|
+
module Parameters
|
5
|
+
class Index
|
6
|
+
DEFAULT_QUERY = '*'
|
7
|
+
|
8
|
+
attr_accessor :raw_parameters
|
9
|
+
|
10
|
+
def initialize(raw_parameters)
|
11
|
+
self.raw_parameters = raw_parameters || {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def present?
|
15
|
+
query
|
16
|
+
end
|
17
|
+
|
18
|
+
def query
|
19
|
+
compacted_parameters['query'] || compacted_parameters['q']
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def compacted_parameters
|
25
|
+
@compacted_parameters ||= raw_parameters.reject do |_name, value|
|
26
|
+
value == '' ||
|
27
|
+
value.nil?
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Apiphobic
|
4
|
+
module Parameters
|
5
|
+
class Page
|
6
|
+
PAGING_PARAMETERS = %w{number size limit offset cursor}.freeze
|
7
|
+
DEFAULT_STARTING_PAGE = 1
|
8
|
+
DEFAULT_PAGE_SIZE = 25
|
9
|
+
DEFAULT_OFFSET = 0
|
10
|
+
|
11
|
+
attr_accessor :raw_parameters
|
12
|
+
|
13
|
+
def initialize(raw_parameters)
|
14
|
+
self.raw_parameters = raw_parameters || {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def present?
|
18
|
+
(raw_parameters.keys & PAGING_PARAMETERS).any?
|
19
|
+
end
|
20
|
+
|
21
|
+
def page_number
|
22
|
+
raw_parameters['number'] || DEFAULT_STARTING_PAGE
|
23
|
+
end
|
24
|
+
|
25
|
+
def per_page
|
26
|
+
raw_parameters['size'] || raw_parameters['limit'] || DEFAULT_PAGE_SIZE
|
27
|
+
end
|
28
|
+
|
29
|
+
def offset
|
30
|
+
raw_parameters['offset'] || DEFAULT_OFFSET
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Apiphobic
|
4
|
+
module Parameters
|
5
|
+
class Sort
|
6
|
+
DESCENDING_PREFIX = '-'
|
7
|
+
|
8
|
+
attr_accessor :raw_parameters
|
9
|
+
|
10
|
+
def initialize(raw_parameters)
|
11
|
+
self.raw_parameters = raw_parameters ? raw_parameters.split(',') : []
|
12
|
+
end
|
13
|
+
|
14
|
+
def present?
|
15
|
+
raw_parameters.any?
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_h
|
19
|
+
@to_h ||= Hash[to_a]
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_a
|
23
|
+
@to_a ||= raw_parameters.map do |raw_parameter|
|
24
|
+
if raw_parameter.start_with?(DESCENDING_PREFIX)
|
25
|
+
[raw_parameter[1..-1], 'desc']
|
26
|
+
else
|
27
|
+
[raw_parameter, 'asc']
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'apiphobic/resource/processors/filtering'
|
4
|
+
require 'apiphobic/resource/processors/including'
|
5
|
+
require 'apiphobic/resource/processors/indexing'
|
6
|
+
require 'apiphobic/resource/processors/paging'
|
7
|
+
require 'apiphobic/resource/processors/sorting'
|
8
|
+
|
9
|
+
module Apiphobic
|
10
|
+
module Resource
|
11
|
+
class Model
|
12
|
+
DEFAULT_PROCESSORS = %w{including}.freeze
|
13
|
+
|
14
|
+
attr_accessor :resource,
|
15
|
+
:parameters
|
16
|
+
attr_reader :processors
|
17
|
+
|
18
|
+
def initialize(resource:, parameters:, **options)
|
19
|
+
self.resource = resource
|
20
|
+
self.parameters = parameters.dup
|
21
|
+
self.processors = options.fetch(:processors, self.class::DEFAULT_PROCESSORS)
|
22
|
+
end
|
23
|
+
|
24
|
+
def processed
|
25
|
+
@processed ||= processors.inject(resource) do |processed_resource, processor|
|
26
|
+
processor.processed(processed_resource, parameters)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def meta
|
31
|
+
@meta ||= processors.inject({}) do |metadata, processor|
|
32
|
+
metadata.merge processor.meta(processed, parameters)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def processors=(other)
|
37
|
+
@processors = other.map do |processor|
|
38
|
+
Object.const_get("::Apiphobic::Resource::Processors::#{processor.capitalize}")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'apiphobic/parameters/filter'
|
4
|
+
|
5
|
+
module Apiphobic
|
6
|
+
module Resource
|
7
|
+
module Processors
|
8
|
+
class Filtering
|
9
|
+
attr_accessor :resource,
|
10
|
+
:parameters
|
11
|
+
|
12
|
+
# rubocop:disable Style/OptionHash
|
13
|
+
def initialize(resource, parameters = {})
|
14
|
+
self.resource = resource
|
15
|
+
self.parameters = Parameters::Filter.new(parameters['filter'] || {})
|
16
|
+
end
|
17
|
+
# rubocop:enable Style/OptionHash
|
18
|
+
|
19
|
+
def self.processed(*attrs)
|
20
|
+
new(*attrs).processed
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.meta(*_attrs)
|
24
|
+
{}
|
25
|
+
end
|
26
|
+
|
27
|
+
def processed
|
28
|
+
parameters.each_with_object(resource) do |(name, value), filtered_resource|
|
29
|
+
filter_method = filter_method_for(name)
|
30
|
+
|
31
|
+
if !filter_method
|
32
|
+
filtered_resource
|
33
|
+
elsif filter_method.arity.zero?
|
34
|
+
filtered_resource.public_send(filter_method.name)
|
35
|
+
else
|
36
|
+
filtered_resource.public_send(filter_method.name, value)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
resource
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def filter_method_for(filter_item)
|
46
|
+
filter_method_name = filter_method_name_for(filter_item)
|
47
|
+
|
48
|
+
resource_class.method(filter_method_name) if filter_method_name
|
49
|
+
end
|
50
|
+
|
51
|
+
def filter_method_name_for(filter_item)
|
52
|
+
if resource_class.respond_to? "for_#{filter_item}"
|
53
|
+
"for_#{filter_item}"
|
54
|
+
elsif resource_class.respond_to? filter_item
|
55
|
+
filter_item
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def resource_class
|
60
|
+
@resource_class ||= if resource.respond_to? :klass
|
61
|
+
resource.klass
|
62
|
+
else
|
63
|
+
resource
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'apiphobic/parameters/include'
|
4
|
+
|
5
|
+
module Apiphobic
|
6
|
+
module Resource
|
7
|
+
module Processors
|
8
|
+
class Including
|
9
|
+
attr_accessor :resource,
|
10
|
+
:parameters
|
11
|
+
|
12
|
+
# rubocop:disable Style/OptionHash
|
13
|
+
def initialize(resource, parameters = {})
|
14
|
+
self.resource = resource
|
15
|
+
self.parameters = Parameters::Include.new(parameters['include'])
|
16
|
+
end
|
17
|
+
# rubocop:enable Style/OptionHash
|
18
|
+
|
19
|
+
def self.processed(*attrs)
|
20
|
+
new(*attrs).processed
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.meta(*_attrs)
|
24
|
+
{}
|
25
|
+
end
|
26
|
+
|
27
|
+
def processed
|
28
|
+
return resource unless parameters.present?
|
29
|
+
|
30
|
+
resource.includes(*parameters.to_a)
|
31
|
+
|
32
|
+
resource
|
33
|
+
end
|
34
|
+
|
35
|
+
def meta
|
36
|
+
{}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'apiphobic/parameters/index'
|
4
|
+
|
5
|
+
module Apiphobic
|
6
|
+
module Resource
|
7
|
+
module Processors
|
8
|
+
class Indexing
|
9
|
+
attr_accessor :resource,
|
10
|
+
:parameters
|
11
|
+
|
12
|
+
# rubocop:disable Style/OptionHash
|
13
|
+
def initialize(resource, parameters = {})
|
14
|
+
self.resource = resource
|
15
|
+
self.parameters = Parameters::Index.new(parameters['filter'] || {})
|
16
|
+
end
|
17
|
+
# rubocop:enable Style/OptionHash
|
18
|
+
|
19
|
+
def self.processed(*attrs)
|
20
|
+
new(*attrs).processed
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.meta(*_attrs)
|
24
|
+
{}
|
25
|
+
end
|
26
|
+
|
27
|
+
def processed
|
28
|
+
return resource unless parameters.present? || force_query
|
29
|
+
|
30
|
+
resource.for_query(parameters.query || Parameters::Index::DEFAULT_QUERY)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def force_query
|
36
|
+
resource.class.name.include? 'Index'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'apiphobic/parameters/page'
|
4
|
+
|
5
|
+
module Apiphobic
|
6
|
+
module Resource
|
7
|
+
module Processors
|
8
|
+
class Paging
|
9
|
+
attr_accessor :resource,
|
10
|
+
:parameters
|
11
|
+
|
12
|
+
# rubocop:disable Style/OptionHash
|
13
|
+
def initialize(resource, parameters = {})
|
14
|
+
self.resource = resource
|
15
|
+
self.parameters = Parameters::Page.new(parameters['page'] || {})
|
16
|
+
end
|
17
|
+
# rubocop:enable Style/OptionHash
|
18
|
+
|
19
|
+
def self.processed(*attrs)
|
20
|
+
new(*attrs).processed
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.meta(*attrs)
|
24
|
+
new(*attrs).meta
|
25
|
+
end
|
26
|
+
|
27
|
+
def processed
|
28
|
+
return resource unless parameters.present?
|
29
|
+
|
30
|
+
resource
|
31
|
+
.page(parameters.page_number)
|
32
|
+
.per(parameters.per_page)
|
33
|
+
end
|
34
|
+
|
35
|
+
def meta
|
36
|
+
return {} unless parameters.present?
|
37
|
+
|
38
|
+
{
|
39
|
+
'total-pages' => resource.total_pages,
|
40
|
+
'current-page' => resource.current_page,
|
41
|
+
'previous-page' => resource.prev_page,
|
42
|
+
'next-page' => resource.next_page,
|
43
|
+
}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'apiphobic/parameters/sort'
|
4
|
+
|
5
|
+
module Apiphobic
|
6
|
+
module Resource
|
7
|
+
module Processors
|
8
|
+
class Sorting
|
9
|
+
attr_accessor :resource,
|
10
|
+
:parameters
|
11
|
+
|
12
|
+
# rubocop:disable Style/OptionHash
|
13
|
+
def initialize(resource, parameters = {})
|
14
|
+
self.resource = resource
|
15
|
+
self.parameters = Parameters::Sort.new(parameters['sort'])
|
16
|
+
end
|
17
|
+
# rubocop:enable Style/OptionHash
|
18
|
+
|
19
|
+
def self.processed(*attrs)
|
20
|
+
new(*attrs).processed
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.meta(*attrs)
|
24
|
+
new(*attrs).meta
|
25
|
+
end
|
26
|
+
|
27
|
+
def processed
|
28
|
+
return resource unless parameters.present?
|
29
|
+
|
30
|
+
resource.order(parameters.to_h)
|
31
|
+
|
32
|
+
resource
|
33
|
+
end
|
34
|
+
|
35
|
+
def meta
|
36
|
+
return {} unless parameters.present?
|
37
|
+
|
38
|
+
{
|
39
|
+
'sort' => parameters.to_h,
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: apiphobic-resource
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- thegranddesign
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
- |
|
12
|
+
-----BEGIN CERTIFICATE-----
|
13
|
+
MIIDqjCCApKgAwIBAgIBATANBgkqhkiG9w0BAQUFADBNMREwDwYDVQQDDAhydWJ5
|
14
|
+
Z2VtczEjMCEGCgmSJomT8ixkARkWE2xpdmluZ2hpZ2hvbnRoZWJsb2cxEzARBgoJ
|
15
|
+
kiaJk/IsZAEZFgNjb20wHhcNMTcwODAyMjI1OTM1WhcNMTgwODAyMjI1OTM1WjBN
|
16
|
+
MREwDwYDVQQDDAhydWJ5Z2VtczEjMCEGCgmSJomT8ixkARkWE2xpdmluZ2hpZ2hv
|
17
|
+
bnRoZWJsb2cxEzARBgoJkiaJk/IsZAEZFgNjb20wggEiMA0GCSqGSIb3DQEBAQUA
|
18
|
+
A4IBDwAwggEKAoIBAQDtLa7+7p49gW15OgOyRZad/F92iZcMdDjZ2kAxZlviXgVe
|
19
|
+
PCtjfdURobH+YMdt++6eRkE25utIFqHyN51Shxfdc21T3fPQe/ZEoMyiJK4tYzbh
|
20
|
+
7VjNJG4ldvKKpS1p7iVz9imnyTxNwb0JaIOsOFCA04T0u6aCQi2acNvAPLviXk0q
|
21
|
+
xJ/CKjI4QUTZKVrBt8Q1Egrp2yzmEnSNftDuTbBb8m4vDR+w325CwbKCgycHJ1/g
|
22
|
+
YZ3FO76TzJuRVbsYS/bU5XKHVEpkeFmWBqEXsk4DuUIWLa6WZEJcoZf+YP+1pycG
|
23
|
+
7YqSbydpINtEdopD+EEI+g+zNJ4nSI8/eQcQyEjBAgMBAAGjgZQwgZEwCQYDVR0T
|
24
|
+
BAIwADALBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFDWuVrg4ve0vLu71kqiGdyBnzJGV
|
25
|
+
MCsGA1UdEQQkMCKBIHJ1YnlnZW1zQGxpdmluZ2hpZ2hvbnRoZWJsb2cuY29tMCsG
|
26
|
+
A1UdEgQkMCKBIHJ1YnlnZW1zQGxpdmluZ2hpZ2hvbnRoZWJsb2cuY29tMA0GCSqG
|
27
|
+
SIb3DQEBBQUAA4IBAQDJIpHjbBPGiaY4wOHcXlltQ+BMmhWQNh+1fZtyajQd+7Ay
|
28
|
+
fv23mO7Mf25Q38gopQlpaODkfxq54Jt8FvQbr5RYRS4j+JEKb75NgrAtehd8USUd
|
29
|
+
CiJJGH+yvGNWug9IGZCGX91HIbTsLQ5IUUWQasC5jGP8nxXufUr9xgAJZZenewny
|
30
|
+
B2qKu8q1A/kj6cw62RCY7yBmUXxlcJBj8g+JKYAFbYYKUdQSzf50k9IiWLWunJM+
|
31
|
+
Y2GAoHKstmfIVhc4XHOPpmTd2o/C29O9oaRgjrkfQEhF/KvJ/PhoV5hvokzsCyI5
|
32
|
+
iUeXPfvrGD/itYIBCgk+fnzyQQ4QtE5hTQaWQ3o2
|
33
|
+
-----END CERTIFICATE-----
|
34
|
+
date: 2018-05-01 00:00:00.000000000 Z
|
35
|
+
dependencies:
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '3.7'
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '3.7'
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: rspeckled
|
52
|
+
requirement: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - "~>"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0.0'
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - "~>"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0.0'
|
64
|
+
description: ''
|
65
|
+
email:
|
66
|
+
- rubygems@livinghighontheblog.com
|
67
|
+
executables: []
|
68
|
+
extensions: []
|
69
|
+
extra_rdoc_files: []
|
70
|
+
files:
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- lib/apiphobic-resource.rb
|
74
|
+
- lib/apiphobic/parameters/filter.rb
|
75
|
+
- lib/apiphobic/parameters/include.rb
|
76
|
+
- lib/apiphobic/parameters/index.rb
|
77
|
+
- lib/apiphobic/parameters/page.rb
|
78
|
+
- lib/apiphobic/parameters/sort.rb
|
79
|
+
- lib/apiphobic/resource/collection.rb
|
80
|
+
- lib/apiphobic/resource/model.rb
|
81
|
+
- lib/apiphobic/resource/processors/filtering.rb
|
82
|
+
- lib/apiphobic/resource/processors/including.rb
|
83
|
+
- lib/apiphobic/resource/processors/indexing.rb
|
84
|
+
- lib/apiphobic/resource/processors/paging.rb
|
85
|
+
- lib/apiphobic/resource/processors/sorting.rb
|
86
|
+
- lib/apiphobic/resource/version.rb
|
87
|
+
homepage: ''
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata:
|
91
|
+
allowed_push_host: https://rubygems.org
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.7.6
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: API Resources
|
112
|
+
test_files: []
|
metadata.gz.sig
ADDED
Binary file
|