Skip to content

Commit 9eb50a1

Browse files
author
Jake Bell
committed
- No need for JS actually (thats what <noscript> is for!)
- Implementing static caching for github api requests.
1 parent 4780e46 commit 9eb50a1

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

gist_filter.module

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ function theme_gist_filter_code($vars) {
8787
*/
8888
function _gist_display_code($matches) {
8989
// Get the Gist from the Github API.
90-
$url = 'https://api.github.com/gists/' . $matches[1];
91-
$response = drupal_http_request($url, array('headers' => array('Content-Type' => 'application/json')));
92-
$data = drupal_json_decode($response->data);
90+
$data = gist_filter_get_gist($matches[1]);
9391

9492
$output = '';
9593

@@ -120,10 +118,8 @@ function _gist_display_embed($matches) {
120118

121119
// Also grab the content and display it in code tags (in case the user does not have JS).
122120
// TODO: 1. there is some caching going on here. 2. this is expensive.
123-
drupal_add_js(drupal_get_path('module', 'gist_filter') . '/js/gist-filter-hide-code.js');
124-
$code = _gist_display_code($matches);
121+
$output = '<noscript>' . _gist_display_code($matches) . '</noscript>';
125122

126-
$output = $code;
127123
$output .= '<script src="' . $gist_url . '"></script>';
128124
return $output;
129125
}
@@ -136,3 +132,24 @@ function _gist_display_link($matches) {
136132
$gist_url = isset($matches[2]) && !empty($matches[2])? $gist_url . '#file_' . $matches[2] : $gist_url;
137133
return l($gist_url, $gist_url);
138134
}
135+
136+
/**
137+
* Helper function to retrieve a Gist from the Github API.
138+
*
139+
* @param $id
140+
* The ID of the Gist to grab.
141+
*
142+
* @return array
143+
* The data from the Github API.
144+
*/
145+
function gist_filter_get_gist($id) {
146+
static $gists = array();
147+
148+
if (!isset($gists[$id])) {
149+
$url = 'https://api.github.com/gists/' . $id;
150+
$response = drupal_http_request($url, array('headers' => array('Content-Type' => 'application/json')));
151+
$gists[$id] = drupal_json_decode($response->data);
152+
}
153+
154+
return $gists[$id];
155+
}

js/gist-filter-hide-code.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)