FeedKit Documentation

repository·main·Indexed 23 days ago

https://github.com/nmdias/feedkit

A Swift library for reading and generating RSS, Atom, and JSON feeds. It supports various namespaces such as iTunes, YouTube, and Media RSS, providing both dedicated types for specific formats and a universal Feed enum for automatic detection. The library includes utilities for identifying feed types via FeedType and supports initializing feeds from URLs, strings, and raw data.

Tokens
1K
Snippets
5
Records
7
Agent score
30%

What's inside FeedKit

  1. Read any feed type using the universal Feed enum

    main

    When the feed format is unknown, use the universal Feed enum. It automatically detects whether the feed is RSS, Atom, or JSON before parsing and decoding. You can then use a switch statement to handle the specific resulting model.

    // Read any type of feed
    let feed = try await Feed(urlString: "https://surprise.me/feed")
    
    // Use a switch to get the resulting feed model
    switch feed {
    case let .atom(feed): // An AtomFeed instance
    case let .rss(feed): // An RSSFeed instance
    case let .json(feed): // A JSONFeed instance
    }
  2. Install FeedKit via Swift Package Manager

    main

    To add FeedKit to your Xcode project:

    1. Open your project in Xcode.
    2. Go to the File menu and select Add Package Dependencies…
    3. Enter the Package URL: https://github.com/nmdias/FeedKit
    4. Select Add Package.
  3. Generate XML feeds (RSS/Atom)

    main

    To generate an XML string, create an instance of RSSFeed or AtomFeed, populate it with data, and call toXMLString(formatted:).

    let feed = RSSFeed(
      channel: .init(
        title: "Breaking News",
        link: "http://www.breakingnews.com/",
        description: "Get the latest updates as they happen.",
        // ...
        items: [
          .init(
            title: "Breaking News: All Hearts are Joyful",
            link: "http://breakingnews.com/2025/01/09/joyful-hearts",
            description: "A heartwarming story of unity and celebration."
            // ...
          ),
        ]
      )
    )
    
    try feed.toXMLString(formatted: true)
  4. Identify feed type without parsing with FeedType

    main

    To identify the type of a feed (RSS, Atom, or JSON) without the overhead of full parsing and decoding, use the FeedType initializer with raw Data. You can also check if the content is XML or JSON using the isXML and isJson properties.

    let feedType = try FeedType(data: data)
    
    // Detect feed type
    switch feedType {
    case .rss: // RSS feed detected
    case .atom: // Atom feed detected
    case .json: // JSON feed detected
    }
    
    // Or feed content type
    if feedType.isXML {
      // XML feed detected
    } else if feedType.isJson {
      // JSON feed detected
    }
  5. Read feeds using dedicated types

    main

    If you know the specific format of the feed (RSS, Atom, or JSON), use the corresponding dedicated type for more direct access to its properties.

    try await RSSFeed(urlString: "https://developer.apple.com/news/rss/news.rss")
  6. Initialize feeds from various sources

    main

    All feed types conform to FeedInitializable, providing several ways to load data. Available initializers include:

    • init(urlString: String) async throws
    • init(url: URL) async throws (handles both local and remote URLs)
    • init(fileURL url: URL) throws (local file URLs)
    • init(remoteURL url: URL) async throws (remote URLs)
    • init(string: String) throws (XML or JSON strings)
    • init(data: Data) throws (raw data)