The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Web::oEmbed - oEmbed consumer

SYNOPSIS

  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
  }

DESCRIPTION

Web::oEmbed is a module that implements oEmbed consumer.

METHODS

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.

METHODS in Web::oEmbed::Response

http_response
  $res = $response->http_response;

Returns an underlying HTTP::Response object.

matched_uri
  $uri = $response->matched_uri;

Returns the matched URL given to the original embed method.

type
version
title
author_name
author_url
provider_name
provider_url
cache_age
thumbnail_url
thumbnail_width
thumbnail_height

Returns the value of response parameters.

url, width, height

Returns the value of response parameters if response type is photo.

html, width, height

Returns the value of response parameters if response type is video or rich.

render
  $html = $response->render;

Returns the HTML that you can use to display the embedded object. This method is an alias to html accessor if there is one in the response (i.e. video or rich), or creates an A tag to represent photo or rich response.

TODO

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 :)

COPYRIGHT

Six Apart, Ltd. <cpan@sixapart.com>

AUTHOR

Tatsuhiko Miyagawa <miyagawa@cpan.org>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

http://www.oembed.com/, JSON::XS, XML::LibXML::Simple