kinto_box 0.1.12 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32a1ab4c6cd2f76e5a07c2c7df08e281b6ef8ae3
4
- data.tar.gz: e4935d11684d5f8a6346c0b9dd7de758ef51548d
3
+ metadata.gz: 96296262d3eed5289517616cd4358a228ae1faba
4
+ data.tar.gz: 14eaa8ec93d64b6c01504d631d8a9c584aab4f86
5
5
  SHA512:
6
- metadata.gz: 84f70e98d251392478f69938e5cfc480bf6ba608af4c03324eedf36d10ba2346bec2a9b69529af9e9f902eb139c175adfe718f5737803cb9cf031959f69871ee
7
- data.tar.gz: 802b3bfc51dd2dbbb21357e8b629e919a70e5206082b6e669f2ede5d7df12ac8dade82ac33cbc6ea6de333b61ffe28ef39d048b3c6cb7f58b29684b4a6b5bf9f
6
+ metadata.gz: b3889be916740f913b0209a42054755eef40847249a9f6cc5b0416d4d94d5779ff13632ab9dbaf05b3e1785d4d9c6ca9d03a134265897f433bb6a0683c949d60
7
+ data.tar.gz: 71d1f0f150bab1cd756680153543afb3862635fce40a36d7546377737e12458788928f7ab382b1431c21d8d531953b694b73f5a09f522a3d8c66648e14a0ea0f
@@ -0,0 +1,33 @@
1
+ AllCops:
2
+ Include:
3
+ - '**/Rakefile'
4
+
5
+ #Style/HashSyntax:
6
+ #EnforcedStyle: hash_rockets
7
+
8
+ Style/GuardClause:
9
+ Enabled: false
10
+
11
+ Style/LineLength:
12
+ Max: 100
13
+
14
+ Style/NumericLiterals:
15
+ Enabled: false
16
+
17
+ Style/ClassAndModuleChildren:
18
+ Enabled: false
19
+
20
+ Style/SignalException:
21
+ EnforcedStyle: only_raise
22
+
23
+ Style/ClassLength:
24
+ Enabled: false
25
+
26
+ Style/MethodLength:
27
+ Enabled: false
28
+
29
+ Metrics/CyclomaticComplexity:
30
+ Enabled: false
31
+
32
+ Style/Alias:
33
+ Enabled: false
@@ -4,33 +4,35 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'kinto_box/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "kinto_box"
7
+ spec.name = 'kinto_box'
8
8
  spec.version = KintoBox::VERSION
9
- spec.authors = ["Kavya Sukumar"]
10
- spec.email = ["Kavya.Sukumar@voxmedia.com"]
9
+ spec.authors = ['Kavya Sukumar']
10
+ spec.email = ['Kavya.Sukumar@voxmedia.com']
11
11
 
12
- spec.summary = %q{Kinto http client in ruby}
13
- spec.description = %q{Kinto http client in ruby}
14
- spec.homepage = "http://github.com/voxmedia/kinto_box"
15
- spec.license = "BSD"
12
+ spec.summary = 'Kinto http client in ruby'
13
+ spec.description = 'Kinto http client in ruby'
14
+ spec.homepage = 'http://github.com/voxmedia/kinto_box'
15
+ spec.license = 'BSD'
16
16
 
17
17
  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
18
  # delete this section to allow pushing this gem to any host.
19
19
  if spec.respond_to?(:metadata)
20
- spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
21
  else
22
- raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
23
  end
24
24
 
25
25
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
- spec.bindir = "exe"
26
+ spec.bindir = 'exe'
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
- spec.require_paths = ["lib"]
28
+ spec.require_paths = ['lib']
29
29
 
30
30
  spec.add_runtime_dependency 'httparty', '~> 0.13.7'
31
31
 
32
- spec.add_development_dependency "bundler", "~> 1.11"
33
- spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency 'bundler', '~> 1.11'
33
+ spec.add_development_dependency 'rake', '~> 10.0'
34
34
  spec.add_development_dependency 'json', '~> 1.8', '>= 1.8.3'
35
- spec.add_development_dependency "minitest", "~> 5.0"
35
+ spec.add_development_dependency 'minitest', '~> 5.0'
36
+ spec.add_development_dependency 'rdoc'
37
+ spec.add_development_dependency 'rubocop'
36
38
  end
@@ -1,19 +1,23 @@
1
- require "kinto_box/version"
2
1
  require 'httparty'
3
- require 'kinto_box/response_handler'
4
- require 'kinto_box/kinto_bucket'
5
2
  require 'base64'
6
3
 
7
- module KintoBox
4
+ require 'kinto_box/version'
5
+ require 'kinto_box/errors'
6
+ require 'kinto_box/kinto_server'
7
+ require 'kinto_box/kinto_batch_request'
8
8
 
9
+ module KintoBox
9
10
  # Initializes a new Kinto client.
10
11
  #
11
12
  # @param [String] server Url of the server without the version
12
- # @param [Hash] options Optional parameter. If the hash contains :username and :password, it will be used to authenticate.
13
- # `options` parameter can be used to pass in credentials. If no credentials are passed, it looks for KINTO_API_TOKEN environment variable
13
+ # @param [Hash] options Optional parameter. If the hash contains :username
14
+ # and :password, it will be used to authenticate.
15
+ # `options` parameter Can be used to pass in
16
+ # credentials. If no credentials are passed, it looks
17
+ # for KINTO_API_TOKEN environment variable.
14
18
  # @return [KintoBox::KintoClient] A kinto client object
15
- def KintoBox.new(server, options = nil)
16
- return KintoClient.new(server, options)
19
+ def self.new(*args, **kwargs, &blk)
20
+ KintoClient.new(*args, **kwargs, &blk)
17
21
  end
18
22
 
19
23
  class KintoClient
@@ -24,49 +28,53 @@ module KintoBox
24
28
  # Initializes a new Kinto client.
25
29
  #
26
30
  # @param [String] server Url of the server without the version
27
- # @param [Hash] options Optional parameter. If the hash contains :username and :password, it will be used to authenticate.
28
- # `options` parameter can be used to pass in credentials. If no credentials are passed, it looks for KINTO_API_TOKEN environment variable
31
+ # @param [Hash] options Optional parameter. If the hash contains :username
32
+ # and :password, it will be used to authenticate.
33
+ # `options` parameter Can be used to pass in
34
+ # credentials. If no credentials are passed, it looks
35
+ # for KINTO_API_TOKEN environment variable.
29
36
  # @return [KintoBox::KintoClient] A kinto client object
30
- def initialize(server, options = nil)
31
- @server = server
32
- self.class.base_uri URI.join(@server, '/v1/').to_s
37
+ def initialize(server, username: nil, password: nil)
38
+ self.class.base_uri(URI.join(server, '/v1/').to_s)
33
39
 
34
- unless options.nil? || options[:username].nil? || options[:password].nil?
35
- @auth = Base64.encode64("#{options[:username]}:#{options[:password]}")
36
- end
40
+ auth = if username && password
41
+ Base64.encode64("#{username}:#{password}")
42
+ else
43
+ ENV['KINTO_API_TOKEN']
44
+ end
37
45
 
38
- @auth = ENV['KINTO_API_TOKEN'] if @auth.nil?
39
- self.class.headers('Authorization' => "Basic #{@auth}")
46
+ self.class.headers('Authorization' => "Basic #{auth}")
47
+
48
+ @server = KintoServer.new(client: self)
40
49
  end
41
50
 
42
51
  # Get reference to a bucket
43
52
  #
44
53
  # @param [String] bucket_id The id of the bucket
45
54
  # @return [KintoBox::KintoBucket] A kinto bucket object
46
- def bucket (bucket_id)
47
- @bucket = KintoBucket.new(self, bucket_id)
48
- @bucket
55
+ def bucket(bucket_id)
56
+ @server.bucket(bucket_id)
49
57
  end
50
58
 
51
59
  # Get server information
52
60
  #
53
61
  # @return [Hash] Server info as a hash
54
62
  def server_info
55
- get '/'
63
+ @server.info
56
64
  end
57
65
 
58
66
  # Get current user id
59
67
  #
60
68
  # @return [String] current user id
61
69
  def current_user_id
62
- server_info['user']['id']
70
+ @server.current_user_id
63
71
  end
64
72
 
65
73
  # List of buckets
66
74
  #
67
75
  # @return [Hash] with list of buckets
68
76
  def list_buckets
69
- get '/buckets'
77
+ @server.list_buckets
70
78
  end
71
79
 
72
80
  # Create a bucket
@@ -74,56 +82,22 @@ module KintoBox
74
82
  # @param [String] bucket_id The id of the bucket
75
83
  # @return [KintoBox::KintoBucket] A kinto bucket object
76
84
  def create_bucket(bucket_id)
77
- put "/buckets/#{bucket_id}"
78
- bucket(bucket_id)
85
+ @server.create_bucket(bucket_id)
79
86
  end
80
87
 
81
88
  # Delete all buckets
82
- #
89
+ # @return [Hash] API response
83
90
  def delete_buckets
84
- delete '/buckets'
91
+ @server.delete_buckets
85
92
  end
86
93
 
87
- # Raw request
88
- #
89
- def request(path, method, data = {})
90
- case method.upcase
91
- when 'PUT'
92
- self.class.put(path, :body => data.to_json)
93
- when 'GET'
94
- self.class.get(path)
95
- when 'POST'
96
- self.class.post(path, :body => data.to_json)
97
- when 'DELETE'
98
- self.class.delete(path)
99
- when 'OPTIONS'
100
- self.class.options(path)
101
- when 'HEAD'
102
- self.class.head(path)
103
- when 'MOVE'
104
- self.class.move(path, :body => data.to_json)
105
- when 'COPY'
106
- self.class.copy(path, :body => data.to_json)
107
- when 'PATCH'
108
- self.class.copy(path, :body => data.to_json)
109
- else
110
- raise HTTPBadRequest
111
- end
112
- end
113
-
114
- # Make batch requests
115
- def create_batch_request
116
- KintoBatchRequest.new(self)
117
- end
118
-
119
-
120
94
  # Calls http PUT on path
121
95
  #
122
96
  # @params [String]path Url path
123
97
  # @params [Hash] data to be sent in the body
124
98
  # @return [Hash] response body
125
99
  def put(path, data = {})
126
- ResponseHandler.handle self.class.put(path, :body => data.to_json)
100
+ request 'PUT', path, body: data.to_json
127
101
  end
128
102
 
129
103
  # Calls http POST on path
@@ -132,7 +106,7 @@ module KintoBox
132
106
  # @params [Hash] data to be sent in the body
133
107
  # @return [Hash] response body
134
108
  def post(path, data = {})
135
- ResponseHandler.handle self.class.post(path, :body => data.to_json)
109
+ request 'POST', path, body: data.to_json
136
110
  end
137
111
 
138
112
  # Calls http PATCH on path
@@ -141,34 +115,113 @@ module KintoBox
141
115
  # @params [Hash] data to be sent in the body
142
116
  # @return [Hash] response body
143
117
  def patch(path, data)
144
- ResponseHandler.handle self.class.patch(path, :body => data.to_json)
118
+ request 'PATCH', path, body: data.to_json
145
119
  end
146
120
 
147
121
  # Calls http DELETE on path
148
122
  #
149
123
  # @params [String]path Url path
150
- # @params [Hash] data to be sent in the body
151
124
  # @return [Hash] response body
152
125
  def delete(path)
153
- ResponseHandler.handle self.class.delete(path)
126
+ request 'DELETE', path
154
127
  end
155
128
 
156
129
  # Calls http GET on path
157
130
  #
158
131
  # @params [String]path Url path
159
- # @params [Hash] data to be sent in the body
160
132
  # @return [Hash] response body
161
133
  def get(path)
162
- ResponseHandler.handle self.class.get(path)
134
+ request 'GET', path
163
135
  end
164
136
 
165
137
  # Calls http HEAD on path
166
138
  #
167
139
  # @params [String]path Url path
168
- # @params [Hash] data to be sent in the body
169
140
  # @return [Hash] response body
170
- def head(path, data = {})
171
- ResponseHandler.get_response_head self.class.head(path)
141
+ def head(path)
142
+ request 'HEAD', path
143
+ end
144
+
145
+ # Get a request object
146
+ # @param [String] method
147
+ # @param [String] path
148
+ # @param [Hash] body
149
+ # @return [KintoRequest] Request object
150
+ def create_request(method, path, body = {})
151
+ KintoRequest.new self, method, path, body
152
+ end
153
+
154
+ # Make batch requests
155
+ # @return [KintoBatchRequest] New back request object
156
+ def create_batch_request
157
+ KintoBatchRequest.new self
158
+ end
159
+
160
+ # Make batch requests
161
+ # results = client.batch do req
162
+ # req.add_request(...)
163
+ # end
164
+ def batch
165
+ req = create_batch_request
166
+ if block_given?
167
+ yield req
168
+ req.execute
169
+ else
170
+ req
171
+ end
172
+ end
173
+
174
+ # Send a prepared request
175
+ # @param [KintoRequest] request
176
+ # @return [Hash] response
177
+ def send_request(request_obj)
178
+ request(request_obj.method, request_obj.path, body: request_obj.body.to_json)
179
+ end
180
+
181
+ private
182
+
183
+ # Handle all the kinds of requests
184
+ #
185
+ # @param [String] HTTP method
186
+ # @param [String] Path to query
187
+ # @return [Hash] Return data
188
+ def request(method, path, **kwargs)
189
+ verbs = %w(put get post delete options head move copy patch)
190
+ method = method.to_s.downcase
191
+ raise HTTPBadRequest("Unsupported HTTP method #{method}") unless verbs.include?(method)
192
+ resp = self.class.send(method.to_sym, path, **kwargs)
193
+ if method == 'head'
194
+ handle_response resp, head_instead: true
195
+ else
196
+ handle_response resp
197
+ end
198
+ end
199
+
200
+ # Parse and process HTTP response. Check for codes, and parse return body.
201
+ # @param [Object] Response object
202
+ # @param [Boolean] <head_instead> Return headers instead of body
203
+ # @return [Hash] Response payload
204
+ def handle_response(resp, head_instead: false)
205
+ case resp.code
206
+ when 200, 201
207
+ head_instead ? resp.headers : JSON.parse(resp.body)
208
+ when 202, 204
209
+ true
210
+ when 400
211
+ raise BadRequest, resp
212
+ when 401
213
+ raise NotAllowed, resp
214
+ when 403
215
+ raise NotAuthorized, resp
216
+ when 404
217
+ raise NotFound, resp
218
+ else
219
+ if resp.code >= 500
220
+ raise ServerError, resp
221
+ else
222
+ raise Error, resp
223
+ end
224
+ end
172
225
  end
173
226
  end
174
227
  end
@@ -28,34 +28,4 @@ module KintoBox
28
28
 
29
29
  # Raised when there is some sort of error on the server
30
30
  class ServerError < Error; end
31
-
32
- class ResponseHandler
33
- def self.handle(resp)
34
- if [200, 201].include? resp.code
35
- JSON.parse resp.body
36
- elsif [202, 204].include? resp.code
37
- true
38
- elsif resp.code == 400
39
- raise BadRequest, resp
40
- elsif resp.code == 404
41
- raise NotFound, resp
42
- elsif resp.code == 401
43
- raise NotAllowed, resp
44
- elsif resp.code == 403
45
- raise NotAuthorized, resp
46
- elsif resp.code >= 500
47
- raise ServerError, resp
48
- else
49
- raise Error, resp
50
- end
51
- end
52
-
53
- def self.get_response_head(resp)
54
- if [200, 201].include? resp.code
55
- resp.headers
56
- else
57
- raise BadRequest, resp
58
- end
59
- end
60
- end
61
31
  end
@@ -1,30 +1,26 @@
1
- require 'kinto_box/kinto_collection'
2
- require 'kinto_box/kinto_object'
3
1
  require 'kinto_box/kinto_request'
4
-
5
2
  module KintoBox
6
- class KintoBatchRequest
3
+ class KintoBatchRequest < KintoRequest
4
+ attr_reader :requests
7
5
 
8
- def initialize(kinto_client)
9
- @kinto_client = kinto_client
10
- @request_data = {'defaults' => {
11
- 'method'=> 'POST',
12
- 'path'=> '/'
13
- },
14
- 'requests' =>[]
15
- }
16
- @requests = @request_data['requests']
6
+ def initialize(client)
7
+ @requests = []
8
+ super(client, 'POST', '/batch')
17
9
  end
18
10
 
19
11
  def add_request(request)
20
- @requests.push(request.hashed_object)
21
- self
12
+ requests.push(request.to_hash)
13
+ self
22
14
  end
23
15
 
24
- def send
25
-
26
- @kinto_client.post('/batch', @request_data)
16
+ def body
17
+ {
18
+ 'defaults' => {
19
+ 'method' => 'POST',
20
+ 'path' => '/'
21
+ },
22
+ 'requests' => requests
23
+ }
27
24
  end
28
-
29
25
  end
30
- end
26
+ end
@@ -1,62 +1,40 @@
1
1
  require 'kinto_box/kinto_collection'
2
- require 'kinto_box/kinto_object'
3
2
  require 'kinto_box/kinto_group'
4
3
  module KintoBox
5
4
  class KintoBucket < KintoObject
5
+ child_class KintoCollection
6
6
 
7
- attr_reader :kinto_client
7
+ alias_method :collection, :child
8
8
 
9
- def initialize (client, bucket_id)
10
- raise ArgumentError if bucket_id.nil? || client.nil?
9
+ alias_method :list_collections, :list_children
10
+ alias_method :delete_collections, :delete_children
11
+ alias_method :count_collections, :count_children
11
12
 
12
- @kinto_client = client
13
- @id = bucket_id
14
- @url_path = "/buckets/#{@id}"
15
- @child_path = '/collections'
16
- end
17
-
18
- def list_collections(filters = nil, sort = nil)
19
- @kinto_client.get(url_w_qsp(filters, sort))
20
- end
21
-
22
- def list_groups
23
- @kinto_client.get("#{@url_path}/groups")
24
- end
25
-
26
- def collection (collection_id)
27
- @collection = KintoCollection.new(self, collection_id)
28
- end
13
+ alias_method :create_collection_request, :create_child_request
14
+ alias_method :list_collections_request, :list_children_request
15
+ alias_method :delete_collections_request, :delete_children_request
16
+ alias_method :count_collections_request, :count_children_request
29
17
 
30
18
  def group(group_id)
31
- @group = KintoGroup.new(self, group_id)
19
+ KintoGroup.new(id: group_id, parent: self)
32
20
  end
33
21
 
34
- def create_collection(collection_id)
35
- @kinto_client.post("#{@url_path}#{@child_path}", { 'data' => { 'id' => collection_id}})
36
- collection(collection_id)
22
+ def list_groups
23
+ @client.get("#{url_path}/groups")
37
24
  end
38
25
 
39
26
  def create_group(group_id, members)
40
27
  members = [members] unless members.is_a?(Array)
41
- @kinto_client.put("#{@url_path}/groups/#{group_id}", { 'data' => { 'members' => members}})
42
- group(group_id)
43
- end
44
-
45
- def delete_collections
46
- @kinto_client.delete(url_w_qsp)
28
+ resp = @client.put("#{url_path}/groups/#{group_id}", 'data' => { 'members' => members })
29
+ KintoGroup.new(parent: self, info: resp)
47
30
  end
48
31
 
49
32
  def delete_groups
50
- @kinto_client.delete("#{@url_path}/groups")
33
+ @client.delete("#{url_path}/groups")
51
34
  end
52
35
 
53
- def count_collections(filters = nil)
54
- @kinto_client.head(url_w_qsp(filters))['Total-Records'].to_i
36
+ def create_collection(id)
37
+ create_child(id: id)
55
38
  end
56
-
57
- alias_method :create_collection_request, :create_child_request
58
- alias_method :list_collection_request, :list_children_request
59
- alias_method :delete_collections_request, :delete_children_request
60
- alias_method :count_collections_request, :count_children_request
61
39
  end
62
- end
40
+ end
@@ -1,52 +1,19 @@
1
1
  require 'kinto_box/kinto_record'
2
- require 'kinto_box/kinto_object'
3
- require 'kinto_box/kinto_batch_request'
4
-
5
2
  module KintoBox
6
3
  class KintoCollection < KintoObject
4
+ child_class KintoRecord
7
5
 
6
+ alias_method :bucket, :parent
7
+ alias_method :record, :child
8
8
 
9
- attr_reader :bucket
10
-
11
- def initialize (bucket, collection_id)
12
- raise ArgumentError if bucket.nil? || collection_id.nil?
13
- @kinto_client = bucket.kinto_client
14
- @bucket = bucket
15
- @id = collection_id
16
- @url_path = "/buckets/#{bucket.id}/collections/#{@id}"
17
- @child_path = '/records'
18
- end
19
-
20
- def record (record_id)
21
- KintoRecord.new(self, record_id)
22
- end
23
-
24
-
25
- def list_records(filters = nil, sort = nil)
26
- @kinto_client.get(url_w_qsp(filters, sort))
27
- end
28
-
29
-
30
- def create_record(data)
31
- resp = @kinto_client.post("#{@url_path}#{@child_path}", { 'data' => data})
32
- record_id = resp['data']['id']
33
- record(record_id)
34
- end
35
-
36
-
37
- def delete_records(filters = nil)
38
- @kinto_client.delete(url_w_qsp(filters))
39
- end
40
-
41
-
42
- def count_records(filters = nil)
43
- @kinto_client.head(url_w_qsp(filters))['Total-Records'].to_i
44
- end
45
-
9
+ alias_method :create_record, :create_child
10
+ alias_method :list_records, :list_children
11
+ alias_method :delete_records, :delete_children
12
+ alias_method :count_records, :count_children
46
13
 
47
14
  alias_method :create_record_request, :create_child_request
48
15
  alias_method :list_records_request, :list_children_request
49
16
  alias_method :delete_records_request, :delete_children_request
50
17
  alias_method :count_records_request, :count_children_request
51
18
  end
52
- end
19
+ end
@@ -1,34 +1,21 @@
1
1
  require 'kinto_box/kinto_object'
2
-
3
2
  module KintoBox
4
3
  class KintoGroup < KintoObject
5
-
6
- attr_accessor :id
7
- attr_reader :bucket
8
-
9
- def initialize (bucket, group_id)
10
- raise ArgumentError if bucket.nil? || group_id.nil?
11
- @kinto_client = bucket.kinto_client
12
- @bucket = bucket
13
- @id = group_id
14
- @url_path = "/buckets/#{bucket.id}/groups/#{@id}"
15
- end
16
-
17
4
  def update_members(members)
18
5
  members = [members] unless members.is_a?(Array)
19
- update({ 'members' => members })
6
+ update 'members' => members
20
7
  end
21
8
 
22
9
  def add_member(member)
23
10
  members = info['data']['members']
24
11
  members << member
25
- update({ 'members' => members })
12
+ update 'members' => members
26
13
  end
27
14
 
28
15
  def remove_member(member)
29
16
  members = info['data']['members']
30
17
  members.delete(member)
31
- update({ 'members' => members })
18
+ update 'members' => members
32
19
  end
33
20
  end
34
- end
21
+ end
@@ -1,90 +1,231 @@
1
1
  module KintoBox
2
2
  class KintoObject
3
+ class << self
4
+ # Get the name of this class suitable for use in url path
5
+ def path_name
6
+ name.sub('KintoBox::Kinto', '').downcase + 's'
7
+ end
3
8
 
4
- attr_accessor :id
5
- attr_reader :url_path
9
+ # Assign or retrieve the child class
10
+ def child_class(value = nil)
11
+ return @child_class if value.nil?
12
+ @child_class = value
13
+ end
14
+ end
6
15
 
7
- def info
8
- @kinto_client.get(@url_path)
16
+ attr_reader :id, :parent, :client
17
+
18
+ def initialize(client: nil, id: nil, parent: nil, info: nil)
19
+ @client = client || parent.client
20
+ @parent = parent
21
+ @id = id || (info ? info['data']['id'] : nil)
22
+ @info = info
23
+ self
9
24
  end
10
25
 
11
- def delete
12
- @kinto_client.delete(@url_path)
26
+ # Get the path to this object in the Kinto API.
27
+ # This method uses the name of this class and whatever @parent.url_path is
28
+ # to create a partial url path.
29
+ # @return [String] URL fragment
30
+ def url_path
31
+ path = "/#{self.class.path_name}/#{id}"
32
+ path = parent.url_path + path unless parent.nil?
33
+ path.gsub(%r{/+}, '/')
13
34
  end
14
35
 
15
- def update(data)
16
- @kinto_client.patch(@url_path, {'data' => data})
36
+ # Get the path to this object in the Kinto API
37
+ # Use the name of url_path and whatever the child class has set for @parent.url_path is
38
+ # to create a partial url path.
39
+ # @return [String] URL fragment
40
+ def child_path
41
+ return unless child_class?
42
+ "#{url_path}/#{child_class.path_name}".gsub(%r{/+}, '/')
17
43
  end
18
44
 
45
+ # Check to see if this Kinto object exists
46
+ # @return [Boolean]
19
47
  def exists?
20
48
  begin
21
- info
49
+ return false if info && info['data'] && info['data']['deleted'] == true
22
50
  rescue
23
51
  return false
24
52
  end
25
53
  true
26
54
  end
27
55
 
28
- def info_request
29
- KintoRequest.new('GET', @url_path)
56
+ # Get the data related to this object
57
+ # @return [Hash] Object data
58
+ def info
59
+ @info ||= info_request.execute
30
60
  end
31
61
 
32
- def update_request(data)
33
- KintoRequest.new('PATCH', @url_path, {'data' => data})
62
+ # Delete the cached info for this object
63
+ def reload
64
+ @info = nil
34
65
  end
35
66
 
36
- def delete_request
37
- KintoRequest.new('DELETE', @url_path)
67
+ # Delete this object
68
+ # @return [Hash] Response
69
+ def delete
70
+ @info = delete_request.execute
38
71
  end
39
72
 
40
- def create_child_request(data)
41
- KintoRequest.new('POST', url_w_qsp, { 'data' => data})
73
+ # Update this object
74
+ # @return [Hash] Response
75
+ def update(data)
76
+ @info = update_request(data).execute
42
77
  end
43
78
 
44
- def list_children_request(filters = nil, sort = nil)
45
- KintoRequest.new('GET', url_w_qsp(filters, sort))
79
+ # Replace all data in the object
80
+ # @return [Hash] Response
81
+ def replace(data)
82
+ @info = replace_request(data).execute
46
83
  end
47
84
 
48
- def delete_children_request(filters = nil)
49
- KintoRequest.new('DELETE', url_w_qsp(filters))
85
+ # Return an object for this child
86
+ # @param [String] Child object ID
87
+ # @return [KintoObject] Child for ID
88
+ def child(child_id)
89
+ child_class.new(id: child_id, parent: self)
50
90
  end
51
91
 
52
- def count_children_request(filters = nil)
53
- KintoRequest.new('HEAD', url_w_qsp(filters))
92
+ # Create a child object
93
+ # @param [String] Child object ID
94
+ # @return [KintoObject] New child
95
+ def create_child(data)
96
+ resp = create_child_request(data).execute
97
+ child_class.new(info: resp, parent: self)
98
+ end
99
+
100
+ # Get all children
101
+ # @param [String,Array] Filters
102
+ # @param [String] Sort by
103
+ # @return [Hash] All children
104
+ def list_children(filters = nil, sort = nil)
105
+ list_children_request(filters, sort).execute
106
+ end
107
+
108
+ # Get all children
109
+ # @param [String,Array] Filters
110
+ # @param [String] Sort by
111
+ # @return [Hash] All children
112
+ def delete_children(filters = nil)
113
+ delete_children_request(filters).execute
54
114
  end
55
115
 
116
+ # Count all children
117
+ # @param [String,Array] Filters
118
+ # @return [Integer] Count
119
+ def count_children(filters = nil)
120
+ count_children_request(filters).execute['Total-Records'].to_i
121
+ end
122
+
123
+ # Add a permission to this object
124
+ # @param [String] Principal aka user or group name
125
+ # @param [String] Permission name i.e. read, write, etc
126
+ # @return [KintoObject] The current instance; for chaining
56
127
  def add_permission(principal, permission)
57
- @kinto_client.patch(@url_path, {'permissions' => { permission => [principal_name(principal)] }})
128
+ @info = client.patch(url_path, 'permissions' => { permission => [principal_name(principal)] })
58
129
  self
59
130
  end
60
131
 
132
+ # Replace all permissions for this object
133
+ # @param [String] Principal aka user or group name
134
+ # @param [String] Permission name i.e. read, write, etc
135
+ # @return [KintoObject] The current instance; for chaining
61
136
  def replace_permission(principal, permission)
62
- @kinto_client.put(@url_path, {'permissions' => { permission => [principal_name(principal)] }})
137
+ @info = client.put(url_path, 'permissions' => { permission => [principal_name(principal)] })
63
138
  self
64
139
  end
65
140
 
141
+ # Get the permissions for the current object
142
+ # @return [Hash] Hash of permissions and assigned principals.
66
143
  def permissions
67
144
  info['permissions']
68
145
  end
69
146
 
147
+ # Get a kinto request object for making an info request
148
+ # @return [KintoRequest] Object representing this request
149
+ def info_request
150
+ client.create_request('GET', url_path)
151
+ end
152
+
153
+ # Get a kinto request object for making an update request
154
+ # @param [Hash] Data
155
+ # @return [KintoRequest] Object representing this request
156
+ def update_request(data)
157
+ client.create_request('PATCH', url_path, 'data' => data)
158
+ end
159
+
160
+ # Get a kinto request object for making a delete request
161
+ # @param [Hash] Data
162
+ # @return [KintoRequest] Object representing this request
163
+ def replace_request(data)
164
+ client.create_request('PUT', url_path, 'data' => data)
165
+ end
166
+
167
+ # Get a kinto request object for making a delete request
168
+ # @return [KintoRequest] Object representing this request
169
+ def delete_request
170
+ client.create_request('DELETE', url_path)
171
+ end
172
+
173
+ # Get a kinto request object for making a create child request
174
+ # @return [KintoRequest] Object representing this request
175
+ def create_child_request(data)
176
+ client.create_request('POST', url_w_qsp, 'data' => data)
177
+ end
178
+
179
+ # Get a kinto request object for making a list children request
180
+ # @return [KintoRequest] Object representing this request
181
+ def list_children_request(filters = nil, sort = nil)
182
+ client.create_request('GET', url_w_qsp(filters, sort))
183
+ end
184
+
185
+ # Get a kinto request object for making a delete children request
186
+ # @return [KintoRequest] Object representing this request
187
+ def delete_children_request(filters = nil)
188
+ client.create_request('DELETE', url_w_qsp(filters))
189
+ end
190
+
191
+ # Get a kinto request object for making a count children request
192
+ # @return [KintoRequest] Object representing this request
193
+ def count_children_request(filters = nil)
194
+ client.create_request('HEAD', url_w_qsp(filters))
195
+ end
70
196
 
71
197
  private
72
198
 
199
+ # Get the class for this object's child
200
+ # @return [KintoObject] Child class
201
+ def child_class
202
+ self.class.child_class
203
+ end
204
+
205
+ # Does this class have a child class?
206
+ # @return [Boolean]
207
+ def child_class?
208
+ !(child_class.nil? || child_class.is_a?(KintoObject))
209
+ end
210
+
211
+ # Convert the principal name to something Kinto will like
212
+ # @return [String] Valid Kinto principal name
73
213
  def principal_name(principal)
74
214
  case principal.downcase
75
- when 'authenticated'
76
- return 'system.Authenticated'
77
- when 'anonymous'
78
- return 'system.Everyone'
79
- when 'everyone'
80
- return 'system.Everyone'
81
- else
82
- return principal
215
+ when 'authenticated'
216
+ 'system.Authenticated'
217
+ when 'anonymous'
218
+ 'system.Everyone'
219
+ when 'everyone'
220
+ 'system.Everyone'
221
+ else
222
+ principal
83
223
  end
84
224
  end
85
225
 
226
+ # Build the path including querystring
86
227
  def url_w_qsp(filters = nil, sort = nil, add_child = true)
87
- url = @child_path.nil? || !add_child ? @url_path : "#{@url_path}#{@child_path}"
228
+ url = child_path.nil? || !add_child ? url_path : child_path
88
229
  query_string = ''
89
230
  query_string = filters unless filters.nil?
90
231
  query_string = "#{query_string}&" unless filters.nil? || sort.nil?
@@ -92,4 +233,4 @@ module KintoBox
92
233
  query_string == '' ? url : "#{url}?#{query_string}"
93
234
  end
94
235
  end
95
- end
236
+ end
@@ -1,17 +1,5 @@
1
1
  require 'kinto_box/kinto_object'
2
2
  module KintoBox
3
3
  class KintoRecord < KintoObject
4
- attr_reader :id
5
- def initialize (collection, record_id)
6
- raise ArgumentError if collection.nil? || record_id.nil?
7
- @kinto_client = collection.bucket.kinto_client
8
- @id = record_id
9
- @url_path = "/buckets/#{collection.bucket.id}/collections/#{collection.id}/records/#{@id}"
10
- @child_path = nil
11
- end
12
-
13
- def replace(data)
14
- @kinto_client.put(@url_path, {'data' => data})
15
- end
16
4
  end
17
- end
5
+ end
@@ -1,18 +1,21 @@
1
1
  module KintoBox
2
2
  class KintoRequest
3
+ attr_reader :method, :path, :body, :headers
3
4
 
4
- def initialize(method = 'GET', path = nil , body = {}, headers = nil)
5
+ def initialize(client, method, path, body = {}, headers: nil)
6
+ @client = client
5
7
  @method = method
6
8
  @path = path
7
9
  @body = body
8
10
  @headers = headers
9
11
  end
10
12
 
11
- def hashed_object
12
- { 'method' => @method,
13
- 'path' => @path,
14
- 'body' => @body
15
- }
13
+ def to_hash
14
+ { 'method' => method, 'path' => path, 'body' => body }
15
+ end
16
+
17
+ def execute
18
+ @client.send_request(self)
16
19
  end
17
20
  end
18
- end
21
+ end
@@ -0,0 +1,31 @@
1
+ require 'kinto_box/kinto_bucket'
2
+ module KintoBox
3
+ class KintoServer < KintoObject
4
+ child_class KintoBox::KintoBucket
5
+
6
+ alias_method :bucket, :child
7
+
8
+ alias_method :list_buckets, :list_children
9
+ alias_method :delete_buckets, :delete_children
10
+ alias_method :count_buckets, :count_children
11
+
12
+ alias_method :create_bucket_request, :create_child_request
13
+ alias_method :list_bucket_request, :list_children_request
14
+ alias_method :delete_buckets_request, :delete_children_request
15
+ alias_method :count_buckets_request, :count_children_request
16
+
17
+ def url_path
18
+ '/'
19
+ end
20
+
21
+ # Get current user id
22
+ # @return [String] current user id
23
+ def current_user_id
24
+ info['user']['id']
25
+ end
26
+
27
+ def create_bucket(id)
28
+ create_child(id: id)
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module KintoBox
2
- VERSION = "0.1.12"
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kinto_box
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kavya Sukumar
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-01 00:00:00.000000000 Z
11
+ date: 2016-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -86,6 +86,34 @@ dependencies:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
88
  version: '5.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rdoc
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
89
117
  description: Kinto http client in ruby
90
118
  email:
91
119
  - Kavya.Sukumar@voxmedia.com
@@ -94,6 +122,7 @@ extensions: []
94
122
  extra_rdoc_files: []
95
123
  files:
96
124
  - ".gitignore"
125
+ - ".rubocop.yml"
97
126
  - ".travis.yml"
98
127
  - Gemfile
99
128
  - LICENSE.txt
@@ -103,6 +132,7 @@ files:
103
132
  - bin/setup
104
133
  - kinto_box.gemspec
105
134
  - lib/kinto_box.rb
135
+ - lib/kinto_box/errors.rb
106
136
  - lib/kinto_box/kinto_batch_request.rb
107
137
  - lib/kinto_box/kinto_bucket.rb
108
138
  - lib/kinto_box/kinto_collection.rb
@@ -110,7 +140,7 @@ files:
110
140
  - lib/kinto_box/kinto_object.rb
111
141
  - lib/kinto_box/kinto_record.rb
112
142
  - lib/kinto_box/kinto_request.rb
113
- - lib/kinto_box/response_handler.rb
143
+ - lib/kinto_box/kinto_server.rb
114
144
  - lib/kinto_box/version.rb
115
145
  homepage: http://github.com/voxmedia/kinto_box
116
146
  licenses:
@@ -133,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
163
  version: '0'
134
164
  requirements: []
135
165
  rubyforge_project:
136
- rubygems_version: 2.4.5
166
+ rubygems_version: 2.4.5.1
137
167
  signing_key:
138
168
  specification_version: 4
139
169
  summary: Kinto http client in ruby