Class: PetstoreApiClient::Clients::PetClient

Inherits:
PetstoreApiClient::Client show all
Includes:
Concerns::Pagination, Concerns::ResourceOperations
Defined in:
lib/petstore_api_client/clients/pet_client.rb

Overview

Client for Pet-related API endpoints Handles creation, retrieval, updating, and deletion of pets

Refactored to use ResourceOperations concern (Template Method pattern) which eliminates ~50 lines of duplication with StoreClient

Constant Summary

Constants included from Concerns::Pagination

Concerns::Pagination::DEFAULT_PAGE, Concerns::Pagination::DEFAULT_PER_PAGE, Concerns::Pagination::MAX_PER_PAGE

Instance Attribute Summary

Attributes inherited from PetstoreApiClient::Client

#configuration

Instance Method Summary collapse

Methods included from Concerns::ResourceOperations

#create_resource, #delete_resource, #get_resource, #update_resource

Methods inherited from PetstoreApiClient::Client

#configure, #initialize

Methods included from Request

#delete, #get, #post, #put

Constructor Details

This class inherits a constructor from PetstoreApiClient::Client

Instance Method Details

#find_by_status(status, options = {}) ⇒ PaginatedCollection<Pet>

Find pets by status with optional pagination

Examples:

# Get first page of available pets
pets = client.pets.find_by_status("available")

# Get second page with custom page size
pets = client.pets.find_by_status("available", page: 2, per_page: 50)

# Search multiple statuses
pets = client.pets.find_by_status(["available", "pending"])

Parameters:

  • status (String, Array<String>)

    Status value(s) to filter by Valid values: “available”, “pending”, “sold”

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

    Optional pagination and filter options

Options Hash (options):

  • :page (Integer)

    Page number (default: 1)

  • :per_page (Integer)

    Items per page (default: 25)

  • :offset (Integer)

    Alternative: offset for results

  • :limit (Integer)

    Alternative: limit for results

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/petstore_api_client/clients/pet_client.rb', line 43

def find_by_status(status, options = {})
  # Validate status values
  statuses = Array(status)
  validate_statuses!(statuses)

  # Make API request (returns all matching pets)
  params = { status: statuses.join(",") }
  resp = get("pet/findByStatus", params: params)

  # Parse response into Pet objects
  pets = Array(resp.body).map { |pet_data| Models::Pet.from_response(pet_data) }

  # Apply client-side pagination (API doesn't support server-side pagination)
  apply_client_side_pagination(pets, options)
end

#find_by_tags(tags, options = {}) ⇒ PaginatedCollection<Pet>

Find pets by tags with optional pagination

Examples:

# Find pets with specific tag
pets = client.pets.find_by_tags("friendly")

# Find pets with multiple tags
pets = client.pets.find_by_tags(["friendly", "vaccinated"], page: 1, per_page: 10)

Parameters:

  • tags (String, Array<String>)

    Tag value(s) to filter by

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

    Optional pagination options

Options Hash (options):

  • :page (Integer)

    Page number (default: 1)

  • :per_page (Integer)

    Items per page (default: 25)

Returns:

Raises:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/petstore_api_client/clients/pet_client.rb', line 73

def find_by_tags(tags, options = {})
  # Ensure tags is an array
  tag_array = Array(tags)
  raise ValidationError, "Tags cannot be empty" if tag_array.empty?

  # Make API request
  params = { tags: tag_array.join(",") }
  resp = get("pet/findByTags", params: params)

  # Parse response into Pet objects
  pets = Array(resp.body).map { |pet_data| Models::Pet.from_response(pet_data) }

  # Apply client-side pagination
  apply_client_side_pagination(pets, options)
end