Created
November 25, 2013 21:42
-
-
Save matula/7649398 to your computer and use it in GitHub Desktop.
Sir Trevor JS block to pull in GitHub data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function parseGithub() | |
{ | |
// Get the URI segments. Example: http://github.com/github/repo | |
$url = explode('/', trim(parse_url($_GET['url'], PHP_URL_PATH), '/')); | |
// Add segments to API endpoint. The 1st segment is the user, the second the repo | |
$api_url = 'https://api.github.com/repos/' . $url[0] . '/' . $url[1]; | |
// Curl to get the json | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $api_url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'YourUsername-YourRepo'); | |
$return = curl_exec($ch); | |
$info = curl_getinfo($ch); | |
curl_close($ch); | |
// Check for valid HTTP code | |
if ($info['http_code'] !== 200) | |
{ | |
return FALSE; | |
} | |
return $return; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SirTrevor.Blocks.Github = (function(){ | |
return SirTrevor.Block.extend({ | |
type: "Github", | |
pastable: true, | |
fetchable: true, | |
paste_options: { | |
html: ['<input type="text" placeholder="', 'Paste Github URL here', | |
'" class="st-block__paste-input st-paste-block">'].join('') | |
}, | |
loadData: function(data) { | |
this.loadRemoteGithub(data.url); | |
}, | |
onContentPasted: function(event){ | |
// Content pasted. | |
var input = $(event.target), | |
val = input.val(); | |
this.handleGithubDropPaste(val); | |
}, | |
handleGithubDropPaste: function(url) { | |
if (!this.validGithubUrl(url)) { | |
this.addMessage("Invalid Github URL"); | |
return; | |
} | |
this.setData({ url: url }); | |
this.loadRemoteGithub(url); | |
}, | |
validGithubUrl: function(url) { | |
return (_.isURI(url) && | |
url.indexOf("github") !== -1); | |
}, | |
onDrop: function(transferData){ | |
var url = transferData.getData('text/plain'); | |
this.handleGithubDropPaste(url); | |
}, | |
loadRemoteGithub: function(url) { | |
this.loading(); | |
var ajaxOptions = { | |
// -- Change the AJAX endpoint here --- // | |
url: "/parseGithub/?url=" + url, | |
dataType: "json" | |
}; | |
this.fetch(ajaxOptions, this.onGithubFetchSuccess, this.onGithubFetchFail); | |
}, | |
onGithubFetchSuccess: function(data) { | |
// -- Returned from the AJAX endpoint -- // | |
this.setData({ | |
url: data.html_url, | |
name: data.name, | |
description: data.description, | |
user: data.owner.login | |
}); | |
this.$inputs.hide(); | |
var html = [ | |
'<div class="st_block__github">', | |
'<img src="/octocat.png">', | |
'<div class="st_block__github_name">', | |
'<a href="' + data.url + '" target="_blank">' + data.name + '</a> <span>from ' + data.owner.login + '</span>', | |
'</div>', | |
'<div class="st_block__github_description">' + data.description + '</div>', | |
'</div>'].join(''); | |
this.$editor.html(html).show(); | |
this.ready(); | |
}, | |
onGithubFetchFail: function(data) { | |
this.addMessage("There was a problem fetching the Github repository"); | |
this.ready(); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment