Class: Hudu::Client

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

Overview

The Client class serves as a wrapper for the Hudu REST API, providing methods to interact with various Hudu resources.

This class dynamically defines methods to fetch, create, update, and manipulate Hudu resources such as companies, articles, assets, and more.

Examples:

Basic Usage

client.companies # Fetch all companies
client.company(1) # Fetch a company by ID
client.update_company(1, { name: "Updated Company" }) # Update a company
client.create_company({ name: "New Company" }) # Create a new company

Constant Summary

Constants included from Configuration

Hudu::Configuration::DEFAULT_PAGE_SIZE, Hudu::Configuration::DEFAULT_PAGINATION, Hudu::Configuration::DEFAULT_UA, Hudu::Configuration::VALID_OPTIONS_KEYS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from API

#config, #initialize

Methods included from Authentication

#login

Methods included from Configuration

extended, #options, #reset

Constructor Details

This class inherits a constructor from Hudu::API

Class Method Details

.api_endpoint(method, singular_method = nil, path = method) ⇒ Object

Dynamically defines methods for interacting with Hudu API resources.

Depending on the arguments, this will define methods to:

  • Fetch all records for a resource

  • Fetch a specific record by ID

  • Update a record

  • Create a new record

Examples:

Defining endpoints

api_endpoint :companies, :company
# Defines:
# - `companies(params = {})` to fetch all companies.
# - `company(id, params = {})` to fetch a single company by ID.
# - `update_companies(id, params = {})` to update a company.
# - `create_companies(params = {})` to create a new company.

Parameters:

  • method (Symbol)

    The method name for fetching all records.

  • singular_method (Symbol, nil) (defaults to: nil)

    The method name for fetching a single record by ID. Optional.

  • path (String) (defaults to: method)

    The API path for the resource. Defaults to the method name.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hudu/client.rb', line 39

def self.api_endpoint(method, singular_method = nil, path = method)
  if singular_method
    # Define method to fetch all records and one by id
    send(:define_method, method) do |params = {}|
      r = get_paged(api_url(path), params)
      hudu_data(r, method)
    end
    # Define method to fetch a single record by ID
    send(:define_method, singular_method) do |id, params = {}|
      r = get(api_url("#{path}/#{id}"), params)
      hudu_data(r, singular_method)
    end
  else
    # Define simple method to fetch data
    send(:define_method, method) do |params = {}|
      get(api_url(path), params)
    end
  end

  # Define method to update a record
  send(:define_method, "update_#{method}") do |id = nil, params = {}|
    r = put(api_url("#{path}/#{id}"), params)
    hudu_data(r, method)
  end

  # Define method to create a record
  send(:define_method, "create_#{method}") do |id = nil, params = {}|
    r = post(api_url("#{path}/#{id}"), params)
    hudu_data(r, method)
  end
end

Instance Method Details

#api_url(path) ⇒ String

Constructs the full API URL for a given path.

Parameters:

  • path (String)

    The API path.

Returns:

  • (String)

    The full API URL.



137
138
139
# File 'lib/hudu/client.rb', line 137

def api_url(path)
  "/api/v1/#{path}"
end

#company_articles(company_id, params = {}) ⇒ Array<Hash>

Fetches all articles for a specific company.

Parameters:

  • company_id (Integer)

    The ID of the company.

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

    Additional query parameters.

Returns:

  • (Array<Hash>)

    A list of articles.



92
93
94
# File 'lib/hudu/client.rb', line 92

def company_articles(company_id, params = {})
  articles({ company_id: company_id }.merge(params))
end

#company_asset(company_id, asset_id, params = {}) ⇒ Hash

Fetches a specific asset for a company.

Parameters:

  • company_id (Integer)

    The ID of the company.

  • asset_id (Integer)

    The ID of the asset.

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

    Additional query parameters.

Returns:

  • (Hash)

    The asset details.



111
112
113
# File 'lib/hudu/client.rb', line 111

def company_asset(company_id, asset_id, params = {})
  get(api_url("companies/#{company_id}/assets/#{asset_id}"), params)
end

#company_assets(id, params = {}) ⇒ Array<Hash>

Fetches all assets for a specific company.

Parameters:

  • id (Integer)

    The ID of the company.

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

    Additional query parameters.

Returns:

  • (Array<Hash>)

    A list of assets.



101
102
103
# File 'lib/hudu/client.rb', line 101

def company_assets(id, params = {})
  get_paged(api_url("companies/#{id}/assets"), params)
end

#create_company_asset(company_id, asset_layout, fields) ⇒ Hash

Creates a new asset for a company.

Parameters:

  • company_id (Integer)

    The ID of the company.

  • asset_layout (Object)

    The asset layout object.

  • fields (Array<Hash>)

    The custom fields for the asset.

Returns:

  • (Hash)

    The newly created asset data.



129
130
131
# File 'lib/hudu/client.rb', line 129

def create_company_asset(company_id, asset_layout, fields)
  hudu_data(post(api_url("companies/#{company_id}/assets"), AssetHelper.create_asset(asset_layout.name, asset_layout.id, fields), false), :asset)
end

#hudu_data(result, resource) ⇒ Object

Extracts resource data from the API response.

Parameters:

  • result (Hash, Object)

    The API response.

  • resource (Symbol)

    The name of the resource to extract.

Returns:

  • (Object)

    The resource data.



146
147
148
149
150
151
152
# File 'lib/hudu/client.rb', line 146

def hudu_data(result, resource)
  if result.is_a?(WrAPI::Request::Entity) && result.attributes[resource.to_s]
    result.send resource.to_s
  else
    result
  end
end

#update_company_asset(asset) ⇒ Hash

Updates an existing company asset.

Parameters:

  • asset (Object)

    The asset object to update.

Returns:

  • (Hash)

    The updated asset data.



119
120
121
# File 'lib/hudu/client.rb', line 119

def update_company_asset(asset)
  hudu_data(put(api_url("companies/#{asset.company_id}/assets/#{asset.id}"), AssetHelper.construct_asset(asset), false), :asset)
end