Skip to content

Commit

Permalink
Merge pull request #68 from JacobBennett/user-config
Browse files Browse the repository at this point in the history
Add GistConfig class
  • Loading branch information
mattstauffer committed Aug 15, 2015
2 parents 0e33cde + 12eacdf commit 6da41fe
Show file tree
Hide file tree
Showing 6 changed files with 427 additions and 0 deletions.
104 changes: 104 additions & 0 deletions app/Gists/GistConfig.php
@@ -0,0 +1,104 @@
<?php namespace Gistlog\Gists;

use ArrayAccess;
use Carbon\Carbon;
use Symfony\Component\Yaml\Yaml;

class GistConfig implements ArrayAccess
{

/**
* @var array
*/
private $defaultSettings = [
'published' => false,
'published_on' => null,
'preview' => null
];

/**
* @var array
*/
public $settings = [];

/**
* @var array
*/
private $dates = ['published_on'];

/**
* @param array|ArrayAccess $githubGist
* @return GistConfig
*/
public static function fromGitHub($githubGist)
{
$config = new self;
$config->settings = $config->defaultSettings;

if (! array_key_exists('gistlog.yml', $githubGist['files'])) {
return $config;
}

$userSettings = Yaml::parse($githubGist['files']['gistlog.yml']['content']);

if (! is_array($userSettings)) {
return $config;
}

foreach ($config->defaultSettings as $setting => $defaultValue) {
$config->settings[$setting] = array_get($userSettings, $setting, $defaultValue);
}

foreach ($config->dates as $setting) {

if (is_null($config->settings[$setting])) {
continue;
}

try {
$config->settings[$setting] = Carbon::createFromTimestamp($config->settings[$setting]);
} catch (\ErrorException $e) {
$config->settings[$setting] = null;
}

}

return $config;
}


/**
* @param mixed $setting
* @return bool
*/
public function offsetExists($setting)
{
return isset($this->settings[$setting]);
}

/**
* @param mixed $setting
* @return mixed
*/
public function offsetGet($setting)
{
return $this->settings[$setting];
}

/**
* @param mixed $setting
* @param mixed $value
*/
public function offsetSet($setting, $value)
{
array_set($this->settings, $setting, $value);
}

/**
* @param mixed $setting
*/
public function offsetUnset($setting)
{
array_set($this->settings, $setting, null);
}
}
7 changes: 7 additions & 0 deletions app/Gists/Gistlog.php
Expand Up @@ -12,6 +12,7 @@ class Gistlog
public $author;
public $avatarUrl;
public $link;
public $config;
private $public;

/**
Expand Down Expand Up @@ -53,6 +54,8 @@ public static function fromGitHub($githubGist, $githubComments = [])
return Comment::fromGitHub($githubGist['id'], $comment);
});

$gistlog->config = GistConfig::fromGitHub($githubGist);

return $gistlog;
}

Expand Down Expand Up @@ -94,6 +97,10 @@ public function isSecret()

public function getPreview()
{
if (! is_null($this->config['preview'])) {
return $this->config['preview'];
}

$body = strip_tags($this->renderHtml());

if (strlen($body) < 200) {
Expand Down
55 changes: 55 additions & 0 deletions tests/GistConfigTest.php
@@ -0,0 +1,55 @@
<?php

use Gistlog\Gists\GistConfig;

class GistConfigTest extends TestCase
{

use GistFixtureHelpers;

/** @test */
public function it_can_be_created_from_github_api_data()
{
$githubGist = $this->loadFixture('8f5ea4d44dbc5ccb77a3.json');

$config = GistConfig::fromGitHub($githubGist);

$this->assertEquals("A test preview.", $config['preview']);
$this->assertEquals("2015-05-15", $config['published_on']->format('Y-m-d'));
$this->assertTrue($config['published']);
}

/** @test */
public function it_returns_default_values_when_no_gistlog_yml_is_present()
{
$githubGist = $this->loadFixture('002ed429c7c21ab89300.json');

$config = GistConfig::fromGitHub($githubGist);

$this->assertNull($config['preview']);
$this->assertNull($config['published_on']);
$this->assertFalse($config['published']);
}

/** @test */
public function it_returns_default_values_when_gistlog_yml_does_not_provide_them()
{
$githubGist = $this->loadFixture('9e5ea4d44dbc5ccb77b4.json');

$config = GistConfig::fromGitHub($githubGist);

$this->assertNull($config['preview']); // excluded
$this->assertFalse($config['published']); // excluded
$this->assertEquals("2015-05-15", $config['published_on']->format('Y-m-d')); // provided
}

/** @test */
public function it_returns_a_null_value_when_a_date_value_is_invalid()
{
$githubGist = $this->loadFixture('bb5ea4d44dbc5ccb77s7.json');

$config = GistConfig::fromGitHub($githubGist);

$this->assertNull($config['published_on']);
}
}
87 changes: 87 additions & 0 deletions tests/fixtures/gists/8f5ea4d44dbc5ccb77a3.json
@@ -0,0 +1,87 @@
{
"url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3",
"forks_url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/forks",
"commits_url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/commits",
"id": "8f5ea4d44dbc5ccb77a3",
"git_pull_url": "https://gist.github.com/8f5ea4d44dbc5ccb77a3.git",
"git_push_url": "https://gist.github.com/8f5ea4d44dbc5ccb77a3.git",
"html_url": "https://gist.github.com/8f5ea4d44dbc5ccb77a3",
"files": {
"blog.MD": {
"filename": "blog.MD",
"type": "text/plain",
"language": "Markdown",
"raw_url": "https://gist.githubusercontent.com/JacobBennett/8f5ea4d44dbc5ccb77a3/raw/fb81c6b2666987c7e3556a67362ed20df76cf3a7/blog.MD",
"size": 963,
"truncated": false,
"content": "The end goal was to order a query by a list of specific values for a column, but then also order those by a created_at date in ascending order and then by id in ascending order\n\nThis proved to be more of a challenge than I originally thought. The solution I came up with is as follows.\n\n```SQL\nSELECT id, clientId, created_at \nFROM `claims` \nWHERE `clientId` IN ('FOO', 'BAR', 'BAZ', 'FIZZ') \nORDER BY clientId = 'FIZZ', clientId = 'BAZ', clientId = 'BAR', clientId = 'FOO', \ncreated_at ASC, id ASC\n```\n\nDirect your attention to line 4. Looking at this, you might assume that the returned order of the results would be `FIZZ, BAZ, BAR, FOO` but in fact, you would be wrong.\n\nWhile normally the order by statements seem to stack on top of the previous order statement, in appears that when using an `=` to define the order, the orders stack in reverse.\n\nThat is, the last order by statement containing the `=` was the first result returned. It's the little things."
},
"gistlog.yml": {
"filename": "gistlog.yml",
"type": "text/x-yaml",
"language": "YAML",
"raw_url": "https://gist.githubusercontent.com/JacobBennett/8f5ea4d44dbc5ccb77a3/raw/f5a9ef036c44acd7c8aba799cf290bdf0feb6e56/gistlog.yml",
"size": 73,
"truncated": false,
"content": "published: true\npreview: A test preview.\npublished_on: 2015-05-15"
}
},
"public": true,
"created_at": "2015-07-22T15:41:53Z",
"updated_at": "2015-07-26T05:01:32Z",
"description": "How SQL orders results when using equals",
"comments": 0,
"user": null,
"comments_url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/comments",
"owner": {
"login": "JacobBennett",
"id": 1517011,
"avatar_url": "https://avatars.githubusercontent.com/u/1517011?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/JacobBennett",
"html_url": "https://github.com/JacobBennett",
"followers_url": "https://api.github.com/users/JacobBennett/followers",
"following_url": "https://api.github.com/users/JacobBennett/following{/other_user}",
"gists_url": "https://api.github.com/users/JacobBennett/gists{/gist_id}",
"starred_url": "https://api.github.com/users/JacobBennett/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/JacobBennett/subscriptions",
"organizations_url": "https://api.github.com/users/JacobBennett/orgs",
"repos_url": "https://api.github.com/users/JacobBennett/repos",
"events_url": "https://api.github.com/users/JacobBennett/events{/privacy}",
"received_events_url": "https://api.github.com/users/JacobBennett/received_events",
"type": "User",
"site_admin": false
},
"forks": [],
"history": [
{
"user": {
"login": "JacobBennett",
"id": 1517011,
"avatar_url": "https://avatars.githubusercontent.com/u/1517011?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/JacobBennett",
"html_url": "https://github.com/JacobBennett",
"followers_url": "https://api.github.com/users/JacobBennett/followers",
"following_url": "https://api.github.com/users/JacobBennett/following{/other_user}",
"gists_url": "https://api.github.com/users/JacobBennett/gists{/gist_id}",
"starred_url": "https://api.github.com/users/JacobBennett/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/JacobBennett/subscriptions",
"organizations_url": "https://api.github.com/users/JacobBennett/orgs",
"repos_url": "https://api.github.com/users/JacobBennett/repos",
"events_url": "https://api.github.com/users/JacobBennett/events{/privacy}",
"received_events_url": "https://api.github.com/users/JacobBennett/received_events",
"type": "User",
"site_admin": false
},
"version": "5c63bf5ac93dfa4d0d62a9677ba015b34c6f9df5",
"committed_at": "2015-07-26T05:01:32Z",
"change_status": {
"total": 3,
"additions": 2,
"deletions": 1
},
"url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/5c63bf5ac93dfa4d0d62a9677ba015b34c6f9df5"
}
]
}
87 changes: 87 additions & 0 deletions tests/fixtures/gists/9e5ea4d44dbc5ccb77b4.json
@@ -0,0 +1,87 @@
{
"url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3",
"forks_url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/forks",
"commits_url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/commits",
"id": "8f5ea4d44dbc5ccb77a3",
"git_pull_url": "https://gist.github.com/8f5ea4d44dbc5ccb77a3.git",
"git_push_url": "https://gist.github.com/8f5ea4d44dbc5ccb77a3.git",
"html_url": "https://gist.github.com/8f5ea4d44dbc5ccb77a3",
"files": {
"blog.MD": {
"filename": "blog.MD",
"type": "text/plain",
"language": "Markdown",
"raw_url": "https://gist.githubusercontent.com/JacobBennett/8f5ea4d44dbc5ccb77a3/raw/fb81c6b2666987c7e3556a67362ed20df76cf3a7/blog.MD",
"size": 963,
"truncated": false,
"content": "The end goal was to order a query by a list of specific values for a column, but then also order those by a created_at date in ascending order and then by id in ascending order\n\nThis proved to be more of a challenge than I originally thought. The solution I came up with is as follows.\n\n```SQL\nSELECT id, clientId, created_at \nFROM `claims` \nWHERE `clientId` IN ('FOO', 'BAR', 'BAZ', 'FIZZ') \nORDER BY clientId = 'FIZZ', clientId = 'BAZ', clientId = 'BAR', clientId = 'FOO', \ncreated_at ASC, id ASC\n```\n\nDirect your attention to line 4. Looking at this, you might assume that the returned order of the results would be `FIZZ, BAZ, BAR, FOO` but in fact, you would be wrong.\n\nWhile normally the order by statements seem to stack on top of the previous order statement, in appears that when using an `=` to define the order, the orders stack in reverse.\n\nThat is, the last order by statement containing the `=` was the first result returned. It's the little things."
},
"gistlog.yml": {
"filename": "gistlog.yml",
"type": "text/x-yaml",
"language": "YAML",
"raw_url": "https://gist.githubusercontent.com/JacobBennett/8f5ea4d44dbc5ccb77a3/raw/f5a9ef036c44acd7c8aba799cf290bdf0feb6e56/gistlog.yml",
"size": 73,
"truncated": false,
"content": "published_on: 2015-05-15"
}
},
"public": true,
"created_at": "2015-07-22T15:41:53Z",
"updated_at": "2015-07-26T05:01:32Z",
"description": "How SQL orders results when using equals",
"comments": 0,
"user": null,
"comments_url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/comments",
"owner": {
"login": "JacobBennett",
"id": 1517011,
"avatar_url": "https://avatars.githubusercontent.com/u/1517011?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/JacobBennett",
"html_url": "https://github.com/JacobBennett",
"followers_url": "https://api.github.com/users/JacobBennett/followers",
"following_url": "https://api.github.com/users/JacobBennett/following{/other_user}",
"gists_url": "https://api.github.com/users/JacobBennett/gists{/gist_id}",
"starred_url": "https://api.github.com/users/JacobBennett/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/JacobBennett/subscriptions",
"organizations_url": "https://api.github.com/users/JacobBennett/orgs",
"repos_url": "https://api.github.com/users/JacobBennett/repos",
"events_url": "https://api.github.com/users/JacobBennett/events{/privacy}",
"received_events_url": "https://api.github.com/users/JacobBennett/received_events",
"type": "User",
"site_admin": false
},
"forks": [],
"history": [
{
"user": {
"login": "JacobBennett",
"id": 1517011,
"avatar_url": "https://avatars.githubusercontent.com/u/1517011?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/JacobBennett",
"html_url": "https://github.com/JacobBennett",
"followers_url": "https://api.github.com/users/JacobBennett/followers",
"following_url": "https://api.github.com/users/JacobBennett/following{/other_user}",
"gists_url": "https://api.github.com/users/JacobBennett/gists{/gist_id}",
"starred_url": "https://api.github.com/users/JacobBennett/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/JacobBennett/subscriptions",
"organizations_url": "https://api.github.com/users/JacobBennett/orgs",
"repos_url": "https://api.github.com/users/JacobBennett/repos",
"events_url": "https://api.github.com/users/JacobBennett/events{/privacy}",
"received_events_url": "https://api.github.com/users/JacobBennett/received_events",
"type": "User",
"site_admin": false
},
"version": "5c63bf5ac93dfa4d0d62a9677ba015b34c6f9df5",
"committed_at": "2015-07-26T05:01:32Z",
"change_status": {
"total": 3,
"additions": 2,
"deletions": 1
},
"url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/5c63bf5ac93dfa4d0d62a9677ba015b34c6f9df5"
}
]
}

0 comments on commit 6da41fe

Please sign in to comment.