Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AFRailsHTTPClient
//
// AFRailsHTTPClient.h
// TopHatter
//
// Created by Leslie Fletcher on 1/30/12.
// Copyright (c) 2012 Blippy. All rights reserved.
//
#import "AFHTTPClient.h"
typedef void (^SessionResetBlock)(NSHTTPURLResponse *response, NSError *error);
@interface AFRailsHTTPClient : AFHTTPClient
@property (nonatomic, retain) NSString *authenticityToken;
@property (nonatomic, copy) SessionResetBlock sessionResetBlock;
+ (AFRailsHTTPClient *)clientWithBaseURL:(NSURL *)url;
- (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(AFHTTPRequestOperation *request, id object))success
failure:(void (^)(AFHTTPRequestOperation *request, NSError *error))failure;
- (void)cancelAll;
@end
//
// AFRailsHTTPClient.m
// TopHatter
//
// Created by Leslie Fletcher on 1/30/12.
// Copyright (c) 2012 Blippy. All rights reserved.
//
#import "AFRailsHTTPClient.h"
#import "AFJSONRequestOperation.h"
@implementation AFRailsHTTPClient
@synthesize authenticityToken = _authenticityToken;
@synthesize sessionResetBlock = _sessionResetBlock;
+ (AFRailsHTTPClient *)clientWithBaseURL:(NSURL *)url
{
return [[self alloc] initWithBaseURL:url];
}
- (id)initWithBaseURL:(NSURL *)url
{
self = [super initWithBaseURL:url];
if ( self ) {
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
}
return self;
}
- (NSString *)authenticityToken:(NSDictionary *)parameters
{
NSString *token = self.authenticityToken;
if ( [parameters objectForKey:@"authenticity_token"] )
token = (NSString *)[parameters objectForKey:@"authenticity_token"];
return token;
}
- (void)getPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *request, id object))success
failure:(void (^)(AFHTTPRequestOperation *request, NSError *error))failure
{
NSString *apiPath = [NSString stringWithFormat:@"%@%@", self.baseURL, path];
NSURLRequest *request = [self requestWithMethod:@"GET" path:apiPath parameters:parameters];
[self enqueueHTTPOperationWithRequest:request success:success failure:failure];
}
- (void)postPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *request, id object))success
failure:(void (^)(AFHTTPRequestOperation *request, NSError *error))failure
{
NSString *apiPath = [NSString stringWithFormat:@"%@%@", self.baseURL, path];
NSMutableURLRequest *request = [self requestWithMethod:@"POST" path:apiPath parameters:parameters];
[request setValue:[self authenticityToken:parameters] forHTTPHeaderField:@"X-CSRF-Token"];
[self enqueueHTTPOperationWithRequest:request success:success failure:failure];
}
- (void)putPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *request, id object))success
failure:(void (^)(AFHTTPRequestOperation *request, NSError *error))failure
{
NSString *apiPath = [NSString stringWithFormat:@"%@%@", self.baseURL, path];
NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:apiPath parameters:parameters];
[request setValue:[self authenticityToken:parameters] forHTTPHeaderField:@"X-CSRF-Token"];
[self enqueueHTTPOperationWithRequest:request success:success failure:failure];
}
- (void)deletePath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *request, id object))success
failure:(void (^)(AFHTTPRequestOperation *request, NSError *error))failure
{
NSString *apiPath = [NSString stringWithFormat:@"%@%@", self.baseURL, path];
NSMutableURLRequest *request = [self requestWithMethod:@"DELETE" path:apiPath parameters:parameters];
[request setValue:[self authenticityToken:parameters] forHTTPHeaderField:@"X-CSRF-Token"];
[self enqueueHTTPOperationWithRequest:request success:success failure:failure];
}
- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData>formData))block
{
NSMutableURLRequest *request = [super multipartFormRequestWithMethod:method path:path parameters:parameters constructingBodyWithBlock:block];
if ( request )
[request setValue:[self authenticityToken:parameters] forHTTPHeaderField:@"X-CSRF-Token"];
return request;
}
- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation {
[self.operationQueue addOperation:operation];
}
- (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(AFHTTPRequestOperation *request, id object))success
failure:(void (^)(AFHTTPRequestOperation *request, NSError *error))failure
{
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:urlRequest
success:^(__unused AFHTTPRequestOperation *request, id JSON) {
NSHTTPURLResponse *response = [request response];
if ( [[response allHeaderFields] objectForKey:@"X-CSRF-Token"] )
self.authenticityToken = [[response allHeaderFields] objectForKey:@"X-CSRF-Token"];
if (success) {
success(request, JSON);
}
} failure:^(AFHTTPRequestOperation *request, NSError *error) {
NSHTTPURLResponse *response = [request response];
if ( self.sessionResetBlock && [response statusCode] == 401 )
self.sessionResetBlock(response, error);
if (failure) {
failure(request, error);
}
}];
[self.operationQueue addOperation:operation];
}
- (void)cancelAll
{
for(NSOperation *operation in [self.operationQueue operations]) {
[operation cancel];
}
}
@end
module ApplicationHelper
def browser
@browser ||= {}
if @browser.empty?
ua = (request.env['HTTP_USER_AGENT'] || '').downcase.strip
case ua
when /webkit/i ; @browser[:engine] = 'webkit'
when /khtml/i ; @browser[:engine] = 'khtml'
when /konqueror/i ; @browser[:engine] = 'konqueror'
when /chrome/i ; @browser[:engine] = 'chrome'
when /presto/i ; @browser[:engine] = 'presto'
when /gecko/i ; @browser[:engine] = 'gecko'
when /msie/i ; @browser[:engine] = 'msie'
when /iphone/i ; @browser[:engine] = 'iphone'
else ; @browser[:engine] = 'unknown'
end
case ua
when /windows nt 6\.0/i ; @browser[:os] = 'windows_vista'
when /windows nt 6\.\d+/i ; @browser[:os] = 'windows_7'
when /windows nt 5\.2/i ; @browser[:os] = 'windows_2003'
when /windows nt 5\.1/i ; @browser[:os] = 'windows_xp'
when /windows nt 5\.0/i ; @browser[:os] = 'windows 2000'
when /os x (\d+)[._](\d+)/i ; @browser[:os] = 'os_x'
when /linux/i ; @browser[:os] = 'linux'
when /wii/i ; @browser[:os] = 'wii'
when /playstation 3/i ; @browser[:os] = 'playstation'
when /playstation portable/i ; @browser[:os] = 'playstation'
when /iphone os/i ; @browser[:os] = 'ios'
else ; @browser[:os] = 'unknown'
end
end
@browser
end
end
class Api::V1::BaseController < ApplicationController
before_filter :iphone_headers
def iphone_headers
response.headers['X-CSRF-Token'] = form_authenticity_token if browser[:engine] == 'iphone'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.