grape-order 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 87c5bc99ba99485f575a497989509f3dfcb44043
4
+ data.tar.gz: 714bc5c8147175c18e8bf52cdb581125653bb650
5
+ SHA512:
6
+ metadata.gz: 9e100b26cef49973d7a9000914cd184b21d049581ace0d2a362507e7bd5383e452a7c30281f7afef18e374031c99dabcc49faeeec6e39de3c33ada443fbcd9fc
7
+ data.tar.gz: fd67b0330f582a5184da528433d284629c86ad1ca0e3cf0fe853e3cd7e927c258eb64b8a6f37f03c4ab7396437df7f96907b564ec7703cd74217f1846f344c9b
@@ -0,0 +1,3 @@
1
+ .idea
2
+ Gemfile.lock
3
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013-2014 Monterail.com LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # Grape::Sort [![Gem Version](https://badge.fury.io/rb/grape-order.png)]
2
+
3
+ Collection ordering for [grape](https://github.com/intridea/grape) API framework.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'grape-order'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install grape-order
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ class MyApi < Grape::API
29
+ # Include Grape::Sort module in your api
30
+ include Grape::Sort
31
+
32
+ resource :comments do
33
+ desc 'Return a list of comments.'
34
+
35
+ # Annotate action with `paginate`.
36
+ # This will add an optional param: order
37
+ #
38
+ # You can optionally set the default :order setting to the fields of your choice.
39
+ #
40
+ order '-created_at'
41
+
42
+ get do
43
+ comments = Comment.where(...)
44
+
45
+ # Use `order` helper to order the collection by passed params.
46
+ order(comments)
47
+ end
48
+
49
+ end
50
+ end
51
+ ```
52
+
53
+ Now you can pass a string `order` param to HTTP request to your endpoint structured as follows:
54
+
55
+ - prepend field name by `-` to get ascending order, like `-created_at`
56
+ - pass multiple fields to order by separating them by comma, like `-created_at, name`
57
+
58
+ ```
59
+ curl -v http://lvh.me:3000/comments?order=-created_at
60
+ ```
61
+
62
+ ## Contributing
63
+
64
+ The gem structure was based on https://github.com/monterail/grape-kaminari
65
+ (which can be used for pagination).
66
+
67
+ Feel free to submit issue with bug report or pull requests on GitHub at https://github.com/PrositAS/grape-order.
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task default: :spec
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'grape/order/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "grape-order"
8
+ spec.version = Grape::Order::VERSION
9
+ spec.authors = ["Grzegorz Brzezinka"]
10
+ spec.email = ["greg@prosit.no"]
11
+ spec.description = %q{collection ordering by params for grape API framework}
12
+ spec.summary = %q{collection ordering for grape}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+
22
+ spec.add_runtime_dependency "grape"
23
+ spec.add_runtime_dependency "activerecord", "> 4.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "rack-test"
29
+ spec.add_development_dependency "rspec", "~> 2.9"
30
+ end
@@ -0,0 +1,11 @@
1
+ module Grape
2
+ module Order
3
+ class << self
4
+ def post_0_9_0_grape?
5
+ Gem::Version.new(Grape::VERSION) > Gem::Version.new('0.9.0')
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ require "grape/order"
@@ -0,0 +1,30 @@
1
+ require "grape"
2
+ require "grape/order/version"
3
+
4
+ module Grape
5
+ module Order
6
+ def self.included(base)
7
+ base.class_eval do
8
+ helpers do
9
+ def order(collection)
10
+ sorters = params[:order].split(',').map do |s|
11
+ if s[0] == '-'
12
+ "#{s} asc"
13
+ else
14
+ "#{s} desc"
15
+ end
16
+ end
17
+ collection.order(sorters.join(','))
18
+ end
19
+ end
20
+
21
+ def self.order(sorter = nil)
22
+ params do
23
+ optional :order, type: String, default: (sorter if sorter.present?),
24
+ desc: 'Set sorting order.'
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ module Grape
2
+ module Order
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+
4
+ class Comment
5
+ include ActiveModel::Model
6
+
7
+ attr_accessor :id, :created_at
8
+ end
9
+
10
+ class CommentsCollection
11
+ def self.all
12
+ now = Time.now
13
+ 1.upto(10).each do |i|
14
+ Comment.new(
15
+ id: i,
16
+ created_at: now + i.minutes
17
+ )
18
+ end
19
+ #TODO is there a way to test it without a db in test?
20
+ end
21
+ end
22
+
23
+ class OrderedAPI < Grape::API
24
+ include Grape::Order
25
+
26
+ order
27
+ get '' do
28
+ order(CommentsCollection.all)
29
+ end
30
+ end
31
+
32
+ describe Grape::Order do
33
+ subject { OrderedAPI.new }
34
+ def app; subject; end
35
+ let(:json) { JSON.parse(last_response.body) }
36
+
37
+ it 'orders by created_at asc' do
38
+ pending 'add db to tests'
39
+ get '/', order: 'created_at'
40
+ expect(json.pluck(:id)).to eq((1..10).to_a)
41
+ end
42
+
43
+
44
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+
4
+ class UnOrderedAPI < Grape::API
5
+ # Intentionally not including Grape::Order
6
+ end
7
+
8
+ class OrderedAPI < Grape::API
9
+ include Grape::Order
10
+ end
11
+
12
+ describe Grape::Order do
13
+
14
+ describe 'unordered api' do
15
+ subject { Class.new(UnOrderedAPI) }
16
+
17
+ it 'raises an error' do
18
+ expect { subject.order }.to raise_error(NoMethodError, /undefined method `order' for/i)
19
+ end
20
+ end
21
+
22
+ describe 'default ordered api' do
23
+ subject { Class.new(OrderedAPI) }
24
+
25
+ it 'adds to declared parameters' do
26
+ subject.order
27
+ if Grape::Order.post_0_9_0_grape?
28
+ expect(subject.inheritable_setting.route[:declared_params]).to eq([:order])
29
+ else
30
+ expect(subject.settings[:declared_params]).to eq([:order])
31
+ end
32
+ end
33
+
34
+ describe 'descriptions, validation, and defaults' do
35
+ before do
36
+ subject.order
37
+ subject.get '/' do; end
38
+ end
39
+ let(:params) {subject.routes.first.route_params}
40
+
41
+ it 'does not require :order' do
42
+ expect(params['order'][:required]).to eq(false)
43
+ end
44
+
45
+ it 'describes :order' do
46
+ expect(params['order'][:desc]).to eq('Set sorting order.')
47
+ end
48
+
49
+ it 'validates :order as String' do
50
+ expect(params['order'][:type]).to eq('String')
51
+ end
52
+
53
+ it 'defaults :order to nil' do
54
+ expect(params['order'][:default]).to eq(nil)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'grape'
5
+ require 'grape-order'
6
+ require 'active_record'
7
+ require 'rack/test'
8
+
9
+ # Requires supporting files with custom matchers and macros, etc,
10
+ # in ./support/ and its subdirectories.
11
+ Dir['#{File.dirname(__FILE__)}/support/**/*.rb'].each {|f| require f}
12
+
13
+ I18n.enforce_available_locales = false
14
+
15
+ RSpec.configure do |config|
16
+ config.include Rack::Test::Methods
17
+ config.treat_symbols_as_metadata_keys_with_true_values = true
18
+ config.order = 'random'
19
+ config.color = true
20
+ config.formatter = :documentation
21
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-order
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Grzegorz Brzezinka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack-test
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.9'
111
+ description: collection ordering by params for grape API framework
112
+ email:
113
+ - greg@prosit.no
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - grape-order.gemspec
124
+ - lib/grape-order.rb
125
+ - lib/grape/order.rb
126
+ - lib/grape/order/version.rb
127
+ - spec/order_helper_spec.rb
128
+ - spec/order_spec.rb
129
+ - spec/spec_helper.rb
130
+ homepage: ''
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.5.1
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: collection ordering for grape
154
+ test_files:
155
+ - spec/order_helper_spec.rb
156
+ - spec/order_spec.rb
157
+ - spec/spec_helper.rb