Class: Hudu::Client
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.
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
-
.api_endpoint(method, singular_method = nil, path = method) ⇒ Object
Dynamically defines methods for interacting with Hudu API resources.
Instance Method Summary collapse
-
#api_url(path) ⇒ String
Constructs the full API URL for a given path.
-
#company_articles(company_id, params = {}) ⇒ Array<Hash>
Fetches all articles for a specific company.
-
#company_asset(company_id, asset_id, params = {}) ⇒ Hash
Fetches a specific asset for a company.
-
#company_assets(id, params = {}) ⇒ Array<Hash>
Fetches all assets for a specific company.
-
#create_company_asset(company_id, asset_layout, fields) ⇒ Hash
Creates a new asset for a company.
-
#hudu_data(result, resource) ⇒ Object
Extracts resource data from the API response.
-
#update_company_asset(asset) ⇒ Hash
Updates an existing company asset.
Methods inherited from API
Methods included from Authentication
Methods included from Configuration
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
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.
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.
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.
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.
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.
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.
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.
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 |