Commit 8b392e7a authored by Ezequiel R. Aguerre's avatar Ezequiel R. Aguerre
Browse files

Merge branch '1-mb-get_a_resource_item' into 'master'

Get a resource item

Get a resource item by an identifier through the API.

Example: Get the item with id 1 from the "items" resource:

```swift
let api = Api( baseURL: <Api URL>, apiPath: "1" )
api.resource( "items" ).item( 1 ) { ( item: Item ) in
   print( "Got item \(item.id)")
}
```
Closes #1

See merge request !1
parents 512ed61d c1085960
//
// Api.swift
// Pods
//
// Created by Mauro Bender on 13/8/16.
//
//
import Alamofire
public typealias ApiMethod = Alamofire.Method
public class Api {
let baseURL: NSURL
let apiPath: String
var apiURL: NSURL { return baseURL.URLByAppendingPathComponent( apiPath ) }
public init( baseURL: NSURL, apiPath: String ) {
self.baseURL = baseURL
self.apiPath = apiPath
}
// RESOURCE
public func resource<T>( path: String ) -> Resource<T> {
return Resource( api: self, path: path )
}
// REQUESTS
public func request( method: ApiMethod, path: String,
parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL,
headers: [String: String]? = nil, completion: Response<AnyObject, NSError> -> Void )
{
Alamofire.request( method, urlForPath( path ).absoluteString, parameters: parameters,
encoding: encoding, headers: headers ).responseJSON( completionHandler: completion )
}
public func get( path: String, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL,
headers: [String: String]? = nil, completion: Response<AnyObject, NSError> -> Void )
{
return request( .GET, path: path, parameters: parameters, encoding: encoding,
headers: headers, completion: completion)
}
public func post( path: String, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL,
headers: [String: String]? = nil, completion: Response<AnyObject, NSError> -> Void )
{
return request( .POST, path: path, parameters: parameters, encoding: encoding,
headers: headers, completion: completion)
}
func urlForPath( path: String ) -> NSURL {
return apiURL.URLByAppendingPathComponent( path )
}
}
// MARK: Equatable
extension Api: Equatable {}
public func ==(lhs: Api, rhs: Api) -> Bool {
return lhs.baseURL == rhs.baseURL && lhs.apiPath == rhs.apiPath
}
\ No newline at end of file
//
// Resource.swift
// Pods
//
// Created by Mauro Bender on 13/8/16.
//
//
import Alamofire
public protocol JSONSerializable {
init( fromJSON json: [ String: AnyObject ] )
func toJSON() -> [ String: AnyObject ]
}
public class Resource<T: JSONSerializable> {
public let api: Api
public let path: String
init( api: Api, path: String ) {
self.api = api
self.path = path
}
public func item( id: AnyObject, parameters: [ String: String ]? = nil,
headers: [ String: String]? = nil, success: ( ( T ) -> Void ) )
{
api.get( resourceMemberPath( id ), parameters: parameters, headers: headers ) { response in
if let json = response.result.value {
success( T( fromJSON: json as! [String : AnyObject] ) )
}
}
}
func resourceMemberPath( memberId: AnyObject ) -> String {
return "\(path)/\(memberId)"
}
}
\ No newline at end of file
......@@ -7,14 +7,17 @@
objects = {
/* Begin PBXBuildFile section */
3F4BA42D6E871B0AFE17AB43 /* Pods_Api_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F010C0FC221BD0E2F9A6AA81 /* Pods_Api_Tests.framework */; };
341078431D5FEAC500273A16 /* ApiTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 341078421D5FEAC500273A16 /* ApiTests.swift */; };
341078451D5FEF4D00273A16 /* Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 341078441D5FEF4D00273A16 /* Fixtures.swift */; };
341078491D5FFE4600273A16 /* ResourceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 341078481D5FFE4600273A16 /* ResourceTests.swift */; };
3410784B1D5FFE6300273A16 /* Matchers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3410784A1D5FFE6300273A16 /* Matchers.swift */; };
607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACD51AFB9204008FA782 /* AppDelegate.swift */; };
607FACD81AFB9204008FA782 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACD71AFB9204008FA782 /* ViewController.swift */; };
607FACDB1AFB9204008FA782 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 607FACD91AFB9204008FA782 /* Main.storyboard */; };
607FACDD1AFB9204008FA782 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDC1AFB9204008FA782 /* Images.xcassets */; };
607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDE1AFB9204008FA782 /* LaunchScreen.xib */; };
607FACEC1AFB9204008FA782 /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACEB1AFB9204008FA782 /* Tests.swift */; };
D436B241353EA963E3D00DF7 /* Pods_Api_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0148D81EA8C7A0067951D8BA /* Pods_Api_Example.framework */; };
81FA57964A8D90D838EC6D8C /* Pods_Api_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 64ABF5FA55768A98CB2BEE80 /* Pods_Api_Tests.framework */; };
B97020B2E389B47460FFD409 /* Pods_Api_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3307CE25A69485A844260DBD /* Pods_Api_Example.framework */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
......@@ -28,8 +31,12 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
0148D81EA8C7A0067951D8BA /* Pods_Api_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Api_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; };
1444DCA0A8868BC364774299 /* Pods-Api_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Api_Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-Api_Example/Pods-Api_Example.release.xcconfig"; sourceTree = "<group>"; };
040718C7CB1BA9B91F451B09 /* Pods-Api_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Api_Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Api_Tests/Pods-Api_Tests.debug.xcconfig"; sourceTree = "<group>"; };
3307CE25A69485A844260DBD /* Pods_Api_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Api_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; };
341078421D5FEAC500273A16 /* ApiTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApiTests.swift; sourceTree = "<group>"; };
341078441D5FEF4D00273A16 /* Fixtures.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Fixtures.swift; sourceTree = "<group>"; };
341078481D5FFE4600273A16 /* ResourceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResourceTests.swift; sourceTree = "<group>"; };
3410784A1D5FFE6300273A16 /* Matchers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Matchers.swift; sourceTree = "<group>"; };
607FACD01AFB9204008FA782 /* Api_Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Api_Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
607FACD41AFB9204008FA782 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
607FACD51AFB9204008FA782 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
......@@ -39,13 +46,12 @@
607FACDF1AFB9204008FA782 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = "<group>"; };
607FACE51AFB9204008FA782 /* Api_Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Api_Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
607FACEA1AFB9204008FA782 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
607FACEB1AFB9204008FA782 /* Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = "<group>"; };
64ABF5FA55768A98CB2BEE80 /* Pods_Api_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Api_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8044670F699EDC65D03A0A35 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
808398A418B0024AD8F5B9AA /* Pods-Api_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Api_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-Api_Tests/Pods-Api_Tests.release.xcconfig"; sourceTree = "<group>"; };
80A1E4B6C9684C944AC09BD9 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
CD62D6C32BB36DE2F6019605 /* Pods-Api_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Api_Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Api_Tests/Pods-Api_Tests.debug.xcconfig"; sourceTree = "<group>"; };
F010C0FC221BD0E2F9A6AA81 /* Pods_Api_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Api_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
F05251153BD43E10056A5F89 /* Pods-Api_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Api_Example.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Api_Example/Pods-Api_Example.debug.xcconfig"; sourceTree = "<group>"; };
A03E9DA01E5A09D861D999A1 /* Pods-Api_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Api_Example.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Api_Example/Pods-Api_Example.debug.xcconfig"; sourceTree = "<group>"; };
A10B5358F04FDFCB63E6340D /* Pods-Api_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Api_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-Api_Tests/Pods-Api_Tests.release.xcconfig"; sourceTree = "<group>"; };
CE255C263791EE4B72F57705 /* Pods-Api_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Api_Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-Api_Example/Pods-Api_Example.release.xcconfig"; sourceTree = "<group>"; };
F67E9A0F3477D0D0D4532E33 /* Api.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = Api.podspec; path = ../Api.podspec; sourceTree = "<group>"; };
/* End PBXFileReference section */
......@@ -54,7 +60,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D436B241353EA963E3D00DF7 /* Pods_Api_Example.framework in Frameworks */,
B97020B2E389B47460FFD409 /* Pods_Api_Example.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -62,18 +68,29 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
3F4BA42D6E871B0AFE17AB43 /* Pods_Api_Tests.framework in Frameworks */,
81FA57964A8D90D838EC6D8C /* Pods_Api_Tests.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
2FE24D1BD29589B4EC7785E5 /* Frameworks */ = {
0B7DE14582959AE402685CAE /* Pods */ = {
isa = PBXGroup;
children = (
0148D81EA8C7A0067951D8BA /* Pods_Api_Example.framework */,
F010C0FC221BD0E2F9A6AA81 /* Pods_Api_Tests.framework */,
A03E9DA01E5A09D861D999A1 /* Pods-Api_Example.debug.xcconfig */,
CE255C263791EE4B72F57705 /* Pods-Api_Example.release.xcconfig */,
040718C7CB1BA9B91F451B09 /* Pods-Api_Tests.debug.xcconfig */,
A10B5358F04FDFCB63E6340D /* Pods-Api_Tests.release.xcconfig */,
);
name = Pods;
sourceTree = "<group>";
};
50F0ACD142E5252EF9E9F4E3 /* Frameworks */ = {
isa = PBXGroup;
children = (
3307CE25A69485A844260DBD /* Pods_Api_Example.framework */,
64ABF5FA55768A98CB2BEE80 /* Pods_Api_Tests.framework */,
);
name = Frameworks;
sourceTree = "<group>";
......@@ -85,8 +102,8 @@
607FACD21AFB9204008FA782 /* Example for Api */,
607FACE81AFB9204008FA782 /* Tests */,
607FACD11AFB9204008FA782 /* Products */,
F370AF886C31C602C3F6AE5A /* Pods */,
2FE24D1BD29589B4EC7785E5 /* Frameworks */,
0B7DE14582959AE402685CAE /* Pods */,
50F0ACD142E5252EF9E9F4E3 /* Frameworks */,
);
sourceTree = "<group>";
};
......@@ -124,7 +141,10 @@
607FACE81AFB9204008FA782 /* Tests */ = {
isa = PBXGroup;
children = (
607FACEB1AFB9204008FA782 /* Tests.swift */,
341078441D5FEF4D00273A16 /* Fixtures.swift */,
3410784A1D5FFE6300273A16 /* Matchers.swift */,
341078421D5FEAC500273A16 /* ApiTests.swift */,
341078481D5FFE4600273A16 /* ResourceTests.swift */,
607FACE91AFB9204008FA782 /* Supporting Files */,
);
path = Tests;
......@@ -148,17 +168,6 @@
name = "Podspec Metadata";
sourceTree = "<group>";
};
F370AF886C31C602C3F6AE5A /* Pods */ = {
isa = PBXGroup;
children = (
F05251153BD43E10056A5F89 /* Pods-Api_Example.debug.xcconfig */,
1444DCA0A8868BC364774299 /* Pods-Api_Example.release.xcconfig */,
CD62D6C32BB36DE2F6019605 /* Pods-Api_Tests.debug.xcconfig */,
808398A418B0024AD8F5B9AA /* Pods-Api_Tests.release.xcconfig */,
);
name = Pods;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
......@@ -274,7 +283,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
8A0587AC68B52035A45FD66B /* [CP] Embed Pods Frameworks */ = {
......@@ -304,7 +313,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
C180C5D7E55D073D931ADCD1 /* [CP] Embed Pods Frameworks */ = {
......@@ -368,7 +377,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
607FACEC1AFB9204008FA782 /* Tests.swift in Sources */,
3410784B1D5FFE6300273A16 /* Matchers.swift in Sources */,
341078431D5FEAC500273A16 /* ApiTests.swift in Sources */,
341078491D5FFE4600273A16 /* ResourceTests.swift in Sources */,
341078451D5FEF4D00273A16 /* Fixtures.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -486,7 +498,7 @@
};
607FACF01AFB9204008FA782 /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = F05251153BD43E10056A5F89 /* Pods-Api_Example.debug.xcconfig */;
baseConfigurationReference = A03E9DA01E5A09D861D999A1 /* Pods-Api_Example.debug.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = Api/Info.plist;
......@@ -499,7 +511,7 @@
};
607FACF11AFB9204008FA782 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 1444DCA0A8868BC364774299 /* Pods-Api_Example.release.xcconfig */;
baseConfigurationReference = CE255C263791EE4B72F57705 /* Pods-Api_Example.release.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = Api/Info.plist;
......@@ -512,7 +524,7 @@
};
607FACF31AFB9204008FA782 /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = CD62D6C32BB36DE2F6019605 /* Pods-Api_Tests.debug.xcconfig */;
baseConfigurationReference = 040718C7CB1BA9B91F451B09 /* Pods-Api_Tests.debug.xcconfig */;
buildSettings = {
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
......@@ -531,7 +543,7 @@
};
607FACF41AFB9204008FA782 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 808398A418B0024AD8F5B9AA /* Pods-Api_Tests.release.xcconfig */;
baseConfigurationReference = A10B5358F04FDFCB63E6340D /* Pods-Api_Tests.release.xcconfig */;
buildSettings = {
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
......
# frozen_string_literal: true
# A sample Gemfile
source "https://rubygems.org"
gem 'cocoapods', git: 'https://github.com/CocoaPods/CocoaPods.git'
GIT
remote: https://github.com/CocoaPods/CocoaPods.git
revision: 1bf338273637cf658e78388ca996cebabbce12db
specs:
cocoapods (1.1.0.beta.1)
activesupport (>= 4.0.2, < 5)
claide (>= 1.0.0, < 2.0)
cocoapods-core (= 1.1.0.beta.1)
cocoapods-deintegrate (>= 1.0.0, < 2.0)
cocoapods-downloader (>= 1.1.0, < 2.0)
cocoapods-plugins (>= 1.0.0, < 2.0)
cocoapods-search (>= 1.0.0, < 2.0)
cocoapods-stats (>= 1.0.0, < 2.0)
cocoapods-trunk (>= 1.0.0, < 2.0)
cocoapods-try (>= 1.1.0, < 2.0)
colored (~> 1.2)
escape (~> 0.0.4)
fourflusher (~> 1.0.1)
gh_inspector (~> 1.0)
molinillo (~> 0.5.0)
nap (~> 1.0)
xcodeproj (>= 1.2.0, < 2.0)
GEM
remote: https://rubygems.org/
specs:
activesupport (4.2.7.1)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
claide (1.0.0)
cocoapods-core (1.1.0.beta.1)
activesupport (>= 4.0.2)
fuzzy_match (~> 2.0.4)
nap (~> 1.0)
cocoapods-deintegrate (1.0.0)
cocoapods-downloader (1.1.0)
cocoapods-plugins (1.0.0)
nap
cocoapods-search (1.0.0)
cocoapods-stats (1.0.0)
cocoapods-trunk (1.0.0)
nap (>= 0.8, < 2.0)
netrc (= 0.7.8)
cocoapods-try (1.1.0)
colored (1.2)
escape (0.0.4)
fourflusher (1.0.1)
fuzzy_match (2.0.4)
gh_inspector (1.0.2)
i18n (0.7.0)
json (1.8.3)
minitest (5.9.0)
molinillo (0.5.0)
nap (1.1.0)
netrc (0.7.8)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
xcodeproj (1.2.0)
activesupport (>= 3)
claide (>= 1.0.0, < 2.0)
colored (~> 1.2)
PLATFORMS
ruby
DEPENDENCIES
cocoapods!
BUNDLED WITH
1.12.5
use_frameworks!
target 'Api_Example' do
pod 'Api', :path => '../'
def common_pods
pod 'Api', :path => '../'
end
target 'Api_Tests' do
inherit! :search_paths
target 'Api_Example' do
common_pods
end
pod 'Quick', '~> 0.8'
pod 'Nimble', '~> 3.0'
end
target 'Api_Tests' do
common_pods
pod 'Quick', '~> 0.8'
pod 'Nimble', '~> 3.0'
pod 'Mockingjay', '1.1.1'
end
PODS:
- Api (0.1.0)
- Alamofire (3.4.1)
- Api (0.1.0):
- Alamofire (~> 3.4)
- Mockingjay (1.1.1):
- Mockingjay/Core (= 1.1.1)
- Mockingjay/XCTest (= 1.1.1)
- Mockingjay/Core (1.1.1):
- URITemplate (~> 1.3)
- Mockingjay/XCTest (1.1.1):
- Mockingjay/Core
- Nimble (3.2.0)
- Quick (0.9.3)
- URITemplate (1.3.1)
DEPENDENCIES:
- Api (from `../`)
- Mockingjay (= 1.1.1)
- Nimble (~> 3.0)
- Quick (~> 0.8)
......@@ -13,10 +24,13 @@ EXTERNAL SOURCES:
:path: "../"
SPEC CHECKSUMS:
Api: 353b3e560c6b9059ff722d3ecee4d666db86d97e
Alamofire: 01a82e2f6c0f860ade35534c8dd88be61bdef40c
Api: d8f98ef3a30ff61b6a870c447b4feee3e70759ad
Mockingjay: 757f80abc947b5ec2918b1bb14f3066574720430
Nimble: 703854335d181df169bbca9c97117b5cf8c47c1d
Quick: 13a2a2b19a5d8e3ed4fd0c36ee46597fd77ebf71
URITemplate: 398a48d6559e23b77bd41176b675b017ad1fc959
PODFILE CHECKSUM: 3a00c58a320d6d5054ff19c91ee85f0dbccf890d
PODFILE CHECKSUM: 96c182174ea46d3ffa1c95cfcd51266de20b3756
COCOAPODS: 1.0.1
COCOAPODS: 1.1.0.beta.1
////
//// ApiTests.swift
//// Api
////
//// Created by Mauro Bender on 13/8/16.
//// Copyright © 2016 CocoaPods. All rights reserved.
////
import Quick
import Nimble
import Mockingjay
import Api
class ApiTests: QuickSpec {
override func setUp() {
super.setUp()
// Register stubs
stub( matchApiPath( "tests/get", method: .GET ), builder: http( 200 ) )
stub( matchApiPath( "tests/delete", method: .DELETE ), builder: http( 200 ) )
}
override func spec() {
describe( "Api" ) {
let api = Api( baseURL: baseURL, apiPath: apiPath )
describe( "resource" ) {
let resource : Resource<Item> = api.resource( "items" )
it( "should have the correct api" ) { expect( resource.api ) == api }
it( "should have the correct path" ) { expect( resource.path ) == "items" }
}
describe( "request" ) {
it( "should call the correct endpoint with the correct method" ) {
waitUntil { done in
api.request( .DELETE, path: "tests/delete" ) { result in done() }
}
}
}
describe( "get" ) {
it( "should call the correct endpoint" ) {
waitUntil { done in api.get( "tests/get" ) { result in done() } }
}
}
}
}
}
//
// Fixtures.swift
// Api
//
// Created by Mauro Bender on 13/8/16.
// Copyright © 2016 CocoaPods. All rights reserved.
//
import Api
struct Item {
let id: Int
let name: String
}
extension Item: JSONSerializable {
init(fromJSON json: [String : AnyObject]) {
self.id = json[ "id" ] as! Int
self.name = json[ "name" ] as! String
}
func toJSON() -> [String : AnyObject] {
return [ String: AnyObject ]()
}
}
\ No newline at end of file
//
// Matchers.swift
// Api
//
// Created by Mauro Bender on 13/8/16.
// Copyright © 2016 CocoaPods. All rights reserved.
//
import Api
import Foundation
import Mockingjay
let baseURL = NSURL( string: "http://api.test.com" )!
let apiPath = "1"
func matchApiPath( path: String, method: ApiMethod = .GET ) -> ( ( NSURLRequest ) -> Bool ) {
let url = baseURL.URLByAppendingPathComponent( apiPath ).URLByAppendingPathComponent( path )
return { ( request: NSURLRequest ) in
return url == request.URL && request.HTTPMethod == method.rawValue
}
}
\ No newline at end of file
//
// ResourceTests.swift
// Api
//
// Created by Mauro Bender on 13/8/16.
// Copyright © 2016 CocoaPods. All rights reserved.
//
import Quick
import Nimble
import Mockingjay
import Api
class ResourceTests: QuickSpec {
override func setUp() {
super.setUp()
// Register stubs
stub( matchApiPath( "items/1" ), builder: json( [ "id": 1, "name": "Item 1" ] ) )
}
override func spec() {
let api = Api( baseURL: baseURL, apiPath: apiPath )
describe( "Resource" ) {
let resource: Resource<Item> = api.resource( "items" )
describe( "item<T>" ) {
context( "when the request is successful" ) {
it( "should call the correct endpoint and return the item" ) {
waitUntil { done in
resource.item( 1 ) { ( item: Item ) in
expect( item.id ) == 1
expect( item.name ) == "Item 1"
done()
}
}
}
}
}
}
}
}
\ No newline at end of file