Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. http://country.io/phone.json // e.g. DE: “Germany”
  2. http://country.io/names.json // e.g. DE: “49”
  3.  
  4. import { Injectable } from '@angular/core';
  5. import { Http } from '@angular/http';
  6. import 'rxjs/Rx';
  7. import { Observable } from 'rxjs/Observable';
  8.  
  9. @Injectable()
  10. export class CountryService {
  11.  
  12. constructor( private _http:Http) { }
  13.  
  14. getCountryCode(): Observable<any> {
  15. return this._http.get('http://crossorigin.me/http://country.io/phone.json')
  16. .map(countryCodes => countryCodes.json());
  17. }
  18.  
  19. getPhonePrefix(): Observable<any> {
  20. return this._http.get('http://crossorigin.me/http://country.io/names.json')
  21. .map(phonePrefix => phonePrefix.json());
  22. }
  23.  
  24. }
  25.  
  26. myPhonePrefixObject;
  27.  
  28. this.countryPhonePrefix()
  29. .then((pp) => {
  30. myPhonePrefixObject = pp;
  31. })
  32. .catch((err) => {
  33. console.log(err);
  34. });
  35.  
  36.  
  37. private getCountryCode() {
  38. return new Promise((resolve) => {
  39. this._countryService.getCountryCode()
  40. .subscribe(
  41. res => resolve(res)
  42. );
  43. });
  44. }
  45.  
  46.  
  47. private getPhonePrefix() {
  48. return new Promise((resolve, reject) => {
  49. return this._countryService.getPhonePrefix()
  50. .subscribe(
  51. res => resolve(res),
  52. error => reject(error)
  53. );
  54. });
  55. }
  56.  
  57.  
  58. private countryPhonePrefix() {
  59. return new Promise((resolve, reject) => {
  60. let cc: Object;
  61. this.getCountryCode()
  62. .then((cCode) => {
  63. cc = cCode;
  64. return this.getPhonePrefix()
  65. })
  66. .then((pPrefix) => {
  67. let pp: Object = {};
  68. Object.keys(cc).forEach((key, index) => {
  69. pp[cc[key]] = pPrefix[key];
  70. });
  71. resolve(pp);
  72. })
  73. .catch((err) => {
  74. reject(err);
  75. });
  76. });
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement