bodhi-slam 0.7.1 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bodhi-slam/associations.rb +48 -12
- data/lib/bodhi-slam/context.rb +2 -0
- data/lib/bodhi-slam/queries.rb +42 -0
- data/lib/bodhi-slam/resource.rb +17 -38
- data/lib/bodhi-slam.rb +1 -0
- metadata +20 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1fbe5ba6be9aceacf0acdc118e78a1a95ddd32ee
|
4
|
+
data.tar.gz: a7eae1661963319627d4694dfc90dd8bc5b1e327
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 938995fe271d5b2085a80c153931e3d0378c05daf587e247ef7baad00c2f48d4741b59299842c4042c0bbb3cf56ab44445983c200fbd6936e445d6ba3fb07dd1
|
7
|
+
data.tar.gz: 8f72e7317218ece3d876e7b803f738e55176e2de400bc368fa1c4d50bb1700c40f58be5d219fa1c2841d63a00a5d8a3cf18b0d1d393c64b30ae96213ef30ff75
|
@@ -50,30 +50,66 @@ module Bodhi
|
|
50
50
|
through_query.where(association[:through][:foreign_key].to_sym => self.send(association[:primary_key]))
|
51
51
|
through_query.select(association[:through][:primary_key])
|
52
52
|
|
53
|
-
|
53
|
+
count = through_query.count
|
54
|
+
pages = (count.to_f / 100.0).ceil
|
54
55
|
|
56
|
+
instance_ids = []
|
55
57
|
method_chain = association[:through][:primary_key].split('.')
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
|
59
|
+
# loop through each page of the through query
|
60
|
+
if pages > 0
|
61
|
+
pages.times.collect do |n|
|
62
|
+
paged_query = through_query.clone
|
63
|
+
paged_query.page(n+1)
|
64
|
+
|
65
|
+
puts paged_query.url
|
66
|
+
|
67
|
+
records = paged_query.all
|
68
|
+
instance_ids << records.map{ |item| method_chain.reduce(item){ |memo, method| memo.send(method) } }
|
61
69
|
end
|
70
|
+
|
71
|
+
instance_ids.flatten!.uniq!
|
62
72
|
end
|
63
73
|
|
64
|
-
|
65
|
-
|
74
|
+
# partition the target query if larger than 4K
|
75
|
+
test_query = query.clone
|
76
|
+
query_size = test_query.where(association[:foreign_key].to_sym => { "$in" => instance_ids }).and(association[:query]).url.bytesize
|
77
|
+
|
78
|
+
if query_size > 4000
|
79
|
+
records = []
|
80
|
+
instance_ids.each_slice(100) do |slice|
|
81
|
+
sliced_query = query.clone
|
82
|
+
sliced_query.where(association[:foreign_key].to_sym => { "$in" => slice })
|
83
|
+
sliced_query.and(association[:query])
|
84
|
+
|
85
|
+
puts sliced_query.url
|
86
|
+
|
87
|
+
records << sliced_query.all
|
88
|
+
end
|
89
|
+
|
90
|
+
records.flatten!
|
91
|
+
return records
|
92
|
+
else
|
93
|
+
query.where(association[:foreign_key].to_sym => { "$in" => instance_ids })
|
94
|
+
query.and(association[:query])
|
95
|
+
|
96
|
+
puts query.url
|
97
|
+
return query.all
|
98
|
+
end
|
99
|
+
else # default :has_many flow
|
66
100
|
instance_id = self.send(association[:primary_key])
|
101
|
+
|
67
102
|
if instance_id.is_a?(Array)
|
68
103
|
query.where(association[:foreign_key].to_sym => { "$in" => instance_id })
|
69
104
|
else
|
70
105
|
query.where(association[:foreign_key].to_sym => instance_id)
|
71
106
|
end
|
72
|
-
end
|
73
107
|
|
74
|
-
|
75
|
-
|
76
|
-
|
108
|
+
query.and(association[:query])
|
109
|
+
|
110
|
+
puts query.url
|
111
|
+
return query.all
|
112
|
+
end
|
77
113
|
end
|
78
114
|
end
|
79
115
|
|
data/lib/bodhi-slam/context.rb
CHANGED
data/lib/bodhi-slam/queries.rb
CHANGED
@@ -71,6 +71,48 @@ module Bodhi
|
|
71
71
|
#query.gsub(/\s+/, "")
|
72
72
|
end
|
73
73
|
|
74
|
+
def count
|
75
|
+
if context.nil?
|
76
|
+
raise ArgumentError.new("a Bodhi::Context is required to query the API")
|
77
|
+
end
|
78
|
+
|
79
|
+
if context.invalid?
|
80
|
+
raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
|
81
|
+
end
|
82
|
+
|
83
|
+
result = context.connection.get do |request|
|
84
|
+
request.url self.url.gsub(klass.name, "#{klass}/count")
|
85
|
+
request.headers[context.credentials_header] = context.credentials
|
86
|
+
end
|
87
|
+
|
88
|
+
if result.status != 200
|
89
|
+
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
90
|
+
end
|
91
|
+
|
92
|
+
result.body["count"]
|
93
|
+
end
|
94
|
+
|
95
|
+
def delete
|
96
|
+
if context.nil?
|
97
|
+
raise ArgumentError.new("a Bodhi::Context is required to query the API")
|
98
|
+
end
|
99
|
+
|
100
|
+
if context.invalid?
|
101
|
+
raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
|
102
|
+
end
|
103
|
+
|
104
|
+
result = context.connection.delete do |request|
|
105
|
+
request.url self.url
|
106
|
+
request.headers[context.credentials_header] = context.credentials
|
107
|
+
end
|
108
|
+
|
109
|
+
if result.status != 204
|
110
|
+
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
111
|
+
end
|
112
|
+
|
113
|
+
result.body
|
114
|
+
end
|
115
|
+
|
74
116
|
def all
|
75
117
|
if context.nil?
|
76
118
|
raise ArgumentError.new("a Bodhi::Context is required to query the API")
|
data/lib/bodhi-slam/resource.rb
CHANGED
@@ -43,26 +43,24 @@ module Bodhi
|
|
43
43
|
batch
|
44
44
|
end
|
45
45
|
|
46
|
-
# Counts all
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
result = context.connection.get do |request|
|
57
|
-
request.url "/#{context.namespace}/resources/#{name}/count"
|
58
|
-
request.headers[context.credentials_header] = context.credentials
|
59
|
-
end
|
60
|
-
|
61
|
-
if result.status != 200
|
62
|
-
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
63
|
-
end
|
46
|
+
# Counts all records that match the given query
|
47
|
+
#
|
48
|
+
# context = Bodhi::Context.new
|
49
|
+
# count = Resource.count(context, name: "Foo")
|
50
|
+
def count(context, query={})
|
51
|
+
query_obj = Bodhi::Query.new(name)
|
52
|
+
query_obj.where(query).from(context)
|
53
|
+
query_obj.count
|
54
|
+
end
|
64
55
|
|
65
|
-
|
56
|
+
# Deletes all records that match the given query
|
57
|
+
#
|
58
|
+
# context = Bodhi::Context.new
|
59
|
+
# count = Resource.delete(context, name: "Foo")
|
60
|
+
def delete!(context, query={})
|
61
|
+
query_obj = Bodhi::Query.new(name)
|
62
|
+
query_obj.where(query).from(context)
|
63
|
+
query_obj.delete
|
66
64
|
end
|
67
65
|
|
68
66
|
def create!(params, context=nil)
|
@@ -181,25 +179,6 @@ module Bodhi
|
|
181
179
|
query_obj.where(query)
|
182
180
|
query_obj
|
183
181
|
end
|
184
|
-
|
185
|
-
# Deletes all records from a resource in the given +context+
|
186
|
-
#
|
187
|
-
# context = Bodhi::Context.new
|
188
|
-
# Resource.delete_all(context)
|
189
|
-
def delete_all(context)
|
190
|
-
if context.invalid?
|
191
|
-
raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
|
192
|
-
end
|
193
|
-
|
194
|
-
result = context.connection.delete do |request|
|
195
|
-
request.url "/#{context.namespace}/resources/#{name}"
|
196
|
-
request.headers[context.credentials_header] = context.credentials
|
197
|
-
end
|
198
|
-
|
199
|
-
if result.status != 204
|
200
|
-
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
201
|
-
end
|
202
|
-
end
|
203
182
|
end
|
204
183
|
|
205
184
|
module InstanceMethods
|
data/lib/bodhi-slam.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bodhi-slam
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- willdavis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: faraday-http-cache
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.2'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.2'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: net-http-persistent
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,16 +84,16 @@ dependencies:
|
|
70
84
|
name: regexp-examples
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
|
-
- -
|
87
|
+
- - '='
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
89
|
+
version: 1.1.3
|
76
90
|
type: :runtime
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
|
-
- -
|
94
|
+
- - '='
|
81
95
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
96
|
+
version: 1.1.3
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: rspec
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|