Class: KintoBox::KintoClient
- Inherits:
-
Object
- Object
- KintoBox::KintoClient
- Includes:
- HTTParty
- Defined in:
- lib/kinto_box.rb
Instance Method Summary collapse
-
#bucket(bucket_id) ⇒ KintoBox::KintoBucket
Get reference to a bucket.
-
#create_batch_request ⇒ Object
Make batch requests.
-
#create_bucket(bucket_id) ⇒ KintoBox::KintoBucket
Create a bucket.
-
#current_user_id ⇒ String
Get current user id.
-
#delete(path) ⇒ Hash
Calls http DELETE on path.
-
#delete_buckets ⇒ Object
Delete all buckets.
-
#get(path) ⇒ Hash
Calls http GET on path.
-
#head(path, data = {}) ⇒ Hash
Calls http HEAD on path.
-
#initialize(server, options = 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.
-
#request(path, method, data = {}) ⇒ Object
Raw request.
-
#server_info ⇒ Hash
Get server information.
Constructor Details
#initialize(server, options = nil) ⇒ KintoBox::KintoClient
Initializes a new Kinto client.
‘options` parameter can be used to pass in credentials. If no credentials are passed, it looks for KINTO_API_TOKEN environment variable
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/kinto_box.rb', line 30 def initialize(server, = nil) @server = server self.class.base_uri URI.join(@server, '/v1/').to_s unless .nil? || [:username].nil? || [:password].nil? @auth = Base64.encode64("#{[:username]}:#{[:password]}") end @auth = ENV['KINTO_API_TOKEN'] if @auth.nil? self.class.headers('Authorization' => "Basic #{@auth}") end |
Instance Method Details
#bucket(bucket_id) ⇒ KintoBox::KintoBucket
Get reference to a bucket
46 47 48 49 |
# File 'lib/kinto_box.rb', line 46 def bucket (bucket_id) @bucket = KintoBucket.new(self, bucket_id) @bucket end |
#create_batch_request ⇒ Object
Make batch requests
115 116 117 |
# File 'lib/kinto_box.rb', line 115 def create_batch_request KintoBatchRequest.new(self) end |
#create_bucket(bucket_id) ⇒ KintoBox::KintoBucket
Create a bucket
76 77 78 79 |
# File 'lib/kinto_box.rb', line 76 def create_bucket(bucket_id) put "/buckets/#{bucket_id}" bucket(bucket_id) end |
#current_user_id ⇒ String
Get current user id
61 62 63 |
# File 'lib/kinto_box.rb', line 61 def current_user_id server_info['user']['id'] end |
#delete(path) ⇒ Hash
Calls http DELETE on path
152 153 154 |
# File 'lib/kinto_box.rb', line 152 def delete(path) ResponseHandler.handle self.class.delete(path) end |
#delete_buckets ⇒ Object
Delete all buckets
83 84 85 |
# File 'lib/kinto_box.rb', line 83 def delete_buckets delete '/buckets' end |
#get(path) ⇒ Hash
Calls http GET on path
161 162 163 |
# File 'lib/kinto_box.rb', line 161 def get(path) ResponseHandler.handle self.class.get(path) end |
#head(path, data = {}) ⇒ Hash
Calls http HEAD on path
170 171 172 |
# File 'lib/kinto_box.rb', line 170 def head(path, data = {}) ResponseHandler.get_response_head self.class.head(path) end |
#list_buckets ⇒ Hash
List of buckets
68 69 70 |
# File 'lib/kinto_box.rb', line 68 def list_buckets get '/buckets' end |
#patch(path, data) ⇒ Hash
Calls http PATCH on path
143 144 145 |
# File 'lib/kinto_box.rb', line 143 def patch(path, data) ResponseHandler.handle self.class.patch(path, :body => data.to_json) end |
#post(path, data = {}) ⇒ Hash
Calls http POST on path
134 135 136 |
# File 'lib/kinto_box.rb', line 134 def post(path, data = {}) ResponseHandler.handle self.class.post(path, :body => data.to_json) end |
#put(path, data = {}) ⇒ Hash
Calls http PUT on path
125 126 127 |
# File 'lib/kinto_box.rb', line 125 def put(path, data = {}) ResponseHandler.handle self.class.put(path, :body => data.to_json) end |
#request(path, method, data = {}) ⇒ Object
Raw request
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/kinto_box.rb', line 89 def request(path, method, data = {}) case method.upcase when 'PUT' self.class.put(path, :body => data.to_json) when 'GET' self.class.get(path) when 'POST' self.class.post(path, :body => data.to_json) when 'DELETE' self.class.delete(path) when 'OPTIONS' self.class.(path) when 'HEAD' self.class.head(path) when 'MOVE' self.class.move(path, :body => data.to_json) when 'COPY' self.class.copy(path, :body => data.to_json) when 'PATCH' self.class.copy(path, :body => data.to_json) else raise HTTPBadRequest end end |
#server_info ⇒ Hash
Get server information
54 55 56 |
# File 'lib/kinto_box.rb', line 54 def server_info get '/' end |