Web::oEmbed - oEmbed consumer
use Web::oEmbed;
my $consumer = Web::oEmbed->new({ format => 'json' });
$consumer->register_provider({
url => 'http://*.flickr.com/*',
api => 'http://www.flickr.com/services/oembed/',
});
my $response = eval { $consumer->embed("http://www.flickr.com/photos/bulknews/2752124387/") };
if ($response) {
$response->matched_uri; # 'http://www.flickr.com/photos/bulknews/2752124387/'
$response->type; # 'photo'
$response->title; # title of the photo
$response->url; # JPEG URL
$response->width; # JPEG width
$response->height; # JPEG height
$response->provider_name; # Flickr
$response->provider_url; # http://www.flickr.com/
print $response->render; # handy shortcut to generate <img/> tag
}
Web::oEmbed is a module that implements oEmbed consumer.
- new
-
$consumer = Web::oEmbed->new;
$consumer = Web::oEmbed->new({ format => 'json' });
Creates a new Web::oEmbed instance. You can specify the
default format that will be used when it's not specified in the
"embed" method.
- register_provider
-
$consumer->register_provider({
name => 'Flickr',
url => 'http://*.flickr.com/*',
api => 'http://www.flickr.com/services/oembed/,
});
Registers a new provider site.
"name" is optional while
"url" and
"api" are required. If you specify the
mangled "url" parameter like
'*://www.flickr.com/' it will die with an error. You can call this
method multiple times to add multiple oEmbed providers.
- embed
-
$response = $consumer->embed("http://www.example.com/");
$response = $consumer->embed( URI->new("http://photos.example.com/"), { format => 'xml' } );
Given an URL it will try to find the correspondent provider
based on their registered URL scheme, and then send the oEmbed request
to the oEmbed provider endpoint and parse the response. The method
returns an instance of Web::oEmbed::Response.
Returns undef if there's no provider found for the URL. Throws
an error if there's an error in JSON/XML parsing etc. (Note: I don't
like this interface because there's no cleaner way to handle and
diagnose errors. This might be changed.)
"format" optional parameter
specifies which "format" is sent to
the provider as a prefered format parameter. When omitted, the default
format parameter set in "new" is used.
If it's not speciied in "new" either,
the default will be "json".
NOT IMPLEMENTED YET: When optional parameter
"discovery" is set, the consumer will
issue the HTTP request to the original URL to discover the oEmbed
discovery tag described in the oEmbed spec chapter 4. If the oEmbed
discovery tag is found in the HTML, it will then issue the oEmbed
request against the provider.
Currently if you register 100 providers, the embed method
could potentially iterate through all of providers to run the regular
expression, which doesn't sound good. I guess we could come up with some
Trie-ish regexp solution that immediately returns the correspondent provider
by compiling all regular expressions into one.
Patches are welcome on this :)
Six Apart, Ltd. <cpan@sixapart.com>
Tatsuhiko Miyagawa <miyagawa@cpan.org>
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
<http://www.oembed.com/>, JSON::XS, XML::LibXML::Simple