Post.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Post.m
  2. //
  3. // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import "Post.h"
  23. #import "User.h"
  24. #import "AFAppDotNetAPIClient.h"
  25. @implementation Post
  26. - (instancetype)initWithAttributes:(NSDictionary *)attributes {
  27. self = [super init];
  28. if (!self) {
  29. return nil;
  30. }
  31. self.postID = (NSUInteger)[[attributes valueForKeyPath:@"id"] integerValue];
  32. self.text = [attributes valueForKeyPath:@"text"];
  33. self.user = [[User alloc] initWithAttributes:[attributes valueForKeyPath:@"user"]];
  34. return self;
  35. }
  36. #pragma mark -
  37. + (NSURLSessionDataTask *)globalTimelinePostsWithBlock:(void (^)(NSArray *posts, NSError *error))block {
  38. return [[AFAppDotNetAPIClient sharedClient] GET:@"stream/0/posts/stream/global" parameters:nil headers: nil progress:nil success:^(NSURLSessionDataTask * __unused task, id JSON) {
  39. NSArray *postsFromResponse = [JSON valueForKeyPath:@"data"];
  40. NSMutableArray *mutablePosts = [NSMutableArray arrayWithCapacity:[postsFromResponse count]];
  41. for (NSDictionary *attributes in postsFromResponse) {
  42. Post *post = [[Post alloc] initWithAttributes:attributes];
  43. [mutablePosts addObject:post];
  44. }
  45. if (block) {
  46. block([NSArray arrayWithArray:mutablePosts], nil);
  47. }
  48. } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
  49. if (block) {
  50. block([NSArray array], error);
  51. }
  52. }];
  53. }
  54. @end
  55. @implementation Post (NSCoding)
  56. - (void)encodeWithCoder:(NSCoder *)aCoder {
  57. [aCoder encodeInteger:(NSInteger)self.postID forKey:@"AF.postID"];
  58. [aCoder encodeObject:self.text forKey:@"AF.text"];
  59. [aCoder encodeObject:self.user forKey:@"AF.user"];
  60. }
  61. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  62. self = [super init];
  63. if (!self) {
  64. return nil;
  65. }
  66. self.postID = (NSUInteger)[aDecoder decodeIntegerForKey:@"AF.postID"];
  67. self.text = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"AF.text"];
  68. self.user = [aDecoder decodeObjectOfClass:[User class] forKey:@"AF.user"];
  69. return self;
  70. }
  71. + (BOOL)supportsSecureCoding {
  72. return YES;
  73. }
  74. @end