Swiftpack.co - Package - SwiftScream/URITemplate

URITemplate license GitHub release

Travis Codecov branch

Swift 4.1 CocoaPods compatible Carthage compatible Swift Package Manager compatible

A robust and performant Swift 4.1 implementation of RFC6570 URI Template. Full Level 4 support is provided.

Getting Started

Manual

Static Framework targets are provided for iOS, MacOS, tvOS, and watchOS; integrate them as you normally would.

CocoaPods

Add pod 'ScreamURITemplate', '~> 2.0' to your Podfile

Carthage

Add github "SwiftScream/URITemplate" ~> 2.0 to your Cartfile

For simulator builds you will need to add -fprofile-instr-generate to OTHER_LDFLAGS; device builds will be fine without it This is due to this open issue with carthage: https://github.com/Carthage/Carthage/issues/2363 The alternative would be to disable code coverage :disappointed:

Swift Package Manager

Add .package(url: "https://github.com/SwiftScream/URITemplate.git", from: "2.0.0") to your package.swift dependencies

Usage

Template Processing

let template = try URITemplate(string:"https://api.github.com/repos/{owner}/{repository}/traffic/views")
let variables = ["owner":"SwiftScream", "repository":"URITemplate"]
let urlString = try template.process(variables)
// https://api.github.com/repos/SwiftScream/URITemplate/traffic/views

When Things Go Wrong

Both template initialization and processing can fail; throwing a URITemplate.Error The error cases contain associated values specifying a string reason for the error and the index into the template string that the error occurred.

do {
    _ = try URITemplate(string: "https://api.github.com/repos/{}/{repository}")
} catch URITemplate.Error.malformedTemplate(let position, let reason) {
    // reason = "Empty Variable Name"
    // position = 29th character
}

Get variable names used in a template

let template = try URITemplate(string:"https://api.github.com/repos/{owner}/{repository}/traffic/views")
let variableNames = template.variableNames
// ["owner", "repository"]

Codable Support

URITemplate implements the Codable protocol, enabling easy serialization to or from JSON objects.

struct HALObject : Codable {
    let _links : [String:URITemplate]
}

Tests

The library is tested against the standard test suite, as well as some additional tests for behavior specific to this implementation. It is intended to keep test coverage as high as possible.

Github

link
Stars: 3
Help us keep the lights on

Dependencies

Releases

2.0.2 - Aug 3, 2018

  • Update README

2.0.1 - Jul 10, 2018

  • Update README

2.0.0 - Jul 9, 2018

  • Provide Static Framework Targets
  • Remove static library and dynamic framework targets

1.1.1 - Jul 1, 2018

  • Support back to iOS 9, tvOS 9, watchOS 2, macOS 10.11
  • Correct dynamic framework packaging

1.1.0 - May 19, 2018

  • Support Codable
  • Provide README.md
  • Add Dynamic Framework Targets
  • Support Carthage
  • Integrate with CodeCov for code coverage analysis