Class: PetstoreApiClient::Models::Pet

Inherits:
Base
  • Object
show all
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

Examples:

Creating a new pet

pet = Pet.new(
  name: "Fluffy",
  status: "available",
  photo_urls: ["https://example.com/photo1.jpg"]
)

Creating a pet with nested category

pet = Pet.new(
  name: "Fluffy",
  status: "available",
  category: { id: 1, name: "Dogs" },
  tags: [{ id: 1, name: "friendly" }],
  photo_urls: ["https://example.com/photo1.jpg"]
)

Creating from API response

data = { "id" => 123, "name" => "Fluffy", "status" => "available" }
pet = Pet.from_response(data)

Converting to API format

pet_hash = pet.to_h
# Returns: { id: 123, name: "Fluffy", photoUrls: [...], status: "available" }

See Also:

Since:

  • 0.1.0

Constant Summary collapse

VALID_STATUSES =

Valid pet status values per API specification

Since:

  • 0.1.0

%w[available pending sold].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Examples:

pet = Pet.new(name: "Fluffy", photo_urls: ["http://example.com/1.jpg"])

Parameters:

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

    Pet attributes

Options Hash (attributes):

  • :id (Integer)

    Pet ID (optional, assigned by server)

  • :name (String)

    Pet name (required)

  • :photo_urls (Array<String>)

    Photo URLs (required)

  • :status (String)

    Pet status (available, pending, sold)

  • :category (Hash, Category)

    Category data or object

  • :tags (Array<Hash>, Array<Tag>)

    Array of tag data or objects

Since:

  • 0.1.0



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
  tags_data = attributes.delete(:tags) || attributes.delete("tags")
  @tags = build_tags(tags_data) if tags_data

  super
end

Instance Attribute Details

#categoryCategory?

Returns Pet category (optional nested object).

Returns:

  • (Category, nil)

    Pet category (optional nested object)



69
70
71
# File 'lib/petstore_api_client/models/pet.rb', line 69

def category
  @category
end

#idInteger?

Returns Pet ID (assigned by server).

Returns:

  • (Integer, nil)

    Pet ID (assigned by server)



51
# File 'lib/petstore_api_client/models/pet.rb', line 51

attribute :id, :integer

#nameString

Returns Pet name (required).

Returns:

  • (String)

    Pet name (required)



55
# File 'lib/petstore_api_client/models/pet.rb', line 55

attribute :name, :string

#photo_urlsArray<String>

Returns URLs of pet photos (required, minimum 1).

Returns:

  • (Array<String>)

    URLs of pet photos (required, minimum 1)



59
# File 'lib/petstore_api_client/models/pet.rb', line 59

attribute :photo_urls, default: -> { [] }

#statusString?

Returns Pet status (available, pending, or sold).

Returns:

  • (String, nil)

    Pet status (available, pending, or sold)



63
# File 'lib/petstore_api_client/models/pet.rb', line 63

attribute :status, :string

#tagsArray<Tag>

Returns Pet tags (optional nested objects).

Returns:

  • (Array<Tag>)

    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).

Examples:

response_data = {
  "id" => 123,
  "name" => "Fluffy",
  "photoUrls" => ["http://example.com/1.jpg"],
  "status" => "available"
}
pet = Pet.from_response(response_data)

Parameters:

  • data (Hash, nil)

    API response data

Returns:

  • (Pet, nil)

    New Pet instance, or nil if data is nil

Since:

  • 0.1.0



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_hHash

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.

Examples:

pet = Pet.new(name: "Fluffy", photo_urls: ["http://example.com/1.jpg"])
pet.to_h
# => { name: "Fluffy", photoUrls: ["http://example.com/1.jpg"] }

Returns:

  • (Hash)

    Pet data in API format with camelCase keys

Since:

  • 0.1.0



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: tags&.map(&:to_h),
    status: status
  }.compact # Remove nil values
end