import { AsyncSource } from './async-source' ; import { AvatarSource } from './avatar-source.enum' ; /**
 *  Vkontakte source implementation.
 *  Fetch avatar source based on vkontakte identifier
 *  and image size
 */ const apiVersion = 5.8 ; export class Vkontakte extends AsyncSource { readonly sourceType : AvatarSource = AvatarSource . VKONTAKTE ; constructor ( sourceId ) { super ( sourceId ) ; } public getAvatar ( size ) { const imgSize = this . getImageSize ( size ) ; return `https://api.vk.com/method/users.get?user_id=${ this . sourceId }&v=${ apiVersion }&fields=${ imgSize }` ; } /**
     * extract vkontakte avatar from json data
     */ public processResponse ( data ) { const sizeProperty = Object . keys ( data [ 'response' ] [ 0 ] ) . pop ( ) ; if ( ! sizeProperty ) { return null ; } return data [ 'response' ] [ 0 ] [ sizeProperty ] || null ; } /**
     * Returns image size related to vkontakte API
     */ private getImageSize ( size ) { if ( size <= 50 ) { return 'photo_50' ; } if ( size <= 100 ) { return 'photo_100' ; } if ( size <= 200 ) { return 'photo_200' ; } return 'photo_max' ; } }