Class: PetstoreApiClient::Models::Pet
- Defined in:
- lib/petstore_api_client/models/pet.rb
Overview
Pet resource model
Represents a pet in the Petstore API with comprehensive validations following the API specification. Uses ActiveModel for validation, attribute management, and serialization.
The Pet model supports:
-
Required and optional attributes with type casting
-
Nested objects (Category, Tags)
-
Status enum validation
-
Bi-directional conversion (to API format and from API responses)
-
ActiveModel validations
Constant Summary collapse
- VALID_STATUSES =
Valid pet status values per API specification
%w[available pending sold].freeze
Instance Attribute Summary collapse
-
#category ⇒ Category?
Pet category (optional nested object).
-
#id ⇒ Integer?
Pet ID (assigned by server).
-
#name ⇒ String
Pet name (required).
-
#photo_urls ⇒ Array<String>
URLs of pet photos (required, minimum 1).
-
#status ⇒ String?
Pet status (available, pending, or sold).
-
#tags ⇒ Array<Tag>
Pet tags (optional nested objects).
Class Method Summary collapse
-
.from_response(data) ⇒ Pet?
Create Pet instance from API response data.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Pet
constructor
Initialize a new Pet model.
-
#to_h ⇒ Hash
Convert pet to hash for API requests.
Constructor Details
#initialize(attributes = {}) ⇒ Pet
Initialize a new Pet model
Accepts attributes as a hash and builds nested Category and Tag objects if provided. Handles both symbol and string keys.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/petstore_api_client/models/pet.rb', line 98 def initialize(attributes = {}) # Handle category separately since it's a nested object category_data = attributes.delete(:category) || attributes.delete("category") @category = build_category(category_data) if category_data # Handle tags separately since they're nested objects = attributes.delete(:tags) || attributes.delete("tags") @tags = () if super end |
Instance Attribute Details
#category ⇒ Category?
Returns Pet category (optional nested object).
69 70 71 |
# File 'lib/petstore_api_client/models/pet.rb', line 69 def category @category end |
#id ⇒ Integer?
Returns Pet ID (assigned by server).
51 |
# File 'lib/petstore_api_client/models/pet.rb', line 51 attribute :id, :integer |
#name ⇒ String
Returns Pet name (required).
55 |
# File 'lib/petstore_api_client/models/pet.rb', line 55 attribute :name, :string |
#photo_urls ⇒ Array<String>
Returns URLs of pet photos (required, minimum 1).
59 |
# File 'lib/petstore_api_client/models/pet.rb', line 59 attribute :photo_urls, default: -> { [] } |
#status ⇒ String?
Returns Pet status (available, pending, or sold).
63 |
# File 'lib/petstore_api_client/models/pet.rb', line 63 attribute :status, :string |
#tags ⇒ Array<Tag>
Returns Pet tags (optional nested objects).
69 |
# File 'lib/petstore_api_client/models/pet.rb', line 69 attr_accessor :category, :tags |
Class Method Details
.from_response(data) ⇒ Pet?
Create Pet instance from API response data
Factory method that creates a Pet object from API response data. Handles both string and symbol keys, and converts camelCase API keys (photoUrls) to snake_case Ruby convention (photo_urls).
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/petstore_api_client/models/pet.rb', line 154 def self.from_response(data) return nil if data.nil? new( id: data["id"] || data[:id], name: data["name"] || data[:name], photo_urls: data["photoUrls"] || data[:photoUrls] || data[:photo_urls], category: data["category"] || data[:category], tags: data["tags"] || data[:tags], status: data["status"] || data[:status] ) end |
Instance Method Details
#to_h ⇒ Hash
Convert pet to hash for API requests
Converts the pet object to a hash suitable for API requests. Uses camelCase keys as expected by the Petstore API. Nested objects (category, tags) are also converted to hashes. Nil values are removed from the output.
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/petstore_api_client/models/pet.rb', line 124 def to_h # puts "Converting pet to hash: #{name}" if ENV['DEBUG'] { id: id, category: category&.to_h, name: name, photoUrls: photo_urls, # camelCase for API tags: &.map(&:to_h), status: status }.compact # Remove nil values end |