Created
March 7, 2018 19:17
-
-
Save jplew/2dfac1d7476ae080166ead216e9e0126 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core' | |
import { HttpClient } from '@angular/common/http' | |
import { environment } from '@env/environment' | |
import { Observable } from 'rxjs/Observable' | |
import 'rxjs/add/operator/map' | |
import { WordDoc } from '@interfaces/WordDoc' | |
import { FragmentMatches } from '@interfaces/FragmentMatches' | |
import { startWith, tap, retryWhen } from 'rxjs/operators' | |
import { pipe } from 'rxjs/Rx' | |
import { of } from 'rxjs/observable/of' | |
import * as sjcl from 'sjcl/sjcl' | |
@Injectable() | |
export class WordbankService { | |
constructor(private httpClient: HttpClient) {} | |
checkIfValidFragment(fragment: string): Observable<boolean> { | |
const route = environment.rootWordBank + 'isvalid/' + fragment | |
return this.cacheResult<boolean>(route) | |
} | |
checkIfWord(word: string): Observable<boolean> { | |
const route = environment.rootWordBank + 'isword/' + word | |
return this.cacheResult<boolean>(route) | |
} | |
getWordDoc(word): Observable<WordDoc> { | |
const route = environment.rootWordBank + 'single/' + word | |
return this.cacheResult<WordDoc>(route) | |
} | |
getMatchingWords( | |
fragment: string, | |
level: number | |
): Observable<FragmentMatches> { | |
const route = | |
environment.rootWordBank + 'fragment/' + fragment + '/level/' + level | |
return this.cacheResult<FragmentMatches>(route) | |
} | |
getWordsByDifficulty( | |
level: number, | |
limit: number = 0 | |
): Observable<FragmentMatches> { | |
const route = | |
environment.rootWordBank + 'level/' + level + '/limit/' + limit | |
return this.cacheResult<FragmentMatches>(route) | |
} | |
doWordsExist(fragment: string, level: number): Observable<boolean> { | |
const route = | |
environment.rootWordBank + | |
'fragment/' + | |
fragment + | |
'/level/' + | |
level + | |
'/exists' | |
return this.cacheResult<boolean>(route) | |
} | |
getRandomWordFromFirstLetter(letter: string) { | |
const letterTable = { | |
a: 'antelope', | |
b: 'bison', | |
c: 'camel', | |
d: 'duck', | |
e: 'elephant', | |
f: 'fox', | |
g: 'giraffe', | |
h: 'horse', | |
i: 'iguana', | |
j: 'jackal', | |
k: 'koala', | |
l: 'lamb', | |
m: 'mouse', | |
n: 'newt', | |
o: 'okapi', | |
p: 'panda', | |
q: 'quokka', | |
r: 'rabbit', | |
s: 'snake', | |
t: 'tiger', | |
u: 'unicorn', | |
v: 'vulture', | |
w: 'walrus', | |
x: 'xerus', | |
y: 'yak', | |
z: 'zebra', | |
} | |
return letterTable[letter] | |
} | |
setCachedValue(route, next) { | |
const encryptedValue = | |
// tslint:disable-next-line:triple-equals | |
next != 'true' && next != 'false' ? this.encrypt(next) : next | |
localStorage.setItem(route, encryptedValue) | |
} | |
getCachedValue(route: string): string { | |
const encryptedValue = localStorage.getItem(route) || null | |
if (!encryptedValue) { | |
return null | |
} | |
const cachedValue: string = this.decrypt(encryptedValue) | |
return cachedValue | |
} | |
cacheResult<T>(route: string): Observable<T> { | |
const cacheValue: string = this.getCachedValue(route) | |
if (cacheValue) { | |
return of(JSON.parse(cacheValue)) | |
} else { | |
console.log('are you logging me 2?') | |
console.log(route) | |
return this.httpClient.get<T>(route).pipe( | |
tap(next => { | |
this.setCachedValue(route, next) | |
}), | |
retryWhen(err => { | |
console.log('retrying again') | |
return err.delay(1000).take(10) | |
}) | |
) | |
} | |
} | |
encrypt(object): string { | |
const string = JSON.stringify(object) | |
return sjcl.encrypt('password', string) | |
} | |
decrypt(string): string { | |
const decryptedValue = sjcl.decrypt('password', string) | |
return decryptedValue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment