Skip to content

Commit

Permalink
Merge pull request #20 from tejen/master
Browse files Browse the repository at this point in the history
Update client from Yelp API v2 to v3
  • Loading branch information
timothy1ee authored Jul 26, 2018
2 parents 75d6dc5 + 4b2caa4 commit 3bd2599
Show file tree
Hide file tree
Showing 45 changed files with 333 additions and 61 deletions.
66 changes: 47 additions & 19 deletions Yelp/Business.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Business: NSObject {
let imageURL: URL?
let categories: String?
let distance: String?
let ratingImageURL: URL?
let ratingImage: UIImage?
let reviewCount: NSNumber?

init(dictionary: NSDictionary) {
Expand All @@ -30,26 +30,23 @@ class Business: NSObject {
let location = dictionary["location"] as? NSDictionary
var address = ""
if location != nil {
let addressArray = location!["address"] as? NSArray
if addressArray != nil && addressArray!.count > 0 {
address = addressArray![0] as! String
}

let neighborhoods = location!["neighborhoods"] as? NSArray
if neighborhoods != nil && neighborhoods!.count > 0 {
if !address.isEmpty {
address += ", "
let addressArray = location!["display_address"] as? NSArray
if addressArray != nil {
if addressArray!.count > 0 {
address = addressArray![0] as! String
}
if addressArray!.count > 1 {
address += ", " + (addressArray![1] as! String)
}
address += neighborhoods![0] as! String
}
}
self.address = address

let categoriesArray = dictionary["categories"] as? [[String]]
let categoriesArray = dictionary["categories"] as? [NSDictionary]
if categoriesArray != nil {
var categoryNames = [String]()
for category in categoriesArray! {
let categoryName = category[0]
let categoryName = category["title"] as! String
categoryNames.append(categoryName)
}
categories = categoryNames.joined(separator: ", ")
Expand All @@ -65,11 +62,42 @@ class Business: NSObject {
distance = nil
}

let ratingImageURLString = dictionary["rating_img_url_large"] as? String
if ratingImageURLString != nil {
ratingImageURL = URL(string: ratingImageURLString!)
let rating = dictionary["rating"] as? Double
if rating != nil {
switch rating {
case 1:
self.ratingImage = UIImage(named: "stars_1")
break
case 1.5:
self.ratingImage = UIImage(named: "stars_1half")
break
case 2:
self.ratingImage = UIImage(named: "stars_2")
break
case 2.5:
self.ratingImage = UIImage(named: "stars_2half")
break
case 3:
self.ratingImage = UIImage(named: "stars_3")
break
case 3.5:
self.ratingImage = UIImage(named: "stars_3half")
break
case 4:
self.ratingImage = UIImage(named: "stars_4")
break
case 4.5:
self.ratingImage = UIImage(named: "stars_4half")
break
case 5:
self.ratingImage = UIImage(named: "stars_5")
break
default:
self.ratingImage = UIImage(named: "stars_0")
break
}
} else {
ratingImageURL = nil
self.ratingImage = UIImage(named: "stars_0")
}

reviewCount = dictionary["review_count"] as? NSNumber
Expand All @@ -88,7 +116,7 @@ class Business: NSObject {
_ = YelpClient.sharedInstance.searchWithTerm(term, completion: completion)
}

class func searchWithTerm(term: String, sort: YelpSortMode?, categories: [String]?, deals: Bool?, completion: @escaping ([Business]?, Error?) -> Void) -> Void {
_ = YelpClient.sharedInstance.searchWithTerm(term, sort: sort, categories: categories, deals: deals, completion: completion)
class func searchWithTerm(term: String, sort: YelpSortMode?, categories: [String]?, completion: @escaping ([Business]?, Error?) -> Void) -> Void {
_ = YelpClient.sharedInstance.searchWithTerm(term, sort: sort, categories: categories, openNow: false, completion: completion)
}
}
25 changes: 12 additions & 13 deletions Yelp/BusinessesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,24 @@ class BusinessesViewController: UIViewController {

Business.searchWithTerm(term: "Thai", completion: { (businesses: [Business]?, error: Error?) -> Void in

self.businesses = businesses
if let businesses = businesses {
for business in businesses {
print(business.name!)
print(business.address!)
self.businesses = businesses
if let businesses = businesses {
for business in businesses {
print(business.name!)
print(business.address!)
}
}
}

}
)

/* Example of Yelp search with more search options specified
Business.searchWithTerm("Restaurants", sort: .distance, categories: ["asianfusion", "burgers"], deals: true) { (businesses: [Business]!, error: Error!) -> Void in
self.businesses = businesses

for business in businesses {
print(business.name!)
print(business.address!)
}
Business.searchWithTerm(term: "Restaurants", sort: .distance, categories: ["asianfusion", "burgers"]) { (businesses, error) in
self.businesses = businesses
for business in self.businesses {
print(business.name!)
print(business.address!)
}
}
*/

Expand Down
15 changes: 15 additions & 0 deletions Yelp/Images.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down Expand Up @@ -29,6 +39,11 @@
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
Expand Down
6 changes: 6 additions & 0 deletions Yelp/Images.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
23 changes: 23 additions & 0 deletions Yelp/Images.xcassets/stars_0.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "regular_0.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "stars_0.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file added Yelp/Images.xcassets/stars_0.imageset/regular_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Yelp/Images.xcassets/stars_0.imageset/stars_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions Yelp/Images.xcassets/stars_1.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "regular_1.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "stars_1.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file added Yelp/Images.xcassets/stars_1.imageset/regular_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Yelp/Images.xcassets/stars_1.imageset/stars_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions Yelp/Images.xcassets/stars_1half.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "regular_1_half.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "stars_1half.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions Yelp/Images.xcassets/stars_2.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "regular_2.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "stars_2.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file added Yelp/Images.xcassets/stars_2.imageset/regular_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Yelp/Images.xcassets/stars_2.imageset/stars_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions Yelp/Images.xcassets/stars_2half.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "regular_2_half.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "stars_2half.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions Yelp/Images.xcassets/stars_3.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "regular_3.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "stars_3.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file added Yelp/Images.xcassets/stars_3.imageset/regular_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Yelp/Images.xcassets/stars_3.imageset/stars_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions Yelp/Images.xcassets/stars_3half.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "regular_3_half.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "stars_3half.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions Yelp/Images.xcassets/stars_4.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "regular_4.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "stars_4.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file added Yelp/Images.xcassets/stars_4.imageset/regular_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Yelp/Images.xcassets/stars_4.imageset/stars_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3bd2599

Please sign in to comment.