Skip to content

Instantly share code, notes, and snippets.

View fabiothiroki's full-sized avatar
👋

Fabio Hiroki fabiothiroki

👋
View GitHub Profile
@fabiothiroki
fabiothiroki / PlacesService.swift
Created January 19, 2018 00:45
Places Service and Protocol
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
@fabiothiroki
fabiothiroki / PlacesServiceErrorSpec.swift
Created January 18, 2018 21:50
Places Service Failure Test Case
class PlacesServiceSpec: XCTestCase {
private var placesService: PlacesService!
func testShouldEmitTheCorrectError() {
let provider = MoyaProvider<PlacesApi>(
endpointClosure: failureEndpointClosure,
stubClosure: MoyaProvider.immediatelyStub)
placesService = PlacesService.init(provider: provider)
@fabiothiroki
fabiothiroki / MoyaProviderFailure.swift
Created January 18, 2018 21:47
Moya Provider Failure Endpoint
let provider = MoyaProvider<PlacesApi>(
endpointClosure: failureEndpointClosure,
stubClosure: MoyaProvider.immediatelyStub)
@fabiothiroki
fabiothiroki / FailureEndpointClosure.swift
Created January 16, 2018 21:48
Failure Endpoint Closure Example
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"])
@fabiothiroki
fabiothiroki / PlacesServiceSpec.swift
Created January 16, 2018 21:38
Places Service First Test
class PlacesServiceSpec: XCTestCase {
private var placesService: PlacesService!
func testMapResponseToModel() {
let provider = MoyaProvider<PlacesApi>(stubClosure: MoyaProvider.immediatelyStub)
placesService = PlacesService.init(provider: provider)
var responseData: LocationPlaces?
@fabiothiroki
fabiothiroki / PlacesApiStubbed.swift
Created January 16, 2018 21:22
Implementation of sampleData method
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!))
@fabiothiroki
fabiothiroki / UserLocationServiceFinal.swift
Created January 3, 2018 01:41
Final version of UserLocationService
import CoreLocation
import RxSwift
protocol LocationManager {
var delegate: CLLocationManagerDelegate? { get set }
func requestWhenInUseAuthorization()
func requestLocation()
}
extension CLLocationManager: LocationManager {}
@fabiothiroki
fabiothiroki / RxMoyaExample.swift
Created November 11, 2017 18:22
Simple RxMoya call
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)
})
@fabiothiroki
fabiothiroki / PlacesApi.swift
Created November 11, 2017 18:07
Initial Places Api for Moya
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")!
@fabiothiroki
fabiothiroki / UserLocationServiceSpec.swift
Last active March 9, 2018 22:57
UserLocationService tests
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)