-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYelpVenue.swift
92 lines (77 loc) · 2.64 KB
/
YelpVenue.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// YelpVenue.swift
// Bourbon-iOS
//
// Created by Alyssa Torres on 5/5/17.
// Copyright © 2017 Ourglass. All rights reserved.
//
import UIKit
import SwiftyJSON
enum YelpError: Error {
case badJson
}
/// Represents a Yelp venue.
class YelpVenue {
var name: String
var imageUrl: String
var distance: String
var yelpId: String
var address1: String
var address2: String
var city: String
var state: String
var zip: String
var country: String
var latitude: Double
var longitude: Double
var displayAddress: [String]
var address: String {
return displayAddress.joined(separator: ", ")
}
init(_ venueJson: JSON) throws {
let location = venueJson["location"]
let coords = venueJson["coordinates"]
guard let name = venueJson["name"].string, let imageUrl = venueJson["image_url"].string,
let yelpId = venueJson["id"].string, let displayAddress = location["display_address"].array,
let address1 = location["address1"].string,
let city = location["city"].string, let state = location["state"].string,
let zip = location["zip_code"].string, let country = location["country"].string,
let lat = coords["latitude"].double, let lng = coords["longitude"].double
else {
throw YelpError.badJson
}
// Handle fields that are not required and can be set to a default
if let dist = venueJson["distance"].double {
self.distance = String(format: "%.1f km", dist / 1000.0)
} else {
self.distance = ""
}
if let address2 = location["address2"].string {
self.address2 = address2
} else {
self.address2 = ""
}
self.name = name
self.imageUrl = imageUrl
self.yelpId = yelpId
self.displayAddress = displayAddress.map({$0.stringValue})
self.address1 = address1
self.city = city
self.state = state
self.zip = zip
self.country = country
self.latitude = lat
self.longitude = lng
}
/// Gives the `OGVenue` representation of this `YelpVenue`.
///
/// - Parameter uuid: UUID to assign to the venue
/// - Returns: an `OGVenue`
func toOGVenue(uuid: String) -> OGVenue {
return OGVenue(name: name, street: address1, street2: address2, city: city, state: state, zip: zip, latitude: latitude, longitude: longitude, uuid: uuid)
}
func description() -> String {
return "name: \(self.name) " +
"address: \(self.address) "
}
}