Skip to content

Commit

Permalink
layout improvements; URL shortener
Browse files Browse the repository at this point in the history
  • Loading branch information
PJW committed Feb 11, 2016
1 parent cc674e9 commit 18981b4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
5 changes: 5 additions & 0 deletions Kynetx/Configure.pm
Expand Up @@ -128,6 +128,11 @@ sub configure {
$config->{'METRICS'}->{'HOSTNAME'} = $host[0];
$config->{'METRICS'}->{'PROC'} = $$;

# HOST values
$config->{'SCHEME'} ||= "https";
$config->{'BASE_URL'} = $config->{'SCHEME'} . "://" . $config->{'EVAL_HOST'};
$config->{'BASE_URL'} .= ":" . $config->{'KNS_PORT'} if(defined $config->{'KNS_PORT'});

config_logging();
return 1;
}
Expand Down
15 changes: 10 additions & 5 deletions Kynetx/RuleManager.pm
Expand Up @@ -121,7 +121,7 @@ sub handler {
# at some point we need a better dispatch function
if ( $method eq "validate" ) {
( $result, $type ) = validate_rule( $req_info, $method, $rid );
} elsif ( $method eq "evalexpr" ) {
} elsif ( $method eq "evalexpr" || $method eq "repl") {
( $result, $type ) = evaluate_expr($r, $req_info, $method, $rid );
} elsif ( $method eq "jsontokrl" ) {
( $result, $type ) = pp_json( $req_info, $method, $rid );
Expand Down Expand Up @@ -252,6 +252,9 @@ sub evaluate_expr {
. "/evaluate_expr.tmpl";
my $test_template = HTML::Template->new( filename => $template );

$test_template->param(GOOGLE_SHORTENER_API_KEY =>
Kynetx::Configure::get_config('GOOGLE_SHORTENER_API_KEY')
);

# $logger->debug("[evaluate_expr] req_info: ", sub {Dumper $req_info});

Expand All @@ -263,21 +266,23 @@ sub evaluate_expr {
my $flavor = Kynetx::Request::get_attr($req_info,'flavor') || '';


my $expr = Kynetx::Request::get_attr($req_info,'expr') || '';
my $code = Kynetx::Request::get_attr($req_info,'code') || '';

my $global = "global { " . $expr . "}";
my $commentless_code = Kynetx::Parser::remove_comments($code);

my $global = "global { " . $commentless_code . "}";
$logger->debug("[evaluate_expr] evaluating ", sub { Dumper($global)});

my ( $json, $tree );
if ($expr) {
if ($code) {

my $results = {};
$tree = Kynetx::Parser::parse_global_decls($global);
$tree = {"global" => $tree};

# $logger->debug("[evaluate_expr] evaluating ", sub { Dumper($tree)});

$test_template->param( EXPR => $expr );
$test_template->param( CODE => $code );

if (ref $tree->{"global"} eq "HASH" && defined $tree->{"global"}->{"error"}) {
$results = $tree->{"global"}->{"error"};
Expand Down
52 changes: 43 additions & 9 deletions etc/tmpl/evaluate_expr.tmpl
@@ -1,6 +1,6 @@
<html>
<head>
<title>Evaluate Expression</title>
<title>Try KRL</title>


<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
Expand Down Expand Up @@ -30,7 +30,7 @@

<div class="container">
<div class="row">
<h2>Evaluate KRL Declarations</h2>
<h2>KRL Declarations Evaluator</h2>

<p>
Enter a list of <a href="http://developer.picolabs.io/display/docs/Declarations">KRL declarations</a> in the editor block. Note that they should not be wrapped in a <code>pre</code> or <code>global</code>. The editor is doing Javascript highlighting, which is close, but not exact.
Expand All @@ -39,21 +39,29 @@ Enter a list of <a href="http://developer.picolabs.io/display/docs/Declarations"

<div class="row">

<div id="editor_div"><TMPL_VAR NAME=EXPR></div>

</div> <!-- row -->

<div class="row">
<div class="col-md-6">
<div id="editor_div"><TMPL_VAR NAME=CODE></div>


<form method="post" action="<TMPL_VAR NAME=ACTION_URL>">

<textarea name="expr" rows="20" cols="80">
<textarea name="code" rows="20" cols="80">
</textarea>

<p style="margin-top:10px">
<input id="submit" type="submit" value="Evaluate!" />
<input id="reset" type="reset" value="Reset" />
<input id="shorten" type="button" value="Short URL" />
<div id="results"></div>
</p>
</form>
<p>
Note: The evaluation doesn't know about persistent variables, event attributes, and other values that are only available within the context of a specific pico.
</p>


</div> <!-- left side -->

Expand All @@ -75,10 +83,8 @@ There is a syntax error:
</div>
</TMPL_IF>

<p>
Note: The evaluation doesn't know about persistent variables, event attributes, and other values that are only available within the context of a specific pico.
</p>
</div> <!-- right side -->

</div><!-- row -->

<div class="row">
Expand Down Expand Up @@ -133,7 +139,7 @@ c = a.map(f)

<script src="https://ajaxorg.github.io/ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
var textarea = $('textarea[name="expr"]');
var textarea = $('textarea[name="code"]');
textarea.hide();
var editor = ace.edit("editor_div");
editor.setTheme("ace/theme/monokai");
Expand All @@ -143,9 +149,37 @@ c = a.map(f)
$('#submit').on('click', function() {
textarea.val(editor.getSession().getValue());
});
$('#reset').on('click', function() {
editor.getSession().setValue("");
});

function makeRequest() {
var longUrl = location.protocol + "://" +
location.hostname +
location.pathname + "?code=" +
encodeURIComponent(editor.getSession().getValue());
var request = gapi.client.urlshortener.url.insert({
'longUrl': longUrl
});
request.then(function(response) {
$('#results').html(response.result.id);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
}

function init() {
gapi.client.setApiKey('<TMPL_VAR NAME=GOOGLE_SHORTENER_API_KEY>');
gapi.client.load('urlshortener', 'v1').then(makeRequest);
}

$("#shorten").on('click', function() {
init();
});

</script>

<script src="https://apis.google.com/js/client.js"></script>


</body>
Expand Down

0 comments on commit 18981b4

Please sign in to comment.