Code Monkey home page Code Monkey logo

cameo's People

Contributors

joamag avatar readmecritic avatar tsilva avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cameo's Issues

Need caching support

Description

Here's an example on how caching is configured in AFNetworking:

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseUrl];
client.requestSerializer = AFHTTPRequestSerializer.serializer;
client.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad;

Response object serialization doesn't work when attributes don't exist

Description

When a key exists in the json map and not in the object, the HMJSONSerializer crashes. Here is a sample error:

'NSUnknownKeyException', reason: '[<Bagger.Category 0x7f9612490130> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key image_url.'

The expected behaviour would be to fail silently and just ignore that attribute (the server may send a lot of values but we may only care about a subset of them).

Category support for merging two dictionaries

Description

Need a method that merges the contents of one dictionary into another recursively. This would be a category of NSDictionary.

Solution

- (NSMutableDictionary *)mergedWithDictionary:(NSDictionary *)dictionary1 {
    // creates a mutable copy of the dictionary
    // into which the contents are going to be merged
    // (otherwise the dictionary may not be editable)
    NSMutableDictionary *dictionary2 = self.mutableCopy;

    // merges each entry of the dictionary
    for(NSString *key in dictionary1) {
        // retrieves the values from both
        // dictionaries for the current key
        id value1 = dictionary1[key];
        id value2 = dictionary2[key];

        // in case both dictionary values are dictionaries
        // then calls this method recursively to merge them
        BOOL isValue1Dict = [value1 isKindOfClass:NSDictionary.class];
        BOOL isValue2Dict = value2 != nil && [value2 isKindOfClass:NSDictionary.class];
        if(isValue1Dict && isValue2Dict) {
            dictionary2[key] = [value2 mergeDictionary:value1];
        }
        // otherwise assigns the first dictionary's
        // value into the second dictionary
        else {
            dictionary2[key] = value1;
        }
    }

    // returns the merged dictionary
    return dictionary2;
}

Cameo http requests don't send error to callback when serializer fails

Description

The callback for retrieval methods has a "response" and an "error" variable. When the response serializer fails (in this case the json serializer), the error is not forwarded to the callback, and the handling code is faced with an unexpected situation where there is no error but there's also no response (in this case it was causing a code crash). I think the error should be forwarded to the callback, otherwise I would have to do two different tests for errors instead of having a unified way of doing so.

Here is the culprit in HMJsonSerializer, notice that it's not returning the error:

- (id)loads:(NSData *)data error:(NSError *)error {
    id object = [NSJSONSerialization JSONObjectWithData:data
                                                options:kNilOptions
                                                  error:&error];
    if (error == nil && self.mapper) {
        object = [self map:object error:error];
    }
    return object;
}

Add support for response object parsing

Description

It would be very helpful to be able to specify which class one wants to have a json response applied to, and have the request methods returns instances of that class instead. For example, if the response was this:

[
    {
        "name" : "test1",
        "price" : 10
    },
    {
        "name" : "test2",
        "price" : 20
    }
]

If I told the method that I wanted that data applied to "Product.class", then I would receive two instances of that class with those values applied to them.

UIColor category methods to retrieve lighter and darker versions of the color

Description

It's sometimes helpful to be able to programatically retrieve a dark or a lighter version of a color. These are good candidates for category methods. Here is an example:

- (UIColor *)lighterByPercentage:(CGFloat)percentage {
    CGFloat red, green, blue, alpha;
    [color getRed:&red green:&green blue:&blue alpha:&alpha];
    red = MIN(red + percentage, 1.0);
    green = MIN(green + percentage, 1.0);
    blue = MIN(blue + percentage, 1.0);
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

- (UIColor *)darkerByPercentage:(CGFloat)percentage {
    CGFloat red, green, blue, alpha;
    [color getRed:&red green:&green blue:&blue alpha:&alpha];
    red = MAX(red - percentage, 0.0);
    green = MAX(green - percentage, 0.0);
    blue = MAX(blue - percentage, 0.0);
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

HMProxy seems slow when requesting an image.

Description

A GET request of a big image with the HMDataSerializer causes a delay on the main thread before invoking the callback.

Example

[self.proxy get:url
    parameters:params
    useSession:YES
    serializer:HMDataSerializer.singleton
      callback:^(NSData *response, NSError *error) {
          UIImage *image = [UIImage imageWithData:response];
          callback(image, error);
      }];

HMJsonRequest has no initWithUrlString:parameters:callback method

HMJsonRequest has the following methods:

โ€“ initWithUrlString:callback:
โ€“ initWithUrlString:parameters:

But it doesn't have a initWithUrlString:parameters:callback method, which forces the user to manually encode the GET parameters in the URL string when he wants to use a callback.

Support for Abstract API class (like Appier)

Description

To be able to simplify the process of creating an API client in Objective-C it's relevant to have an abstract class that handles, things like:

  • Session Management
  • OAuth process
  • Header manipulation

Inspiration

The creation of this feature is heavily inspired by the Appier API class.

Support for creating colors from hexadecimal

#import "UIColor+Utils.h"

@implementation UIColor(Utils)

+ (UIColor *)colorWithHex:(NSInteger)hex {
    NSInteger red = (hex & 0xFF0000) >> 16;
    NSInteger green = (hex & 0xFF00) >> 8;
    NSInteger blue = (hex & 0xFF);
    UIColor *color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
    return color;
}

- (NSString *)hexString {
    CGFloat red, green, blue, alpha;
    [self getRed:&red green:&green blue:&blue alpha:&alpha];
    NSInteger redI = (NSUInteger)(255.0 * red);
    NSInteger greenI = (NSUInteger)(255.0 * green);
    NSInteger blueI = (NSUInteger)(255.0 * blue);
    NSString *hexString = [NSString stringWithFormat:@"%02lx%02lx%02lx", (long)redI, (long)greenI, (long)blueI];
    return hexString;
}

@end

Support for setting background color for a state in UIButton

Description

Buttons only support [button setBackgroundImage:forState:]. The following category method can be added to UIButton to solve this:

- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state {
    UIImage *image = [UIImage imageWithColor:backgroundColor];
    [self setBackgroundImage:image forState:state];
}

And the following to UIImage:

+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0, 0.0, 1.0, 1.0);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

Add support for request parameters to support types other than NSString

Description

Currently the parameters provided to requests only support strings, meaning that I can't send a map like this:

{
    "number" : @1
}

Solution

A simple way to allow all object datatypes and have them all converted to string is to pass them all through this method:

NSString *parameter = [NSString stringWithFormat:@"%@", parameter];

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.