[Jifty-commit] r4268 - in jifty/trunk: . examples/ShrinkURL examples/ShrinkURL/bin examples/ShrinkURL/doc examples/ShrinkURL/lib examples/ShrinkURL/lib/ShrinkURL examples/ShrinkURL/lib/ShrinkURL/Action examples/ShrinkURL/lib/ShrinkURL/Model examples/ShrinkURL/log examples/ShrinkURL/share examples/ShrinkURL/share/po examples/ShrinkURL/share/web examples/ShrinkURL/share/web/static examples/ShrinkURL/share/web/templates examples/ShrinkURL/t examples/ShrinkURL/var examples/ShrinkURL/var/mason

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Oct 19 23:26:41 EDT 2007


Author: sartak
Date: Fri Oct 19 23:26:41 2007
New Revision: 4268

Added:
   jifty/trunk/examples/ShrinkURL/
   jifty/trunk/examples/ShrinkURL/Makefile.PL
   jifty/trunk/examples/ShrinkURL/bin/
   jifty/trunk/examples/ShrinkURL/bin/jifty   (contents, props changed)
   jifty/trunk/examples/ShrinkURL/doc/
   jifty/trunk/examples/ShrinkURL/etc/
   jifty/trunk/examples/ShrinkURL/etc/config.yml
   jifty/trunk/examples/ShrinkURL/lib/
   jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/
   jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/Action/
   jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/Action/CreateShrunkenURL.pm
   jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/Dispatcher.pm
   jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/Model/
   jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/Model/ShrunkenURL.pm
   jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/View.pm
   jifty/trunk/examples/ShrinkURL/log/
   jifty/trunk/examples/ShrinkURL/share/
   jifty/trunk/examples/ShrinkURL/share/po/
   jifty/trunk/examples/ShrinkURL/share/web/
   jifty/trunk/examples/ShrinkURL/share/web/static/
   jifty/trunk/examples/ShrinkURL/share/web/templates/
   jifty/trunk/examples/ShrinkURL/t/
   jifty/trunk/examples/ShrinkURL/var/
   jifty/trunk/examples/ShrinkURL/var/mason/
Modified:
   jifty/trunk/   (props changed)

Log:
 r43907 at onn:  sartak | 2007-10-19 23:26:32 -0400
 Another example app: ShrinkURL, which is basically (surprise!) tinyurl


Added: jifty/trunk/examples/ShrinkURL/Makefile.PL
==============================================================================
--- (empty file)
+++ jifty/trunk/examples/ShrinkURL/Makefile.PL	Fri Oct 19 23:26:41 2007
@@ -0,0 +1,7 @@
+use inc::Module::Install;
+
+name        'ShrinkURL';
+version     '0.01';
+requires    'Jifty' => '0.70824';
+
+WriteAll;

Added: jifty/trunk/examples/ShrinkURL/bin/jifty
==============================================================================
--- (empty file)
+++ jifty/trunk/examples/ShrinkURL/bin/jifty	Fri Oct 19 23:26:41 2007
@@ -0,0 +1,11 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use File::Basename qw(dirname); 
+use UNIVERSAL::require;
+
+use Jifty;
+use Jifty::Script;
+
+local $SIG{INT} = sub { warn "Stopped\n"; exit; };
+Jifty::Script->dispatch();

Added: jifty/trunk/examples/ShrinkURL/etc/config.yml
==============================================================================
--- (empty file)
+++ jifty/trunk/examples/ShrinkURL/etc/config.yml	Fri Oct 19 23:26:41 2007
@@ -0,0 +1,68 @@
+--- 
+framework: 
+  SkipAccessControl: 1
+  AdminMode: 1
+  ApplicationClass: ShrinkURL
+  ApplicationName: ShrinkURL
+  ApplicationUUID: CA394F5C-7E9E-11DC-8297-EA4406D64C5E
+  ConfigFileVersion: 2
+  Database: 
+    CheckSchema: 1
+    Database: shrinkurl
+    Driver: SQLite
+    Host: localhost
+    Password: ''
+    RecordBaseClass: Jifty::DBI::Record::Cachable
+    User: ''
+    Version: 0.0.1
+  DevelMode: 1
+  L10N: 
+    PoDir: share/po
+  LogLevel: DEBUG
+  Mailer: Sendmail
+  MailerArgs: []
+
+  Plugins: 
+    - 
+      LetMe: {}
+
+    - 
+      SkeletonApp: {}
+
+    - 
+      REST: {}
+
+    - 
+      Halo: {}
+
+    - 
+      ErrorTemplates: {}
+
+    - 
+      OnlineDocs: {}
+
+    - 
+      CompressedCSSandJS: {}
+
+    - 
+      AdminUI: {}
+
+  PubSub: 
+    Backend: Memcached
+    Enable: ~
+  SkipAccessControl: 0
+  TemplateClass: ShrinkURL::View
+  Web: 
+    BaseURL: http://localhost
+    DataDir: var/mason
+    Globals: []
+
+    MasonConfig: 
+      autoflush: 0
+      default_escape_flags: h
+      error_format: text
+      error_mode: fatal
+    Port: 8888
+    ServeStaticFiles: 1
+    StaticRoot: share/web/static
+    TemplateRoot: share/web/templates

Added: jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/Action/CreateShrunkenURL.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/Action/CreateShrunkenURL.pm	Fri Oct 19 23:26:41 2007
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+package ShrinkURL::Action::CreateShrunkenURL;
+use strict;
+use warnings;
+
+use base qw/Jifty::Action::Record::Create/;
+sub record_class { 'ShrinkURL::Model::ShrunkenURL' }
+
+# have we already shrunk this URL? if so, no need to do it again!
+sub take_action {
+    my $self = shift;
+    my $url = $self->argument_value('url');
+
+    my $shrunkenurl = ShrinkURL::Model::ShrunkenURL->new;
+    $shrunkenurl->load_by_cols(url => $url);
+
+    if ($shrunkenurl->id) {
+        $self->record($shrunkenurl);
+        $self->result->content(id => $shrunkenurl->id);
+        $self->report_success;
+        return $shrunkenurl->id;
+    }
+
+    return $self->SUPER::take_action(@_);
+}
+
+sub report_success {
+    my $self = shift;
+    $self->result->message(_("URL shrunked to %1", $self->record->shrunken));
+}
+
+1;
+

Added: jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/Dispatcher.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/Dispatcher.pm	Fri Oct 19 23:26:41 2007
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+package ShrinkURL::Dispatcher;
+use strict;
+use warnings;
+use Jifty::Dispatcher -base;
+
+# visiting / will let users create new shrunken URLs
+on '/' => show 'shrink';
+
+# any other URL is potentially a shrunken URL
+on '*' => run {
+    my $url = $1;
+
+    my $shrunkenurl = ShrinkURL::Model::ShrunkenURL->new;
+    $shrunkenurl->load_by_shrunken($url);
+
+    if ($shrunkenurl->id) {
+        redirect($shrunkenurl->url);
+    }
+
+    # if there's no valid URL, just let the person create a new one :)
+    redirect('/');
+};
+
+1;
+

Added: jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/Model/ShrunkenURL.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/Model/ShrunkenURL.pm	Fri Oct 19 23:26:41 2007
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+package ShrinkURL::Model::ShrunkenURL;
+use strict;
+use warnings;
+use Number::RecordLocator;
+my $generator = Number::RecordLocator->new;
+
+use Jifty::DBI::Schema;
+use Jifty::Record schema {
+    column url =>
+        is distinct,
+        is varchar(1000),
+        is indexed;
+};
+
+# shrunken URL is just an encoding of ID
+sub shrunken {
+    my $self = shift;
+    Jifty->web->url(path => $generator->encode($self->id));
+}
+
+# helper function so we can easily change the internal representation of
+# shrunken URLs if we desire
+sub load_by_shrunken {
+    my $self = shift;
+    my $shrunken = shift;
+    my $id = $generator->decode($shrunken);
+
+    return $self->load($id);
+}
+
+# prepend http:// if the scheme is not already there
+sub canonicalize_url {
+    my $self = shift;
+    my $url = shift;
+
+    $url = "http://$url"
+        unless $url =~ m{^\w+://};
+
+    return $url;
+}
+
+1;
+

Added: jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/View.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/examples/ShrinkURL/lib/ShrinkURL/View.pm	Fri Oct 19 23:26:41 2007
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+package ShrinkURL::View;
+use strict;
+use warnings;
+use Jifty::View::Declare -base;
+
+template 'shrink' => page {
+    render_region(
+        name => 'new_shrink',
+        path => '/misc/new_shrink',
+    );
+    render_region(
+        name => 'new_shrinks',
+    );
+};
+
+template '/misc/new_shrink' => sub {
+    my $action = new_action(class => 'CreateShrunkenURL');
+    form {
+        Jifty->web->form->register_action($action);
+        render_action($action => ['url']);
+        form_submit(
+            submit  => $action,
+            label   => _('Shrink it!'),
+            onclick => [
+                { submit => $action },
+                {
+                    region => 'new_shrinks',
+                    prepend => '/misc/shrunk_region',
+                    args => {
+                        foo => "bar",
+                        id => { result_of => $action, name => 'id' },
+                    },
+                },
+            ],
+        );
+    };
+};
+
+template '/misc/shrunk_region' => sub {
+    my $id = get 'id';
+    my $shrunken = ShrinkURL::Model::ShrunkenURL->new;
+    $shrunken->load($id);
+    if ($shrunken->id) {
+        div {
+            strong { a { attr { href => $shrunken->shrunken } $shrunken->shrunken  } };
+            outs _(" is now a shortcut for %1.", $shrunken->url);
+        }
+    }
+};
+
+1;
+


More information about the Jifty-commit mailing list