token_pagination 0.0.1

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: 633f8feea7395ccdfb2d4e12ed37bb4600fc8f0c
4
+ data.tar.gz: 3176ad8711d7cb965d57f3f83d9cd6f4d6fe1fbb
5
+ SHA512:
6
+ metadata.gz: f6fabc35624e5d91d5ebdfa7e7de28df34cf664692ba3ac2fffedd39ccec25c24ab25142de35142aa6eb56a855190525066deb41e98ac35a5da1aa5abe1ccf94
7
+ data.tar.gz: 30f206aeb3fbb3b6a50481cf147f07b9d420cfd81a07e9d5d3727344858a378e309efdcb39e953ce7154bb2c63adfabe3a03c3ae483cf387ddf1808df7670f0f
@@ -0,0 +1,20 @@
1
+ Copyright 2015 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ = TokenPagination
2
+
3
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'TokenPagination'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
@@ -0,0 +1,11 @@
1
+ require "token_pagination/version"
2
+ require "token_pagination/page_token"
3
+ require "token_pagination/active_record_relation_extention"
4
+ require "token_pagination/railtie"
5
+ require "token_pagination/collection"
6
+
7
+ module TokenPagination
8
+ # Your code goes here...
9
+ class JWTDecodeError < StandardError; end
10
+ class UnmatchCollectionError < StandardError; end
11
+ end
@@ -0,0 +1,116 @@
1
+ require 'jwt'
2
+ require 'digest'
3
+ require 'active_support/concern'
4
+ require 'token_pagination/page_token'
5
+ module TokenPagination
6
+ module ActiveRecordRelationExtention
7
+ extend ActiveSupport::Concern
8
+ included do
9
+ def page_by_token(count, page_token_string=nil)
10
+ begin
11
+ page_by_token!(count, page_token_string)
12
+ rescue TokenPagination::JWTDecodeError
13
+ return TokenPagination::Collection.new([])
14
+ rescue TokenPagination::UnmatchCollectionError
15
+ return TokenPagination::Collection.new([])
16
+ end
17
+ end
18
+
19
+ def page_by_token!(count, page_token_string=nil)
20
+ result_set = self
21
+
22
+ if self.order_values.empty? then
23
+ raise "Order caluse must be specified"
24
+ end
25
+
26
+ unless page_token_string.nil? then
27
+ token = TokenPagination::PageToken.from_string(page_token_string).verify_c_hash!(self.to_c_hash)
28
+ result_set = result_set.where(pointer_to_where_values(token.pointer_instance))
29
+ end
30
+
31
+ result = result_set.take(count+1)
32
+
33
+ if result.size == count+1 then
34
+ result.pop
35
+ pointer_instance = result.last
36
+ pointer_instance_values = self.order_values.map{|item| item.value.name }.map{| attribute| pointer_instance.send(attribute) }
37
+ next_page_token = TokenPagination::PageToken.new(to_c_hash, pointer_instance_values).to_s
38
+ end
39
+
40
+ result = TokenPagination::Collection.new(result)
41
+ result.next_page_token = next_page_token
42
+ result
43
+ end
44
+
45
+ # attribute1 >= val AND (attribute 1 > val or ( attribute2 > val OR ( attribute2 >= val AND attribute3 > val) OR (attribute 2 >= val AND attribute3 >= val AND attribute 4 > val )
46
+ def pointer_to_where_values(pointer_instance)
47
+ orders = self.order_values.dup # copy
48
+ primal_order = orders.shift
49
+ primal_attribute = primal_order.value.relation[primal_order.value.name]
50
+ primal_value = pointer_instance.shift
51
+
52
+ primal_condition = _eq_or_next(primal_order, primal_attribute, primal_value)
53
+ on_boader_condition = _next(primal_order, primal_attribute, primal_value)
54
+
55
+ all_eq_upto = primal_attribute.eq(primal_value)
56
+
57
+ orders.each do|o|
58
+ attribute = o.value.relation[o.value.name]
59
+ value = pointer_instance.shift
60
+
61
+ on_boader_condition = on_boader_condition.or(
62
+ all_eq_upto.and(
63
+ _next(o, attribute, value)
64
+ )
65
+ )
66
+ all_eq_upto = all_eq_upto.and(attribute.eq(value))
67
+ end
68
+ result = primal_condition.and(on_boader_condition)
69
+ return result
70
+ end
71
+
72
+ def to_c_hash
73
+ Digest::MD5.hexdigest(self.to_sql) #TODO
74
+ end
75
+
76
+ private
77
+ # These methods respec mysql behaviour
78
+ # Mysql treets nil as bottom elements when ordered ascending
79
+ # But when specified with where query, column >= NULL returns empty set.
80
+ # These method allows as to order by null
81
+
82
+ def _eq_or_next(order, attr, value)
83
+ if order.ascending? then
84
+ if value.nil? then
85
+ # everything matches
86
+ return nil
87
+ else
88
+ return attr.gteq(value)
89
+ end
90
+ else
91
+ if value.nil? then
92
+ return attr.eq(nil)
93
+ else
94
+ return attr.lteq(value)
95
+ end
96
+ end
97
+ end
98
+
99
+ def _next(order, attr, value)
100
+ if order.ascending? then
101
+ if value.nil? then
102
+ return attr.not_eq(nil)
103
+ else
104
+ return attr.gt(value)
105
+ end
106
+ else
107
+ if value.nil? then
108
+ return attr.lt(nil) # Nothing matches
109
+ else
110
+ return attr.lt(value)
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,5 @@
1
+ module TokenPagination
2
+ class Collection < Array
3
+ attr_accessor :next_page_token
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ require 'jwt'
2
+ module TokenPagination
3
+ class PageToken
4
+ attr_reader :c_hash, :pointer_instance
5
+ def initialize(c_hash, pointer_instance)
6
+ @c_hash = c_hash
7
+ @pointer_instance = pointer_instance
8
+ end
9
+
10
+ def self.from_string(token_string)
11
+ begin
12
+ claims, header = JWT.decode(token_string, Rails.application.secrets.secret_key_base)
13
+ return self.new(claims["_ext"]["c_hash"], claims["_ext"]["pointer_instance"])
14
+ rescue JWT::DecodeError => e
15
+ raise TokenPagination::JWTDecodeError.new("token not decoded: #{e.to_s}")
16
+ end
17
+ end
18
+
19
+ def to_s
20
+ JWT.encode({
21
+ _ext: {
22
+ c_hash: @c_hash,
23
+ pointer_instance: @pointer_instance
24
+ }
25
+ }, Rails.application.secrets.secret_key_base)
26
+ end
27
+
28
+ def verify_c_hash!(c_hash_string)
29
+ if (c_hash_string != self.c_hash) then
30
+ raise TokenPagination::UnmatchCollectionError.new("c_hash not match")
31
+ end
32
+ self
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,9 @@
1
+ module TokenPagination
2
+ class Railtie < ::Rails::Railtie
3
+ initializer 'token_pagination' do |_app|
4
+ ActiveSupport.on_load(:active_record) do
5
+ ::ActiveRecord::Relation.send :include, TokenPagination::ActiveRecordRelationExtention
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module TokenPagination
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: token_pagination
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - maedama
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jwt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.2.1
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.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.3.10
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.3.10
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
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-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Provides page token based pagination for your active record
112
+ email:
113
+ - maedama85@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - MIT-LICENSE
119
+ - README.rdoc
120
+ - Rakefile
121
+ - lib/token_pagination.rb
122
+ - lib/token_pagination/active_record_relation_extention.rb
123
+ - lib/token_pagination/collection.rb
124
+ - lib/token_pagination/page_token.rb
125
+ - lib/token_pagination/railtie.rb
126
+ - lib/token_pagination/version.rb
127
+ homepage: http://maedama.hatenablog.com
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.2.2
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Provides page token based pagination for your active record
151
+ test_files: []