import { Injectable } from '@angular/core'; import { Http, Response, Headers, RequestOptions } from '@angular/http'; import { Observable } from 'rxjs/Rx'; import { NewsCardModel } from '../models/card/news-card.model'; @Injectable() export class TwitterService { //apiKey = "&sortBy=top&apiKey=35715a9a13a548b990874cca6c88e757"; private twitterUrl = 'https://api.twitter.com/1.1/search/tweets.json?q=news%20'; constructor(private http: Http) { } getTweets(city: string): Observable { return this.http.get(this.twitterUrl+city) // ...and calling .json() on the response to return data .map(this.extractJson) //...errors if any .catch((error:any) => Observable.throw(error.json().error || 'Server error')); } extractJson(res: Response) { let rawArticles = res.json()["statuses"]; let count = 0; let articles: any[] = []; for(let article of rawArticles) { count++; let status = { name: article["user"]["name"], text: article["text"], time: article["created_at"] }; articles.push(article); if(count>9) break; } return articles; } }