Skip to content

Commit

Permalink
- Updated read me.
Browse files Browse the repository at this point in the history
- Added example code
- Added manipulation with concurrent operation count and requestURL property.
  • Loading branch information
Josip Bernat committed Apr 11, 2014
1 parent fa28d70 commit f0df163
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 19 deletions.
86 changes: 84 additions & 2 deletions JBMessage/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5023" systemVersion="13A603" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5053" systemVersion="13C64" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
</dependencies>
<scenes>
<!--class Prefix:identifier View Controller-->
<!--View Controller-->
<scene sceneID="ufC-wZ-h7g">
<objects>
<viewController id="vXZ-lx-hvc" customClass="JBViewController" sceneMemberID="viewController">
Expand All @@ -15,6 +15,88 @@
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="D1S-vi-dW9">
<rect key="frame" x="20" y="89" width="112" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="GET - base URL">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="onGetWithBaseURL:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="FSx-tA-FdP"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cvj-HH-Shy">
<rect key="frame" x="20" y="127" width="122" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="POST - base URL">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="onPostWithBaseURL:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="Cag-FE-Ba0"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Eb2-SK-Q9Q">
<rect key="frame" x="20" y="165" width="112" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="PUT - base URL">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="onPutWithBaseURL:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="gx6-Bq-DHl"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="BBm-g8-b5k">
<rect key="frame" x="20" y="195" width="138" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="DELETE - base URL">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="onDeleteWithBaseURL:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="SU4-Oq-CY9"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="WXB-Rf-OCS">
<rect key="frame" x="225" y="89" width="75" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="GET - URL">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="onGetWithRequestURL:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="pdl-nC-qOH"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="1aq-0Z-3KX">
<rect key="frame" x="215" y="127" width="85" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="POST - URL">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="onPostWithRequestURL:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="GYY-lU-hrZ"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="OY7-jk-PkW">
<rect key="frame" x="230" y="165" width="70" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="PUT -URL">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="onPostWithRequestURL:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="UIe-yb-Lrz"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="v2q-KR-EvV">
<rect key="frame" x="199" y="195" width="101" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="DELETE - URL">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="onDeleteWithRequestURL:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="uXO-66-aOp"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
</viewController>
Expand Down
31 changes: 28 additions & 3 deletions JBMessage/JBMessage/JBMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ typedef void (^JBDownloadBlock)(NSUInteger bytesRead, NSInteger totalBytesRead,
*/
@property (nonatomic, copy) NSString *action;

/**
* URL used for the request. If set the registrated base URL is not used.
*/
@property (nonatomic, strong) NSURL *requestURL;

/**
* HTTP method for the request. Default is JBHTTPMethodPOST.
*/
Expand Down Expand Up @@ -121,12 +126,19 @@ typedef void (^JBDownloadBlock)(NSUInteger bytesRead, NSInteger totalBytesRead,
#pragma mark - URL Registration

/**
* Register baseUrl in order to enable request execution. The easiest way is to call it directly from application:didFinishLaunchingWithOptions:.
* Register baseUrl in order to enable request execution. The easiest way is to call it directly from application:didFinishLaunchingWithOptions:. Once it's set it cannot be canged.
*
* @param baseUrl Url to register, i.e. http://example.com/api/.
*/
+ (void)registerBaseUrl:(NSString *)baseUrl;

/**
* Sets number of concurrent messages in messages queue.
*
* @param maxConcurrentMessages Number of concurrent messages to be set.
*/
+ (void)requsterMaxNumberOfConcurrentMessages:(NSUInteger)maxConcurrentMessages;

#pragma mark - Operation Controll

/**
Expand All @@ -147,18 +159,31 @@ typedef void (^JBDownloadBlock)(NSUInteger bytesRead, NSInteger totalBytesRead,
* @param parameters Web service parameters to be sent.
* @param responseBlock Response callback. Will contain parsed objects depending on the message or an error if there was one.
*
* @return an instance of VCMessage
* @return an instance of JBMessage.
*/
+ (instancetype)messageWithParameters:(NSDictionary *) parameters
responseBlock:(JBResponseBlock) responseBlock;

/**
* Initializes the message with given URL and parameters.
*
* @param URL URL of web service. Will not be saved or retained for other requests.
* @param parameters Web service parameters to be sent.
* @param responseBlock Response callback. Will contain parsed objects depending on the message or an error if there was one.
*
* @return An instance of JBMessage.
*/
+ (instancetype)messageWithURL:(NSURL *)URL
parameters:(NSDictionary *)parameters
responseBlock:(JBResponseBlock) responseBlock;

/**
* Initializes the message with parameters
*
* @param parameters Web service parameters to be sent.
* @param responseBlock Response callback. Will contain parsed objects depending on the message or an error if there was one.
*
* @return an instance of VCMessage
* @return an instance of JBMessage.
*/
- (id)initWithParameters:(NSDictionary *)parameters
responseBlock:(JBResponseBlock) responseBlock;
Expand Down
53 changes: 40 additions & 13 deletions JBMessage/JBMessage/JBMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
JBHTTPMethod const JBHTTPMethodPUT = @"PUT";
JBHTTPMethod const JBHTTPMethodDELETE = @"DELETE";


@interface JBMessage () {

BOOL _isCancelled;
BOOL _isFinished;
BOOL _isExecuting;
}

#pragma mark - Shared Queue
+ (NSOperationQueue *)sharedQueue;

@property (nonatomic) UIBackgroundTaskIdentifier backgroundTaskIdentifier;

@end
Expand All @@ -45,6 +49,20 @@ - (void) dealloc {
_uploadBlock = nil;
}

#pragma mark - Shared Queue

+ (NSOperationQueue *)sharedQueue {

static NSOperationQueue *queue = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;
});

return queue;
}

#pragma mark - URL Registration

static NSString *baseUrlString = nil;
Expand All @@ -57,6 +75,11 @@ + (void)registerBaseUrl:(NSString *)baseUrl {
});
}

+ (void)requsterMaxNumberOfConcurrentMessages:(NSUInteger)maxConcurrentMessages {

[[JBMessage sharedQueue] setMaxConcurrentOperationCount:maxConcurrentMessages];
}

#pragma mark - Initialization

+ (instancetype)messageWithParameters:(NSDictionary *) parameters
Expand All @@ -67,6 +90,19 @@ + (instancetype)messageWithParameters:(NSDictionary *) parameters
return message;
}

+ (instancetype)messageWithURL:(NSURL *)URL
parameters:(NSDictionary *)parameters
responseBlock:(JBResponseBlock) responseBlock {

JBMessage *message = [[self alloc] init];
message.requestURL = URL;
message.parameters = parameters;
message.responseBlock = responseBlock;
[message initialize];

return message;
}

- (id)initWithParameters:(NSDictionary *)parameters
responseBlock:(JBResponseBlock)responseBlock {

Expand Down Expand Up @@ -240,18 +276,6 @@ - (id)parseResponse:(id)rawResponse error:(NSError *__autoreleasing *)error{

@implementation JBMessage (VCMessageCenter)

+ (NSOperationQueue *)sharedQueue {

static NSOperationQueue *queue = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;
});

return queue;
}

- (void)send {

[[[self class] sharedQueue] addOperation:self];
Expand Down Expand Up @@ -323,7 +347,10 @@ - (AFHTTPRequestOperationManager *)requestOperationManager {
}

- (NSString *)actionUrlString {
return [NSString stringWithFormat:@"%@%@", baseUrlString, self.action];

return (self.requestURL ?
(self.action ? [[self.requestURL absoluteString] stringByAppendingString:self.action] : [self.requestURL absoluteString]) :
[NSString stringWithFormat:@"%@%@", baseUrlString, self.action]);
}

- (NSMutableURLRequest *)urlRequest {
Expand Down
11 changes: 11 additions & 0 deletions JBMessage/JBViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,15 @@

@interface JBViewController : UIViewController

#pragma mark - Button Selectors
- (IBAction)onGetWithBaseURL:(id)sender;
- (IBAction)onPostWithBaseURL:(id)sender;
- (IBAction)onPutWithBaseURL:(id)sender;
- (IBAction)onDeleteWithBaseURL:(id)sender;

- (IBAction)onGetWithRequestURL:(id)sender;
- (IBAction)onPostWithRequestURL:(id)sender;
- (IBAction)onPutWithRequestURL:(id)sender;
- (IBAction)onDeleteWithRequestURL:(id)sender;

@end
94 changes: 93 additions & 1 deletion JBMessage/JBViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "JBViewController.h"
#import "JBMessage.h"

@interface JBViewController ()

Expand All @@ -17,7 +18,8 @@ @implementation JBViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[JBMessage registerBaseUrl:@"http://example.com/"];
}

- (void)didReceiveMemoryWarning
Expand All @@ -26,4 +28,94 @@ - (void)didReceiveMemoryWarning
// Dispose of any resources that can be recreated.
}

#pragma mark - Button Selectors

- (IBAction)onGetWithBaseURL:(id)sender {

JBMessage *message = [JBMessage messageWithParameters:@{@"foo": @"bar"}
responseBlock:^(id responseObject, NSError *error) {
NSLog(@"%@", responseObject);
}];
message.httpMethod = JBHTTPMethodGET;
message.action = @"resources.json";
[message send];
}

- (IBAction)onPostWithBaseURL:(id)sender {

JBMessage *message = [JBMessage messageWithParameters:@{@"foo": @"bar"}
responseBlock:^(id responseObject, NSError *error) {
NSLog(@"%@", responseObject);
}];
message.httpMethod = JBHTTPMethodPOST;
message.action = @"resources.json";
[message send];
}

- (IBAction)onPutWithBaseURL:(id)sender {

JBMessage *message = [JBMessage messageWithParameters:@{@"foo": @"bar"}
responseBlock:^(id responseObject, NSError *error) {
NSLog(@"%@", responseObject);
}];
message.httpMethod = JBHTTPMethodPUT;
message.action = @"resources.json";
[message send];
}

- (IBAction)onDeleteWithBaseURL:(id)sender {

JBMessage *message = [JBMessage messageWithParameters:@{@"foo": @"bar"}
responseBlock:^(id responseObject, NSError *error) {
NSLog(@"%@", responseObject);
}];
message.httpMethod = JBHTTPMethodDELETE;
message.action = @"resources.json";
[message send];
}

- (IBAction)onGetWithRequestURL:(id)sender {

JBMessage *message = [JBMessage messageWithURL:[NSURL URLWithString:@"http://example.com/resources.json"]
parameters:@{@"foo": @"bar"}
responseBlock:^(id responseObject, NSError *error) {
NSLog(@"%@", responseObject);
}];
message.httpMethod = JBHTTPMethodGET;
[message send];
}

- (IBAction)onPostWithRequestURL:(id)sender {

JBMessage *message = [JBMessage messageWithURL:[NSURL URLWithString:@"http://example.com/resources.json"]
parameters:@{@"foo": @"bar"}
responseBlock:^(id responseObject, NSError *error) {
NSLog(@"%@", responseObject);
}];
message.httpMethod = JBHTTPMethodPOST;
[message send];
}

- (IBAction)onPutWithRequestURL:(id)sender {

JBMessage *message = [JBMessage messageWithURL:[NSURL URLWithString:@"http://example.com/resources.json"]
parameters:@{@"foo": @"bar"}
responseBlock:^(id responseObject, NSError *error) {
NSLog(@"%@", responseObject);
}];
message.httpMethod = JBHTTPMethodPUT;
[message send];
}

- (IBAction)onDeleteWithRequestURL:(id)sender {

JBMessage *message = [JBMessage messageWithURL:[NSURL URLWithString:@"http://example.com/resources.json"]
parameters:@{@"foo": @"bar"}
responseBlock:^(id responseObject, NSError *error) {
NSLog(@"%@", responseObject);
}];
message.httpMethod = JBHTTPMethodDELETE;
[message send];
}

@end
Loading

0 comments on commit f0df163

Please sign in to comment.