-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBSEligibleKit.m
107 lines (84 loc) · 2.95 KB
/
BSEligibleKit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// BSEligibleKit.m
// WaitingRoom3
//
// Created by Brian Soule on 1/26/13.
// Copyright (c) 2013 Soule Mobile. All rights reserved.
//
#import "BSEligibleKit.h"
#import "AFHTTPRequestOperation.h"
#import "AFJSONRequestOperation.h"
#import "AFHTTPClient.h"
@implementation BSEligibleKit
+ (BSEligibleKit *)sharedInstance
{
static BSEligibleKit *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[BSEligibleKit alloc] init];
// Do any other initialisation stuff here
});
return sharedInstance;
}
- (id) init
{
if (self = [super init])
{
self.previousQueries = [[NSMutableArray alloc] init];
[self getInformationSources];
NSLog(@"EligibleKit initialized");
}
return self;
}
+ (id) getEligible:(NSString *)apiKey
{
[BSEligibleKit sharedInstance].apiKey = apiKey;
[BSEligibleKit sharedInstance].previousQueries = [[NSMutableArray alloc] init];
NSLog(@"EligibleKit initialized with api key:%@", apiKey);
return self;
}
+ (void) getDataFor:(NSString *)resource
withParams:(NSMutableDictionary *)params
success:(void (^)(NSDictionary *data))success
failure:(void (^)(NSError *error))failure
{
resource = [NSString stringWithFormat:@"/%@.json", resource];
[params setObject:[BSEligibleKit sharedInstance].apiKey forKey:@"api_key"];
NSURL *nurl = [NSURL URLWithString:[@"https://v1.eligibleapi.net" stringByAppendingString: resource]];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:nurl];
[httpClient getPath:resource parameters:params success:^(AFHTTPRequestOperation *operation, id responseData) {
NSError* error;
NSDictionary* data = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSLog(@"Success Result String: %@", data);
if ([data objectForKey:@"error"]) {
//TODO: SPECIFY AN ERROR DESPITE JSON
if (success) {
success(data);
}
NSLog(@"Error: %@,\n Please: %@",[[data objectForKey:@"error"] objectForKey:@"reject_reason_description"],
[[data objectForKey:@"error"] objectForKey:@"follow-up_action_description"]);
}
else {
if (success) {
[[BSEligibleKit sharedInstance].previousQueries addObject:data];
success(data);
}
}
//NSLog(@"Primary Insurance: %@", [[JSON valueForKeyPath:@"primary_insurance"] valueForKeyPath:@"name"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
if (failure) {
failure(error);
}
}];
}
- (void) getInformationSources {
NSString *fname = [[NSBundle mainBundle] pathForResource:@"information-sources" ofType:@"json"];
NSData *d = [NSData dataWithContentsOfFile:fname];
NSArray *jsonObjects = [NSJSONSerialization JSONObjectWithData:d options:NSJSONReadingMutableContainers error:nil];
self.informationSourcesJSON = jsonObjects;
}
@end