Class: KintoBox::KintoClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/kinto_box.rb

Instance Method Summary collapse

Constructor Details

#initialize(server, username: nil, password: nil) ⇒ KintoBox::KintoClient

Initializes a new Kinto client.

Parameters:

  • server (String)

    Url of the server without the version

  • options (Hash)

    Optional parameter. If the hash contains :username and :password, it will be used to authenticate. ‘options` parameter Can be used to pass in credentials. If no credentials are passed, it looks for KINTO_API_TOKEN environment variable.



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

#batchObject

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

Parameters:

  • bucket_id (String)

    The id of the bucket

Returns:



55
56
57
# File 'lib/kinto_box.rb', line 55

def bucket(bucket_id)
  @server.bucket(bucket_id)
end

#create_batch_requestKintoBatchRequest

Make batch requests

Returns:



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

Parameters:

  • bucket_id (String)

    The id of the bucket

Returns:



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

Parameters:

  • method (String)
  • path (String)
  • body (Hash) (defaults to: {})

Returns:



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_idString

Get current user id

Returns:

  • (String)

    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

Returns:

  • (Hash)

    response body



125
126
127
# File 'lib/kinto_box.rb', line 125

def delete(path)
  request 'DELETE', path
end

#delete_bucketsHash

Delete all buckets

Returns:

  • (Hash)

    API response



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

Returns:

  • (Hash)

    response body



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

Returns:

  • (Hash)

    response body



141
142
143
# File 'lib/kinto_box.rb', line 141

def head(path)
  request 'HEAD', path
end

#list_bucketsHash

List of buckets

Returns:

  • (Hash)

    with 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

Returns:

  • (Hash)

    response body



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

Returns:

  • (Hash)

    response body



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

Returns:

  • (Hash)

    response body



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

Parameters:

Returns:

  • (Hash)

    response



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_infoHash

Get server information

Returns:

  • (Hash)

    Server info as a hash



62
63
64
# File 'lib/kinto_box.rb', line 62

def server_info
  @server.info
end