Commit 683e6967 authored by Bosko Petreski's avatar Bosko Petreski Committed by Bosko Petreski

singletons added

parent 522bcc3c
......@@ -15,40 +15,45 @@
@implementation PIOViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Create Instance
PrimedIO *primed = [[PrimedIO alloc] initWithPublicKey:@"somekey" secretKey:@"somesecretkeywithalotofchars" connectionString:@"API_URL_GO_HERE"];
#pragma mark - IBActions
-(IBAction)onBtnMakeConvert:(UIButton *)sender{
// Make conversion
[PrimedIO.sharedInstance convert:@"RUUID_GO_HERE"];
// Make conversion with data
NSDictionary *dictData = @{@"device": @"iphone",
@"userid": @"someuserid"
};
[PrimedIO.sharedInstance convert:@"RUUID_GO_HERE" data:dictData];
}
-(IBAction)onBtnPersonalise:(UIButton *)sender{
NSDictionary *dictSignals = @{@"device": @"iphone",
@"userid": @"someuserid"
};
[primed personalise:@"frontpage.article.bottom" signals:dictSignals limit:3 abvariantLabel:@"A" success:^(NSDictionary *response) {
[PrimedIO.sharedInstance personalise:@"frontpage.recommendations" signals:dictSignals limit:3 abvariantLabel:@"A" success:^(NSDictionary *response) {
//Handle response
NSLog(@"personalise: %@",response);
} failed:^(NSString *message) {
//Handle message
NSLog(@"personalise error: %@",message);
}];
}
-(IBAction)onBtnTrackView:(UIButton *)sender{
// Make conversion
[primed convert:@"RUUID_GO_HERE"];
// Make conversion with data
NSDictionary *dictData = @{@"device": @"iphone",
@"userid": @"someuserid"
};
[primed convert:@"RUUID_GO_HERE" data:dictData];
}
- (void)didReceiveMemoryWarning
{
#pragma mark - UIViewDelegates
-(void)viewDidLoad{
[super viewDidLoad];
// Create PrimedIO instance
[PrimedIO.sharedInstance initWithPublicKey:@"mypubkey" secretKey:@"mysecretkey" connectionString:@"https://gw.staging.primed.io:443"];
PrimedIO.sharedInstance.debug = YES;
}
-(void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
......@@ -8,21 +8,32 @@
#import <Foundation/Foundation.h>
#define PrimedIOLog(FORMAT, ...) fprintf(stderr,"[%s][Line %d]%s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
typedef void(^APISuccess)(NSDictionary *response);
typedef void(^APIFailed)(NSString *message);
@interface PrimedIO : NSObject
@property (nonatomic,assign) BOOL debug;
@property (nonatomic,strong) NSString *sha512Signature;
@property (nonatomic,strong) NSString *public_key;
@property (nonatomic,strong) NSString *nonce;
@property (nonatomic,strong) NSString *connectionStringPrimedIO;
/**
* Create instance of PrimedIO class
* @return PrimedIO shared instance
*/
+(PrimedIO *)sharedInstance;
/**
* Create instance of PrimedIO class
* @param publicKey Public Key provided by PrimedIO
* @param secretKey Secret Key provided by PrimedIO
* @param connectionString URL to API for PrimedIO service
* @return PrimedIO instance
*/
-(PrimedIO *)initWithPublicKey:(NSString *)publicKey secretKey:(NSString *)secretKey connectionString:(NSString *)connectionString;
-(void)initWithPublicKey:(NSString *)publicKey secretKey:(NSString *)secretKey connectionString:(NSString *)connectionString;
/**
* Deal with the results as they are returned
......
......@@ -9,26 +9,19 @@
#import "PrimedIO.h"
#import <CommonCrypto/CommonCrypto.h>
#define PrimedIOLog(FORMAT, ...) fprintf(stderr,"[%s][Line %d]%s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
@implementation PrimedIO{
NSString *sha512Signature;
NSString *public_key;
NSString *nonce;
NSString *connectionStringPrimedIO;
}
@implementation PrimedIO
#pragma mark - HTTP Methods
-(NSMutableURLRequest *)requestWithURL:(NSString *)urlLink method:(NSString *)method{
NSString *strUrl = [NSString stringWithFormat:@"%@%@",connectionStringPrimedIO,urlLink];
NSString *strUrl = [NSString stringWithFormat:@"%@%@",self.connectionStringPrimedIO,urlLink];
NSURL *URL = [NSURL URLWithString:strUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:public_key forHTTPHeaderField:@"X-Authorization-Key"];
[request addValue:sha512Signature forHTTPHeaderField:@"X-Authorization-Signature"];
[request addValue:nonce forHTTPHeaderField:@"X-Authorization-Nonce"];
[request addValue:self.public_key forHTTPHeaderField:@"X-Authorization-Key"];
[request addValue:self.sha512Signature forHTTPHeaderField:@"X-Authorization-Signature"];
[request addValue:self.nonce forHTTPHeaderField:@"X-Authorization-Nonce"];
[request setHTTPMethod:method];
return request;
......@@ -110,23 +103,37 @@
}
#pragma mark - Initialization
-(PrimedIO *)initWithPublicKey:(NSString *)publicKey secretKey:(NSString *)secretKey connectionString:(NSString *)connectionString{
if ((self = [super init])){
if(self.debug){ PrimedIOLog(@"Primed Debug Activated") }
public_key = publicKey;
nonce = [NSString stringWithFormat:@"%.f",NSDate.date.timeIntervalSince1970];
NSString *prepSignature = [NSString stringWithFormat:@"%@%@%@",publicKey,secretKey,nonce];
sha512Signature = [PrimedIO hmac:prepSignature];
connectionStringPrimedIO = connectionString;
+(PrimedIO *)sharedInstance{
static dispatch_once_t pred;
static PrimedIO *sharedBridge = nil;
dispatch_once(&pred, ^{
sharedBridge = [[self alloc] init];
});
return sharedBridge;
}
-(id)init{
self = [super init];
if(self) {
}
return self;
}
-(void)initWithPublicKey:(NSString *)publicKey secretKey:(NSString *)secretKey connectionString:(NSString *)connectionString{
if(self.debug){
PrimedIOLog(@"Primed Debug Activated");
}
self.public_key = publicKey;
self.nonce = [NSString stringWithFormat:@"%.f",NSDate.date.timeIntervalSince1970];
NSString *prepSignature = [NSString stringWithFormat:@"%@%@%@",publicKey,secretKey,self.nonce];
self.sha512Signature = [PrimedIO hmac:prepSignature];
self.connectionStringPrimedIO = connectionString;
}
#pragma mark - API Main Functions
-(void)personalise:(NSString *)campaign signals:(NSDictionary *)signals limit:(NSUInteger)limit abvariantLabel:(NSString *)abvariantLabel success:(APISuccess)success failed:(APIFailed)failed;{
NSError *errorJSON;
NSData *signalsJSON = [NSJSONSerialization dataWithJSONObject:signals options:0 error:&errorJSON];
NSData *signalsJSON = [NSJSONSerialization dataWithJSONObject:signals options:NSJSONWritingPrettyPrinted error:&errorJSON];
if(errorJSON){
failed(@"Signals not valid format");
return;
......@@ -137,7 +144,7 @@
NSMutableString *generateURL = [NSMutableString stringWithString:@"/api/v1/personalise?"];
[generateURL appendFormat:@"campaign=%@",campaign];
[generateURL appendFormat:@"&limit=%lu",(unsigned long)limit];
[generateURL appendFormat:@"&abvariantLabel=%@",[abvariantLabel stringByAddingPercentEncodingWithAllowedCharacters:setEncoding]];
[generateURL appendFormat:@"&abvariant=%@",[abvariantLabel stringByAddingPercentEncodingWithAllowedCharacters:setEncoding]];
[generateURL appendFormat:@"&signals=%@",[signalToString stringByAddingPercentEncodingWithAllowedCharacters:setEncoding]];
[self get:generateURL success:^(NSDictionary *response) {
......
......@@ -10,6 +10,12 @@
@interface PrimedTracker : PrimedIO
/**
* Create instance of PrimedTracker class
* @return PrimedTracker shared instance
*/
+(PrimedTracker *)sharedInstance;
/**
* Create instance of PrimedTracker class
* @param publicKey Public Key provided by PrimedIO
......@@ -18,20 +24,33 @@
* @param trackingConnectionString URL to API for PrimedIO tracking service
* @param heartbeatInterval need info
* @param eventQueueRetryInterval need info
* @return PrimedTracker instance
*/
-(PrimedTracker *)initWithPublicKey:(NSString *)publicKey secretKey:(NSString *)secretKey connectionString:(NSString *)connectionString trackingConnectionString:(NSString *)trackingConnectionString heartbeatInterval:(NSInteger)heartbeatInterval eventQueueRetryInterval:(NSInteger)eventQueueRetryInterval;
-(void)initWithPublicKey:(NSString *)publicKey secretKey:(NSString *)secretKey connectionString:(NSString *)connectionString trackingConnectionString:(NSString *)trackingConnectionString heartbeatInterval:(NSInteger)heartbeatInterval eventQueueRetryInterval:(NSInteger)eventQueueRetryInterval;
//PRIMEDTRACKER.trackClick( int x,int y, ClickType clickType)
//PRIMEDTRACKER.trackView(...)
//PRIMEDTRACKER.trackScroll( ScrollDirection direction, int distance)
/**
* Create instance of PrimedTracker class
* @param url URL Tracking
*/
-(void)trackView:(NSString *)url;
//PRIMEDTRACKER.trackEnterViewPort( String campaign, Array[int] elements)
/**
* Track when enter to screen
* @param campaign Campaign
* @param elements Elements
*/
-(void)trackEnterViewPort:(NSString *)campaign elements:(NSArray *)elements;
//PRIMEDTRACKER.trackEnterViewPort( String campaign, Array[int] elements)
/**
* Track when exit from screen
* @param campaign Campaign
* @param elements Elements
*/
-(void)trackExitViewPort:(NSString *)campaign elements:(NSArray *)elements;
//PRIMEDTRACKER.trackEnterViewPort( String campaign, Array[int] elements)
/**
* Create instance of PrimedTracker class
* @param data Custom JSON Data for event
*/
-(void)trackCustomEvent:(NSDictionary *)data;
@end
......@@ -6,15 +6,51 @@
//
#import "PrimedTracker.h"
#import "PrimedIO.h"
@implementation PrimedTracker
-(PrimedTracker *)initWithPublicKey:(NSString *)publicKey secretKey:(NSString *)secretKey connectionString:(NSString *)connectionString trackingConnectionString:(NSString *)trackingConnectionString heartbeatInterval:(NSInteger)heartbeatInterval eventQueueRetryInterval:(NSInteger)eventQueueRetryInterval{
if ((self = [super init])){
#pragma mark - Initialization
+(PrimedTracker *)sharedInstance{
static dispatch_once_t pred;
static PrimedTracker *sharedBridge = nil;
dispatch_once(&pred, ^{
sharedBridge = [[self alloc] init];
});
return sharedBridge;
}
-(id)init{
self = [super init];
if(self) {
}
return self;
}
-(void)initWithPublicKey:(NSString *)publicKey secretKey:(NSString *)secretKey connectionString:(NSString *)connectionString trackingConnectionString:(NSString *)trackingConnectionString heartbeatInterval:(NSInteger)heartbeatInterval eventQueueRetryInterval:(NSInteger)eventQueueRetryInterval{
if(self.debug){
PrimedIOLog(@"Primed Tracker Debug Activated");
}
self.public_key = publicKey;
self.nonce = [NSString stringWithFormat:@"%.f",NSDate.date.timeIntervalSince1970];
NSString *prepSignature = [NSString stringWithFormat:@"%@%@%@",publicKey,secretKey,self.nonce];
self.sha512Signature = [PrimedIO hmac:prepSignature];
self.connectionStringPrimedIO = connectionString;
}
#pragma mark - API Main Functions
-(void)trackView:(NSString *)url{
}
-(void)trackEnterViewPort:(NSString *)campaign elements:(NSArray *)elements{
}
-(void)trackExitViewPort:(NSString *)campaign elements:(NSArray *)elements{
}
-(void)trackCustomEvent:(NSDictionary *)data{
}
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment