Skip to content

rryam/QuoteKit

Repository files navigation

QuoteKit

QuoteKit is a Swift framework for accessing quotes from the Quotable API. It provides a clean, async/await interface for fetching quotes, authors, and tags.

Requirements

As it uses the async/await feature of Swift 5.5, the platforms supported are iOS 15.0+, macOS 12.0+, watchOS 8.0+, and tvOS 15.0+.

Installation

Add QuoteKit to your project using Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/rudrankriyam/QuoteKit.git", .upToNextMajor(from: "2.0.0"))
]

Usage

QuoteKit provides static methods for fetching data. All methods are async and throw errors on failure.

Random Quote

Returns a single random quote:

let quote = try await QuoteKit.randomQuote()
print(quote.content)

You can filter by tags, length, or author:

// Quotes with specific tags
try await QuoteKit.randomQuote(tags: ["wisdom", "life"])

// Quotes within length range
try await QuoteKit.randomQuote(minLength: 50, maxLength: 150)

// Quotes by specific authors
try await QuoteKit.randomQuote(authors: ["albert-einstein"])

List Quotes

Returns a paginated list of quotes. By default returns 10 quotes per page:

let quotes = try await QuoteKit.quotes()
print("Found \(quotes.count) quotes")

Filter and sort quotes:

// Filter by tags
try await QuoteKit.quotes(tags: ["love", "friendship"])

// Filter by length
try await QuoteKit.quotes(minLength: 50, maxLength: 150)

// Filter by author
try await QuoteKit.quotes(authors: ["mark-twain"])

// Sort by date added
try await QuoteKit.quotes(sortBy: .dateAdded, order: .descending)

// Pagination
try await QuoteKit.quotes(limit: 25, page: 2)

Quote By ID

Returns a single quote by its ID:

let quote = try await QuoteKit.quote(id: "abc123")

List Authors

Returns a paginated list of authors. By default returns 10 authors per page:

let authors = try await QuoteKit.authors()

Filter and sort authors:

// Pagination
try await QuoteKit.authors(limit: 25, page: 2)

// Sort by name
try await QuoteKit.authors(sortBy: .name)

// Sort by quote count
try await QuoteKit.authors(sortBy: .quoteCount, order: .descending)

Author By ID

Returns a single author by ID:

let author = try await QuoteKit.author(id: "abc123")

Author Profile Image URL

Returns the image URL for an author's profile:

let imageURL = QuoteKit.authorImage(with: "albert-einstein")

List Tags

Returns all available tags:

let tags = try await QuoteKit.tags()

Search Quotes

Search for quotes containing specific text:

let quotes = try await QuoteKit.searchQuotes(for: "love")

Search Authors

Search for authors by name:

let authors = try await QuoteKit.searchAuthors(for: "einstein")

Data Models

Quote

Represents a single quote:

struct Quote: Decodable, Identifiable {
    let id: String
    let tags: [String]
    let content: String
    let author: String
    let authorSlug: String
    let length: Int
    let dateAdded: String
    let dateModified: String
}

Author

Represents a single author:

struct Author: Decodable, Identifiable {
    let id: String
    let link: String
    let bio: String
    let description: String
    let name: String
    let quoteCount: Int
    let slug: String
    let dateAdded: String
    let dateModified: String
    let quotes: [Quote]?
}

Quotes and Authors

These are collections that support pagination:

typealias Quotes = QuoteItemCollection<Quote>
typealias Authors = QuoteItemCollection<Author>

Tag

Represents a quote tag:

struct Tag: Decodable, Identifiable {
    let id: String
    let name: String
    let quoteCount: Int
    let dateAdded: String
    let dateModified: String
}

QuoteItemCollection

A generic paginated collection:

struct QuoteItemCollection<Item: Decodable>: Decodable {
    let count: Int
    let totalCount: Int
    let page: Int
    let totalPages: Int
    let lastItemIndex: Int?
    let results: [Item]
}

Sponsor this project

 

Packages

No packages published

Contributors 2

  •  
  •  

Languages