| Conditions | 10 |
| Total Lines | 74 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 1 |
| CRAP Score | 94.2302 |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like wechatpy.enterprise.client.api.appchat.WeChatAppChat._build_msg_content() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # -*- coding: utf-8 -*- |
||
| 112 | 10 | def _build_msg_content(self, msgtype='text', **kwargs): |
|
| 113 | """ |
||
| 114 | 构造消息内容 |
||
| 115 | |||
| 116 | :param content: 消息内容,最长不超过2048个字节 |
||
| 117 | :param msgtype: 消息类型,可以为text/image/voice/video/file/textcard/news/mpnews/markdown |
||
| 118 | :param kwargs: 具体消息类型的扩展参数 |
||
| 119 | :return: |
||
| 120 | """ |
||
| 121 | data = {'msgtype': msgtype} |
||
| 122 | if msgtype == 'text': |
||
| 123 | data[msgtype] = {'content': kwargs.get('content')} |
||
| 124 | elif msgtype == 'image' or msgtype == 'voice' or msgtype == 'file': |
||
| 125 | data[msgtype] = {'media_id': kwargs.get('media_id')} |
||
| 126 | elif msgtype == 'video': |
||
| 127 | data[msgtype] = { |
||
| 128 | 'media_id': kwargs.get('media_id'), |
||
| 129 | 'title': kwargs.get('title'), |
||
| 130 | 'description': kwargs.get('description') |
||
| 131 | } |
||
| 132 | elif msgtype == 'textcard': |
||
| 133 | data[msgtype] = { |
||
| 134 | 'title': kwargs.get('title'), |
||
| 135 | 'description': kwargs.get('description'), |
||
| 136 | 'url': kwargs.get('url'), |
||
| 137 | 'btntxt': kwargs.get('btntxt'), |
||
| 138 | } |
||
| 139 | elif msgtype == 'news': |
||
| 140 | # { |
||
| 141 | # "articles" : |
||
| 142 | # [ |
||
| 143 | # { |
||
| 144 | # "title" : "中秋节礼品领取", |
||
| 145 | # "description" : "今年中秋节公司有豪礼相送", |
||
| 146 | # "url":"https://zhidao.baidu.com/question/2073647112026042748.html", |
||
| 147 | # "picurl":"http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png" |
||
| 148 | # } |
||
| 149 | # ] |
||
| 150 | # } |
||
| 151 | data[msgtype] = kwargs |
||
| 152 | elif msgtype == 'mpnews': |
||
| 153 | # { |
||
| 154 | # "articles":[ |
||
| 155 | # { |
||
| 156 | # "title": "地球一小时", |
||
| 157 | # "thumb_media_id": "biz_get(image)", |
||
| 158 | # "author": "Author", |
||
| 159 | # "content_source_url": "https://work.weixin.qq.com", |
||
| 160 | # "content": "3月24日20:30-21:30 \n办公区将关闭照明一小时,请各部门同事相互转告", |
||
| 161 | # "digest": "3月24日20:30-21:30 \n办公区将关闭照明一小时" |
||
| 162 | # } |
||
| 163 | # ] |
||
| 164 | # } |
||
| 165 | data[msgtype] = kwargs |
||
| 166 | elif msgtype == 'markdown': |
||
| 167 | # { |
||
| 168 | # "content": "您的会议室已经预定,稍后会同步到`邮箱` |
||
| 169 | # >**事项详情** |
||
| 170 | # >事 项:<font color=\"info\">开会</font> |
||
| 171 | # >组织者:@miglioguan |
||
| 172 | # >参与者:@miglioguan、@kunliu、@jamdeezhou、@kanexiong、@kisonwang |
||
| 173 | # > |
||
| 174 | # >会议室:<font color=\"info\">广州TIT 1楼 301</font> |
||
| 175 | # >日 期:<font color=\"warning\">2018年5月18日</font> |
||
| 176 | # >时 间:<font color=\"comment\">上午9:00-11:00</font> |
||
| 177 | # > |
||
| 178 | # >请准时参加会议。 |
||
| 179 | # > |
||
| 180 | # >如需修改会议信息,请点击:[修改会议信息](https://work.weixin.qq.com)" |
||
| 181 | # } |
||
| 182 | data[msgtype] = kwargs |
||
| 183 | else: |
||
| 184 | raise TypeError('不能识别的msgtype: %s' % msgtype) |
||
| 185 | return data |
||
| 186 |