Repository: Moya/Moya
File: Endpoint.swift (Swift)

Code before:
//
//  Endpoint.swift
//  Moya
//
//  Created by Ash Furrow on 2014-08-16.
//  Copyright (c) 2014 Ash Furrow. All rights reserved.
//

import Foundation
import Alamofire

/// Used for stubbing responses.
public enum EndpointSampleResponse {
    case Success(Int, NSData)
    case Error(Int?, NSError?, NSData?)
    case Lazy(()-> EndpointSampleResponse)

    func evaluate() -> EndpointSampleResponse {
        switch self {
        case Success, Error: return self
        case Lazy(let lazy):
            return lazy().evaluate()
        }
    }
}


/// Class for reifying a target of the T enum unto a concrete Endpoint
public class Endpoint<T> {
    public let URL: String
    public let method: Moya.Method
    public let sampleResponse: EndpointSampleResponse
    public let parameters: [String: AnyObject]
    public let parameterEncoding: Moya.ParameterEncoding
    public let httpHeaderFields: [String: AnyObject]
    
    /// Main initializer for Endpoint.
    public init(URL: String, sampleResponse: EndpointSampleResponse, method: Moya.Method = Moya.Method.GET, parameters: [String: AnyObject] = [String: AnyObject](), parameterEncoding: Moya.ParameterEncoding = .URL, httpHeaderFields: [String: AnyObject] = [String: AnyObject]()) {
        self.URL = URL
        self.sampleResponse = sampleResponse
        self.method = method
        self.parameters = parameters
        self.parameterEncoding = parameterEncoding
        self.httpHeaderFields = httpHeaderFields
    }

Review comment (refactor):
Nice – didn't know you could use commas to delimit a list of case statements. I've been using `fallthrough` – this should clean up my code a bunch!


Code after:
//
//  Endpoint.swift
//  Moya
//
//  Created by Ash Furrow on 2014-08-16.
//  Copyright (c) 2014 Ash Furrow. All rights reserved.
//

import Foundation
import Alamofire

/// Used for stubbing responses.
public enum EndpointSampleResponse {
    case Success(Int, NSData)
    case Error(Int?, NSError?, NSData?)
    case Closure(@autoclosure () -> EndpointSampleResponse)

    func evaluate() -> EndpointSampleResponse {
        switch self {
        case Success, Error: return self
        case Closure(let closure):
            return closure().evaluate()
        }
    }
}


/// Class for reifying a target of the T enum unto a concrete Endpoint
public class Endpoint<T> {
    public let URL: String
    public let method: Moya.Method
    public let sampleResponse: EndpointSampleResponse
    public let parameters: [String: AnyObject]
    public let parameterEncoding: Moya.ParameterEncoding
    public let httpHeaderFields: [String: AnyObject]
    
    /// Main initializer for Endpoint.
    public init(URL: String, sampleResponse: EndpointSampleResponse, method: Moya.Method = Moya.Method.GET, parameters: [String: AnyObject] = [String: AnyObject](), parameterEncoding: Moya.ParameterEncoding = .URL, httpHeaderFields: [String: AnyObject] = [String: AnyObject]()) {
        self.URL = URL
        self.sampleResponse = sampleResponse
        self.method = method
        self.parameters = parameters
        self.parameterEncoding = parameterEncoding
        self.httpHeaderFields = httpHeaderFields
    }