Skip to content

Instantly share code, notes, and snippets.

Created October 24, 2017 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0703a591f97fa9f6ad35b29234805fbd to your computer and use it in GitHub Desktop.
Save anonymous/0703a591f97fa9f6ad35b29234805fbd to your computer and use it in GitHub Desktop.
//
// Establishment.swift
//
import UIKit
class Scores: Decodable {
// set as string because we can expect values such as 'exempt'
let Hygiene: String
let Structural: String
let ConfidenceInManagement: String
init(Hygiene: String, Structural: String, ConfidenceInManagement: String) {
self.Hygiene = Hygiene
self.Structural = Structural
self.ConfidenceInManagement = ConfidenceInManagement
}
}
class Geocode: Decodable {
let longitude: Double
let latitude: Double
init(longitude: Double, latitude: Double) {
self.longitude = longitude
self.latitude = latitude
}
}
class Meta: Decodable {
let dataSource: String
let extractDate: String
let itemCount: Int
let returncode: String
let totalCount: Int
let totalPages: Int
let pageSize: Int
let pageNumber: Int
init(dataSource: String, extractDate: String, itemCount: Int, returncode: String, totalCount: Int, totalPages: Int, pageSize: Int, pageNumber: Int) {
self.dataSource = dataSource
self.extractDate = extractDate
self.itemCount = itemCount
self.returncode = returncode
self.totalCount = totalCount
self.totalPages = totalPages
self.pageSize = pageSize
self.pageNumber = pageNumber
}
}
class Establishments: Decodable {
let establishments: [Establishment]
init(establishments: [Establishment]) {
self.establishments = establishments
}
}
class Establishment: Decodable {
let FHRSID: Int
let LocalAuthorityBusinessID: String
let BusinessName: String
let BusinessType: String
let BusinessTypeID: Int
let AddressLine1: String
let AddressLine2: String
let AddressLine3: String
let AddressLine4: String
let PostCode: String
let Phone: String
let RatingValue: Int
let RatingKey: String
let RatingDate: String
let LocalAuthorityCode: Int
let LocalAuthorityName: String
let LocalAuthorityWebSite: String
let LocalAuthorityEmailAddress: String
let scores: Scores
let SchemeType: String
let geocode: Geocode
let RightToReply: String
let Distance: Double
let NewRatingPending: Bool
let meta: Meta
let links: [String]
init(FHRSID: Int, LocalAuthorityBusinessID: String, BusinessName: String, BusinessType: String, BusinessTypeID: Int, AddressLine1: String, AddressLine2: String, AddressLine3: String, AddressLine4: String, PostCode: String, Phone: String, RatingValue: Int, RatingKey: String, RatingDate: String, LocalAuthorityCode: Int, LocalAuthorityName: String, LocalAuthorityWebSite: String, LocalAuthorityEmailAddress: String, scores: Scores, SchemeType: String, geocode: Geocode, RightToReply: String, Distance: Double, NewRatingPending: Bool, meta: Meta, links: [String]) {
self.FHRSID = FHRSID
self.LocalAuthorityBusinessID = LocalAuthorityBusinessID
self.BusinessName = BusinessName
self.BusinessType = BusinessType
self.BusinessTypeID = BusinessTypeID
self.AddressLine1 = AddressLine1
self.AddressLine2 = AddressLine2
self.AddressLine3 = AddressLine3
self.AddressLine4 = AddressLine4
self.PostCode = PostCode
self.Phone = Phone
self.RatingValue = RatingValue
self.RatingKey = RatingKey
self.RatingDate = RatingDate
self.LocalAuthorityCode = LocalAuthorityCode
self.LocalAuthorityName = LocalAuthorityName
self.LocalAuthorityWebSite = LocalAuthorityWebSite
self.LocalAuthorityEmailAddress = LocalAuthorityEmailAddress
self.scores = scores
self.SchemeType = SchemeType
self.geocode = geocode
self.RightToReply = RightToReply
self.Distance = Distance
self.NewRatingPending = NewRatingPending
self.meta = meta
self.links = links
}
}
{
"establishments": [
{
"FHRSID": 876596,
"LocalAuthorityBusinessID": "PI/000060353",
"BusinessName": "Green Dragon",
"BusinessType": "Takeaway/sandwich shop",
"BusinessTypeID": 1144,
"AddressLine1": "My Local Road",
"AddressLine2": "My Local Town",
"AddressLine3": "My County",
"AddressLine4": "",
"PostCode": "PO5 7CO",
"Phone": "",
"RatingValue": "4",
"RatingKey": "fhrs_4_en-gb",
"RatingDate": "2016-07-11T00:00:00",
"LocalAuthorityCode": "420",
"LocalAuthorityName": "AuthorityName",
"LocalAuthorityWebSite": "http://www.website.gov.uk",
"LocalAuthorityEmailAddress": "environmental.health@website.gov.uk",
"scores": {
"Hygiene": 10,
"Structural": 5,
"ConfidenceInManagement": 5
},
"SchemeType": "FHRS",
"geocode": {
"longitude": "-1.000000",
"latitude": "50.000000"
},
"RightToReply": "",
"Distance": 0.10468279020884118,
"NewRatingPending": false,
"meta": {
"dataSource": null,
"extractDate": "0001-01-01T00:00:00",
"itemCount": 0,
"returncode": null,
"totalCount": 0,
"totalPages": 0,
"pageSize": 0,
"pageNumber": 0
},
"links": []
}
],
"meta": {
"dataSource": "Lucene",
"extractDate": "0001-01-01T00:00:00",
"itemCount": 0,
"returncode": "OK",
"totalCount": 1,
"totalPages": 1,
"pageSize": 5000,
"pageNumber": 1
},
"links": []
}
URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
guard let data = data, error == nil, response != nil else {
print("Something went wrong")
return
}
//print("test")
do {
let establishments = try JSONDecoder().decode(Establishments.self, from: data)
print(establishments)
} catch {
print("summots wrong")
}
}).resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment