Class: BridgetownDirectus::Client

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

Overview

Client for interacting with the Directus API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url:, token:) ⇒ Client

Returns a new instance of Client.

Raises:

  • (StandardError)


11
12
13
14
15
16
17
# File 'lib/bridgetown_directus/client.rb', line 11

def initialize(api_url:, token:)
  @api_url = api_url
  @token = token
  return unless @token.nil? || @api_url.nil?

  raise StandardError, "Invalid Directus configuration: missing API token or URL"
end

Instance Attribute Details

#api_urlObject (readonly)

Returns the value of attribute api_url.



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

def api_url
  @api_url
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#fetch_collection(collection, params = {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/bridgetown_directus/client.rb', line 19

def fetch_collection(collection, params = {})
  response = connection.get("/items/#{collection}") do |req|
    req.params.merge!(prepare_params(params))
  end
  handle_response(response)
end

#fetch_item(collection, id, params = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/bridgetown_directus/client.rb', line 26

def fetch_item(collection, id, params = {})
  response = connection.get("/items/#{collection}/#{id}") do |req|
    req.params.merge!(prepare_params(params))
  end
  handle_response(response)
end

#fetch_items_with_filter(collection, filter, params = {}) ⇒ Object



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

def fetch_items_with_filter(collection, filter, params = {})
  merged_params = params.merge(filter: filter)
  fetch_collection(collection, merged_params)
end


38
39
40
41
42
43
# File 'lib/bridgetown_directus/client.rb', line 38

def fetch_related_items(collection, id, relation, params = {})
  response = connection.get("/items/#{collection}/#{id}/#{relation}") do |req|
    req.params.merge!(prepare_params(params))
  end
  handle_response(response)
end