Permalink
Browse files

let's be able to handle oembed

  • Loading branch information...
netfeed committed Feb 17, 2012
1 parent 8e3802e commit bd2b3bd652ae25c56d4948cbdd697cf114fdc40f
Showing with 70 additions and 0 deletions.
  1. +9 −0 lib/Coxify.pm
  2. +24 −0 lib/Coxify/Controller/Oembed.pm
  3. +37 −0 lib/Mojolicious/Plugin/Oembed.pm
View
@@ -8,6 +8,13 @@ sub startup {
my $config = $self->plugin('JSONConfig' => { file => "config/config.json" });
$self->secret($config->{secret});
+
+ $self->plugin(Oembed => [
+ {
+ scheme => 'http://*.flickr.com/*',
+ endpoint => 'http://www.flickr.com/services/oembed/'
+ }
+ ]);
# Routes
my $r = $self->routes;
@@ -21,6 +28,8 @@ sub startup {
$r->route('/image/:year/:month/:day')->to("image#day");
$r->route('/image/:year/:month/:day/:id')->to("image#image");
+ $r->route('/oembed/lookup')->to('oembed#lookup');
+
$r->route('/thousand')->to("image#thousand");
$r->route('/atom')->to('atom#atom');
@@ -0,0 +1,24 @@
+package Coxify::Controller::Oembed;
+
+use strict;
+
+use base 'Mojolicious::Controller';
+
+use URI::Escape;
+
+sub lookup {
+ my ($self) = @_;
+
+ my $result;
+ if ($self->param('url')) {
+ $result = $self->oembed(uri_unescape($self->param('url')));
+ }
+
+ if ($result) {
+ $self->render(json => { "success" => "true", "url" => $result });
+ } else {
+ $self->render(json => { "success" => "false" });
+ }
+}
+
+1;
@@ -0,0 +1,37 @@
+package Mojolicious::Plugin::Oembed;
+use Mojo::Base 'Mojolicious::Plugin';
+
+use URI::Escape;
+use Mojo::UserAgent;
+
+our $VERSION = '0.01';
+
+sub register {
+ my ($self, $app, $endpoints) = @_;
+
+ $self->{endpoints} = $endpoints;
+
+ $app->helper(oembed => sub {
+ my ($app, $url) = @_;
+
+ my $endpoint;
+ for my $hsh (@{ $self->{endpoints} }) {
+ my $scheme = $hsh->{scheme};
+ $scheme =~ s/\*/.*?/;
+ $endpoint = $hsh->{endpoint};
+ last;
+ }
+
+ return undef unless $endpoint;
+
+ my $location = "${endpoint}?url=" . uri_escape($url);
+
+ my $ret;
+ my $ua = Mojo::UserAgent->new;
+ $ua->get($location)->res->dom('url')->each(sub { $ret = $_->text; });
+
+ return $ret;
+ });
+}
+
+1;

0 comments on commit bd2b3bd

Please sign in to comment.