This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol PlacesDatasource { | |
func placesAround(latitude: Double, longitude: Double) -> Observable<LocationPlaces> | |
} | |
struct PlacesService: PlacesDatasource { | |
private let provider: MoyaProvider<PlacesApi> | |
init(provider: MoyaProvider<PlacesApi>) { | |
self.provider = provider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PlacesServiceSpec: XCTestCase { | |
private var placesService: PlacesService! | |
func testShouldEmitTheCorrectError() { | |
let provider = MoyaProvider<PlacesApi>( | |
endpointClosure: failureEndpointClosure, | |
stubClosure: MoyaProvider.immediatelyStub) | |
placesService = PlacesService.init(provider: provider) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let provider = MoyaProvider<PlacesApi>( | |
endpointClosure: failureEndpointClosure, | |
stubClosure: MoyaProvider.immediatelyStub) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func url(_ route: TargetType) -> String { | |
return route.baseURL.appendingPathComponent(route.path).absoluteString | |
} | |
let failureEndpointClosure = { (target: PlacesApi) -> Endpoint<PlacesApi> in | |
let error = NSError( | |
domain: "com.moya.moyaerror", | |
code: 0, | |
userInfo: [NSLocalizedDescriptionKey: "Houston, we have a problem"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PlacesServiceSpec: XCTestCase { | |
private var placesService: PlacesService! | |
func testMapResponseToModel() { | |
let provider = MoyaProvider<PlacesApi>(stubClosure: MoyaProvider.immediatelyStub) | |
placesService = PlacesService.init(provider: provider) | |
var responseData: LocationPlaces? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sampleData: Data { | |
switch self { | |
case .recommended: | |
return stubbedResponse("venues") | |
} | |
} | |
private func stubbedResponse(_ filename: String) -> Data! { | |
let url = Bundle.main.url(forResource: filename, withExtension: "json") | |
return (try? Data(contentsOf: url!)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CoreLocation | |
import RxSwift | |
protocol LocationManager { | |
var delegate: CLLocationManagerDelegate? { get set } | |
func requestWhenInUseAuthorization() | |
func requestLocation() | |
} | |
extension CLLocationManager: LocationManager {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
provider: MoyaProvider<PlacesApi> = MoyaProvider<PlacesApi>() // providers should be retained | |
provider.rx.request(.recommended(latitude: -23.5666151, longitude: -46.6463977)) | |
.map(to: LocationPlaces.self, keyPath: "response") | |
.subscribe(onSuccess: { (place) in | |
print(place) | |
}, onError: { (error) in | |
print(error) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Moya | |
enum PlacesApi { | |
case recommended(latitude: Double, longitude: Double) | |
} | |
extension PlacesApi: TargetType { | |
var baseURL: URL { | |
return URL(string: "https://api.foursquare.com/v2")! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func testShouldProvideLocation() { | |
let mockLocation = CLLocation.init(latitude: -23.5666151, longitude: -46.6463977) | |
let locationStream = userLocationService.getUserLocation() | |
locationStream.subscribe(onNext: { (location) in | |
XCTAssertEqual(mockLocation, location) | |
}, onError: { (_) in | |
XCTFail("Should not return error") | |
}).disposed(by: disposeBag) |