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:

  • The method name for fetching all records.

  • (defaults to: nil)

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

  • (defaults to: method)

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



38
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
# File 'lib/hudu/client.rb', line 38

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:

  • The API path.

Returns:

  • The full API URL.



144
145
146
# File 'lib/hudu/client.rb', line 144

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

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

Fetches all articles for a specific company.

Parameters:

  • The ID of the company.

  • (defaults to: {})

    Additional query parameters.

Returns:

  • A list of articles.



90
91
92
# File 'lib/hudu/client.rb', line 90

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:

  • The ID of the company.

  • The ID of the asset.

  • (defaults to: {})

    Additional query parameters.

Returns:

  • The asset details.



109
110
111
# File 'lib/hudu/client.rb', line 109

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:

  • The ID of the company.

  • (defaults to: {})

    Additional query parameters.

Returns:

  • A list of assets.



99
100
101
# File 'lib/hudu/client.rb', line 99

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:

  • The ID of the company.

  • The asset layout object.

  • The custom fields for the asset.

Returns:

  • The newly created asset data.



130
131
132
133
134
135
136
137
138
# File 'lib/hudu/client.rb', line 130

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:

  • The API response.

  • The name of the resource to extract.

Returns:

  • The resource data.



153
154
155
156
157
158
159
# File 'lib/hudu/client.rb', line 153

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:

  • The asset object to update.

Returns:

  • The updated asset data.



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

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