Skip to content

Commit 54a7a5c

Browse files
committed
Venkat - Added a simple rest client implementation
1 parent d177ef5 commit 54a7a5c

File tree

10 files changed

+531
-5
lines changed

10 files changed

+531
-5
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ build/
1313
target/
1414

1515
# Cocoapods
16-
#Pods/
16+
Pods/
1717

1818
# XCode Private Settings
1919
*.pbxuser
@@ -32,6 +32,11 @@ xcuserdata
3232
#Archives
3333
*.zip
3434
*.ipa
35+
artifacts
36+
37+
#Gems
38+
.bundle
39+
vendor/
3540

3641
#Node
3742
node_modules

HelloRest.xcworkspace/contents.xcworkspacedata

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HelloRest/HelloRest.xcodeproj/project.pbxproj

Lines changed: 257 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,62 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="JMR-3a-qCz">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
4+
<deployment identifier="iOS"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
6+
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
57
</dependencies>
68
<scenes>
79
<!--View Controller-->
810
<scene sceneID="tne-QT-ifu">
911
<objects>
10-
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
12+
<viewController storyboardIdentifier="ViewController" id="BYZ-38-t0r" customClass="ViewController" customModule="HelloRest" customModuleProvider="target" sceneMemberID="viewController">
1113
<layoutGuides>
1214
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
1315
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
1416
</layoutGuides>
1517
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
1618
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
1719
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
20+
<subviews>
21+
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="yMQ-rS-8Ze">
22+
<rect key="frame" x="30" y="0.0" width="540" height="600"/>
23+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
24+
</tableView>
25+
</subviews>
1826
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
27+
<constraints>
28+
<constraint firstItem="yMQ-rS-8Ze" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="topMargin" id="79v-4f-Jeh"/>
29+
<constraint firstItem="yMQ-rS-8Ze" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="10" id="Cvy-mk-jJ5"/>
30+
<constraint firstAttribute="trailingMargin" secondItem="yMQ-rS-8Ze" secondAttribute="trailing" constant="10" id="D3O-3F-lGd"/>
31+
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="yMQ-rS-8Ze" secondAttribute="bottom" id="lOV-5D-G8u"/>
32+
</constraints>
1933
</view>
34+
<navigationItem key="navigationItem" id="bhD-w0-KCM"/>
35+
<connections>
36+
<outlet property="tableView" destination="yMQ-rS-8Ze" id="Xha-8v-nek"/>
37+
</connections>
2038
</viewController>
2139
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
2240
</objects>
41+
<point key="canvasLocation" x="1588" y="522"/>
42+
</scene>
43+
<!--Navigation Controller-->
44+
<scene sceneID="d5F-4k-jHV">
45+
<objects>
46+
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="JMR-3a-qCz" sceneMemberID="viewController">
47+
<toolbarItems/>
48+
<navigationBar key="navigationBar" contentMode="scaleToFill" id="WIR-Lf-qLA">
49+
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
50+
<autoresizingMask key="autoresizingMask"/>
51+
</navigationBar>
52+
<nil name="viewControllers"/>
53+
<connections>
54+
<segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="Cf6-4G-KwB"/>
55+
</connections>
56+
</navigationController>
57+
<placeholder placeholderIdentifier="IBFirstResponder" id="GUe-6J-KJY" userLabel="First Responder" sceneMemberID="firstResponder"/>
58+
</objects>
59+
<point key="canvasLocation" x="859" y="515"/>
2360
</scene>
2461
</scenes>
2562
</document>

HelloRest/HelloRest/Info.plist

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
<string>1</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
25+
<key>NSAppTransportSecurity</key>
26+
<dict>
27+
<key>NSAllowsArbitraryLoads</key>
28+
<true/>
29+
<key>NSExceptionDomains</key>
30+
<dict>
31+
<key>localhost:8000</key>
32+
<string></string>
33+
</dict>
34+
</dict>
2535
<key>UILaunchStoryboardName</key>
2636
<string>LaunchScreen</string>
2737
<key>UIMainStoryboardFile</key>

HelloRest/HelloRest/ViewController.swift

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,78 @@
77
//
88

99
import UIKit
10+
import Alamofire
11+
import SwiftyJSON
1012

11-
class ViewController: UIViewController {
13+
class ViewController: UIViewController{
14+
15+
@IBOutlet weak var tableView: UITableView!
16+
var tableAdapter: TableAdapter?
1217

18+
var jsonResults: JSON = [] {
19+
didSet {
20+
self.tableAdapter?.items = jsonResults
21+
self.tableView?.reloadData()
22+
}
23+
}
24+
1325
override func viewDidLoad() {
1426
super.viewDidLoad()
1527
// Do any additional setup after loading the view, typically from a nib.
28+
self.title = "Hello Rest!"
29+
30+
self.tableAdapter = TableAdapter(jsonData: self.jsonResults)
31+
self.tableView?.dataSource = self.tableAdapter
32+
getData()
33+
1634
}
1735

1836
override func didReceiveMemoryWarning() {
1937
super.didReceiveMemoryWarning()
2038
// Dispose of any resources that can be recreated.
39+
40+
}
41+
42+
func getData() {
43+
Alamofire
44+
.request(.GET, "http://localhost:8000/listall")
45+
.validate(statusCode: 200..<400)
46+
.responseJSON { [weak self] response in
47+
if let value = response.result.value {
48+
self?.jsonResults = JSON(value)
49+
}
50+
}
51+
2152
}
2253

2354

2455
}
2556

57+
class TableAdapter: NSObject, UITableViewDataSource {
58+
59+
var items: JSON
60+
let cellIdentifier = "SOME_IDENTIFIER"
61+
62+
63+
init(jsonData: JSON) {
64+
self.items = jsonData
65+
super.init()
66+
}
67+
68+
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
69+
return items.count
70+
}
71+
72+
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
73+
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(self.cellIdentifier) ?? UITableViewCell(style: .Default, reuseIdentifier: self.cellIdentifier)
74+
75+
cell.textLabel?.text = items[indexPath.row, "name"].stringValue
76+
77+
return cell
78+
}
79+
80+
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
81+
return 1
82+
}
83+
}
84+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// HelloRestServerSpecs.swift
3+
// HelloRest
4+
//
5+
// Created by Venkat on 6/24/16.
6+
// Copyright © 2016 Venkat. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import Quick
11+
import Nimble
12+
13+
class HelloRestServerSpecs: QuickSpec {
14+
override func spec() {
15+
describe("My First Test in iOS") {
16+
it("should pass"){
17+
expect(true).to(beTrue())
18+
}
19+
}
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// HelloRestSpecs.swift
3+
// HelloRestSpecs
4+
//
5+
// Created by Venkat on 6/23/16.
6+
// Copyright © 2016 Venkat. All rights reserved.
7+
//
8+
9+
import Quick
10+
import Nimble
11+
12+
class HelloRestSpecs: QuickSpec {
13+
14+
override func spec() {
15+
describe("myfirsttest") {
16+
it("should validate true to be true") {
17+
expect(true).to(beTrue())
18+
}
19+
20+
}
21+
}
22+
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleSignature</key>
20+
<string>????</string>
21+
<key>CFBundleVersion</key>
22+
<string>1</string>
23+
</dict>
24+
</plist>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// ViewControllerSpec.swift
3+
// HelloRest
4+
//
5+
// Created by Venkat on 6/24/16.
6+
// Copyright © 2016 Venkat. All rights reserved.
7+
//
8+
9+
import Quick
10+
import Nimble
11+
import SwiftyJSON
12+
import OHHTTPStubs
13+
import Alamofire
14+
@testable import HelloRest
15+
16+
import UIKit
17+
18+
19+
class FakeViewController: ViewController {
20+
var getDataCalled: Bool = false
21+
22+
23+
override func getData() {
24+
getDataCalled = true;
25+
}
26+
}
27+
28+
class ViewControllerSpec: QuickSpec {
29+
override func spec() {
30+
beforeEach {
31+
stub(isHost("localhost") && isPath("/listall") && isMethodGET()) { request in
32+
33+
let obj = [["key1":"value1"], ["key2":["value2A","value2B"]]]
34+
35+
return OHHTTPStubsResponse(JSONObject: obj, statusCode: 200, headers: nil).responseTime(OHHTTPStubsDownloadSpeed3G)
36+
37+
}
38+
}
39+
40+
afterEach {
41+
OHHTTPStubs.removeAllStubs()
42+
}
43+
44+
describe(".getData") {
45+
it("should get data from list all") {
46+
47+
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
48+
49+
let vc = storyBoard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
50+
51+
vc.getData();
52+
53+
expect(vc.jsonResults).toEventually(haveCount(2))
54+
}
55+
56+
}
57+
58+
describe(".viewDidLoad") {
59+
var fakeVC: FakeViewController!
60+
61+
beforeEach{
62+
fakeVC = FakeViewController()
63+
fakeVC.viewDidLoad()
64+
}
65+
66+
it("should fetch data from server"){
67+
expect(fakeVC.getDataCalled).to(beTrue())
68+
}
69+
70+
it("should setup title and adapter"){
71+
expect(fakeVC.title).to(equal("Hello Rest!"))
72+
73+
let adapter = fakeVC.tableAdapter
74+
expect(adapter).toNot(beNil())
75+
76+
expect(adapter?.items).to(haveCount(0))
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)