Skip to content

Instantly share code, notes, and snippets.

View Lordnibbler's full-sized avatar

Ben Radler Lordnibbler

View GitHub Profile
# channels controller
def index
@channels = Channel.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => { :channels => @channels } }
end
end
{"channels":{"78":{"id":78,"name":"Zombie Bass","num_of_listeners":1,"created_at":"2012-03-21T04:04:36Z","updated_at":"2012-04-20T01:21:12Z"},"82":{"id":82,"name":"The Beatles123","num_of_listeners":1,"created_at":"2012-03-22T20:28:34Z","updated_at":"2012-04-17T04:34:07Z"},"125":{"id":125,"name":"New!","num_of_listeners":1,"created_at":"2012-04-19T04:05:47Z","updated_at":"2012-04-19T04:05:47Z"},"132":{"id":132,"name":"Test!","num_of_listeners":1,"created_at":"2012-04-26T21:00:30Z","updated_at":"2012-04-26T21:00:30Z"}}}
- (void)setupObjectMapping {
RKObjectMapping* channelMapping = [RKObjectMapping mappingForClass:[Channel class]];
[channelMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[channelMapping mapKeyPath:@"name" toAttribute:@"name"];
//[channelMapping mapKeyPath:@"created_at" toAttribute:@"createdAt"];
//[channelMapping mapKeyPath:@"updated_at" toAttribute:@"updatedAt"];
[channelMapping mapKeyPath:@"num_of_listeners" toAttribute:@"numOfListeners"];
//object mapping for GETs
[[RKObjectManager sharedManager].mappingProvider setMapping:channelMapping forKeyPath:@"channels"];
{
"channels": {
"78": {
"id": 78,
"name": "Zombie Bass",
"num_of_listeners": 1,
"created_at": "2012-03-21T04:04:36Z",
"updated_at": "2012-04-20T01:21:12Z"
},
"82": {
//Channel.h
@interface Channel : NSObject <RKRequestDelegate, RKObjectLoaderDelegate> {
NSNumber *identifier;
NSString *name;
NSNumber *numOfListeners;
}
//AppDelegate.m
- (void)setupObjectMapping {
RKObjectMapping* channelMapping = [RKObjectMapping mappingForClass:[Channel class]];
{
"channels": [
{
"number": 78,
"name": "Zombie Bass",
"num_of_listeners": 1,
"created_at": "2012-03-21T04:04:36Z",
"updated_at": "2012-04-20T01:21:12Z"
},
{
{
"channels": {
"78": {
"name": "Zombie Bass",
"num_of_listeners": 1,
"created_at": "2012-03-21T04:04:36Z",
"updated_at": "2012-04-20T01:21:12Z"
},
"82": {
"name": "The Beatles123",
{
"channels": [
{
"number": 78,
"name": "Zombie Bass",
"num_of_listeners": 1,
"created_at": "2012-03-21T04:04:36Z",
"updated_at": "2012-04-20T01:21:12Z"
},
]
RKObjectMapping* channelMapping = [RKObjectMapping mappingForClass:[Channel class]];
[channelMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[channelMapping mapKeyPath:@"name" toAttribute:@"name"];
[channelMapping mapKeyPath:@"num_of_listeners" toAttribute:@"numOfListeners"];
//object mapping for GETs
[[RKObjectManager sharedManager].mappingProvider setMapping:channelMapping forKeyPath:@"channels"];
//set up object serialization for POSTing back to rails
RKObjectMapping* channelSerializationMapping = [channelMapping inverseMapping];
{
"channels": {
"channel_78": {
"name": "Zombie Bass",
"num_of_listeners": 1,
"created_at": "2012-03-21T04:04:36Z",
"updated_at": "2012-04-20T01:21:12Z"
},
"channel_82": {
"name": "The Beatles123",