Last active
December 5, 2016 18:15
-
-
Save ibolmo/3d4c768b0a0eb2bef137466292e21ab6 to your computer and use it in GitHub Desktop.
Google Apps Script (Google Form) web service for creating service like: yourdomain.com/l/slug to http://web.com/page
This file contains 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 | |
use Illuminate\Console\Command; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Input\InputArgument; | |
class UpdateLinksCommand extends Command { | |
protected $name = 'links:update'; | |
protected $description = ''; | |
public function fire() | |
{ | |
$ch = curl_init(); | |
curl_setopt_array($ch, array( | |
CURLOPT_URL => $_ENV['URL_TO_SPREADSHEET_SERVICE'], | |
CURLOPT_HEADER => 0, | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_FOLLOWLOCATION => 1 | |
)); | |
$output = curl_exec($ch); | |
if (!$output) trigger_error(curl_error($ch)); | |
curl_close($ch); | |
$data = json_decode($output, true); | |
if (!empty($data)) file_put_contents(base_path() . '/data/links.json', json_encode($data)); | |
} | |
protected function getArguments() | |
{ | |
return []; | |
} | |
protected function getOptions() | |
{ | |
return []; | |
} | |
} |
This file contains 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 | |
use Illuminate\Database\Eloquent\ModelNotFoundException; | |
class Link { | |
static $all = []; | |
static function all() | |
{ | |
if (count(self::$all)) return self::$all; | |
$path = base_path() . '/data/links.json'; | |
$raw = json_decode(@file_get_contents($path), true); | |
return (self::$all = @$raw['links']); | |
} | |
static function findBySlugOrFail($slug) | |
{ | |
$found = current(array_filter(self::all(), function($record) use($slug){ | |
return $record['slug'] == $slug; | |
})); | |
if ($found) return (object) $found; | |
throw with(new ModelNotFoundException)->setModel(get_called_class()); | |
} | |
} |
This file contains 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
# Laravel 4.1.x | |
Route::get('/admin/links/up/{secret}', function($secret){ | |
if ($secret != $_ENV['link.secret']) throw new ErrorException('Not authorized.'); | |
Artisan::call('links:update'); | |
}); | |
Route::get('/l/{slug}', function($slug){ | |
$link = Link::findBySlugOrFail($slug); | |
return Redirect::to($link->url); | |
}); |
This file contains 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
SHEET_ID = '...'; | |
LINKS_UPDATE_WEBHOOK_URL = '...'; | |
function doGet(request) { | |
if (!request) request = { parameter: {secret: null} }; | |
var set = {}; | |
SpreadsheetApp.openById(SHEET_ID).getSheets().forEach(function(sheet){ | |
var name = sheet.getName().toLowerCase(); | |
set[name] = getObjectFromSheet(sheet); | |
}); | |
return respond(set); | |
} | |
var getObjectFromSheet = function(sheet){ | |
var rows = sheet.getDataRange().getValues(); | |
var headers = rows.shift().map(function(header){ | |
return keyify(header); | |
}); | |
return rows.map(function(row){ | |
var object = {}; | |
row.forEach(function(column, i){ | |
object[headers[i]] = column; | |
}); | |
return object; | |
}); | |
}; | |
var keyify = function(string){ | |
return slugify(string) | |
.replace(/-\D/g, function(match){ | |
return match.charAt(1).toUpperCase(); | |
}) | |
.replace(/^[\s\.]+|[\s\.]+$/g, ''); | |
}; | |
var slugify = function(text){ | |
return (text + '').toLowerCase() | |
.replace(/\s+/g, '-') // Replace spaces with - | |
.replace(/[^\w\-]+/g, '') // Remove all non-word chars | |
.replace(/\-\-+/g, '-') // Replace multiple - with single - | |
.replace(/^-+/, '') // Trim - from start of text | |
.replace(/-+$/, ''); // Trim - from end of text | |
}; | |
var respond = function(data){ | |
if (!data.toLowerCase) data = JSON.stringify(data); | |
return ContentService.createTextOutput(data).setMimeType(ContentService.MimeType.TEXT) | |
}; | |
function onSubmit(e){ | |
Logger.log('updating server'); | |
UrlFetchApp.fetch(LINKS_UPDATE_WEBHOOK_URL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment