Class: KintoBox::KintoClient

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

Instance Method Summary collapse

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

Parameters:

  • server (String)

    Url of the server without the version

  • options (Hash) (defaults to: nil)

    Optional parameter. If the hash contains :username and :password, it will be used to authenticate.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kinto_box.rb', line 30

def initialize(server, options = nil)
  @server = server
  self.class.base_uri URI.join(@server, '/v1/').to_s

  unless options.nil? || options[:username].nil? || options[:password].nil?
    @auth = Base64.encode64("#{options[:username]}:#{options[: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

Parameters:

  • bucket_id (String)

    The id of the bucket

Returns:



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_requestObject

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

Parameters:

  • bucket_id (String)

    The id of the bucket

Returns:



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_idString

Get current user id

Returns:

  • (String)

    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

Returns:

  • (Hash)

    response body



152
153
154
# File 'lib/kinto_box.rb', line 152

def delete(path)
  ResponseHandler.handle self.class.delete(path)
end

#delete_bucketsObject

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

Returns:

  • (Hash)

    response body



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

Returns:

  • (Hash)

    response body



170
171
172
# File 'lib/kinto_box.rb', line 170

def head(path, data = {})
  ResponseHandler.get_response_head self.class.head(path)
end

#list_bucketsHash

List of buckets

Returns:

  • (Hash)

    with 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

Returns:

  • (Hash)

    response body



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

Returns:

  • (Hash)

    response body



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

Returns:

  • (Hash)

    response body



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.options(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_infoHash

Get server information

Returns:

  • (Hash)

    Server info as a hash



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

def server_info
  get '/'
end