Skip to content

Commit

Permalink
Carthage rebuild for Swift 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dreymonde committed Oct 3, 2017
1 parent ea85728 commit e46bc42
Show file tree
Hide file tree
Showing 19 changed files with 258 additions and 155 deletions.
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "dreymonde/Shallows" ~> 0.3.1
github "dreymonde/Shallows" ~> 0.4.0
github "dreymonde/Avenues"
github "dreymonde/Alba"
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "dreymonde/Alba" "0.4.0"
github "dreymonde/Avenues" "0.1.4"
github "dreymonde/Shallows" "0.3.3"
github "dreymonde/Shallows" "0.4.1"
27 changes: 12 additions & 15 deletions Kit/API/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Shallows

public protocol APIProvider {

init(dataProvider: ReadOnlyCache<String, Data>)
init(dataProvider: ReadOnlyCache<APIPath, Data>)

}

Expand All @@ -21,7 +21,7 @@ internal protocol APIPoint : APIProvider {

internal protocol APICachePoint {

init(dataProvider: Cache<String, Data>)
init(dataProvider: Cache<APIPath, Data>)

}

Expand All @@ -34,17 +34,13 @@ extension APIProvider {

public static func heroku(networkCache: ReadOnlyCache<URL, Data> = Self.makeUrlSessionCache()) -> Self {
let baseURL = URL(string: "https://the-great-game-ruby.herokuapp.com")!
let subcache: ReadOnlyCache<String, Data> = networkCache
.mapKeys({ URL.init(string: $0, relativeTo: baseURL)! })
.renaming(to: "heroku-server")
let subcache: ReadOnlyCache<APIPath, Data> = WebAPI(networkProvider: networkCache, baseURL: baseURL).asReadOnlyCache()
return Self(dataProvider: subcache)
}

public static func digitalOcean(networkCache: ReadOnlyCache<URL, Data> = Self.makeUrlSessionCache()) -> Self {
let baseURL = Server.digitalOceanStorageBaseURL
let subcache: ReadOnlyCache<String, Data> = networkCache
.mapKeys({ baseURL.appendingPathComponent($0) })
.renaming(to: "digital-ocean-droplet")
let subcache: ReadOnlyCache<APIPath, Data> = WebAPI(networkProvider: networkCache, baseURL: baseURL).asReadOnlyCache()
return Self(dataProvider: subcache)
}

Expand All @@ -55,8 +51,8 @@ extension APIProvider {

public static func macBookSteve() -> Self {
let directory = URL(fileURLWithPath: "/Users/oleg/Development/TheGreatGame/Storage/content")
let rawFS = RawFileSystemCache(directoryURL: directory)
.mapKeys(RawFileSystemCache.FileName.init)
let rawFS: Cache<APIPath, Data> = RawFileSystemCache(directoryURL: directory)
.mapKeys({ RawFileSystemCache.FileName.init(validFileName: Filename(rawValue: $0.rawValue)) })
let cache = rawFS
.asReadOnlyCache()
.latency(ofInterval: 1.0)
Expand Down Expand Up @@ -84,7 +80,7 @@ public final class API : APIProvider {
self.groups = groups
}

public convenience init(dataProvider: ReadOnlyCache<String, Data>) {
public convenience init(dataProvider: ReadOnlyCache<APIPath, Data>) {
let teamsAPI = TeamsAPI.init(dataProvider: dataProvider)
let matchesAPI = MatchesAPI.init(dataProvider: dataProvider)
let groupsAPI = GroupsAPI.init(dataProvider: dataProvider)
Expand All @@ -109,10 +105,11 @@ public final class APICache : Storing {
self.groups = groups
}

public convenience init(diskCache cache: Cache<String, Data>) {
self.init(teams: TeamsAPICache.init(dataProvider: cache),
matches: MatchesAPICache.init(dataProvider: cache),
groups: GroupsAPICache.init(dataProvider: cache))
public convenience init(diskCache cache: Cache<Filename, Data>) {
let apiPathCache: Cache<APIPath, Data> = cache.mapKeys({ Filename(rawValue: $0.rawValue) })
self.init(teams: TeamsAPICache.init(dataProvider: apiPathCache),
matches: MatchesAPICache.init(dataProvider: apiPathCache),
groups: GroupsAPICache.init(dataProvider: apiPathCache))
}

}
Expand Down
73 changes: 73 additions & 0 deletions Kit/API/APIPath.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// APIPath.swift
// WebAPI
//
// Created by Олег on 08.08.17.
//
//

import Foundation

public struct APIPath : RawRepresentable, Hashable, ExpressibleByStringLiteral {

public static let separator = "/"

public typealias RawValue = String

public var rawValue: String

public var components: [String] {
return rawValue.components(separatedBy: APIPath.separator)
}

public init(rawValue: String) {
self.rawValue = rawValue
}

public init(components: [String]) {
self.rawValue = components.joined(separator: APIPath.separator)
}

public var hashValue: Int {
return rawValue.hashValue
}

public init(stringLiteral value: String) {
self.rawValue = value
}

public init(unicodeScalarLiteral value: String) {
self.rawValue = value
}

public init(extendedGraphemeClusterLiteral value: String) {
self.rawValue = value
}

}

extension APIPath : ExpressibleByArrayLiteral {

public typealias Element = APIPath

public init(arrayLiteral elements: Element...) {
self.init(components: elements.map({ $0.rawValue }))
}

}

extension APIPath {

public static func + (lhs: APIPath, rhs: APIPath) -> APIPath {
return APIPath(components: [lhs.rawValue, rhs.rawValue])
}

}

extension URL {

public func appendingPath(_ path: APIPath) -> URL {
return appendingPathComponent(path.rawValue)
}

}
14 changes: 7 additions & 7 deletions Kit/API/Groups.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import Shallows

public struct GroupsEndpoint {

public let path: String
public let path: APIPath

init(path: String) {
self.path = "groups/\(path)"
init(path: APIPath) {
self.path = "groups" + path
}

public static let all = GroupsEndpoint(path: "all.json")
Expand All @@ -26,9 +26,9 @@ public final class GroupsAPI : APIPoint {
public let provider: ReadOnlyCache<GroupsEndpoint, [String : Any]>
public let all: Retrieve<Editioned<Groups>>

private let dataProvider: ReadOnlyCache<String, Data>
private let dataProvider: ReadOnlyCache<APIPath, Data>

public init(dataProvider: ReadOnlyCache<String, Data>) {
public init(dataProvider: ReadOnlyCache<APIPath, Data>) {
self.dataProvider = dataProvider
self.provider = dataProvider
.mapJSONDictionary()
Expand All @@ -45,9 +45,9 @@ public final class GroupsAPICache : APICachePoint {
public let provider: Cache<GroupsEndpoint, [String : Any]>
public let all: Cache<Void, Editioned<Groups>>

private let dataProvider: Cache<String, Data>
private let dataProvider: Cache<APIPath, Data>

public init(dataProvider: Cache<String, Data>) {
public init(dataProvider: Cache<APIPath, Data>) {
self.dataProvider = dataProvider
self.provider = dataProvider
.mapJSONDictionary()
Expand Down
16 changes: 8 additions & 8 deletions Kit/API/Matches.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import Shallows

public struct MatchesEndpoint {

public let path: String
public let path: APIPath

init(path: String) {
self.path = "matches/\(path)"
init(path: APIPath) {
self.path = "matches" + path
}

public static let all = MatchesEndpoint(path: "all.json")
public static let allFull = MatchesEndpoint(path: "all-full.json")
public static func fullMatch(withID id: Match.ID) -> MatchesEndpoint {
return MatchesEndpoint(path: "\(id.rawID).json")
return MatchesEndpoint(path: APIPath(rawValue: "\(id.rawID).json"))
}
public static let stages = MatchesEndpoint(path: "stages.json")

Expand All @@ -34,9 +34,9 @@ public final class MatchesAPI : APIPoint {
public let stages: Retrieve<Editioned<Stages>>
public let fullMatch: ReadOnlyCache<Match.ID, Editioned<Match.Full>>

private let dataProvider: ReadOnlyCache<String, Data>
private let dataProvider: ReadOnlyCache<APIPath, Data>

public init(dataProvider: ReadOnlyCache<String, Data>) {
public init(dataProvider: ReadOnlyCache<APIPath, Data>) {
self.dataProvider = dataProvider
self.provider = dataProvider
.mapJSONDictionary()
Expand Down Expand Up @@ -65,9 +65,9 @@ public final class MatchesAPICache : APICachePoint {
public let stages: Cache<Void, Editioned<Stages>>
public let fullMatch: Cache<Match.ID, Editioned<Match.Full>>

private let dataProvider: Cache<String, Data>
private let dataProvider: Cache<APIPath, Data>

init(dataProvider: Cache<String, Data>) {
init(dataProvider: Cache<APIPath, Data>) {
self.dataProvider = dataProvider
self.provider = dataProvider
.mapJSONDictionary()
Expand Down
16 changes: 8 additions & 8 deletions Kit/API/Teams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import Foundation

public struct TeamsEndpoint {

public let path: String
public let path: APIPath

init(path: String) {
self.path = "teams/\(path)"
init(path: APIPath) {
self.path = "teams" + path
}

public static let all = TeamsEndpoint(path: "all.json")
public static func fullTeam(withID id: Team.ID) -> TeamsEndpoint {
return TeamsEndpoint(path: "\(id.rawID).json")
return TeamsEndpoint(path: APIPath(rawValue: "\(id.rawID).json"))
}

}
Expand All @@ -30,9 +30,9 @@ public final class TeamsAPI : APIPoint {
public let all: Retrieve<Editioned<Teams>>
public let fullTeam: ReadOnlyCache<Team.ID, Editioned<Team.Full>>

private let dataProvider: ReadOnlyCache<String, Data>
private let dataProvider: ReadOnlyCache<APIPath, Data>

public init(dataProvider: ReadOnlyCache<String, Data>) {
public init(dataProvider: ReadOnlyCache<APIPath, Data>) {
self.dataProvider = dataProvider
self.provider = dataProvider
.mapJSONDictionary()
Expand All @@ -53,9 +53,9 @@ public final class TeamsAPICache : APICachePoint {
public let all: Cache<Void, Editioned<Teams>>
public let fullTeam: Cache<Team.ID, Editioned<Team.Full>>

private let dataProvider: Cache<String, Data>
private let dataProvider: Cache<APIPath, Data>

public init(dataProvider: Cache<String, Data>) {
public init(dataProvider: Cache<APIPath, Data>) {
self.dataProvider = dataProvider
self.provider = dataProvider
.mapJSONDictionary()
Expand Down
90 changes: 90 additions & 0 deletions Kit/API/WebAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import Foundation
import Shallows

public struct WebAPI : ReadableCacheProtocol {

private let underlying: ReadOnlyCache<APIPath, Data>

public init(networkProvider: ReadOnlyCache<URL, Data>,
baseURL: URL) {
self.underlying = networkProvider
.mapKeys({ baseURL.appendingPath($0) })
}

public func retrieve(forKey key: APIPath, completion: @escaping (Result<Data>) -> ()) {
underlying.retrieve(forKey: key, completion: completion)
}

}

extension WebAPI {

public init(urlSession: URLSession, baseURL: URL) {
let networkProvider = urlSession.asReadOnlyCache()
.droppingResponse()
.usingURLKeys()
self.init(networkProvider: networkProvider, baseURL: baseURL)
}

}

extension URLSession : ReadableCacheProtocol {

public enum Key {
case url(URL)
case urlRequest(URLRequest)
}

public enum CacheError : Error {
case taskError(Error)
case responseIsNotHTTP(URLResponse?)
case noData
}

public func retrieve(forKey request: Key, completion: @escaping (Result<(HTTPURLResponse, Data)>) -> ()) {
let completion: (Data?, URLResponse?, Error?) -> () = { (data, response, error) in
if let error = error {
completion(.failure(CacheError.taskError(error)))
return
}
guard let httpResponse = response as? HTTPURLResponse else {
completion(.failure(CacheError.responseIsNotHTTP(response)))
return
}
guard let data = data else {
completion(.failure(CacheError.noData))
return
}
completion(.success(httpResponse, data))
}
let task: URLSessionTask
switch request {
case .url(let url):
task = self.dataTask(with: url, completionHandler: completion)
case .urlRequest(let request):
task = self.dataTask(with: request, completionHandler: completion)
}
task.resume()
}

}

extension ReadOnlyCache where Key == URLSession.Key {

public func usingURLKeys() -> ReadOnlyCache<URL, Value> {
return mapKeys({ .url($0) })
}

public func usingURLRequestKeys() -> ReadOnlyCache<URLRequest, Value> {
return mapKeys({ .urlRequest($0) })
}

}

extension ReadOnlyCache where Value == (HTTPURLResponse, Data) {

public func droppingResponse() -> ReadOnlyCache<Key, Data> {
return mapValues({ $0.1 })
}

}
4 changes: 2 additions & 2 deletions Kit/Images/Images.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public final class Images : Storing {
fileprivate let imageFetchingSession = URLSession(configuration: .default)
fileprivate let diskCache: Cache<URL, UIImage>

public init(diskCache: Cache<String, Data>) {
public init(diskCache: Cache<Filename, Data>) {
self.diskCache = diskCache
.mapKeys({ $0.absoluteString })
.mapKeys({ Filename(rawValue: $0.absoluteString) })
.mapImage()
}

Expand Down
Loading

0 comments on commit e46bc42

Please sign in to comment.