Skip to content

Commit

Permalink
Merge pull request #51 from bsm/feature/up-to-date
Browse files Browse the repository at this point in the history
Preparations for v0.2.0
  • Loading branch information
dim committed Jun 18, 2020
2 parents e178e91 + d0338bb commit e08613b
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 124 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
Expand Down
26 changes: 26 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
AllCops:
TargetRubyVersion: "2.5"
NewCops: enable

Metrics/AbcSize:
Enabled: false
Metrics/BlockLength:
Exclude:
- spec/**/*_spec.rb
Metrics/MethodLength:
Max: 20

Naming/FileName:
Exclude:
- lib/grape-kaminari.rb

Style/Documentation:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: false
Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: consistent_comma
Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: consistent_comma
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: consistent_comma
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: ruby
cache: bundler
rvm:
- 2.7
- 2.6
- 2.5
117 changes: 117 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
PATH
remote: .
specs:
grape-kaminari (0.2.0)
grape (>= 1.0)
kaminari-grape

GEM
remote: https://rubygems.org/
specs:
activesupport (6.0.3.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2)
ast (2.4.1)
builder (3.2.4)
concurrent-ruby (1.1.6)
diff-lcs (1.3)
dry-configurable (0.11.5)
concurrent-ruby (~> 1.0)
dry-core (~> 0.4, >= 0.4.7)
dry-equalizer (~> 0.2)
dry-container (0.7.2)
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.1, >= 0.1.3)
dry-core (0.4.9)
concurrent-ruby (~> 1.0)
dry-equalizer (0.3.0)
dry-inflector (0.2.0)
dry-logic (1.0.6)
concurrent-ruby (~> 1.0)
dry-core (~> 0.2)
dry-equalizer (~> 0.2)
dry-types (1.4.0)
concurrent-ruby (~> 1.0)
dry-container (~> 0.3)
dry-core (~> 0.4, >= 0.4.4)
dry-equalizer (~> 0.3)
dry-inflector (~> 0.1, >= 0.1.2)
dry-logic (~> 1.0, >= 1.0.2)
grape (1.3.3)
activesupport
builder
dry-types (>= 1.1)
mustermann-grape (~> 1.0.0)
rack (>= 1.3.0)
rack-accept
i18n (1.8.3)
concurrent-ruby (~> 1.0)
kaminari-core (1.2.1)
kaminari-grape (1.0.1)
grape
kaminari-core (~> 1.0)
minitest (5.14.1)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
mustermann-grape (1.0.1)
mustermann (>= 1.0.0)
parallel (1.19.2)
parser (2.7.1.3)
ast (~> 2.4.0)
rack (2.2.3)
rack-accept (0.4.5)
rack (>= 0.4)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rainbow (3.0.0)
rake (13.0.1)
regexp_parser (1.7.1)
rexml (3.2.4)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.2)
rspec-support (~> 3.9.3)
rspec-expectations (3.9.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.3)
rubocop (0.85.1)
parallel (~> 1.10)
parser (>= 2.7.0.1)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.7)
rexml
rubocop-ast (>= 0.0.3)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (0.0.3)
parser (>= 2.7.0.1)
ruby-progressbar (1.10.1)
ruby2_keywords (0.0.2)
thread_safe (0.3.6)
tzinfo (1.2.7)
thread_safe (~> 0.1)
unicode-display_width (1.7.0)
zeitwerk (2.3.0)

PLATFORMS
ruby

DEPENDENCIES
bundler
grape-kaminari!
rack-test
rake
rspec
rubocop

BUNDLED WITH
2.1.4
1 change: 1 addition & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
The MIT License (MIT)

Copyright (c) 2013-2014 Monterail.com LLC
Copyright (c) 2020 Black Square Media Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Grape::Kaminari [![Gem Version](https://badge.fury.io/rb/grape-kaminari.png)](http://badge.fury.io/rb/grape-kaminari) [![Circle CI](https://circleci.com/gh/monterail/grape-kaminari.png?style=shield)](https://circleci.com/gh/monterail/grape-kaminari)
# Grape::Kaminari

[kaminari](https://github.com/amatsuda/kaminari) paginator integration for [grape](https://github.com/intridea/grape) API framework.
[![Build Status](https://travis-ci.org/bsm/grape-kaminari.png?branch=master)](https://travis-ci.org/bsm/grape-kaminari)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

[kaminari](https://github.com/kaminari/kaminari) paginator integration for [grape](https://github.com/intridea/grape) API framework.

## Installation

Expand Down Expand Up @@ -87,4 +90,4 @@ X-Offset: 10

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/teamon/tesla.
Bug reports and pull requests are welcome on GitHub at https://github.com/bsm/grape-kaminari.
9 changes: 6 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require "bundler/gem_tasks"

task default: :spec
require 'bundler/gem_tasks'
require 'rubocop/rake_task'
require 'rspec/core/rake_task'

RuboCop::RakeTask.new
RSpec::Core::RakeTask.new

task default: %i[rubocop spec]
38 changes: 19 additions & 19 deletions grape-kaminari.gemspec
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'grape/kaminari/version'

Gem::Specification.new do |spec|
spec.name = "grape-kaminari"
spec.name = 'grape-kaminari'
spec.version = Grape::Kaminari::VERSION
spec.authors = ["Tymon Tobolski"]
spec.email = ["tymon.tobolski@monterail.com"]
spec.description = %q{kaminari paginator integration for grape API framework}
spec.summary = %q{kaminari integration for grape}
spec.homepage = ""
spec.license = "MIT"
spec.authors = ['Tymon Tobolski', 'Black Square Media']
spec.email = ['info@blacksquaremedia.com']
spec.description = 'kaminari paginator integration for grape API framework'
spec.summary = 'kaminari integration for grape'
spec.homepage = ''
spec.license = 'MIT'

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.start_with?('spec/') }
spec.test_files = `git ls-files -z -- spec/*`.split("\x0")
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 2.5'

spec.add_runtime_dependency 'grape', '>= 1.0'
spec.add_runtime_dependency 'kaminari-grape'

spec.add_runtime_dependency "grape"
spec.add_runtime_dependency "kaminari"

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency 'rspec', '~> 2.9'
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rack-test'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'rubocop'
end
12 changes: 1 addition & 11 deletions lib/grape-kaminari.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
module Grape
module Kaminari
class << self
def post_0_9_0_grape?
Gem::Version.new(Grape::VERSION) > Gem::Version.new('0.9.0')
end
end
end
end

require "grape/kaminari"
require 'grape/kaminari'
74 changes: 40 additions & 34 deletions lib/grape/kaminari.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
require "grape"
require "grape/kaminari/version"
require "grape/kaminari/max_value_validator"
require "kaminari/grape"
require 'grape'
require 'grape/kaminari/version'
require 'grape/kaminari/max_value_validator'
require 'kaminari/grape'

module Grape
module Kaminari
def self.included(base)
base.class_eval do
helpers do
def paginate(collection)
collection.page(params[:page].to_i).per(params[:per_page].to_i).padding(params[:offset].to_i).tap do |data|
header "X-Total", data.total_count.to_s
header "X-Total-Pages", data.total_pages.to_s
header "X-Per-Page", data.limit_value.to_s
header "X-Page", data.current_page.to_s
header "X-Next-Page", data.next_page.to_s
header "X-Prev-Page", data.prev_page.to_s
header "X-Offset", params[:offset].to_s
end
end
module HelperMethods # :nodoc:
def paginate(collection)
collection.page(params[:page].to_i)
.per(params[:per_page].to_i)
.padding(params[:offset].to_i)
.tap do |data|
header 'X-Total', data.total_count.to_s
header 'X-Total-Pages', data.total_pages.to_s
header 'X-Per-Page', data.limit_value.to_s
header 'X-Page', data.current_page.to_s
header 'X-Next-Page', data.next_page.to_s
header 'X-Prev-Page', data.prev_page.to_s
header 'X-Offset', params[:offset].to_s
end
end
end

def self.paginate(options = {})
options.reverse_merge!(
per_page: ::Kaminari.config.default_per_page || 10,
max_per_page: ::Kaminari.config.max_per_page,
offset: 0
)
params do
optional :page, type: Integer, default: 1,
desc: 'Page offset to fetch.'
optional :per_page, type: Integer, default: options[:per_page],
desc: 'Number of results to return per page.',
max_value: options[:max_per_page]
if options[:offset].is_a? Numeric
optional :offset, type: Integer, default: options[:offset],
desc: 'Pad a number of results.'
end
module ClassMethods # :nodoc:
def paginate(**options)
options.reverse_merge!(
per_page: ::Kaminari.config.default_per_page || 10,
max_per_page: ::Kaminari.config.max_per_page,
offset: 0,
)
params do
optional :page, type: Integer, default: 1,
desc: 'Page offset to fetch.'
optional :per_page, type: Integer, default: options[:per_page],
desc: 'Number of results to return per page.',
max_value: options[:max_per_page]
if options[:offset].is_a? Numeric
optional :offset, type: Integer, default: options[:offset],
desc: 'Pad a number of results.'
end
end
end
end

def self.included(base)
base.helpers HelperMethods
base.extend ClassMethods
end
end
end
24 changes: 7 additions & 17 deletions lib/grape/kaminari/max_value_validator.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
module Grape
module Kaminari
base = if post_0_9_0_grape?
Grape::Validations::Base
else
Grape::Validations::SingleOptionValidator
end

class MaxValueValidator < base
class MaxValueValidator < Grape::Validations::Base
def validate_param!(attr_name, params)
return unless params[attr_name]

attr = params[attr_name]
if attr && @option && attr > @option
message = "must be less than or equal #{@option}"
if Gem::Version.new(Grape::VERSION) >= Gem::Version.new('0.9.0')
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message
else
raise Grape::Exceptions::Validation, param: @scope.full_name(attr_name), message: message
end
end
return unless attr && @option && attr > @option

raise Grape::Exceptions::Validation.new(
params: [@scope.full_name(attr_name)],
message: "must be less than or equal #{@option}",
)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/kaminari/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Grape
module Kaminari
VERSION = "0.1.9"
VERSION = '0.2.0'.freeze
end
end
Loading

0 comments on commit e08613b

Please sign in to comment.