Class: Hanko::Api::BaseResource

Inherits:
Object
  • Object
show all
Defined in:
lib/hanko/api/base_resource.rb

Overview

Base class for RESTful API resources providing standard CRUD operations. Subclasses inherit #list, #get, #create, #update, and #delete.

Instance Method Summary collapse

Constructor Details

#initialize(connection, base_path) ⇒ BaseResource

Initialize a new resource endpoint.

Parameters:

  • connection (Hanko::Connection)

    the HTTP connection to use

  • base_path (String)

    the base API path for this resource



15
16
17
18
# File 'lib/hanko/api/base_resource.rb', line 15

def initialize(connection, base_path)
  @connection = connection
  @base_path = base_path
end

Instance Method Details

#create(**params) ⇒ Resource

Create a new resource with the given attributes.

Parameters:

  • params (Hash)

    the attributes for the new resource

Returns:

  • (Resource)

    the newly created resource



42
43
44
45
# File 'lib/hanko/api/base_resource.rb', line 42

def create(**params)
  response = @connection.post(@base_path, params)
  parse_resource(response.body)
end

#delete(id) ⇒ void

This method returns an undefined value.

Delete a resource by its ID.

Parameters:

  • id (String)

    the unique identifier of the resource



61
62
63
64
# File 'lib/hanko/api/base_resource.rb', line 61

def delete(id)
  @connection.delete("#{@base_path}/#{id}")
  nil
end

#get(id) ⇒ Resource

Fetch a single resource by its ID.

Parameters:

  • id (String)

    the unique identifier of the resource

Returns:



33
34
35
36
# File 'lib/hanko/api/base_resource.rb', line 33

def get(id)
  response = @connection.get("#{@base_path}/#{id}")
  parse_resource(response.body)
end

#list(params = {}) ⇒ Array<Resource>

List all resources, optionally filtered by query parameters.

Parameters:

  • params (Hash) (defaults to: {})

    optional query parameters for filtering

Returns:

  • (Array<Resource>)

    an array of Resource objects



24
25
26
27
# File 'lib/hanko/api/base_resource.rb', line 24

def list(params = {})
  response = @connection.get(@base_path, params)
  parse_array(response.body)
end

#update(id, **params) ⇒ Resource

Update an existing resource by its ID.

Parameters:

  • id (String)

    the unique identifier of the resource

  • params (Hash)

    the attributes to update

Returns:



52
53
54
55
# File 'lib/hanko/api/base_resource.rb', line 52

def update(id, **params)
  response = @connection.put("#{@base_path}/#{id}", params)
  parse_resource(response.body)
end