Last active
April 8, 2017 18:30
-
-
Save rjriel/7b0856076d29d99657f510385407c540 to your computer and use it in GitHub Desktop.
Get Github Info
This file contains 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 { Http, Response } from '@angular/http' | |
import { User } from './models/user' | |
import { Repo } from './models/repo' | |
import { Observable } from 'rxjs/Observable' | |
import 'rxjs/add/operator/catch'; | |
import 'rxjs/add/operator/map'; | |
@Injectable() | |
export class GithubService { | |
userUrl: string; | |
constructor(private http: Http) { | |
this.userUrl = "https://api.github.com/users/macdonst" | |
} | |
getUser(): Observable<User> { | |
return this.http.get(this.userUrl) | |
.map((response: Response) => this.toUser(response.json())) | |
} | |
getRepos(user: User): Observable<Repo[]> { | |
return this.http.get(user.repos_url) | |
.map(this.mapRepos) | |
} | |
private mapRepos = (response:Response): Repo[] => { | |
return response.json().map(this.toRepo) | |
} | |
private toUser(json: any): User { | |
return new User(json) | |
} | |
private toRepo(json: any): Repo { | |
return new Repo(json) | |
} | |
} |
This file contains 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
export class Repo { | |
name: string; | |
forks_count: number; | |
open_issues: number; | |
constructor(json: {}) { | |
this.name = json.name; | |
this.forks_count = json.forks_count; | |
this.open_issues = json.open_issues; | |
} | |
} |
This file contains 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
this.userRepos.push(new Repo({ name: "first_repo", forks_count: 4, open_issues: 8})) | |
this.userRepos.push(new Repo({ name: "second_repo", forks_count: 6, open_issues: 1})) | |
this.userRepos.push(new Repo({ name: "third_repo", forks_count: 8, open_issues: 1})) | |
this.userRepos.push(new Repo({ name: "fourth_repo", forks_count: 11, open_issues: 2})) | |
this.userRepos.push(new Repo({ name: "fifth_repo", forks_count: 14, open_issues: 1})) |
This file contains 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
export class User { | |
login: string; | |
name: string; | |
repos_url: string; | |
public_repos: number; | |
public_gists: number; | |
followers: number; | |
following: number; | |
constructor(attributes: {} = null) { | |
this.attributes = attributes; | |
} | |
set attributes(attributes: {}) { | |
for (var k in attributes) { | |
this[k] = attributes[k]; | |
} | |
} | |
get attributes(): {} { | |
let obj: User = new User() | |
obj.name = this.name | |
obj.login = this.login | |
obj.repos_url = this.repos_url | |
obj.public_repos = this.public_repos | |
obj.public_gists = this.public_gists | |
obj.followers = this.followers | |
obj.following = this.following | |
return obj | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment