Completed
Push — master ( 72b331...d4b7d2 )
by Steffen
02:14
created

SoldHistoryParser.parse()   A

Complexity

Conditions 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
rs 9.75
c 0
b 0
f 0
cc 3
nop 1
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4
import json
5
6
from kuon.api_response import APIResponse
7
from kuon.watcher.adapters.models.sold_history import SoldHistory
8
from kuon.watcher.adapters.models.sold_item import SoldItem
9
10
11
class SoldHistoryParser:
12
    """Parser class to parse the response of the sold history to the unified format"""
13
14
    @staticmethod
15
    def parse(results):
16
        """Parse the sold history model
17
18
        :param results:
19
        :return:
20
        """
21
        response = SoldHistory(success=results['status'] == 'success')
22
        for item in results['data']['sales']:
23
            wear = item['wear_value']
24
            if wear is None:
25
                wear = -1.0
26
27
            sold_item = SoldItem(
28
                price=int(float(item['price']) * 100),
29
                wear_value=float(wear),
30
                sold_at=int(item['sold_at'])
31
            )
32
            response.add_sale(sold_item)
33
        return APIResponse(json.dumps(response.__dict__))
34