Skip to content

Instantly share code, notes, and snippets.

@ahmic
Last active July 23, 2019 23:04
Show Gist options
  • Save ahmic/833bf4ad69a07c1474e422e2db3cadb8 to your computer and use it in GitHub Desktop.
Save ahmic/833bf4ad69a07c1474e422e2db3cadb8 to your computer and use it in GitHub Desktop.
Github user score
<?php
public function calculateScore($username)
{
// Fetch user info from Github API
$url = 'https://api.github.com/users/'.$username.'/events/public';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Simple validation
if($httpcode != 200) {
throw new \Exception('User not found');
}
// Create Laravel collection from API response
$userData = collect(json_decode($data));
// Filter collection to easily get all items by object key and value
$pushEventCount = $userData->where('type', 'PushEvent')->count();
$pullRequestEventCount = $userData->where('type', 'PullRequestEvent')->count();
$issueCommentEventCount = $userData->where('type', 'IssueCommentEvent')->count();
// Calculations
$otherEventsCount = $userData->count() - $pushEventCount - $pullRequestEventCount - $issueCommentEventCount;
$selectedScore = $pushEventCount * 10 + $pullRequestEventCount * 5 + $issueCommentEventCount * 4;
$totalScore = $otherEventsCount + $selectedScore;
return $totalScore;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment