Skip to content

Instantly share code, notes, and snippets.

@ahmic
Last active July 23, 2019 23:04
Show Gist options
  • Select an option

  • Save ahmic/833bf4ad69a07c1474e422e2db3cadb8 to your computer and use it in GitHub Desktop.

Select an option

Save ahmic/833bf4ad69a07c1474e422e2db3cadb8 to your computer and use it in GitHub Desktop.

Revisions

  1. ahmic revised this gist Feb 6, 2019. No changes.
  2. ahmic revised this gist Feb 6, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions helpers.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php

    public function calculateScore($username)
    {
    public function calculateScore($username)
    {
    // Fetch user info from Github API
    $url = 'https://api.github.com/users/'.$username.'/events/public';
    $ch = curl_init();
    @@ -33,4 +33,4 @@ public function calculateScore($username)
    $totalScore = $otherEventsCount + $selectedScore;

    return $totalScore;
    }
    }
  3. ahmic created this gist Feb 6, 2019.
    36 changes: 36 additions & 0 deletions helpers.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    <?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;
    }