Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function for returning a dictionary #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Pod/Classes/OMHSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,13 @@
*/
- (NSString*)jsonForSample:(HKSample*)sample error:(NSError**)error;

/**
Serializes HealthKit samples into Open mHealth compliant json data points.
@param sample the HealthKit sample to be serialized
@param error an NSError that is passed by reference and can be checked to identify specific errors
@return an NSDictionary object containing the sample's data in a format that adheres to the appropriate Open mHealth schema.
*/
- (NSDictionary<NSString*,NSObject*>*)dictForSample:(HKSample*)sample error:(NSError**)error;

@end

47 changes: 47 additions & 0 deletions Pod/Classes/OMHSerializer.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,53 @@ - (NSString*)jsonForSample:(HKSample*)sample error:(NSError**)error {
return jsonString;
}

/**
Serializes HealthKit samples into Open mHealth compliant json data points.
@param sample the HealthKit sample to be serialized
@param error an NSError that is passed by reference and can be checked to identify specific errors
@return an NSDictionary object containing the sample's data in a format that adheres to the appropriate Open mHealth schema
*/
- (NSDictionary*)dictForSample:(HKSample*)sample error:(NSError**)error {
NSParameterAssert(sample);
// first, verify we support the sample type
NSArray* supportedTypeIdentifiers = [[self class] supportedTypeIdentifiers];
NSString* sampleTypeIdentifier = sample.sampleType.identifier;
NSString* serializerClassName;
if ([supportedTypeIdentifiers includes:sampleTypeIdentifier]){
serializerClassName = [OMHHealthKitConstantsMapper allSupportedTypeIdentifiersToClasses][sampleTypeIdentifier];
}
else{
if (error) {
NSString* errorMessage =
[NSString stringWithFormat: @"Unsupported HKSample type: %@",
sampleTypeIdentifier];
NSDictionary* userInfo = @{ NSLocalizedDescriptionKey : errorMessage };
*error = [NSError errorWithDomain: OMHErrorDomain
code: OMHErrorCodeUnsupportedType
userInfo: userInfo];
}
return nil;
}
// if we support it, select appropriate subclass for sample

//For sleep analysis, the OMH schema does not capture an 'inBed' state, so if that value is set we need to use a generic category serializer
//otherwise, it defaults to using the OMH schema for the 'asleep' state.
if ([sampleTypeIdentifier isEqualToString:HKCategoryTypeIdentifierSleepAnalysis]){
HKCategorySample* categorySample = (HKCategorySample*)sample;
if(categorySample.value == 0){
serializerClassName = @"OMHSerializerGenericCategorySample";
}
}
Class serializerClass = NSClassFromString(serializerClassName);
// subclass verifies it supports sample's values
if (![serializerClass canSerialize:sample error:error]) {
return nil;
}
// instantiate a serializer
OMHSerializer* serializer = [[serializerClass alloc] initWithSample:sample];
return [serializer data];
}

+ (NSString*)parseUnitFromQuantity:(HKQuantity*)quantity {
NSArray *arrayWithSplitUnitAndValue = [quantity.description
componentsSeparatedByCharactersInSet:[NSCharacterSet
Expand Down