Class: Chatpdf::Client

Inherits:
Object
  • Object
show all
Includes:
Actionable
Defined in:
lib/chatpdf/client.rb

Constant Summary collapse

BASE_URL =
"https://api.chatpdf.com/v1".freeze

Instance Method Summary collapse

Methods included from Actionable

included

Constructor Details

#initializeClient

Returns a new instance of Client.



9
10
11
# File 'lib/chatpdf/client.rb', line 9

def initialize
  @api_key = api_key_or_raise
end

Instance Method Details

#api_key_or_raiseObject



37
38
39
40
# File 'lib/chatpdf/client.rb', line 37

def api_key_or_raise
  raise Chatpdf::InvalidConfiguration, "Chatpdf is not configured" if Chatpdf.configuration.nil?
  Chatpdf.configuration.api_key || raise(Chatpdf::InvalidConfiguration, "API key is not set")
end

#endpoint(path) ⇒ Object



29
30
31
# File 'lib/chatpdf/client.rb', line 29

def endpoint(path)
  BASE_URL + path
end

#headersObject



33
34
35
# File 'lib/chatpdf/client.rb', line 33

def headers
  { "x-api-key" => @api_key, "Content-Type" => "application/json" }
end

#request(path, options = {}) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/chatpdf/client.rb', line 13

def request(path, options = {})
  HTTParty.post(
    endpoint(path),
    headers: headers,
    body: options[:body].to_json
  )
end

#upload_file(path, options = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/chatpdf/client.rb', line 21

def upload_file(path, options = {})
  HTTParty.post(
    endpoint(path),
    headers: { "x-api-key" => @api_key },
    body: options[:body]
  )
end