Class: Kami::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kami/client.rb

Constant Summary collapse

HOSTNAME =
'api.notablepdf.com'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Client

Returns a new instance of Client.



5
6
7
# File 'lib/kami/client.rb', line 5

def initialize(key)
  @kami_api_key = key
end

Instance Method Details

#base_urlObject



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

def base_url
  "https://#{HOSTNAME}"
end

#delete(path) ⇒ Object



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

def delete(path)
  RestClient.delete(url(path), request_headers)
end

#delete_document(document_id) ⇒ Object



35
36
37
38
# File 'lib/kami/client.rb', line 35

def delete_document(document_id)
  delete("documents/#{document_id}")
  true
end

#document(id) ⇒ Object



16
17
18
# File 'lib/kami/client.rb', line 16

def document(id)
  Kami::Document.new(client: self, id: id)
end

#documentsObject



9
10
11
12
13
14
# 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) ⇒ Object



40
41
42
43
# File 'lib/kami/client.rb', line 40

def get(path)
  response = RestClient.get(url(path), request_headers)
  JSON[response]
end

#post(path, payload) ⇒ Object



45
46
47
48
49
50
51
52
# 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_headersObject



66
67
68
69
70
71
72
# 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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



58
59
60
# File 'lib/kami/client.rb', line 58

def url(path)
  base_url + "/embed/#{path}"
end