FEWeiboModel.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // FEWeiboModel.m
  3. // ModelBenchmark
  4. //
  5. // Created by ibireme on 15/9/18.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "FEWeiboModel.h"
  9. #import "FEMDeserializer.h"
  10. #import "DateFormatter.h"
  11. #import "FEMSerializer.h"
  12. @implementation FEWeiboPictureMetadata
  13. + (FEMMapping *)defaultMapping {
  14. static FEMMapping *mapping;
  15. static dispatch_once_t onceToken;
  16. dispatch_once(&onceToken, ^{
  17. mapping = [[FEMMapping alloc] initWithEntityName:@"FEWeiboPictureMetadata"];
  18. [mapping addAttributesFromDictionary:@{
  19. @"url" : @"url",
  20. @"width" : @"width",
  21. @"height" : @"height",
  22. @"type" : @"type",
  23. @"cutType" : @"cut_type"
  24. }];
  25. });
  26. return mapping;
  27. }
  28. @end
  29. @implementation FEWeiboPicture
  30. + (FEMMapping *)defaultMapping {
  31. static FEMMapping *mapping;
  32. static dispatch_once_t onceToken;
  33. dispatch_once(&onceToken, ^{
  34. mapping = [[FEMMapping alloc] initWithEntityName:@"FEWeiboPicture"];
  35. [mapping addAttributesFromDictionary:@{
  36. @"picID" : @"pic_id",
  37. @"keepSize" : @"keep_size",
  38. @"photoTag" : @"photo_tag",
  39. @"objectID" : @"object_id",
  40. // @"thumbnail" : @"thumbnail",
  41. // @"bmiddle" : @"bmiddle",
  42. // @"middlePlus" : @"middleplus",
  43. // @"large" : @"large",
  44. // @"largest" : @"largest",
  45. // @"original" : @"original"
  46. }];
  47. FEMMapBlock map = (id)^(NSDictionary *value) {
  48. if ([value isKindOfClass:[NSDictionary class]]) {
  49. FEMMapping *mapping = [FEWeiboPictureMetadata defaultMapping];
  50. FEWeiboPictureMetadata *meta = [FEWeiboPictureMetadata new];
  51. [FEMDeserializer fillObject:meta fromRepresentation:value mapping:mapping];
  52. return meta;
  53. }
  54. return (FEWeiboPictureMetadata*)nil;
  55. };
  56. FEMMapBlock reverseMap = (id)^(NSDictionary *value) {
  57. if ([value isKindOfClass:[FEWeiboPictureMetadata class]]) {
  58. FEMMapping *mapping = [FEWeiboPictureMetadata defaultMapping];
  59. id meta = [FEMSerializer serializeObject:value usingMapping:mapping];
  60. return meta;
  61. }
  62. return (id)nil;
  63. };
  64. [mapping addAttribute:[[FEMAttribute alloc] initWithProperty:@"thumbnail" keyPath:@"thumbnail" map:map reverseMap:reverseMap]];
  65. [mapping addAttribute:[[FEMAttribute alloc] initWithProperty:@"bmiddle" keyPath:@"bmiddle" map:map reverseMap:reverseMap]];
  66. [mapping addAttribute:[[FEMAttribute alloc] initWithProperty:@"middlePlus" keyPath:@"middleplus" map:map reverseMap:reverseMap]];
  67. [mapping addAttribute:[[FEMAttribute alloc] initWithProperty:@"large" keyPath:@"large" map:map reverseMap:reverseMap]];
  68. [mapping addAttribute:[[FEMAttribute alloc] initWithProperty:@"largest" keyPath:@"largest" map:map reverseMap:reverseMap]];
  69. [mapping addAttribute:[[FEMAttribute alloc] initWithProperty:@"original" keyPath:@"original" map:map reverseMap:reverseMap]];
  70. });
  71. return mapping;
  72. }
  73. @end
  74. @implementation FEWeiboURL
  75. + (FEMMapping *)defaultMapping {
  76. static FEMMapping *mapping;
  77. static dispatch_once_t onceToken;
  78. dispatch_once(&onceToken, ^{
  79. mapping = [[FEMMapping alloc] initWithEntityName:@"FEWeiboURL"];
  80. [mapping addAttributesFromDictionary:@{
  81. @"result" : @"result",
  82. @"log" : @"log",
  83. @"oriURL" : @"ori_url",
  84. @"urlTitle" : @"url_title",
  85. @"urlTypePic" : @"url_type_pic",
  86. @"urlType" : @"url_type",
  87. @"shortURL" : @"short_url",
  88. @"actionLog" : @"actionlog",
  89. @"pageID" : @"page_id",
  90. @"storageType" : @"storage_type"
  91. }];
  92. });
  93. return mapping;
  94. }
  95. @end
  96. @implementation FEWeiboUser
  97. + (FEMMapping *)defaultMapping {
  98. static FEMMapping *mapping;
  99. static dispatch_once_t onceToken;
  100. dispatch_once(&onceToken, ^{
  101. mapping = [[FEMMapping alloc] initWithEntityName:@"FEWeiboUser"];
  102. [mapping addAttributesFromDictionary:@{
  103. @"userID" : @"id",
  104. @"idString" : @"idstr",
  105. @"genderString" : @"gender",
  106. @"desc" : @"description",
  107. @"domain" : @"domain",
  108. @"name" : @"name",
  109. @"screenName" : @"screen_name",
  110. @"remark" : @"remark",
  111. @"followersCount" : @"followers_count",
  112. @"friendsCount" : @"friends_count",
  113. @"biFollowersCount" : @"bi_followers_count",
  114. @"favouritesCount" : @"favourites_count",
  115. @"statusesCount" : @"statuses_count",
  116. @"pagefriendsCount" : @"pagefriends_count",
  117. @"followMe" : @"follow_me",
  118. @"following" : @"following",
  119. @"province" : @"province",
  120. @"city" : @"city",
  121. @"url" : @"url",
  122. @"profileImageURL" : @"profile_image_url",
  123. @"avatarLarge" : @"avatar_large",
  124. @"avatarHD" : @"avatar_hd",
  125. @"coverImage" : @"cover_image",
  126. @"coverImagePhone" : @"cover_image_phone",
  127. @"profileURL" : @"profile_url",
  128. @"type" : @"type",
  129. @"ptype" : @"ptype",
  130. @"mbtype" : @"mbtype",
  131. @"urank" : @"urank",
  132. @"uclass" : @"class",
  133. @"ulevel" : @"ulevel",
  134. @"mbrank" : @"mbrank",
  135. @"star" : @"star",
  136. @"level" : @"level",
  137. //@"createdAt" : @"created_at",
  138. @"allowAllActMsg" : @"allow_all_act_msg",
  139. @"allowAllComment" : @"allow_all_comment",
  140. @"geoEnabled" : @"geo_enabled",
  141. @"onlineStatus" : @"online_status",
  142. @"location" : @"location",
  143. @"icons" : @"icons",
  144. @"weihao" : @"weihao",
  145. @"badgeTop" : @"badge_top",
  146. @"blockWord" : @"block_word",
  147. @"blockApp" : @"block_app",
  148. @"hasAbilityTag" : @"has_ability_tag",
  149. @"creditScore" : @"credit_score",
  150. @"badge" : @"badge",
  151. @"lang" : @"lang",
  152. @"userAbility" : @"user_ability",
  153. @"extend" : @"extend",
  154. @"verified" : @"verified",
  155. @"verifiedType" : @"verified_type",
  156. @"verifiedLevel" : @"verified_level",
  157. @"verifiedState" : @"verified_state",
  158. @"verifiedContactEmail" : @"verified_contact_email",
  159. @"verifiedContactMobile" : @"verified_contact_mobile",
  160. @"verifiedTrade" : @"verified_trade",
  161. @"verifiedContactName" : @"verified_contact_name",
  162. @"verifiedSource" : @"verified_source",
  163. @"verifiedSourceURL" : @"verified_source_url",
  164. @"verifiedReason" : @"verified_reason",
  165. @"verifiedReasonURL" : @"verified_reason_url",
  166. @"verifiedReasonModified" : @"verified_reason_modified"
  167. }];
  168. FEMAttribute *createdAt = [[FEMAttribute alloc] initWithProperty:@"createdAt" keyPath:@"created_at" map:(id)^(NSString *value) {
  169. if ([value isKindOfClass:[NSString class]]) {
  170. return [[DateFormatter weiboDataFormatter] dateFromString:value];
  171. }
  172. return (NSDate*)nil;
  173. } reverseMap:(id)^(id value) {
  174. if ([value isKindOfClass:[NSDate class]]) {
  175. return [[DateFormatter weiboDataFormatter] stringFromDate:value];
  176. }
  177. return (NSString *)[NSNull null];
  178. }];
  179. [mapping addAttribute:createdAt];
  180. });
  181. return mapping;
  182. }
  183. @end
  184. @implementation FEWeiboStatus
  185. + (FEMMapping *)defaultMapping {
  186. static FEMMapping *mapping;
  187. static dispatch_once_t onceToken;
  188. dispatch_once(&onceToken, ^{
  189. mapping = [[FEMMapping alloc] initWithEntityName:@"FEWeiboStatus"];
  190. [mapping addAttributesFromDictionary:@{
  191. @"statusID" : @"id",
  192. @"idstr" : @"idstr",
  193. @"mid" : @"mid",
  194. @"rid" : @"rid",
  195. //@"createdAt" : @"created_at",
  196. //@"user" : @"user",
  197. @"userType" : @"userType",
  198. @"text" : @"text",
  199. @"picIds" : @"pic_ids",
  200. //@"picInfos" : @"pic_infos",
  201. //@"urlStruct" : @"url_struct",
  202. @"favorited" : @"favorited",
  203. @"truncated" : @"truncated",
  204. @"repostsCount" : @"reposts_count",
  205. @"commentsCount" : @"comments_count",
  206. @"attitudesCount" : @"attitudes_count",
  207. @"attitudesStatus" : @"attitudes_status",
  208. @"recomState" : @"recom_state",
  209. @"inReplyToScreenName" : @"in_reply_to_screen_name",
  210. @"inReplyToStatusId" : @"in_reply_to_status_id",
  211. @"inReplyToUserId" : @"in_reply_to_user_id",
  212. @"source" : @"source",
  213. @"sourceType" : @"source_type",
  214. @"sourceAllowClick" : @"source_allowclick",
  215. @"geo" : @"geo",
  216. @"annotations" : @"annotations",
  217. @"bizFeature" : @"biz_feature",
  218. @"mblogid" : @"mblogid",
  219. @"mblogTypeName" : @"mblogtypename",
  220. @"mblogType" : @"mblogtype",
  221. @"scheme" : @"scheme",
  222. @"visible" : @"visible",
  223. @"darwinTags" : @"darwin_tags"
  224. }];
  225. FEMAttribute *createdAt = [[FEMAttribute alloc] initWithProperty:@"createdAt" keyPath:@"created_at" map:(id)^(NSString *value) {
  226. if ([value isKindOfClass:[NSString class]]) {
  227. return [[DateFormatter weiboDataFormatter] dateFromString:value];
  228. }
  229. return (NSDate*)nil;
  230. } reverseMap:(id)^(id value) {
  231. if ([value isKindOfClass:[NSDate class]]) {
  232. return [[DateFormatter weiboDataFormatter] stringFromDate:value];
  233. }
  234. return (NSString *)[NSNull null];
  235. }];
  236. [mapping addAttribute:createdAt];
  237. FEMAttribute *user = [[FEMAttribute alloc] initWithProperty:@"user" keyPath:@"user" map:(id)^(NSDictionary *value) {
  238. if ([value isKindOfClass:[NSDictionary class]]) {
  239. FEMMapping *mapping = [FEWeiboUser defaultMapping];
  240. FEWeiboUser *user = [FEWeiboUser new];
  241. [FEMDeserializer fillObject:user fromRepresentation:value mapping:mapping];
  242. return user;
  243. }
  244. return (FEWeiboUser*)nil;
  245. } reverseMap:(id)^(FEWeiboUser *value) {
  246. if ([value isKindOfClass:[FEWeiboUser class]]) {
  247. FEMMapping *mapping = [FEWeiboUser defaultMapping];
  248. id user = [FEMSerializer serializeObject:value usingMapping:mapping];
  249. return user;
  250. }
  251. return (id)nil;
  252. }];
  253. [mapping addAttribute:user];
  254. FEMAttribute *picInfos = [[FEMAttribute alloc] initWithProperty:@"picInfos" keyPath:@"pic_infos" map:(id)^(NSDictionary *value) {
  255. if ([value isKindOfClass:[NSDictionary class]]) {
  256. NSMutableDictionary *infos = [NSMutableDictionary new];
  257. FEMMapping *picMapping = [FEWeiboPicture defaultMapping];
  258. [value enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  259. FEWeiboPicture *pic = [FEWeiboPicture new];
  260. [FEMDeserializer fillObject:pic fromRepresentation:obj mapping:picMapping];
  261. infos[key] = pic;
  262. }];
  263. return (id)infos;
  264. }
  265. return (id)nil;
  266. } reverseMap:(id) ^ (NSDictionary *value) {
  267. FEMMapping *picMapping = [FEWeiboPicture defaultMapping];
  268. NSMutableDictionary *infos = [NSMutableDictionary new];
  269. [value enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  270. id pic = [FEMSerializer serializeObject:obj usingMapping:picMapping];
  271. if (pic) infos[key] = pic;
  272. }];
  273. return infos;
  274. }];
  275. [mapping addAttribute:picInfos];
  276. FEMAttribute *urlStruct = [[FEMAttribute alloc] initWithProperty:@"urlStruct" keyPath:@"url_struct" map:(id)^(NSArray *value) {
  277. if ([value isKindOfClass:[NSArray class]]) {
  278. NSMutableArray *urls = [NSMutableArray new];
  279. FEMMapping *picMapping = [FEWeiboURL defaultMapping];
  280. [value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  281. FEWeiboURL *url = [FEWeiboURL new];
  282. [FEMDeserializer fillObject:url fromRepresentation:obj mapping:picMapping];
  283. [urls addObject:url];
  284. }];
  285. return (id)urls;
  286. }
  287. return (id)nil;
  288. } reverseMap:(id) ^ (NSArray *value) {
  289. FEMMapping *picMapping = [FEWeiboURL defaultMapping];
  290. NSMutableArray *urls = [NSMutableArray new];
  291. [value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  292. id url = [FEMSerializer serializeObject:obj usingMapping:picMapping];
  293. if (url) [urls addObject:url];
  294. }];
  295. return urls;
  296. }];
  297. [mapping addAttribute:urlStruct];
  298. });
  299. return mapping;
  300. }
  301. @end