class Kami::Client

Constants

HOSTNAME

Public Class Methods

new(key) click to toggle source
# File lib/kami/client.rb, line 5
def initialize(key)
  @kami_api_key = key
end

Public Instance Methods

base_url() click to toggle source
# File lib/kami/client.rb, line 62
def base_url
  "https://#{HOSTNAME}"
end
delete(path) click to toggle source
# File lib/kami/client.rb, line 54
def delete(path)
  RestClient.delete(url(path), request_headers)
end
delete_document(document_id) click to toggle source
# File lib/kami/client.rb, line 35
def delete_document(document_id)
  delete("documents/#{document_id}")
  true
end
document(id) click to toggle source
# File lib/kami/client.rb, line 16
def document(id)
  Kami::Document.new(client: self, id: id)
end
documents() click to toggle source
# File lib/kami/client.rb, line 9
def documents
  response = get('documents')
  response['documents'].map do |hash|
    Kami::Document.new(client: self, data: hash)
  end
end
get(path) click to toggle source
# File lib/kami/client.rb, line 40
def get(path)
  response = RestClient.get(url(path), request_headers)
  JSON[response]
end
post(path, payload) click to toggle source
# File lib/kami/client.rb, line 45
def post(path, payload)
  data = payload[:multipart] ? payload : payload.to_json

  path = path.include?(base_url) ? path : url(path)
  response = RestClient.post(path, data, request_headers)

  JSON[response]
end
request_headers() click to toggle source
# File lib/kami/client.rb, line 66
def request_headers
  {
    accept: 'application/json',
    content_type: 'application/json',
    authorization: "Token #{@kami_api_key}"
  }
end
upload(name: nil, document_url: nil, file: nil) click to toggle source
# File lib/kami/client.rb, line 20
def upload(name: nil, document_url: nil, file: nil)
  path = url('documents')

  if document_url
    payload = { name: name, document_url: document_url }
  elsif file
    payload = { name: name, document: File.open(file, 'rb'), multipart: true }
    # Path changes for multipart uploads
    path = base_url + '/upload/embed/documents'
  end
  raise 'Must provide document_url or file' if payload.nil?

  post(path, payload)
end
url(path) click to toggle source
# File lib/kami/client.rb, line 58
def url(path)
  base_url + "/embed/#{path}"
end