Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GistConfig class #68

Merged
merged 4 commits into from Aug 15, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
83 changes: 83 additions & 0 deletions app/Gists/GistConfig.php
@@ -0,0 +1,83 @@
<?php namespace Gistlog\Gists;

use Illuminate\Support\Arr;
use Symfony\Component\Yaml\Yaml;

class GistConfig implements \ArrayAccess
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use ArrayAccess

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

{

/**
* @var array
*/
private $default_settings = array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use short array syntax here and on :21

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we've done camelCase everywhere else in the app, both for properties and for inline variables

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this include the keys in the defaultSettings array?

for example:

$defaultSettings = [
    'published',
    'publishedOn',
    'preview',
];

'published' => false,
'published_on' => null,
'preview' => null
);

/**
* @var array
*/
public $settings = array();

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

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

$user_settings = Yaml::parse($githubGist['files']['gistlog.yml']['content']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline after please :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any contexts in which gistlog.yml wouldn't have a content key? I assume no, but just checking :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did some checking on this over at https://developer.github.com/v3/gists and I didn't see any cases in which that would be a concern. Should always contain the content key

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

foreach ($config->default_settings as $setting => $default_value) {
$config->settings[$setting] = Arr::get($user_settings, $setting, $default_value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we've been using array_get instead of Arr::get

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed all those out and removed that dependency

}

return $config;
}


/**
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 49:82, why are these named offset instead of setting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boilerplate...

{
return isset($this->settings[$offset]);
}

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

/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
Arr::set($this->settings, $offset, $value);
}

/**
* @param mixed $offset
*/
public function offsetUnset($offset)
{
Arr::set($this->settings, $offset, null);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected 1 newline at end of file; 0 found

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
44 changes: 44 additions & 0 deletions tests/GistConfigTest.php
@@ -0,0 +1,44 @@
<?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("05-15-2015", $config['published_on']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, if we're choosing any date format I would choose YYYY-MM-DD so it's globally clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call on this

http://symfony.com/legacy/doc/reference/1_3/en/02-YAML#chapter_02_sub_dates

Specifies that YAML dates must be in this format in order to be recognized as dates. Only issue this introduces is that the Symfony YAML parser now returns a timestamp instead of the actual date string when parsing through said date.

$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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method name GistConfigTest::it_returns_default_values_when_gistlog_yml_does_not_provide_them is not in camel caps format

{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opening brace indented incorrectly; expected 0 spaces, found 4

$githubGist = $this->loadFixture('9e5ea4d44dbc5ccb77b4.json');

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

$this->assertNull($config['preview']); // excluded
$this->assertFalse($config['published']); // excluded
$this->assertEquals("05-15-2015", $config['published_on']); // provided
}
}
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: 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"
}
]
}
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: 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"
}
]
}