Created
March 26, 2012 05:52
-
-
Save shanna/2203267 to your computer and use it in GitHub Desktop.
OEmbed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
var providers = {}; | |
/* | |
Given a URL find a provider that can OEmbed the url given. | |
*/ | |
function find (url) { | |
var site = url.match(/^https?:\/\/([^/]+)/)[1]; | |
for (var domain in providers) | |
if (site.indexOf(domain) > -1) | |
return providers[domain]; | |
} | |
/* | |
Register a provider. | |
@example | |
// Goliath JSONP gateway. | |
$.oembed.provider('youtube', ['youtube.com', 'youtu.be'], window.document.location.host + '/oembed'); | |
$.oembed.provider('yfrog', ['yfrog.com'], window.document.location.host + '/oembed'); | |
*/ | |
function provider (name, domains, endpoint) { | |
$.each(domains, function () { | |
providers[this] = {name: name, endpoint: endpoint}; | |
}); | |
} | |
/* | |
GET the OEmbed JSON for a given URL. | |
@note There is no error callback yet. | |
@example | |
$.oembed.get('http://vimeo.com/7100569', function (json) { | |
// oembed json. | |
}); | |
*/ | |
function get (url, success) { | |
var provider = find(url); | |
if (!provider) return; | |
// TODO: Chrome blocks http endpoints on https. | |
// TODO: Flickr uses non-standard jsoncallback instead of callback for JSONP. | |
$.getJSON('http://' + provider.endpoint + '?callback=?&format=json', {url: url}, success); | |
} | |
provider('instagram', ['instagr.am'], 'api.instagram.com/oembed'); | |
provider('vimeo', ['vimeo.com'], 'api/oembed.json'); | |
$.oembed = {get: get, provider: provider}; | |
})(jQuery); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'goliath' | |
require 'em-synchrony' | |
require 'em-synchrony/em-http' | |
require 'addressable/uri' | |
# Goliath API to wrap endpoints without jsonp so the JS the jquery lib may use them. | |
# | |
# Usually endpoints that don't provide jsonp do so because they have rate limiting and other restrictions. To make | |
# them usable client side this thin Goliath API wraps the responses in a jsonp callback. | |
# | |
# @note Some common domains are already registered. | |
# * yfrog.com | |
# * youtu.be | |
# * youtube.com | |
# | |
# @example | |
# Oembed.provider 'youtube', %w{youtu.be youtube.com}, 'http://www.youtube.com/oembed' | |
# Oembed.provider 'yfrog', %w{yfrog.com}, 'http://www.yfrog.com/api/oembed' | |
class Oembed < Goliath::API | |
# List all registered providers. | |
def self.providers | |
@providers ||= [] | |
end | |
# Register a provider. | |
# | |
# @example | |
# Oembed.provider 'youtube', %w{youtu.be youtube.com}, 'http://www.youtube.com/oembed' | |
# Oembed.provider 'yfrog', %w{yfrog.com}, 'http://www.yfrog.com/api/oembed' | |
# | |
#-- | |
# TODO: Don't add providers more than once. | |
def self.provider name, domains, endpoint | |
[domains].flatten.each{|domain| providers << [name, domain, endpoint]} | |
end | |
provider 'youtube', %w{youtu.be youtube.com}, 'http://www.youtube.com/oembed' | |
provider 'yfrog', %w{yfrog.com}, 'http://www.yfrog.com/api/oembed' | |
use Goliath::Rack::Params | |
use Goliath::Rack::JSONP | |
def response env | |
uri = Addressable::URI.parse(params['url']) | |
provider = self.class.providers.find{|provider| uri.host.include?(provider[1])} | |
return [404, {}, ''] unless provider | |
endpoint = Addressable::URI.parse(provider[2]) | |
endpoint.query = env['QUERY_STRING'] | |
# TODO: Chunked response streaming? | |
http = EventMachine::HttpRequest.new(endpoint).get(redirects: 4) | |
[http.response_header.status, {}, http.response] | |
end | |
end # Oembed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment