-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathdata.service.ts
More file actions
92 lines (78 loc) · 3.24 KB
/
Copy pathdata.service.ts
File metadata and controls
92 lines (78 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { offlineData } from '../../assets/offlineData';
import { flattenObject } from '../core/utils';
import { Observable } from 'rxjs';
import { sortDataByKey, fillFromJSON } from '../core/utils';
import { CoinItem, BlockItem } from '../core/interfaces';
import { map } from 'rxjs/operators';
@Injectable()
export class DataService {
private apiKey = 'c6d27a61006e744bde9f940f6a6ef693aa724d8682b47c1bd26d361befc13faa';
private baseUrl = `https://min-api.cryptocompare.com/data/top/mktcapfull?limit=100&tsym=USD&api_key=${this.apiKey}`;
private allCoinsDataUrl = `https://min-api.cryptocompare.com/data/all/coinlist?api_key=${this.apiKey}`;
private histoDataUrl = 'https://min-api.cryptocompare.com/data/histoday?fsym=';
private priceMultiFullUrl = 'https://min-api.cryptocompare.com/data/pricemultifull?fsyms=';
coins: CoinItem[];
constructor(private http: HttpClient) { }
getData(): Observable<CoinItem[]> {
return this.http.get(this.baseUrl)
.pipe(map(result => {
return this.transformData(result);
}));
}
getSpecificCoinData(symbol): Observable<BlockItem> {
return this.http.get(this.priceMultiFullUrl + symbol + '&tsyms=USD&api_key=' + this.apiKey)
.pipe(map(result => {
const returnedCoin = flattenObject(result['RAW'][symbol]['USD']);
const coin = new BlockItem();
fillFromJSON(coin, returnedCoin);
return coin;
}));
}
getBetweenDaysPrices(symbol: string, forDays: number): Observable<any> {
return this.http.get(this.histoDataUrl + symbol + '&tsym=USD&limit=' + forDays + '&api_key=' + this.apiKey)
.pipe(map(result => {
return result;
}));
}
getHistoricalData(symbol: string): Observable<any> {
return this.http.get(this.histoDataUrl + symbol + '&tsym=USD&limit=730&api_key=' + this.apiKey)
.pipe(map(result => {
return { data: result, symbol: symbol };
}));
}
getCryptoIdFromSymbol(symbol): Observable<any[]> {
return this.http.get(this.allCoinsDataUrl)
.pipe(map(result => {
const crypto = result['Data'][symbol];
return crypto;
}));
}
private transformData(data) {
const transformedData = [];
this.coins = [];
if (data['Message'] === 'Success' && data['HasWarning'] === false && data['Data'].length !== 0) {
const indexes = Object.keys(data['Data']);
for (const idx of indexes) {
const newCoin = new CoinItem();
transformedData.push(flattenObject(data['Data'][idx]));
fillFromJSON(newCoin, transformedData[idx]);
if (newCoin.changePct24Hour >= 0) {
newCoin.dailyScale = true;
} else {
newCoin.dailyScale = false;
}
newCoin.rank = Number(idx) + 1;
this.coins.push(newCoin);
}
} else {
for (let i = 0; i < offlineData.length; i++) {
const coin = new CoinItem();
fillFromJSON(coin, offlineData[i]);
this.coins.push(coin);
}
}
return sortDataByKey(this.coins, 'rank');
}
}