arrest 0.0.36 → 0.0.37

Sign up to get free protection for your applications and to get access to all the features.
data/lib/arrest.rb CHANGED
@@ -19,6 +19,7 @@ require "arrest/source"
19
19
  require "arrest/helper/filter"
20
20
  require "arrest/helper/child_collection"
21
21
  require "arrest/helper/has_many_collection"
22
+ require "arrest/helper/ordered_collection"
22
23
  require "arrest/exceptions"
23
24
  require "arrest/http_source"
24
25
  require "arrest/mem_source"
@@ -1,5 +1,6 @@
1
1
  module Arrest
2
2
  class HasManyCollection #< BasicObject
3
+
3
4
  def initialize(parent, has_many_attribute)
4
5
  @parent = parent
5
6
  @clazz_name = (StringUtils.classify(has_many_attribute.clazz_name.to_s))
@@ -8,6 +9,9 @@ module Arrest
8
9
  @foreign_key_name = (StringUtils.underscore(@parent.class.name).gsub(/^.*\//, '') + '_id').to_sym
9
10
  define_filters
10
11
  @attribute = has_many_attribute
12
+ @page = 1
13
+ @page_size = nil
14
+ @per_page = 5
11
15
  end
12
16
 
13
17
  def build attributes = {}
@@ -23,12 +27,95 @@ module Arrest
23
27
  children.inspect
24
28
  end
25
29
 
30
+ def limit(count)
31
+ if count != @page_size
32
+ @children = nil
33
+ end
34
+ @page_size = count
35
+ self
36
+ end
37
+
38
+ def offset(count)
39
+ if @page_size
40
+ new_page = count / @page_size
41
+ else
42
+ new_page = 1
43
+ end
44
+ if new_page != @page
45
+ @children = nil
46
+ end
47
+ @page = new_page
48
+ self
49
+ end
50
+
51
+ def total_count
52
+ children() # make sure request was made before
53
+ @total_count
54
+ end
55
+
56
+ # == for kaminari
57
+ # TODO: move to external module
58
+
59
+ def limit_value #:nodoc:
60
+ @page_size || 0
61
+ end
62
+
63
+ def offset_value #:nodoc:
64
+ ((@page_size || 0) * (@page - 1)) || 0
65
+ end
66
+
67
+ def current_page
68
+ @page
69
+ end
70
+
71
+ def num_pages
72
+ if @page_size
73
+ (total_count.to_f / @page_size).ceil
74
+ else
75
+ 1
76
+ end
77
+ end
78
+
79
+ def first_page?
80
+ current_page == 1
81
+ end
82
+
83
+ def last_page?
84
+ current_page >= num_pages
85
+ end
86
+
87
+ def current_page_count #:nodoc:
88
+ @page
89
+ end
90
+
91
+ def per(num)
92
+ @per_page = num.to_i
93
+ self
94
+ end
95
+
96
+ def page(num)
97
+ num ||= 1
98
+ if @page_size != @per_page || @page != num
99
+ @children = nil
100
+ end
101
+ @page_size = @per_page
102
+ @page = num.to_i
103
+ self
104
+ end
105
+
26
106
  private
27
107
 
28
108
  def children
29
109
  if @children == nil
30
- url = @parent.resource_location + '/' + @url_part.to_s
31
- @children = resolved_class.by_url(@parent.context, url)
110
+ if @page_size
111
+ query_params = "?pageSize=#{@page_size}&page=#{@page}"
112
+ else
113
+ query_params = ''
114
+ end
115
+ url = @parent.resource_location + '/' + @url_part.to_s + query_params
116
+ response = resolved_class.by_url(@parent.context, url)
117
+ @total_count = response[:result_count]
118
+ @children = response[:collection]
32
119
  end
33
120
  @children
34
121
  end
@@ -0,0 +1,121 @@
1
+ module Arrest
2
+ class OrderedCollection #< BasicObject
3
+
4
+ def initialize(context,class_or_class_name, base_url)
5
+ @context = context
6
+ if class_or_class_name.is_a?(String) || class_or_class_name.is_a?(Symbol)
7
+ @clazz_name = (StringUtils.classify(class_name.to_s))
8
+ else
9
+ @clazz = class_or_class_name
10
+ end
11
+ @base_url = base_url
12
+ @collection = nil
13
+ @page = 1
14
+ @page_size = nil
15
+ @per_page = 5
16
+ end
17
+
18
+ def method_missing(*args, &block)
19
+ collection.send(*args, &block)
20
+ end
21
+
22
+ def limit(count)
23
+ if count != @page_size
24
+ @collection = nil
25
+ end
26
+ @page_size = count
27
+ self
28
+ end
29
+
30
+ def offset(count)
31
+ if @page_size
32
+ new_page = count / @page_size
33
+ else
34
+ new_page = 1
35
+ end
36
+ if new_page != @page
37
+ @collection = nil
38
+ end
39
+ @page = new_page
40
+ self
41
+ end
42
+
43
+ def total_count
44
+ collection() # make sure request was made before
45
+ @total_count
46
+ end
47
+
48
+ # == for kaminari
49
+ # TODO: move to external module
50
+
51
+ def limit_value #:nodoc:
52
+ @page_size || 0
53
+ end
54
+
55
+ def offset_value #:nodoc:
56
+ ((@page_size || 0) * (@page - 1)) || 0
57
+ end
58
+
59
+ def current_page
60
+ @page
61
+ end
62
+
63
+ def num_pages
64
+ if @page_size
65
+ (total_count.to_f / @page_size).ceil
66
+ else
67
+ 1
68
+ end
69
+ end
70
+
71
+ def first_page?
72
+ current_page == 1
73
+ end
74
+
75
+ def last_page?
76
+ current_page >= num_pages
77
+ end
78
+
79
+ def current_page_count #:nodoc:
80
+ @page
81
+ end
82
+
83
+ def per(num)
84
+ @per_page = num.to_i
85
+ self
86
+ end
87
+
88
+ def page(num)
89
+ num ||= 1
90
+ if @page_size != @per_page || @page != num
91
+ @collection = nil
92
+ end
93
+ @page_size = @per_page
94
+ @page = num.to_i
95
+ self
96
+ end
97
+
98
+ private
99
+
100
+ def collection
101
+ if @collection == nil
102
+ if @page_size
103
+ query_params = "?pageSize=#{@page_size}&page=#{@page}"
104
+ else
105
+ query_params = ''
106
+ end
107
+ url = @base_url + '/' + @url_part.to_s + query_params
108
+ response = resolved_class.by_url(@context, url)
109
+ @total_count = response[:result_count]
110
+ @collection = response[:collection]
111
+ end
112
+ @collection
113
+ end
114
+
115
+ def resolved_class
116
+ @clazz ||= Source.mod.const_get(@clazz_name)
117
+ end
118
+
119
+ end
120
+ end
121
+
@@ -7,17 +7,26 @@ module Arrest
7
7
  "#{self.resource_name}"
8
8
  end
9
9
 
10
+ # Rertrieves a collection of objects and returns them
11
+ # in a hash combined with metadata
10
12
  def by_url(context, url)
11
13
  begin
12
- body = body_root(source().get_many(context, url))
14
+ response = source().get_many(context, url)
15
+ parsed_hash = JSON.parse(response)
16
+ result_count = parsed_hash['result_count']
17
+ body = body_root(response)
13
18
  rescue Arrest::Errors::DocumentNotFoundError
14
19
  Arrest::logger.info "DocumentNotFoundError for #{url} gracefully returning []"
15
20
  return []
16
21
  end
17
22
  body ||= []
18
- body.map do |h|
23
+ collection = body.map do |h|
19
24
  self.build(context, h)
20
25
  end
26
+ {
27
+ :result_count => result_count,
28
+ :collection => collection
29
+ }
21
30
  end
22
31
 
23
32
  def first(context, filter={})
@@ -41,6 +50,32 @@ module Arrest
41
50
  end
42
51
  end
43
52
 
53
+ # ========================================================
54
+ # Methods for pagination
55
+ def limit_value #:nodoc:
56
+ query.options[:limit] || 0
57
+ end
58
+
59
+ def offset_value #:nodoc:
60
+ query.options[:offset] || 0
61
+ end
62
+
63
+ def total_count(context, filter = {}) #:nodoc:
64
+ begin
65
+ response = source().get_many(context, self.resource_path, filter)
66
+ all = JSON.parse(response)
67
+ all['result_count'].to_i
68
+ rescue Arrest::Errors::DocumentNotFoundError
69
+ Arrest::logger.info "DocumentNotFoundError for #{self.resource_path} gracefully returning []"
70
+ return 0
71
+ end
72
+ end
73
+
74
+
75
+ def current_page_count #:nodoc:
76
+ count
77
+ end
78
+ # ========================================================
44
79
 
45
80
  def find(context, id)
46
81
  if id == nil || "" == id
@@ -89,9 +124,7 @@ module Arrest
89
124
  else
90
125
  send :define_singleton_method, name do |context|
91
126
  resource_name = options[:resource_name] || name
92
- body_root(source().get_many(context, self.scoped_path(resource_name))).map do |h|
93
- self.build(context, h)
94
- end
127
+ Arrest::OrderedCollection.new(context, self, self.scoped_path(resource_name))
95
128
  end
96
129
  end
97
130
 
@@ -1,3 +1,3 @@
1
1
  module Arrest
2
- VERSION = "0.0.36"
2
+ VERSION = "0.0.37"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.36
4
+ version: 0.0.37
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-02 00:00:00.000000000Z
12
+ date: 2012-03-08 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &22880140 !ruby/object:Gem::Requirement
16
+ requirement: &2152811300 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *22880140
24
+ version_requirements: *2152811300
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: faraday
27
- requirement: &22879620 !ruby/object:Gem::Requirement
27
+ requirement: &2152803700 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - =
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.7.5
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *22879620
35
+ version_requirements: *2152803700
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activemodel
38
- requirement: &22878840 !ruby/object:Gem::Requirement
38
+ requirement: &2152802060 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '3'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *22878840
46
+ version_requirements: *2152802060
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &22878180 !ruby/object:Gem::Requirement
49
+ requirement: &2152789320 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.0.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *22878180
57
+ version_requirements: *2152789320
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &22877620 !ruby/object:Gem::Requirement
60
+ requirement: &2152787020 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *22877620
68
+ version_requirements: *2152787020
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rdoc
71
- requirement: &22877160 !ruby/object:Gem::Requirement
71
+ requirement: &2152784240 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *22877160
79
+ version_requirements: *2152784240
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &22876660 !ruby/object:Gem::Requirement
82
+ requirement: &2152781080 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '2'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *22876660
90
+ version_requirements: *2152781080
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rr
93
- requirement: &22876200 !ruby/object:Gem::Requirement
93
+ requirement: &2152779060 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *22876200
101
+ version_requirements: *2152779060
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: simplecov
104
- requirement: &22875660 !ruby/object:Gem::Requirement
104
+ requirement: &2152777420 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *22875660
112
+ version_requirements: *2152777420
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rack
115
- requirement: &22875120 !ruby/object:Gem::Requirement
115
+ requirement: &2152775440 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,7 +120,7 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *22875120
123
+ version_requirements: *2152775440
124
124
  description: Consume a rest API in a AR like fashion
125
125
  email:
126
126
  - axel.tetzlaff@fortytools.com
@@ -152,6 +152,7 @@ files:
152
152
  - lib/arrest/helper/filter.rb
153
153
  - lib/arrest/helper/has_many_collection.rb
154
154
  - lib/arrest/helper/logger.rb
155
+ - lib/arrest/helper/ordered_collection.rb
155
156
  - lib/arrest/http_source.rb
156
157
  - lib/arrest/mem_source.rb
157
158
  - lib/arrest/nested_resource.rb
@@ -194,4 +195,13 @@ rubygems_version: 1.8.10
194
195
  signing_key:
195
196
  specification_version: 3
196
197
  summary: Another ruby rest client
197
- test_files: []
198
+ test_files:
199
+ - spec/arrest_spec.rb
200
+ - spec/spec_helper.rb
201
+ - spec/support/models/user.rb
202
+ - test/context.rb
203
+ - test/has_attributed.rb
204
+ - test/models.rb
205
+ - test/nested_resource.rb
206
+ - test/unit.rb
207
+ - test/validations.rb