Conditions | 10 |
Total Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 110 |
Changes | 1 | ||
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 WebSocket.on_playing() 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 | from plugin.core.helpers.variable import to_integer |
||
26 | def on_playing(self, info): |
||
27 | if InterfaceMessages.critical: |
||
28 | return |
||
29 | |||
30 | # Create or retrieve existing session |
||
31 | session = WSessionManager.get.or_create(info, fetch=True) |
||
32 | |||
33 | # Validate session |
||
34 | if session.updated_at is None or (datetime.utcnow() - session.updated_at) > timedelta(minutes=5): |
||
35 | log.info('Updating session, last update was over 5 minutes ago') |
||
36 | WSessionManager.update(session, info, fetch=True) |
||
37 | return |
||
38 | |||
39 | if session.duration is None or session.view_offset is None: |
||
40 | # Update session |
||
41 | WSessionManager.update(session, info, fetch=lambda s, i: ( |
||
42 | s.rating_key != to_integer(i.get('ratingKey')) or |
||
43 | s.duration is None |
||
44 | )) |
||
45 | return |
||
46 | |||
47 | # Parse `info` to events |
||
48 | events = self.to_events(session, info) |
||
49 | |||
50 | if not events: |
||
51 | return |
||
52 | |||
53 | # Check for changed media |
||
54 | media_changed = session.rating_key != to_integer(info.get('ratingKey')) |
||
55 | |||
56 | # Parse `events` |
||
57 | actions = self.engine.process(session, events) |
||
58 | |||
59 | for action, payload in actions: |
||
60 | # Build request for the event |
||
61 | request = self.build_request( |
||
62 | session, |
||
63 | part=payload.get('part', 1), |
||
64 | rating_key=payload.get('rating_key'), |
||
65 | view_offset=payload.get('view_offset') |
||
66 | ) |
||
67 | |||
68 | if not request: |
||
69 | log.info('No request returned for action %r (payload: %r)', action, payload) |
||
70 | continue |
||
71 | |||
72 | # Queue request to be sent |
||
73 | ActionManager.queue('/'.join(['scrobble', action]), request, session) |
||
74 | |||
75 | # Update session |
||
76 | WSessionManager.update(session, info, fetch=media_changed) |
||
77 | |||
127 |