| Conditions | 10 |
| Total Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 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 ServiceWallabag.read_data() 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 |
||
| 42 | def read_data(self, **kwargs): |
||
| 43 | """ |
||
| 44 | get the data from the service |
||
| 45 | as the pocket service does not have any date |
||
| 46 | in its API linked to the note, |
||
| 47 | add the triggered date to the dict data |
||
| 48 | thus the service will be triggered when data will be found |
||
| 49 | |||
| 50 | :param kwargs: contain keyword args : trigger_id at least |
||
| 51 | :type kwargs: dict |
||
| 52 | |||
| 53 | :rtype: list |
||
| 54 | """ |
||
| 55 | self.date_triggered = kwargs.get('date_triggered') |
||
| 56 | self.trigger_id = kwargs.get('trigger_id') |
||
| 57 | self.user = kwargs.get('user') if kwargs.get('user') else '' |
||
| 58 | |||
| 59 | us = UserService.objects.get(token=self.token, name='ServiceWallabag') |
||
| 60 | |||
| 61 | params = dict({'access_token': self.token, |
||
| 62 | 'archive': 0, |
||
| 63 | 'star': 0, |
||
| 64 | 'delete': 0, |
||
| 65 | 'sort': 'created', |
||
| 66 | 'order': 'desc', |
||
| 67 | 'page': 1, |
||
| 68 | 'perPage': 30, |
||
| 69 | 'tags': []}) |
||
| 70 | |||
| 71 | responses = requests.get(us.host + '/api/entries.json', |
||
| 72 | params=params) |
||
| 73 | if responses.status_code == 401: |
||
| 74 | params['access_token'] = self._refresh_token() |
||
| 75 | responses = requests.get(us.host + '/api/entries.json', |
||
| 76 | params=params) |
||
| 77 | elif responses.status_code != 200: |
||
| 78 | raise HTTPError(responses.status_code, responses.json()) |
||
| 79 | |||
| 80 | json_data = {} |
||
| 81 | data = [] |
||
| 82 | try: |
||
| 83 | json_data = responses.json() |
||
| 84 | |||
| 85 | for d in json_data['_embedded']['items']: |
||
| 86 | created_at = arrow.get(d.get('created_at')) |
||
| 87 | date_triggered = arrow.get(self.date_triggered) |
||
| 88 | |||
| 89 | if created_at > date_triggered: |
||
| 90 | data.append({'title': d.get('title'), |
||
| 91 | 'content': d.get('content')}) |
||
| 92 | if len(data) > 0: |
||
| 93 | cache.set('th_wallabag_' + str(self.trigger_id), data) |
||
| 94 | except Exception as e: |
||
| 95 | if json_data.get('errors'): |
||
| 96 | for error in json_data['errors']: |
||
| 97 | error_json = json_data['errors'][error]['content'] |
||
| 98 | logger.critical("Wallabag: {error}".format( |
||
| 99 | error=error_json)) |
||
| 100 | logger.critical(e) |
||
| 101 | return data |
||
| 102 | |||
| 243 |