Class: PetstoreApiClient::Models::NamedEntity

Inherits:
Base
  • Object
show all
Defined in:
lib/petstore_api_client/models/named_entity.rb

Overview

Base class for simple entities with just ID and name Used by Category and Tag models to eliminate duplication

This is an example of the DRY principle - both Category and Tag were 100% identical, so we extracted their common behavior here.

Direct Known Subclasses

Category, Tag

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_response(data) ⇒ Object

Create from API response data Handles both string and symbol keys



26
27
28
29
30
31
32
33
# File 'lib/petstore_api_client/models/named_entity.rb', line 26

def self.from_response(data)
  return nil if data.nil?

  new(
    id: extract_field(data, :id),
    name: extract_field(data, :name)
  )
end

Instance Method Details

#to_hObject

Override to_h to use symbol keys (API expects this format)



17
18
19
20
21
22
# File 'lib/petstore_api_client/models/named_entity.rb', line 17

def to_h
  {
    id: id,
    name: name
  }.compact
end