Skip to content

Commit 0a77dc7

Browse files
committed
Always return an empty array on scraper failure
1 parent 6eed2bf commit 0a77dc7

File tree

4 files changed

+41
-49
lines changed

4 files changed

+41
-49
lines changed

RepresentsMe/Models/Official.swift

+12-29
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,19 @@ class Official: Equatable, CustomStringConvertible {
3535
return repr() // official. Conforms class to
3636
} // CustomStringConvertible protocol.
3737

38-
/// Creates an Official given the values for each field.
39-
///
40-
/// - Parameter index: The index of the official
41-
/// - Parameter name: The name of the official
42-
/// - Parameter photoURL: The URL to the photo of the official
43-
/// - Parameter party: The political party of the official
44-
/// - Parameter addresses: An Array of addresses for the official
45-
/// - Parameter phones: An Array of phone numbers for the official
46-
/// - Parameter emails: An Array of emails for the official
47-
/// - Parameter urls: An Array of URLs for the official
48-
/// - Parameter socialMedia: An Array of social media accounts
49-
/// - Parameter office: The name of the official's office
50-
/// - Parameter division: The name of the office's division
51-
init(_ index:Int, _ name:String, _ photoURL:URL?, _ party:String,
52-
_ addresses:[[String: String]], _ phones:[String], _ urls:[URL?],
53-
_ emails:[String], _ socialMedia:[[String: String]],
54-
_ office:String, _ division:String) {
55-
self.index = index
56-
self.name = name
57-
self.photoURL = photoURL
58-
self.party = PoliticalParty.determine(for: party)
59-
self.addresses = addresses
60-
self.phones = phones
61-
self.emails = emails
62-
self.urls = urls
63-
self.socialMedia = socialMedia
64-
self.office = office
65-
self.division = division
38+
init() {
39+
self.index = -1
40+
self.name = ""
41+
self.party = PoliticalParty.unknown
42+
self.addresses = [[:]]
43+
self.phones = []
44+
self.urls = []
45+
self.emails = []
46+
self.socialMedia = [[:]]
47+
self.office = ""
48+
self.division = ""
6649
}
67-
50+
6851
/// Builds an Official
6952
///
7053
/// - Parameter index: the index of this Official in the JSON

RepresentsMe/Scraper/OfficialScraper.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class OfficialScraper {
4444
do {
4545
urlString = try buildURL(address: address.description, apikey: apikey)
4646
} catch {
47-
return completion(nil, error as? ParserError)
47+
// Failed to build the url, return an empty Array
48+
return completion([], nil)
4849
}
4950

5051
let url = URL(string: urlString!)
@@ -53,17 +54,16 @@ class OfficialScraper {
5354
URLSession.shared.dataTask(with: request) { data, response, error in
5455
if error != nil {
5556
// Error occurred, abort
56-
return completion(nil, ParserError.requestFailedError(
57-
error?.localizedDescription ?? "Request failed."))
57+
return completion([], nil)
5858
}
5959

6060
do {
6161
// Try to parse the JSON
6262
let jsonData = try parseJSON(data: data!)
6363
return completion(Official.buildOfficials(data: jsonData), nil)
6464
} catch {
65-
// Error occurred while parsing JSON
66-
return completion(nil, error as? ParserError)
65+
// Error occurred while parsing JSON, return an empty Array
66+
return completion([], nil)
6767
}
6868
}.resume()
6969
}

RepresentsMeTests/OfficialScraperTests.swift

+22-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ import XCTest
1010
@testable import RepresentsMe
1111

1212
class OfficialScraperTests: XCTestCase {
13+
14+
let validAddress = Address(streetNumber: "2317",
15+
streetName: "Speedway",
16+
city: "Austin",
17+
state: "TX",
18+
zipcode: "78712")
19+
let invalidAddress = Address(streetNumber: "invalid",
20+
streetName: "",
21+
city: "",
22+
state: "",
23+
zipcode: "")
1324

1425
override func setUp() {
1526
// Put setup code here. This method is called before the invocation of each test method in the class.
@@ -21,35 +32,33 @@ class OfficialScraperTests: XCTestCase {
2132

2233
/// Test successful scraping of data
2334
func testScrapingData() {
24-
let result = makeRequest(address: "2317 Speedway, Austin, TX 78712",
35+
let result = makeRequest(address: validAddress,
2536
apikey: civic_api_key)
2637
XCTAssertNotNil(result.0) // Assert no errors
2738
XCTAssertNil(result.1) // Assert some result returned
2839
}
2940

30-
/// Test that an error is received if an invalid address is given
41+
/// Test that an empty array is received if an invalid address is given
3142
func testInvalidAddress() {
32-
let result = makeRequest(address: "invalid", apikey: civic_api_key)
43+
let result = makeRequest(address: invalidAddress,
44+
apikey: civic_api_key)
3345
let officials = result.0
3446
let error = result.1
3547

36-
if case .invalidAddressError = error! {
37-
XCTAssertTrue(true)
38-
} else {
39-
XCTFail("Did not throw ParserError.invalidAddressError")
40-
}
41-
XCTAssertNil(officials)
48+
XCTAssertNotNil(officials)
49+
XCTAssertNil(error)
50+
XCTAssertEqual(officials, [])
4251
}
4352

44-
/// Test that an error is received if an invalid api key is given
53+
/// Test that an empty array is received if an invalid API key is given
4554
func testInvalidAPIKey() {
46-
let result = makeRequest(address: "2317 Speedway, Austin, TX 78712",
55+
let result = makeRequest(address: validAddress,
4756
apikey: "invalid")
4857
let officials = result.0
4958
let error = result.1
5059

51-
XCTAssertNil(error)
5260
XCTAssertNotNil(officials)
61+
XCTAssertNil(error)
5362
XCTAssertEqual(officials, [])
5463
}
5564

@@ -101,7 +110,7 @@ class OfficialScraperTests: XCTestCase {
101110
/// - Parameter apikey: The apikey to use
102111
///
103112
/// - Returns: a tuple with the resulting Officials and errors if any
104-
func makeRequest(address:String, apikey:String,
113+
func makeRequest(address:Address, apikey:String,
105114
timeout:Double = 10) -> ([Official]?, ParserError?) {
106115
var error:ParserError?
107116
var officials:[Official]?

RepresentsMeTests/OfficialScraperTestsData.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class OfficialScraperTestsData {
9393

9494
/// Extend Official with hardcoded values for creating officials when testing
9595
extension Official {
96-
96+
9797
/// Creates an Official given the values for each field.
9898
///
9999
/// - Parameter index: The index of the official
@@ -116,7 +116,7 @@ extension Official {
116116
self.index = index
117117
self.name = name
118118
self.photoURL = URL(string: photoURL)
119-
self.party = party
119+
self.party = PoliticalParty.determine(for: party)
120120
self.addresses = addresses
121121
self.phones = phones
122122
self.emails = emails

0 commit comments

Comments
 (0)