Class: KintoBox::KintoClient
- Inherits:
-
Object
- Object
- KintoBox::KintoClient
- Includes:
- HTTParty
- Defined in:
- lib/kinto_box.rb
Instance Method Summary collapse
-
#batch ⇒ Object
Make batch requests results = client.batch do req req.add_request(…) end.
-
#bucket(bucket_id) ⇒ KintoBox::KintoBucket
Get reference to a bucket.
-
#create_batch_request ⇒ KintoBatchRequest
Make batch requests.
-
#create_bucket(bucket_id) ⇒ KintoBox::KintoBucket
Create a bucket.
-
#create_request(method, path, body = {}) ⇒ KintoRequest
Get a request object.
-
#current_user_id ⇒ String
Get current user id.
-
#delete(path) ⇒ Hash
Calls http DELETE on path.
-
#delete_buckets ⇒ Hash
Delete all buckets.
-
#get(path) ⇒ Hash
Calls http GET on path.
-
#head(path) ⇒ Hash
Calls http HEAD on path.
-
#initialize(server, username: nil, password: nil) ⇒ KintoBox::KintoClient
constructor
Initializes a new Kinto client.
-
#list_buckets ⇒ Hash
List of buckets.
-
#patch(path, data) ⇒ Hash
Calls http PATCH on path.
-
#post(path, data = {}) ⇒ Hash
Calls http POST on path.
-
#put(path, data = {}) ⇒ Hash
Calls http PUT on path.
-
#send_request(request_obj) ⇒ Hash
Send a prepared request.
-
#server_info ⇒ Hash
Get server information.
Constructor Details
#initialize(server, username: nil, password: nil) ⇒ KintoBox::KintoClient
Initializes a new Kinto client.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/kinto_box.rb', line 37 def initialize(server, username: nil, password: nil) self.class.base_uri(URI.join(server, '/v1/').to_s) auth = if username && password Base64.encode64("#{username}:#{password}") else ENV['KINTO_API_TOKEN'] end self.class.headers('Authorization' => "Basic #{auth}") @server = KintoServer.new(client: self) end |
Instance Method Details
#batch ⇒ Object
Make batch requests
results = client.batch do req
req.add_request(...)
end
164 165 166 167 168 169 170 171 172 |
# File 'lib/kinto_box.rb', line 164 def batch req = create_batch_request if block_given? yield req req.execute else req end end |
#bucket(bucket_id) ⇒ KintoBox::KintoBucket
Get reference to a bucket
55 56 57 |
# File 'lib/kinto_box.rb', line 55 def bucket(bucket_id) @server.bucket(bucket_id) end |
#create_batch_request ⇒ KintoBatchRequest
Make batch requests
156 157 158 |
# File 'lib/kinto_box.rb', line 156 def create_batch_request KintoBatchRequest.new self end |
#create_bucket(bucket_id) ⇒ KintoBox::KintoBucket
Create a bucket
84 85 86 |
# File 'lib/kinto_box.rb', line 84 def create_bucket(bucket_id) @server.create_bucket(bucket_id) end |
#create_request(method, path, body = {}) ⇒ KintoRequest
Get a request object
150 151 152 |
# File 'lib/kinto_box.rb', line 150 def create_request(method, path, body = {}) KintoRequest.new self, method, path, body end |
#current_user_id ⇒ String
Get current user id
69 70 71 |
# File 'lib/kinto_box.rb', line 69 def current_user_id @server.current_user_id end |
#delete(path) ⇒ Hash
Calls http DELETE on path
125 126 127 |
# File 'lib/kinto_box.rb', line 125 def delete(path) request 'DELETE', path end |
#delete_buckets ⇒ Hash
Delete all buckets
90 91 92 |
# File 'lib/kinto_box.rb', line 90 def delete_buckets @server.delete_buckets end |
#get(path) ⇒ Hash
Calls http GET on path
133 134 135 |
# File 'lib/kinto_box.rb', line 133 def get(path) request 'GET', path end |
#head(path) ⇒ Hash
Calls http HEAD on path
141 142 143 |
# File 'lib/kinto_box.rb', line 141 def head(path) request 'HEAD', path end |
#list_buckets ⇒ Hash
List of buckets
76 77 78 |
# File 'lib/kinto_box.rb', line 76 def list_buckets @server.list_buckets end |
#patch(path, data) ⇒ Hash
Calls http PATCH on path
117 118 119 |
# File 'lib/kinto_box.rb', line 117 def patch(path, data) request 'PATCH', path, body: data.to_json end |
#post(path, data = {}) ⇒ Hash
Calls http POST on path
108 109 110 |
# File 'lib/kinto_box.rb', line 108 def post(path, data = {}) request 'POST', path, body: data.to_json end |
#put(path, data = {}) ⇒ Hash
Calls http PUT on path
99 100 101 |
# File 'lib/kinto_box.rb', line 99 def put(path, data = {}) request 'PUT', path, body: data.to_json end |
#send_request(request_obj) ⇒ Hash
Send a prepared request
177 178 179 |
# File 'lib/kinto_box.rb', line 177 def send_request(request_obj) request(request_obj.method, request_obj.path, body: request_obj.body.to_json) end |
#server_info ⇒ Hash
Get server information
62 63 64 |
# File 'lib/kinto_box.rb', line 62 def server_info @server.info end |