MTWeiboModel.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // MTWeiboModel.m
  3. // ModelBenchmark
  4. //
  5. // Created by ibireme on 15/9/18.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "MTWeiboModel.h"
  9. #import "DateFormatter.h"
  10. @implementation MTWeiboPictureMetadata
  11. + (NSDictionary *)JSONKeyPathsByPropertyKey {
  12. return @{@"url" : @"url",
  13. @"width" : @"width",
  14. @"height" : @"height",
  15. @"type" : @"type",
  16. @"cutType" : @"cut_type"};
  17. }
  18. + (NSValueTransformer *)widthJSONTransformer {
  19. return [MTLValueTransformer transformerUsingForwardBlock:^id(NSNumber *num, BOOL *success, NSError *__autoreleasing *error) {
  20. if ([num isKindOfClass:[NSString class]]) {
  21. num = @([((NSString *)num) integerValue]);
  22. }
  23. return num;
  24. } reverseBlock:^id(NSNumber *num, BOOL *success, NSError *__autoreleasing *error) {
  25. return num;
  26. }];
  27. return [MTLJSONAdapter dictionaryTransformerWithModelClass:MTWeiboPicture.class];
  28. }
  29. + (NSValueTransformer *)heightJSONTransformer {
  30. return [MTLValueTransformer transformerUsingForwardBlock:^id(NSNumber *num, BOOL *success, NSError *__autoreleasing *error) {
  31. if ([num isKindOfClass:[NSString class]]) {
  32. num = @([((NSString *)num) integerValue]);
  33. }
  34. return num;
  35. } reverseBlock:^id(NSNumber *num, BOOL *success, NSError *__autoreleasing *error) {
  36. return num;
  37. }];
  38. return [MTLJSONAdapter dictionaryTransformerWithModelClass:MTWeiboPicture.class];
  39. }
  40. @end
  41. @implementation MTWeiboPicture
  42. + (NSDictionary *)JSONKeyPathsByPropertyKey {
  43. return @{@"picID" : @"pic_id",
  44. @"keepSize" : @"keep_size",
  45. @"photoTag" : @"photo_tag",
  46. @"objectID" : @"object_id",
  47. @"thumbnail" : @"thumbnail",
  48. @"bmiddle" : @"bmiddle",
  49. @"middlePlus" : @"middleplus",
  50. @"large" : @"large",
  51. @"largest" : @"largest",
  52. @"original" : @"original"};
  53. }
  54. @end
  55. @implementation MTWeiboURL
  56. + (NSDictionary *)JSONKeyPathsByPropertyKey {
  57. return @{@"result" : @"result",
  58. @"log" : @"log",
  59. @"oriURL" : @"ori_url",
  60. @"urlTitle" : @"url_title",
  61. @"urlTypePic" : @"url_type_pic",
  62. @"urlType" : @"url_type",
  63. @"shortURL" : @"short_url",
  64. @"actionLog" : @"actionlog",
  65. @"pageID" : @"page_id",
  66. @"storageType" : @"storage_type"};
  67. }
  68. @end
  69. @implementation MTWeiboUser
  70. + (NSDictionary *)JSONKeyPathsByPropertyKey {
  71. return @{@"userID" : @"id",
  72. @"idString" : @"idstr",
  73. @"genderString" : @"gender",
  74. @"desc" : @"description",
  75. @"domain" : @"domain",
  76. @"name" : @"name",
  77. @"screenName" : @"screen_name",
  78. @"remark" : @"remark",
  79. @"followersCount" : @"followers_count",
  80. @"friendsCount" : @"friends_count",
  81. @"biFollowersCount" : @"bi_followers_count",
  82. @"favouritesCount" : @"favourites_count",
  83. @"statusesCount" : @"statuses_count",
  84. @"pagefriendsCount" : @"pagefriends_count",
  85. @"followMe" : @"follow_me",
  86. @"following" : @"following",
  87. @"province" : @"province",
  88. @"city" : @"city",
  89. @"url" : @"url",
  90. @"profileImageURL" : @"profile_image_url",
  91. @"avatarLarge" : @"avatar_large",
  92. @"avatarHD" : @"avatar_hd",
  93. @"coverImage" : @"cover_image",
  94. @"coverImagePhone" : @"cover_image_phone",
  95. @"profileURL" : @"profile_url",
  96. @"type" : @"type",
  97. @"ptype" : @"ptype",
  98. @"mbtype" : @"mbtype",
  99. @"urank" : @"urank",
  100. @"uclass" : @"class",
  101. @"ulevel" : @"ulevel",
  102. @"mbrank" : @"mbrank",
  103. @"star" : @"star",
  104. @"level" : @"level",
  105. @"createdAt" : @"created_at",
  106. @"allowAllActMsg" : @"allow_all_act_msg",
  107. @"allowAllComment" : @"allow_all_comment",
  108. @"geoEnabled" : @"geo_enabled",
  109. @"onlineStatus" : @"online_status",
  110. @"location" : @"location",
  111. @"icons" : @"icons",
  112. @"weihao" : @"weihao",
  113. @"badgeTop" : @"badge_top",
  114. @"blockWord" : @"block_word",
  115. @"blockApp" : @"block_app",
  116. @"hasAbilityTag" : @"has_ability_tag",
  117. @"creditScore" : @"credit_score",
  118. @"badge" : @"badge",
  119. @"lang" : @"lang",
  120. @"userAbility" : @"user_ability",
  121. @"extend" : @"extend",
  122. @"verified" : @"verified",
  123. @"verifiedType" : @"verified_type",
  124. @"verifiedLevel" : @"verified_level",
  125. @"verifiedState" : @"verified_state",
  126. @"verifiedContactEmail" : @"verified_contact_email",
  127. @"verifiedContactMobile" : @"verified_contact_mobile",
  128. @"verifiedTrade" : @"verified_trade",
  129. @"verifiedContactName" : @"verified_contact_name",
  130. @"verifiedSource" : @"verified_source",
  131. @"verifiedSourceURL" : @"verified_source_url",
  132. @"verifiedReason" : @"verified_reason",
  133. @"verifiedReasonURL" : @"verified_reason_url",
  134. @"verifiedReasonModified" : @"verified_reason_modified"};
  135. }
  136. + (NSValueTransformer *)createdAtJSONTransformer {
  137. return [MTLValueTransformer transformerUsingForwardBlock:^id(NSString *dateString, BOOL *success, NSError *__autoreleasing *error) {
  138. return [[DateFormatter weiboDataFormatter] dateFromString:dateString];
  139. } reverseBlock:^id(NSDate *date, BOOL *success, NSError *__autoreleasing *error) {
  140. return [[DateFormatter weiboDataFormatter] stringFromDate:date];
  141. }];
  142. }
  143. @end
  144. @implementation MTWeiboStatus
  145. + (NSDictionary *)JSONKeyPathsByPropertyKey {
  146. return @{@"statusID" : @"id",
  147. @"idstr" : @"idstr",
  148. @"mid" : @"mid",
  149. @"rid" : @"rid",
  150. @"createdAt" : @"created_at",
  151. @"user" : @"user",
  152. @"userType" : @"userType",
  153. @"text" : @"text",
  154. @"picIds" : @"pic_ids",
  155. @"picInfos" : @"pic_infos",
  156. @"urlStruct" : @"url_struct",
  157. @"favorited" : @"favorited",
  158. @"truncated" : @"truncated",
  159. @"repostsCount" : @"reposts_count",
  160. @"commentsCount" : @"comments_count",
  161. @"attitudesCount" : @"attitudes_count",
  162. @"attitudesStatus" : @"attitudes_status",
  163. @"recomState" : @"recom_state",
  164. @"inReplyToScreenName" : @"in_reply_to_screen_name",
  165. @"inReplyToStatusId" : @"in_reply_to_status_id",
  166. @"inReplyToUserId" : @"in_reply_to_user_id",
  167. @"source" : @"source",
  168. @"sourceType" : @"source_type",
  169. @"sourceAllowClick" : @"source_allowclick",
  170. @"geo" : @"geo",
  171. @"annotations" : @"annotations",
  172. @"bizFeature" : @"biz_feature",
  173. @"mblogid" : @"mblogid",
  174. @"mblogTypeName" : @"mblogtypename",
  175. @"mblogType" : @"mblogtype",
  176. @"scheme" : @"scheme",
  177. @"visible" : @"visible",
  178. @"darwinTags" : @"darwin_tags"};
  179. }
  180. + (NSValueTransformer *)picInfosJSONTransformer {
  181. static MTLJSONAdapter *pictureAdapter;
  182. static dispatch_once_t onceToken;
  183. dispatch_once(&onceToken, ^{
  184. pictureAdapter = [[MTLJSONAdapter alloc] initWithModelClass:[MTWeiboPicture class]];
  185. });
  186. return [MTLValueTransformer transformerUsingForwardBlock:^id(NSDictionary *dic, BOOL *success, NSError *__autoreleasing *error) {
  187. NSMutableDictionary *pics = [NSMutableDictionary new];
  188. [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  189. MTWeiboPicture *pic = [pictureAdapter modelFromJSONDictionary:obj error:nil];
  190. if (pic) pics[key] = pic;
  191. }];
  192. return pics;
  193. } reverseBlock:^id(NSDictionary *dic, BOOL *success, NSError *__autoreleasing *error) {
  194. NSMutableDictionary *ret = [NSMutableDictionary new];
  195. [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  196. if ([obj isKindOfClass:[MTWeiboPicture class]]) {
  197. NSDictionary *one = [pictureAdapter JSONDictionaryFromModel:obj error:nil];
  198. if (one) ret[key] = one;
  199. }
  200. }];
  201. return ret;
  202. }];
  203. return [MTLJSONAdapter dictionaryTransformerWithModelClass:MTWeiboPicture.class];
  204. }
  205. + (NSValueTransformer *)urlStructJSONTransformer {
  206. return [MTLJSONAdapter arrayTransformerWithModelClass:MTWeiboURL.class];
  207. }
  208. + (NSValueTransformer *)createdAtJSONTransformer {
  209. return [MTLValueTransformer transformerUsingForwardBlock:^id(NSString *dateString, BOOL *success, NSError *__autoreleasing *error) {
  210. return [[DateFormatter weiboDataFormatter] dateFromString:dateString];
  211. } reverseBlock:^id(NSDate *date, BOOL *success, NSError *__autoreleasing *error) {
  212. return [[DateFormatter weiboDataFormatter] stringFromDate:date];
  213. }];
  214. }
  215. @end