diff --git a/BreweryDB/BDBBeer.h b/BreweryDB/BDBBeer.h index 5054991..ce52108 100644 --- a/BreweryDB/BDBBeer.h +++ b/BreweryDB/BDBBeer.h @@ -22,6 +22,7 @@ #import +@class BDBStyle; #pragma mark - @interface BDBBeer : NSObject @@ -37,7 +38,7 @@ @property (nonatomic) NSNumber *glasswareId; @property (nonatomic) NSDictionary *glass; @property (nonatomic) NSNumber *styleId; -@property (nonatomic) NSDictionary *style; +@property (nonatomic) BDBStyle *style; @property (nonatomic, assign, getter = isOrganic) BOOL organic; @property (nonatomic) NSDictionary *labels; @property (nonatomic) NSNumber *servingTemperature; diff --git a/BreweryDB/BDBBrewery.h b/BreweryDB/BDBBrewery.h index 105a5a2..d6c4727 100644 --- a/BreweryDB/BDBBrewery.h +++ b/BreweryDB/BDBBrewery.h @@ -34,6 +34,7 @@ @property (nonatomic, copy) NSString *mailingListURL; @property (nonatomic, assign, getter = isOrganic) BOOL organic; @property (nonatomic) NSDictionary *images; +@property (nonatomic) NSArray *locations; @property (nonatomic, copy, readonly) NSString *status; diff --git a/BreweryDB/BDBBrewery.m b/BreweryDB/BDBBrewery.m index e47c42e..61e719e 100644 --- a/BreweryDB/BDBBrewery.m +++ b/BreweryDB/BDBBrewery.m @@ -21,6 +21,7 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #import "BDBBrewery.h" +#import "BDBLocation.h" #pragma mark - @@ -46,6 +47,17 @@ - (id)initWithDictionary:(NSDictionary *)dictionary _organic = [dictionary[NSStringFromSelector(@selector(isOrganic))] boolValue]; _images = dictionary[NSStringFromSelector(@selector(images))]; + NSMutableArray *mutableLocations = [NSMutableArray array]; + for (NSDictionary *locationDictionary in dictionary[NSStringFromSelector(@selector(locations))]) + { + BDBLocation *location = [[BDBLocation alloc] initWithDictionary:locationDictionary]; + if (location) + [mutableLocations addObject:location]; + else + NSLog(@"Could not parse location: %@", location); + } + _locations = mutableLocations; + _status = dictionary[NSStringFromSelector(@selector(status))]; } @catch (NSException *exception) diff --git a/BreweryDB/BDBCategory.h b/BreweryDB/BDBCategory.h new file mode 100644 index 0000000..dbf97e8 --- /dev/null +++ b/BreweryDB/BDBCategory.h @@ -0,0 +1,37 @@ +// +// BDBCategory.h +// +// Copyright (c) 2013 Bradley David Bergeron +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#import + + +#pragma mark - +@interface BDBCategory : NSObject + +@property (nonatomic, copy, readonly) NSString *categoryId; +@property (nonatomic) NSNumber *createDate; +@property (nonatomic, copy) NSString *name; + +@property (nonatomic, copy, readonly) NSString *status; + +- (id)initWithDictionary:(NSDictionary *)dictionary; + +@end diff --git a/BreweryDB/BDBCategory.m b/BreweryDB/BDBCategory.m new file mode 100644 index 0000000..64910a2 --- /dev/null +++ b/BreweryDB/BDBCategory.m @@ -0,0 +1,55 @@ +// +// BDBCategory.m +// +// Copyright (c) 2013 Bradley David Bergeron +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#import "BDBCategory.h" + + +#pragma mark - +@implementation BDBCategory + +- (id)initWithDictionary:(NSDictionary *)dictionary +{ + self = [super init]; + if (!self) + return nil; + + if (!dictionary) + return self; + + @try + { + _categoryId = dictionary[@"id"]; + _createDate = dictionary[NSStringFromSelector(@selector(createDate))]; + _name = dictionary[NSStringFromSelector(@selector(name))]; + + _status = dictionary[NSStringFromSelector(@selector(status))]; + } + @catch (NSException *exception) + { + NSLog(@"Could not parse category: %@", exception); + return nil; + } + + return self; +} + +@end diff --git a/BreweryDB/BDBFermentable.h b/BreweryDB/BDBFermentable.h new file mode 100644 index 0000000..0af1910 --- /dev/null +++ b/BreweryDB/BDBFermentable.h @@ -0,0 +1,53 @@ +// +// BDBFermentable.h +// +// Copyright (c) 2013 Bradley David Bergeron +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#import + +#pragma mark - +@interface BDBFermentable : NSObject + +@property (nonatomic, copy, readonly) NSString *fermentableId; +@property (nonatomic, copy) NSString *name; +@property (nonatomic, copy) NSString *descriptionString; +@property (nonatomic, copy) NSString *countryOfOrigin; +@property (nonatomic) NSNumber *srmId; +@property (nonatomic) NSNumber *srmPrecise; +@property (nonatomic) NSDictionary *srm; +@property (nonatomic) NSNumber *moistureContent; +@property (nonatomic) NSNumber *coarseFineDifference; +@property (nonatomic) NSNumber *diastaticPower; +@property (nonatomic) NSNumber *dryYield; +@property (nonatomic) NSNumber *potential; +@property (nonatomic) NSNumber *protein; +@property (nonatomic) NSNumber *solubleNitrogenRatio; +@property (nonatomic) NSNumber *maxInBatch; +@property (nonatomic, assign, getter = requiresMashing) BOOL mashing; +@property (nonatomic, copy) NSString *category; +@property (nonatomic, copy) NSString *categoryDisplay; +@property (nonatomic) NSDictionary *country; +@property (nonatomic) NSDictionary *characteristics; + +@property (nonatomic, copy, readonly) NSString *status; + +- (id)initWithDictionary:(NSDictionary *)dictionary; + +@end diff --git a/BreweryDB/BDBFermentable.m b/BreweryDB/BDBFermentable.m new file mode 100644 index 0000000..65bd776 --- /dev/null +++ b/BreweryDB/BDBFermentable.m @@ -0,0 +1,72 @@ +// +// BDBFermentable.m +// +// Copyright (c) 2013 Bradley David Bergeron +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#import "BDBFermentable.h" + +#pragma mark - +@implementation BDBFermentable + +- (id)initWithDictionary:(NSDictionary *)dictionary +{ + self = [super init]; + if (!self) + return nil; + + if (!dictionary) + return self; + + @try + { + _fermentableId = dictionary[@"id"]; + _name = dictionary[NSStringFromSelector(@selector(name))]; + _descriptionString = [dictionary[@"description"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + + _countryOfOrigin = dictionary[NSStringFromSelector(@selector(countryOfOrigin))]; + _srmId = dictionary[NSStringFromSelector(@selector(srmId))]; + _srmPrecise = dictionary[NSStringFromSelector(@selector(srmPrecise))]; + _srm = dictionary[NSStringFromSelector(@selector(srm))]; + _moistureContent = dictionary[NSStringFromSelector(@selector(moistureContent))]; + _coarseFineDifference = dictionary[NSStringFromSelector(@selector(coarseFineDifference))]; + _diastaticPower = dictionary[NSStringFromSelector(@selector(diastaticPower))]; + _dryYield = dictionary[NSStringFromSelector(@selector(dryYield))]; + _potential = dictionary[NSStringFromSelector(@selector(potential))]; + _protein = dictionary[NSStringFromSelector(@selector(protein))]; + _solubleNitrogenRatio = dictionary[NSStringFromSelector(@selector(solubleNitrogenRatio))]; + _maxInBatch = dictionary[NSStringFromSelector(@selector(maxInBatch))]; + _mashing = [dictionary[NSStringFromSelector(@selector(requiresMashing))] boolValue]; + _category = dictionary[NSStringFromSelector(@selector(category))]; + _categoryDisplay = dictionary[NSStringFromSelector(@selector(categoryDisplay))]; + _country = dictionary[NSStringFromSelector(@selector(country))]; + _characteristics = dictionary[NSStringFromSelector(@selector(characteristics))]; + + _status = dictionary[NSStringFromSelector(@selector(status))]; + } + @catch (NSException *exception) + { + NSLog(@"Could not parse beer: %@", exception); + return nil; + } + + return self; +} + +@end diff --git a/BreweryDB/BDBHop.h b/BreweryDB/BDBHop.h new file mode 100644 index 0000000..83fda69 --- /dev/null +++ b/BreweryDB/BDBHop.h @@ -0,0 +1,59 @@ +// +// BDBHop.h +// +// Copyright (c) 2013 Bradley David Bergeron +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#import + +#pragma mark - +@interface BDBHop : NSObject + +@property (nonatomic, copy, readonly) NSString *hopId; +@property (nonatomic, copy) NSString *name; +@property (nonatomic, copy) NSString *descriptionString; +@property (nonatomic, copy) NSString *countryOfOrigin; + +@property (nonatomic) NSNumber *alphaAcidMin; +@property (nonatomic) NSNumber *alphaAcidMax; +@property (nonatomic) NSNumber *betaAcidMin; +@property (nonatomic) NSNumber *betaAcidMax; +@property (nonatomic) NSNumber *humuleneMin; +@property (nonatomic) NSNumber *humuleneMax; +@property (nonatomic) NSNumber *caryophylleneMin; +@property (nonatomic) NSNumber *caryophylleneMax; +@property (nonatomic) NSNumber *cohumuloneMin; +@property (nonatomic) NSNumber *cohumuloneMax; +@property (nonatomic) NSNumber *myrceneMin; +@property (nonatomic) NSNumber *myrceneMax; +@property (nonatomic) NSNumber *farneseneMin; +@property (nonatomic) NSNumber *farneseneMax; +@property (nonatomic, assign, getter = isNobel) BOOL nobel; +@property (nonatomic, assign, getter = isForBittering) BOOL forBittering; +@property (nonatomic, assign, getter = isForFlavor) BOOL forFlavor; +@property (nonatomic, assign, getter = isForAroma) BOOL forAroma; +@property (nonatomic) NSString *category; +@property (nonatomic) NSString *categoryDisplay; +@property (nonatomic) NSDictionary *country; + +@property (nonatomic, copy, readonly) NSString *status; + +- (id)initWithDictionary:(NSDictionary *)dictionary; + +@end diff --git a/BreweryDB/BDBHop.m b/BreweryDB/BDBHop.m new file mode 100644 index 0000000..b66f877 --- /dev/null +++ b/BreweryDB/BDBHop.m @@ -0,0 +1,80 @@ +// +// BDBHop.m +// +// Copyright (c) 2013 Bradley David Bergeron +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#import "BDBHop.h" + +#pragma mark - +@implementation BDBHop + +- (id)initWithDictionary:(NSDictionary *)dictionary +{ + self = [super init]; + if (!self) + return nil; + + if (!dictionary) + return self; + + @try + { + _hopId = dictionary[@"id"]; + _name = dictionary[NSStringFromSelector(@selector(name))]; + _descriptionString = [dictionary[@"description"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + + _countryOfOrigin = dictionary[NSStringFromSelector(@selector(countryOfOrigin))]; + + _alphaAcidMin = dictionary[NSStringFromSelector(@selector(alphaAcidMin))]; + _alphaAcidMax = dictionary[NSStringFromSelector(@selector(alphaAcidMax))]; + _betaAcidMin = dictionary[NSStringFromSelector(@selector(betaAcidMin))]; + _betaAcidMax = dictionary[NSStringFromSelector(@selector(betaAcidMax))]; + _humuleneMin = dictionary[NSStringFromSelector(@selector(humuleneMin))]; + _humuleneMax = dictionary[NSStringFromSelector(@selector(humuleneMax))]; + _caryophylleneMin = dictionary[NSStringFromSelector(@selector(caryophylleneMin))]; + _caryophylleneMax = dictionary[NSStringFromSelector(@selector(caryophylleneMax))]; + _cohumuloneMin = dictionary[NSStringFromSelector(@selector(cohumuloneMin))]; + _cohumuloneMax = dictionary[NSStringFromSelector(@selector(cohumuloneMax))]; + _myrceneMin = dictionary[NSStringFromSelector(@selector(myrceneMin))]; + _myrceneMax = dictionary[NSStringFromSelector(@selector(myrceneMax))]; + _farneseneMin = dictionary[NSStringFromSelector(@selector(farneseneMin))]; + _farneseneMax = dictionary[NSStringFromSelector(@selector(farneseneMax))]; + + _nobel = [dictionary[NSStringFromSelector(@selector(isNobel))] boolValue]; + _forBittering = [dictionary[NSStringFromSelector(@selector(isForBittering))] boolValue]; + _forFlavor = [dictionary[NSStringFromSelector(@selector(isForFlavor))] boolValue]; + _forAroma = [dictionary[NSStringFromSelector(@selector(isForAroma))] boolValue]; + + _category = dictionary[NSStringFromSelector(@selector(category))]; + _categoryDisplay = dictionary[NSStringFromSelector(@selector(categoryDisplay))]; + _country = dictionary[NSStringFromSelector(@selector(country))]; + + _status = dictionary[NSStringFromSelector(@selector(status))]; + } + @catch (NSException *exception) + { + NSLog(@"Could not parse beer: %@", exception); + return nil; + } + + return self; +} + +@end diff --git a/BreweryDB/BDBLocation.h b/BreweryDB/BDBLocation.h new file mode 100644 index 0000000..90babcd --- /dev/null +++ b/BreweryDB/BDBLocation.h @@ -0,0 +1,69 @@ +// +// BDBLocation.h +// +// Copyright (c) 2013 Bradley David Bergeron +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#import + +@class BDBBrewery; + +#pragma mark - +@interface BDBLocation : NSObject + +@property (nonatomic, copy, readonly) NSString *locationId; +@property (nonatomic, copy) NSString *name; + +@property (nonatomic, copy) NSString *streetAddress; +@property (nonatomic, copy) NSString *extendedAddress; +@property (nonatomic, copy) NSString *locality; +@property (nonatomic, copy) NSString *region; +@property (nonatomic, copy) NSString *postalCode; +@property (nonatomic, copy) NSString *phone; +@property (nonatomic, copy) NSString *website; +@property (nonatomic, copy) NSString *hoursOfOperation; +@property (nonatomic, copy) NSString *hoursOfOperationExplicit; +@property (nonatomic, copy) NSString *hoursOfOperationNotes; +@property (nonatomic, copy) NSString *tourInfo; +@property (nonatomic, copy) NSString *timezone; + +@property (nonatomic) NSNumber *latitude; +@property (nonatomic) NSNumber *longitude; + +@property (nonatomic) BDBBrewery *brewery; + +@property (nonatomic, assign, getter = isPrimary) BOOL primary; +@property (nonatomic, assign, getter = inPlanning) BOOL planning; +@property (nonatomic, assign, getter = isClosed) BOOL closed; +@property (nonatomic, assign, getter = isOpenToPublic) BOOL openToPublic; + +@property (nonatomic, copy) NSString *locationType; +@property (nonatomic, copy) NSString *locationTypeDisplay; +@property (nonatomic, copy) NSString *countryIsoCode; + +@property (nonatomic, copy) NSDictionary *country; + +@property (nonatomic) NSNumber *yearOpened; +@property (nonatomic) NSNumber *yearClosed; + +@property (nonatomic, copy, readonly) NSString *status; + +- (id)initWithDictionary:(NSDictionary *)dictionary; + +@end diff --git a/BreweryDB/BDBLocation.m b/BreweryDB/BDBLocation.m new file mode 100644 index 0000000..cd644ef --- /dev/null +++ b/BreweryDB/BDBLocation.m @@ -0,0 +1,86 @@ +// +// BDBLocation.m +// +// Copyright (c) 2013 Bradley David Bergeron +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#import "BDBLocation.h" +#import "BDBBrewery.h" + +#pragma mark - +@implementation BDBLocation + +- (id)initWithDictionary:(NSDictionary *)dictionary +{ + self = [super init]; + if (!self) + return nil; + + if (!dictionary) + return self; + + @try + { + _locationId = dictionary[@"id"]; + _name = dictionary[NSStringFromSelector(@selector(name))]; + + _streetAddress = dictionary[NSStringFromSelector(@selector(streetAddress))]; + _extendedAddress = dictionary[NSStringFromSelector(@selector(extendedAddress))]; + _locality = dictionary[NSStringFromSelector(@selector(locality))]; + _region = dictionary[NSStringFromSelector(@selector(region))]; + _postalCode = dictionary[NSStringFromSelector(@selector(postalCode))]; + _phone = dictionary[NSStringFromSelector(@selector(phone))]; + _website = dictionary[NSStringFromSelector(@selector(website))]; + _hoursOfOperation = dictionary[NSStringFromSelector(@selector(hoursOfOperation))]; + _hoursOfOperationExplicit = dictionary[NSStringFromSelector(@selector(hoursOfOperationExplicit))]; + _hoursOfOperationNotes = dictionary[NSStringFromSelector(@selector(hoursOfOperationNotes))]; + _tourInfo = dictionary[NSStringFromSelector(@selector(tourInfo))]; + _timezone = dictionary[NSStringFromSelector(@selector(timezone))]; + + _latitude = dictionary[NSStringFromSelector(@selector(latitude))]; + _longitude = dictionary[NSStringFromSelector(@selector(longitude))]; + + NSDictionary* breweryDictionary = dictionary[NSStringFromSelector(@selector(brewery))]; + _brewery = [[BDBBrewery alloc] initWithDictionary:breweryDictionary]; + + _primary = [dictionary[NSStringFromSelector(@selector(isPrimary))] boolValue]; + _planning = [dictionary[NSStringFromSelector(@selector(inPlanning))] boolValue]; + _closed = [dictionary[NSStringFromSelector(@selector(isClosed))] boolValue]; + _openToPublic = [dictionary[NSStringFromSelector(@selector(openToPublic))] boolValue]; + + _locationType = dictionary[NSStringFromSelector(@selector(locationType))]; + _locationTypeDisplay = dictionary[NSStringFromSelector(@selector(locationTypeDisplay))]; + _countryIsoCode = dictionary[NSStringFromSelector(@selector(countryIsoCode))]; + + _country = dictionary[NSStringFromSelector(@selector(country))]; + _yearOpened = dictionary[NSStringFromSelector(@selector(yearOpened))]; + _yearClosed = dictionary[NSStringFromSelector(@selector(yearClosed))]; + + _status = dictionary[NSStringFromSelector(@selector(status))]; + } + @catch (NSException *exception) + { + NSLog(@"Could not parse beer: %@", exception); + return nil; + } + + return self; +} + +@end diff --git a/BreweryDB/BDBStyle.h b/BreweryDB/BDBStyle.h index b145ef8..4dd3627 100644 --- a/BreweryDB/BDBStyle.h +++ b/BreweryDB/BDBStyle.h @@ -22,12 +22,13 @@ #import +@class BDBCategory; #pragma mark - @interface BDBStyle : NSObject @property (nonatomic, copy, readonly) NSString *styleId; -@property (nonatomic) NSDictionary *category; +@property (nonatomic) BDBCategory *category; @property (nonatomic) NSNumber *srmMax; @property (nonatomic) NSNumber *ibuMax; @property (nonatomic) NSNumber *srmMin; @@ -38,6 +39,7 @@ @property (nonatomic) NSNumber *fgMax; @property (nonatomic) NSNumber *abvMax; @property (nonatomic) NSNumber *ogMin; +@property (nonatomic) NSNumber *ogMax; @property (nonatomic) NSNumber *abvMin; @property (nonatomic, copy) NSString *name; @property (nonatomic) NSNumber *categoryId; diff --git a/BreweryDB/BDBStyle.m b/BreweryDB/BDBStyle.m index b9e382f..83972cb 100644 --- a/BreweryDB/BDBStyle.m +++ b/BreweryDB/BDBStyle.m @@ -21,7 +21,7 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #import "BDBStyle.h" - +#import "BDBCategory.h" #pragma mark - @implementation BDBStyle @@ -37,8 +37,10 @@ - (id)initWithDictionary:(NSDictionary *)dictionary @try { - _styleId = dictionary[NSStringFromSelector(@selector(styleId))]; - _category = dictionary[NSStringFromSelector(@selector(category))]; + _styleId = dictionary[@"id"]; + + NSDictionary* categoryDictionary = dictionary[NSStringFromSelector(@selector(category))]; + _category = [[BDBCategory alloc] initWithDictionary:categoryDictionary]; _srmMax = dictionary[NSStringFromSelector(@selector(srmMax))]; _ibuMax = dictionary[NSStringFromSelector(@selector(ibuMax))]; @@ -50,6 +52,7 @@ - (id)initWithDictionary:(NSDictionary *)dictionary _fgMax = dictionary[NSStringFromSelector(@selector(fgMax))]; _abvMax = dictionary[NSStringFromSelector(@selector(abvMax))]; _ogMin = dictionary[NSStringFromSelector(@selector(ogMin))]; + _ogMax = dictionary[NSStringFromSelector(@selector(ogMax))]; _abvMin = dictionary[NSStringFromSelector(@selector(abvMin))]; _name = dictionary[NSStringFromSelector(@selector(name))]; _categoryId = dictionary[NSStringFromSelector(@selector(categoryId))]; @@ -58,7 +61,7 @@ - (id)initWithDictionary:(NSDictionary *)dictionary } @catch (NSException *exception) { - NSLog(@"Could not parse beer: %@", exception); + NSLog(@"Could not parse style: %@", exception); return nil; } diff --git a/BreweryDB/BDBYeast.h b/BreweryDB/BDBYeast.h new file mode 100644 index 0000000..e0f6ded --- /dev/null +++ b/BreweryDB/BDBYeast.h @@ -0,0 +1,52 @@ +// +// BDBYeast.h +// +// Copyright (c) 2013 Bradley David Bergeron +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#import + +#pragma mark - +@interface BDBYeast : NSObject + +@property (nonatomic, copy, readonly) NSString *yeastId; +@property (nonatomic, copy) NSString *name; +@property (nonatomic, copy) NSString *descriptionString; + +@property (nonatomic, copy) NSString *yeastType; + +@property (nonatomic) NSNumber *attenuationMin; +@property (nonatomic) NSNumber *attenuationMax; +@property (nonatomic) NSNumber *fermentTempMin; +@property (nonatomic) NSNumber *fermentTempMax; +@property (nonatomic) NSNumber *alcoholToleranceMin; +@property (nonatomic) NSNumber *alcoholToleranceMax; + +@property (nonatomic, copy) NSString *productId; +@property (nonatomic, copy) NSString *supplier; +@property (nonatomic, copy) NSString *yeastFormat; + +@property (nonatomic, copy) NSString *category; +@property (nonatomic, copy) NSString *categoryDisplay; + +@property (nonatomic, copy, readonly) NSString *status; + +- (id)initWithDictionary:(NSDictionary *)dictionary; + +@end diff --git a/BreweryDB/BDBYeast.m b/BreweryDB/BDBYeast.m new file mode 100644 index 0000000..9951f5f --- /dev/null +++ b/BreweryDB/BDBYeast.m @@ -0,0 +1,70 @@ +// +// BDBYeast.m +// +// Copyright (c) 2013 Bradley David Bergeron +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#import "BDBYeast.h" + +#pragma mark - +@implementation BDBYeast + +- (id)initWithDictionary:(NSDictionary *)dictionary +{ + self = [super init]; + if (!self) + return nil; + + if (!dictionary) + return self; + + @try + { + _yeastId = dictionary[@"id"]; + _name = dictionary[NSStringFromSelector(@selector(name))]; + _descriptionString = [dictionary[@"description"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + + _yeastType = dictionary[NSStringFromSelector(@selector(yeastType))]; + + _attenuationMin = dictionary[NSStringFromSelector(@selector(attenuationMin))]; + _attenuationMax = dictionary[NSStringFromSelector(@selector(attenuationMax))]; + _fermentTempMin = dictionary[NSStringFromSelector(@selector(fermentTempMin))]; + _fermentTempMax = dictionary[NSStringFromSelector(@selector(fermentTempMax))]; + _alcoholToleranceMin = dictionary[NSStringFromSelector(@selector(alcoholToleranceMin))]; + _alcoholToleranceMax = dictionary[NSStringFromSelector(@selector(alcoholToleranceMax))]; + + _productId = dictionary[NSStringFromSelector(@selector(productId))]; + _supplier = dictionary[NSStringFromSelector(@selector(supplier))]; + _yeastFormat = dictionary[NSStringFromSelector(@selector(yeastFormat))]; + + _category = dictionary[NSStringFromSelector(@selector(category))]; + _categoryDisplay = dictionary[NSStringFromSelector(@selector(categoryDisplay))]; + + _status = dictionary[NSStringFromSelector(@selector(status))]; + } + @catch (NSException *exception) + { + NSLog(@"Could not parse beer: %@", exception); + return nil; + } + + return self; +} + +@end diff --git a/BreweryDB/BreweryDB.h b/BreweryDB/BreweryDB.h index 1272150..425896d 100644 --- a/BreweryDB/BreweryDB.h +++ b/BreweryDB/BreweryDB.h @@ -26,6 +26,11 @@ #import "BDBBrewery.h" #import "BDBGuild.h" #import "BDBStyle.h" +#import "BDBCategory.h" +#import "BDBLocation.h" +#import "BDBFermentable.h" +#import "BDBHop.h" +#import "BDBYeast.h" typedef NS_ENUM(NSInteger, BreweryDBSearchType) @@ -34,7 +39,11 @@ typedef NS_ENUM(NSInteger, BreweryDBSearchType) BreweryDBSearchTypeBrewery, BreweryDBSearchTypeBeer, BreweryDBSearchTypeGuild, - BreweryDBSearchTypeEvent + BreweryDBSearchTypeEvent, + BreweryDBSearchTypeFermentable, + BreweryDBSearchTypeHop, + BreweryDBSearchTypeYeast, + BreweryDBSearchTypeLocation, }; @@ -53,6 +62,25 @@ typedef NS_ENUM(NSInteger, BreweryDBSearchType) */ + (instancetype)brew:(NSString *)apiKey; +#pragma mark Search +/** + * Perform a search query on the BreweryDB. + * + * @param queryString What you're searching for. + * @param type The type of result you're searching for. + * @param withBreweries Whether or not to return brewery information with the results. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)search:(NSString *)queryString + type:(BreweryDBSearchType)type +withBreweryInfo:(BOOL)withBreweryInfo + parameters:(NSDictionary *)parameters + success:(void (^)(NSArray *results, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure; + #pragma mark Beers /** * Fetch an array of beers pertaining to the specified parameters. @@ -65,7 +93,7 @@ typedef NS_ENUM(NSInteger, BreweryDBSearchType) */ + (void)fetchBeersWithParameters:(NSDictionary *)parameters withBreweryInfo:(BOOL)withBreweryInfo - success:(void (^)(NSArray *beers))success + success:(void (^)(NSArray *beers, NSUInteger currentPage, NSUInteger numberOfPages))success failure:(void (^)(NSError *error))failure; /** @@ -95,7 +123,7 @@ typedef NS_ENUM(NSInteger, BreweryDBSearchType) * @since 1.0.0 */ + (void)fetchBreweriesWithParameters:(NSDictionary *)parameters - success:(void (^)(NSArray *breweries))success + success:(void (^)(NSArray *breweries, NSUInteger currentPage, NSUInteger numberOfPages))success failure:(void (^)(NSError *error))failure; /** @@ -113,28 +141,110 @@ typedef NS_ENUM(NSInteger, BreweryDBSearchType) success:(void (^)(BDBBrewery *brewery))success failure:(void (^)(NSError *error))failure; -#pragma mark Search +#pragma mark Styles /** - * Perform a search query on the BreweryDB. + * Fetch an array of styles pertaining to the specified parameters. * - * @param queryString What you're searching for. - * @param type The type of result you're searching for. - * @param withBreweries Whether or not to return brewery information with the results. + * @param parameters Filtering paramaters. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)fetchStylesWithParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *styles, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure; + +/** + * Fetch a single style object specified by the styleId. + * + * @param styleId Unique ID describing the style. + * @param parameters Filtering parameters. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)fetchStyleWithId:(NSString *)styleId + parameters:(NSDictionary *)parameters + success:(void (^)(BDBStyle *style))success + failure:(void (^)(NSError *error))failure; + +#pragma mark Categories +/** + * Fetch an array of categories pertaining to the specified parameters. + * + * @param parameters Filtering paramaters. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)fetchCategoriesWithParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *categories, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure; + +/** + * Fetch a single category object specified by the categoryId. + * + * @param categoryId Unique ID describing the category. + * @param parameters Filtering parameters. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)fetchCategoryWithId:(NSString *)categoryId + parameters:(NSDictionary *)parameters + success:(void (^)(BDBCategory *category))success + failure:(void (^)(NSError *error))failure; + +#pragma mark Fermentables +/** + * Fetch an array of fermentables pertaining to the specified parameters. + * + * @param parameters Filtering paramaters. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)fetchFermentablesWithParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *fermentables, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure; + +/** + * Fetch an array of fermentables for a specific beer pertaining to the specified parameters. + * + * @param parameters Filtering paramaters. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)fetchFermentablesForBeerId:(NSString *)beerId + withParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *fermentables, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure; + +/** + * Fetch a single fermentable object specified by the fermentableId. + * + * @param fermentableId Unique ID describing the fermentable. + * @param parameters Filtering parameters. * @param success Callback function performed on successful retrieval of results. * @param failure Callback function performed when an error occurs. * * @since 1.0.0 */ -+ (void)search:(NSString *)queryString - type:(BreweryDBSearchType)type -withBreweryInfo:(BOOL)withBreweryInfo - parameters:(NSDictionary *)parameters - success:(void (^)(NSArray *results, NSUInteger currentPage, NSUInteger numberOfPages))success - failure:(void (^)(NSError *error))failure; ++ (void)fetchFermentableWithId:(NSString *)fermentableId + parameters:(NSDictionary *)parameters + success:(void (^)(BDBFermentable *fermentable))success + failure:(void (^)(NSError *error))failure; -#pragma mark Styles +#pragma mark Hops /** - * Fetch an array of styles pertaining to the specified parameters. + * Fetch an array of hops pertaining to the specified parameters. * * @param parameters Filtering paramaters. * @param success Callback function performed on successful retrieval of results. @@ -142,23 +252,123 @@ withBreweryInfo:(BOOL)withBreweryInfo * * @since 1.0.0 */ -+ (void)fetchStylesWithParameters:(NSDictionary *)parameters - success:(void (^)(NSArray *beers))success ++ (void)fetchHopsWithParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *hops, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure; + +/** + * Fetch an array of hops for a specific beer pertaining to the specified parameters. + * + * @param parameters Filtering paramaters. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)fetchHopsForBeerId:(NSString *)beerId + withParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *hops, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure; + +/** + * Fetch a single hop object specified by the hopId. + * + * @param hopId Unique ID describing the hop. + * @param parameters Filtering parameters. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)fetchHopWithId:(NSString *)hopId + parameters:(NSDictionary *)parameters + success:(void (^)(BDBHop *hop))success + failure:(void (^)(NSError *error))failure; + +#pragma mark Yeasts +/** + * Fetch an array of yeasts pertaining to the specified parameters. + * + * @param parameters Filtering paramaters. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)fetchYeastsWithParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *yeasts, NSUInteger currentPage, NSUInteger numberOfPages))success failure:(void (^)(NSError *error))failure; /** - * Fetch a single style object specified by the styleId. + * Fetch an array of yeasts for a specific beer pertaining to the specified parameters. + * + * @param parameters Filtering paramaters. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)fetchYeastsForBeerId:(NSString *)beerId + withParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *yeasts, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure; + +/** + * Fetch a single yeast object specified by the yeastId. * - * @param styleId Unique ID describing the beer. + * @param yeastId Unique ID describing the yeast. * @param parameters Filtering parameters. * @param success Callback function performed on successful retrieval of results. * @param failure Callback function performed when an error occurs. * * @since 1.0.0 */ -+ (void)fetchStyleWithId:(NSString *)styleId ++ (void)fetchYeastWithId:(NSString *)yeastId parameters:(NSDictionary *)parameters - success:(void (^)(BDBBeer *beer))success + success:(void (^)(BDBYeast *yeast))success failure:(void (^)(NSError *error))failure; +#pragma mark Locations +/** + * Fetch an array of locations pertaining to the specified parameters. + * + * @param parameters Filtering paramaters. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)fetchLocationsWithParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *locations, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure; + +/** + * Fetch an array of locations for a specific brewery pertaining to the specified parameters. + * + * @param parameters Filtering paramaters. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)fetchLocationsForBreweryId:(NSString *)breweryId + withParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *locations, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure; + +/** + * Fetch a single location object specified by the locationId. + * + * @param yeastId Unique ID describing the location. + * @param parameters Filtering parameters. + * @param success Callback function performed on successful retrieval of results. + * @param failure Callback function performed when an error occurs. + * + * @since 1.0.0 + */ ++ (void)fetchLocationWithId:(NSString *)locationId + parameters:(NSDictionary *)parameters + success:(void (^)(BDBLocation *location))success + failure:(void (^)(NSError *error))failure; + @end diff --git a/BreweryDB/BreweryDB.m b/BreweryDB/BreweryDB.m index 0f62674..a3211b5 100644 --- a/BreweryDB/BreweryDB.m +++ b/BreweryDB/BreweryDB.m @@ -29,11 +29,11 @@ NSString * const BreweryDBErrorDomain = @"com.brewerydb.api.error"; NSString * const BreweryDBAPIURL = @"https://api.brewerydb.com/v2"; -NSString * const BreweryDBResponseStatusKey = @"status"; -NSString * const BreweryDBResponseErrorKey = @"errorMessage"; -NSString * const BreweryDBResponseDataKey = @"data"; - - +NSString * const BreweryDBResponseStatusKey = @"status"; +NSString * const BreweryDBResponseErrorKey = @"errorMessage"; +NSString * const BreweryDBResponseDataKey = @"data"; +NSString * const BreweryDBResponseNumberOfPagesKey = @"numberOfPages"; +NSString * const BreweryDBResponseCurrentPageKey = @"currentPage"; #pragma mark - @interface BreweryDB () @@ -93,10 +93,128 @@ - (NSError *)errorWithCode:(NSInteger)code description:(NSString *)description return [NSError errorWithDomain:BreweryDBErrorDomain code:code userInfo:@{NSLocalizedDescriptionKey:description}]; } +#pragma mark Search ++ (void)search:(NSString *)queryString + type:(BreweryDBSearchType)type +withBreweryInfo:(BOOL)withBreweryInfo + parameters:(NSDictionary *)parameters + success:(void (^)(NSArray *, NSUInteger, NSUInteger))success + failure:(void (^)(NSError *))failure +{ + NSParameterAssert(queryString); + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + mutableParameters[@"q"] = queryString; + + if (withBreweryInfo) + mutableParameters[@"withBreweries"] = @"Y"; + + switch (type) + { + case BreweryDBSearchTypeBrewery: + { + mutableParameters[@"type"] = @"brewery"; + break; + } + case BreweryDBSearchTypeBeer: + { + mutableParameters[@"type"] = @"beer"; + break; + } + case BreweryDBSearchTypeGuild: + { + mutableParameters[@"type"] = @"guild"; + break; + } + case BreweryDBSearchTypeEvent: + { + mutableParameters[@"type"] = @"event"; + break; + } + case BreweryDBSearchTypeAll: + default: + break; + } + + [[[[self class] sharedInstance] networkManager] GET:@"search" + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + NSMutableArray *searchResults = [NSMutableArray array]; + for (NSDictionary *resultDictionary in response[BreweryDBResponseDataKey]) + { + if ([resultDictionary[@"type"] isEqualToString:@"beer"]) + { + BDBBeer *beer = [[BDBBeer alloc] initWithDictionary:resultDictionary]; + if (beer) + [searchResults addObject:beer]; + else + { + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + break; + } + } + else if ([resultDictionary[@"type"] isEqualToString:@"brewery"]) + { + BDBBrewery *brewery = [[BDBBrewery alloc] initWithDictionary:resultDictionary]; + if (brewery) + [searchResults addObject:brewery]; + else + { + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_GUILD_OBJECT_CREATION_FAILED + description:BDB_ERROR_GUILD_OBJECT_CREATION_FAILED]); + break; + } + } + else if ([resultDictionary[@"type"] isEqualToString:@"guild"]) + { + BDBGuild *guild = [[BDBGuild alloc] initWithDictionary:resultDictionary]; + if (guild) + [searchResults addObject:guild]; + else + { + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_GUILD_OBJECT_CREATION_FAILED + description:BDB_ERROR_GUILD_OBJECT_CREATION_FAILED]); + break; + } + } + else + [searchResults addObject:resultDictionary]; + } + NSUInteger numberOfPages = [response[BreweryDBResponseNumberOfPagesKey] unsignedIntegerValue]; + NSUInteger currentPage = [response[BreweryDBResponseCurrentPageKey] unsignedIntegerValue]; + success(searchResults, currentPage, numberOfPages); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + #pragma mark Beers + (void)fetchBeersWithParameters:(NSDictionary *)parameters withBreweryInfo:(BOOL)withBreweryInfo - success:(void (^)(NSArray *))success + success:(void (^)(NSArray *, NSUInteger, NSUInteger))success failure:(void (^)(NSError *))failure { NSParameterAssert(success); @@ -134,7 +252,9 @@ + (void)fetchBeersWithParameters:(NSDictionary *)parameters break; } } - success(beers); + NSUInteger numberOfPages = [response[BreweryDBResponseNumberOfPagesKey] unsignedIntegerValue]; + NSUInteger currentPage = [response[BreweryDBResponseCurrentPageKey] unsignedIntegerValue]; + success(beers, currentPage, numberOfPages); } else failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR @@ -199,7 +319,7 @@ + (void)fetchBeerWithId:(NSString *)beerId #pragma mark Breweries + (void)fetchBreweriesWithParameters:(NSDictionary *)parameters - success:(void (^)(NSArray *))success + success:(void (^)(NSArray *, NSUInteger, NSUInteger))success failure:(void (^)(NSError *))failure { NSParameterAssert(success); @@ -234,7 +354,9 @@ + (void)fetchBreweriesWithParameters:(NSDictionary *)parameters break; } } - success(breweries); + NSUInteger numberOfPages = [response[BreweryDBResponseNumberOfPagesKey] unsignedIntegerValue]; + NSUInteger currentPage = [response[BreweryDBResponseCurrentPageKey] unsignedIntegerValue]; + success(breweries, currentPage, numberOfPages); } else failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR @@ -293,58 +415,23 @@ + (void)fetchBreweryWithId:(NSString *)breweryId }]; } -#pragma mark Search -+ (void)search:(NSString *)queryString - type:(BreweryDBSearchType)type -withBreweryInfo:(BOOL)withBreweryInfo - parameters:(NSDictionary *)parameters - success:(void (^)(NSArray *, NSUInteger, NSUInteger))success - failure:(void (^)(NSError *))failure +#pragma mark Styles ++ (void)fetchStylesWithParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *, NSUInteger, NSUInteger))success + failure:(void (^)(NSError *))failure { - NSParameterAssert(queryString); NSParameterAssert(success); NSParameterAssert(failure); - + if (![[[self class] sharedInstance] readyToBrew]) return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); - + NSMutableDictionary *mutableParameters = parameters.mutableCopy; if (!mutableParameters) mutableParameters = [NSMutableDictionary dictionary]; mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; - mutableParameters[@"q"] = queryString; - - if (withBreweryInfo) - mutableParameters[@"withBreweries"] = @"Y"; - - switch (type) - { - case BreweryDBSearchTypeBrewery: - { - mutableParameters[@"type"] = @"brewery"; - break; - } - case BreweryDBSearchTypeBeer: - { - mutableParameters[@"type"] = @"beer"; - break; - } - case BreweryDBSearchTypeGuild: - { - mutableParameters[@"type"] = @"guild"; - break; - } - case BreweryDBSearchTypeEvent: - { - mutableParameters[@"type"] = @"event"; - break; - } - case BreweryDBSearchTypeAll: - default: - break; - } - - [[[[self class] sharedInstance] networkManager] GET:@"search" + + [[[[self class] sharedInstance] networkManager] GET:@"styles" parameters:mutableParameters success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject isKindOfClass:[NSDictionary class]]) @@ -352,51 +439,22 @@ + (void)search:(NSString *)queryString NSDictionary *response = responseObject; if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) { - NSMutableArray *searchResults = [NSMutableArray array]; - for (NSDictionary *resultDictionary in response[BreweryDBResponseDataKey]) + NSMutableArray *styles = [NSMutableArray array]; + for (NSDictionary *dictionary in response[BreweryDBResponseDataKey]) { - if ([resultDictionary[@"type"] isEqualToString:@"beer"]) - { - BDBBeer *beer = [[BDBBeer alloc] initWithDictionary:resultDictionary]; - if (beer) - [searchResults addObject:beer]; - else - { - failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED - description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); - break; - } - } - else if ([resultDictionary[@"type"] isEqualToString:@"brewery"]) - { - BDBBrewery *brewery = [[BDBBrewery alloc] initWithDictionary:resultDictionary]; - if (brewery) - [searchResults addObject:brewery]; - else - { - failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_GUILD_OBJECT_CREATION_FAILED - description:BDB_ERROR_GUILD_OBJECT_CREATION_FAILED]); - break; - } - } - else if ([resultDictionary[@"type"] isEqualToString:@"guild"]) + BDBStyle *style = [[BDBStyle alloc] initWithDictionary:dictionary]; + if (style) + [styles addObject:style]; + else { - BDBGuild *guild = [[BDBGuild alloc] initWithDictionary:resultDictionary]; - if (guild) - [searchResults addObject:guild]; - else - { - failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_GUILD_OBJECT_CREATION_FAILED - description:BDB_ERROR_GUILD_OBJECT_CREATION_FAILED]); - break; - } + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + break; } - else - [searchResults addObject:resultDictionary]; } NSUInteger numberOfPages = [response[BreweryDBResponseNumberOfPagesKey] unsignedIntegerValue]; NSUInteger currentPage = [response[BreweryDBResponseCurrentPageKey] unsignedIntegerValue]; - success(searchResults, currentPage, numberOfPages); + success(styles, currentPage, numberOfPages); } else failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR @@ -411,10 +469,10 @@ + (void)search:(NSString *)queryString }]; } -#pragma mark Styles -+ (void)fetchStylesWithParameters:(NSDictionary *)parameters - success:(void (^)(NSArray *))success - failure:(void (^)(NSError *))failure ++ (void)fetchStyleWithId:(NSString *)styleId + parameters:(NSDictionary *)parameters + success:(void (^)(BDBBeer *))success + failure:(void (^)(NSError *))failure { NSParameterAssert(success); NSParameterAssert(failure); @@ -427,7 +485,7 @@ + (void)fetchStylesWithParameters:(NSDictionary *)parameters mutableParameters = [NSMutableDictionary dictionary]; mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; - [[[[self class] sharedInstance] networkManager] GET:@"styles" + [[[[self class] sharedInstance] networkManager] GET:[@"style" stringByAppendingFormat:@"/%@", styleId] parameters:mutableParameters success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject isKindOfClass:[NSDictionary class]]) @@ -435,20 +493,12 @@ + (void)fetchStylesWithParameters:(NSDictionary *)parameters NSDictionary *response = responseObject; if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) { - NSMutableArray *styles = [NSMutableArray array]; - for (NSDictionary *dictionary in response[BreweryDBResponseDataKey]) - { - BDBStyle *style = [[BDBStyle alloc] initWithDictionary:dictionary]; - if (style) - [styles addObject:style]; - else - { - failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED - description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); - break; - } - } - success(styles); + BDBStyle *style = [[BDBStyle alloc] initWithDictionary:response[BreweryDBResponseDataKey]]; + if (style) + success(style); + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); } else failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR @@ -463,10 +513,10 @@ + (void)fetchStylesWithParameters:(NSDictionary *)parameters }]; } -+ (void)fetchStyleWithId:(NSString *)styleId - parameters:(NSDictionary *)parameters - success:(void (^)(BDBBeer *))success - failure:(void (^)(NSError *))failure +#pragma mark Categories ++ (void)fetchCategoriesWithParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *, NSUInteger, NSUInteger))success + failure:(void (^)(NSError *))failure { NSParameterAssert(success); NSParameterAssert(failure); @@ -479,7 +529,7 @@ + (void)fetchStyleWithId:(NSString *)styleId mutableParameters = [NSMutableDictionary dictionary]; mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; - [[[[self class] sharedInstance] networkManager] GET:[@"style" stringByAppendingFormat:@"/%@", styleId] + [[[[self class] sharedInstance] networkManager] GET:@"categories" parameters:mutableParameters success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject isKindOfClass:[NSDictionary class]]) @@ -487,9 +537,671 @@ + (void)fetchStyleWithId:(NSString *)styleId NSDictionary *response = responseObject; if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) { - BDBStyle *style = [[BDBBeer alloc] initWithDictionary:response[BreweryDBResponseDataKey]]; - if (style) - success(style); + NSMutableArray *categories = [NSMutableArray array]; + for (NSDictionary *dictionary in response[BreweryDBResponseDataKey]) + { + BDBCategory *category = [[BDBCategory alloc] initWithDictionary:dictionary]; + if (category) + [categories addObject:category]; + else + { + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + break; + } + } + NSUInteger numberOfPages = [response[BreweryDBResponseNumberOfPagesKey] unsignedIntegerValue]; + NSUInteger currentPage = [response[BreweryDBResponseCurrentPageKey] unsignedIntegerValue]; + success(categories, currentPage, numberOfPages); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + ++ (void)fetchCategoryWithId:(NSString *)categoryId + parameters:(NSDictionary *)parameters + success:(void (^)(BDBBeer *))success + failure:(void (^)(NSError *))failure +{ + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + + [[[[self class] sharedInstance] networkManager] GET:[@"category" stringByAppendingFormat:@"/%@", categoryId] + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + BDBCategory *category = [[BDBCategory alloc] initWithDictionary:response[BreweryDBResponseDataKey]]; + if (category) + success(category); + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + +#pragma mark Fermentables ++ (void)fetchFermentablesWithParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *fermentables, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure +{ + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + + [[[[self class] sharedInstance] networkManager] GET:@"fermentables" + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + NSMutableArray *fermentables = [NSMutableArray array]; + for (NSDictionary *dictionary in response[BreweryDBResponseDataKey]) + { + BDBFermentable *fermentable = [[BDBFermentable alloc] initWithDictionary:dictionary]; + if (fermentable) + [fermentables addObject:fermentable]; + else + { + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + break; + } + } + NSUInteger numberOfPages = [response[BreweryDBResponseNumberOfPagesKey] unsignedIntegerValue]; + NSUInteger currentPage = [response[BreweryDBResponseCurrentPageKey] unsignedIntegerValue]; + success(fermentables, currentPage, numberOfPages); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + ++ (void)fetchFermentablesForBeerId:(NSString *)beerId + withParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *fermentables, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure +{ + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + + [[[[self class] sharedInstance] networkManager] GET:[@"beer" stringByAppendingFormat:@"/%@/fermentables", beerId] + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + NSMutableArray *fermentables = [NSMutableArray array]; + for (NSDictionary *dictionary in response[BreweryDBResponseDataKey]) + { + BDBFermentable *fermentable = [[BDBFermentable alloc] initWithDictionary:dictionary]; + if (fermentable) + [fermentables addObject:fermentable]; + else + { + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + break; + } + } + NSUInteger numberOfPages = [response[BreweryDBResponseNumberOfPagesKey] unsignedIntegerValue]; + NSUInteger currentPage = [response[BreweryDBResponseCurrentPageKey] unsignedIntegerValue]; + success(fermentables, currentPage, numberOfPages); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + ++ (void)fetchFermentableWithId:(NSString *)fermentableId + parameters:(NSDictionary *)parameters + success:(void (^)(BDBFermentable *fermentable))success + failure:(void (^)(NSError *error))failure +{ + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + + [[[[self class] sharedInstance] networkManager] GET:[@"fermentable" stringByAppendingFormat:@"/%@", fermentableId] + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + BDBFermentable *fermentable = [[BDBFermentable alloc] initWithDictionary:response[BreweryDBResponseDataKey]]; + if (fermentable) + success(fermentable); + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + +#pragma mark Hops ++ (void)fetchHopsWithParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *hops, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure +{ + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + + [[[[self class] sharedInstance] networkManager] GET:@"hops" + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + NSMutableArray *hops = [NSMutableArray array]; + for (NSDictionary *dictionary in response[BreweryDBResponseDataKey]) + { + BDBHop *hop = [[BDBHop alloc] initWithDictionary:dictionary]; + if (hop) + [hops addObject:hop]; + else + { + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + break; + } + } + NSUInteger numberOfPages = [response[BreweryDBResponseNumberOfPagesKey] unsignedIntegerValue]; + NSUInteger currentPage = [response[BreweryDBResponseCurrentPageKey] unsignedIntegerValue]; + success(hops, currentPage, numberOfPages); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + ++ (void)fetchHopsForBeerId:(NSString *)beerId + withParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *hops, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure +{ + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + + [[[[self class] sharedInstance] networkManager] GET:[@"beer" stringByAppendingFormat:@"/%@/hops", beerId] + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + NSMutableArray *hops = [NSMutableArray array]; + for (NSDictionary *dictionary in response[BreweryDBResponseDataKey]) + { + BDBHop *hop = [[BDBHop alloc] initWithDictionary:dictionary]; + if (hop) + [hops addObject:hop]; + else + { + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + break; + } + } + NSUInteger numberOfPages = [response[BreweryDBResponseNumberOfPagesKey] unsignedIntegerValue]; + NSUInteger currentPage = [response[BreweryDBResponseCurrentPageKey] unsignedIntegerValue]; + success(hops, currentPage, numberOfPages); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + ++ (void)fetchHopWithId:(NSString *)hopId + parameters:(NSDictionary *)parameters + success:(void (^)(BDBHop *hop))success + failure:(void (^)(NSError *error))failure +{ + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + + [[[[self class] sharedInstance] networkManager] GET:[@"hop" stringByAppendingFormat:@"/%@", hopId] + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + BDBHop *hop = [[BDBHop alloc] initWithDictionary:response[BreweryDBResponseDataKey]]; + if (hop) + success(hop); + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + +#pragma mark Yeasts ++ (void)fetchYeastsWithParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *yeast, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure +{ + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + + [[[[self class] sharedInstance] networkManager] GET:@"yeasts" + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + NSMutableArray *yeasts = [NSMutableArray array]; + for (NSDictionary *dictionary in response[BreweryDBResponseDataKey]) + { + BDBYeast *yeast = [[BDBYeast alloc] initWithDictionary:dictionary]; + if (yeast) + [yeasts addObject:yeast]; + else + { + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + break; + } + } + NSUInteger numberOfPages = [response[BreweryDBResponseNumberOfPagesKey] unsignedIntegerValue]; + NSUInteger currentPage = [response[BreweryDBResponseCurrentPageKey] unsignedIntegerValue]; + success(yeasts, currentPage, numberOfPages); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + ++ (void)fetchYeastsForBeerId:(NSString *)beerId + withParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *yeast, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure +{ + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + + [[[[self class] sharedInstance] networkManager] GET:[@"beer" stringByAppendingFormat:@"/%@/yeasts", beerId] + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + NSMutableArray *yeasts = [NSMutableArray array]; + for (NSDictionary *dictionary in response[BreweryDBResponseDataKey]) + { + BDBYeast *yeast = [[BDBYeast alloc] initWithDictionary:dictionary]; + if (yeast) + [yeasts addObject:yeast]; + else + { + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + break; + } + } + NSUInteger numberOfPages = [response[BreweryDBResponseNumberOfPagesKey] unsignedIntegerValue]; + NSUInteger currentPage = [response[BreweryDBResponseCurrentPageKey] unsignedIntegerValue]; + success(yeasts, currentPage, numberOfPages); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + ++ (void)fetchYeastWithId:(NSString *)yeastId + parameters:(NSDictionary *)parameters + success:(void (^)(BDBYeast *yeast))success + failure:(void (^)(NSError *error))failure +{ + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + + [[[[self class] sharedInstance] networkManager] GET:[@"yeast" stringByAppendingFormat:@"/%@", yeastId] + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + BDBYeast *yeast = [[BDBYeast alloc] initWithDictionary:response[BreweryDBResponseDataKey]]; + if (yeast) + success(yeast); + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + +#pragma mark Locations ++ (void)fetchLocationsWithParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *locations, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure +{ + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + + [[[[self class] sharedInstance] networkManager] GET:@"locations" + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + NSMutableArray *locations = [NSMutableArray array]; + for (NSDictionary *dictionary in response[BreweryDBResponseDataKey]) + { + BDBLocation *location = [[BDBLocation alloc] initWithDictionary:dictionary]; + if (location) + [locations addObject:location]; + else + { + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + break; + } + } + NSUInteger numberOfPages = [response[BreweryDBResponseNumberOfPagesKey] unsignedIntegerValue]; + NSUInteger currentPage = [response[BreweryDBResponseCurrentPageKey] unsignedIntegerValue]; + success(locations, currentPage, numberOfPages); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + ++ (void)fetchLocationsForBreweryId:(NSString *)breweryId + withParameters:(NSDictionary *)parameters + success:(void (^)(NSArray *locations, NSUInteger currentPage, NSUInteger numberOfPages))success + failure:(void (^)(NSError *error))failure +{ + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + + [[[[self class] sharedInstance] networkManager] GET:[@"location" stringByAppendingFormat:@"/%@/locations", breweryId] + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + NSMutableArray *locations = [NSMutableArray array]; + for (NSDictionary *dictionary in response[BreweryDBResponseDataKey]) + { + BDBLocation *location = [[BDBLocation alloc] initWithDictionary:dictionary]; + if (location) + [locations addObject:location]; + else + { + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED + description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]); + break; + } + } + NSUInteger numberOfPages = [response[BreweryDBResponseNumberOfPagesKey] unsignedIntegerValue]; + NSUInteger currentPage = [response[BreweryDBResponseCurrentPageKey] unsignedIntegerValue]; + success(locations, currentPage, numberOfPages); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_API_ERROR + description:response[BreweryDBResponseErrorKey]]); + } + else + failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BAD_API_RESPONSE + description:BDB_ERROR_BAD_API_RESPONSE]); + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + failure(error); + }]; +} + ++ (void)fetchLocationWithId:(NSString *)locationId + parameters:(NSDictionary *)parameters + success:(void (^)(BDBLocation *location))success + failure:(void (^)(NSError *error))failure +{ + NSParameterAssert(success); + NSParameterAssert(failure); + + if (![[[self class] sharedInstance] readyToBrew]) + return failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_MISSING_API_KEY description:BDB_ERROR_MISSING_API_KEY]); + + NSMutableDictionary *mutableParameters = parameters.mutableCopy; + if (!mutableParameters) + mutableParameters = [NSMutableDictionary dictionary]; + mutableParameters[@"key"] = [[[self class] sharedInstance] apiKey]; + + [[[[self class] sharedInstance] networkManager] GET:[@"location" stringByAppendingFormat:@"/%@", locationId] + parameters:mutableParameters + success:^(NSURLSessionDataTask *task, id responseObject) { + if ([responseObject isKindOfClass:[NSDictionary class]]) + { + NSDictionary *response = responseObject; + if ([response[BreweryDBResponseStatusKey] isEqualToString:@"success"]) + { + BDBLocation *location = [[BDBLocation alloc] initWithDictionary:response[BreweryDBResponseDataKey]]; + if (location) + success(location); else failure([[[self class] sharedInstance] errorWithCode:BDB_ERRNO_BEER_OBJECT_CREATION_FAILED description:BDB_ERROR_BEER_OBJECT_CREATION_FAILED]);