diff --git a/app/Gists/GistConfig.php b/app/Gists/GistConfig.php new file mode 100644 index 00000000..68e9d282 --- /dev/null +++ b/app/Gists/GistConfig.php @@ -0,0 +1,104 @@ + 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); + } +} diff --git a/app/Gists/Gistlog.php b/app/Gists/Gistlog.php index 3e79557e..7a98ca2b 100644 --- a/app/Gists/Gistlog.php +++ b/app/Gists/Gistlog.php @@ -12,6 +12,7 @@ class Gistlog public $author; public $avatarUrl; public $link; + public $config; private $public; /** @@ -53,6 +54,8 @@ public static function fromGitHub($githubGist, $githubComments = []) return Comment::fromGitHub($githubGist['id'], $comment); }); + $gistlog->config = GistConfig::fromGitHub($githubGist); + return $gistlog; } @@ -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) { diff --git a/tests/GistConfigTest.php b/tests/GistConfigTest.php new file mode 100644 index 00000000..fb96b228 --- /dev/null +++ b/tests/GistConfigTest.php @@ -0,0 +1,55 @@ +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']); + } +} diff --git a/tests/fixtures/gists/8f5ea4d44dbc5ccb77a3.json b/tests/fixtures/gists/8f5ea4d44dbc5ccb77a3.json new file mode 100644 index 00000000..e7cd8062 --- /dev/null +++ b/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" + } + ] +} \ No newline at end of file diff --git a/tests/fixtures/gists/9e5ea4d44dbc5ccb77b4.json b/tests/fixtures/gists/9e5ea4d44dbc5ccb77b4.json new file mode 100644 index 00000000..7ca7daae --- /dev/null +++ b/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" + } + ] +} \ No newline at end of file diff --git a/tests/fixtures/gists/bb5ea4d44dbc5ccb77s7.json b/tests/fixtures/gists/bb5ea4d44dbc5ccb77s7.json new file mode 100644 index 00000000..903fe233 --- /dev/null +++ b/tests/fixtures/gists/bb5ea4d44dbc5ccb77s7.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: 05-15-2015" + } + }, + "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" + } + ] +} \ No newline at end of file