class OEmbed::Response
@response.width #=> ‘500’
@response.field(‘width’) #=> ‘500’
@response.type #=> ‘rich’
For example:
or by using the appropriate automatically-defined helper method.
stored in Response instances can be accessed by either using the field method
Contains oEmbed data about a URL, as returned by an OEmbed::Provider. The data
def self.create_for(raw, provider, url, format)
which is data from the provider, about the url, in the given
Create a new Response instance of the correct type given raw
def self.create_for(raw, provider, url, format) fields = OEmbed::Formatter.decode(format, raw) resp_type = case fields['type'] when 'photo' then OEmbed::Response::Photo when 'video' then OEmbed::Response::Video when 'link' then OEmbed::Response::Link when 'rich' then OEmbed::Response::Rich else self end resp_type.new(fields, provider, url, format) end
def define_methods!
def define_methods! @fields.keys.each do |key| next if self.respond_to?(key) && !must_override.include?(key.to_s) class << self self end.send(:define_method, key) do field(key) end end end
def field(key)
# The following returns the value given by the Provider
@response.clone #=> #
For example, if the Provider returns a "clone" value of "true":
non-standard values that conflict with Ruby methods.
like Response#version, the field method is helpful if the Provider returns
The String value associated with this key. While you can use helper methods
def field(key) @fields[key.to_s].to_s end
def initialize(fields, provider, url=nil, format=nil)
def initialize(fields, provider, url=nil, format=nil) @fields = fields @provider = provider @request_url = url @format = format define_methods! end
def link?
def link? is_a?(OEmbed::Response::Link) end
def must_override
existing methods, so this Array is important if some other library has
when is's called. In general, define_methods! tries its best _not_ to override
An Array of helper methods names define_methods! must be able to override
def must_override %w{ type version title author_name author_url provider_name provider_url cache_age thumbnail_url thumbnail_width thumbnail_height } end
def photo?
def photo? is_a?(OEmbed::Response::Photo) end
def rich?
def rich? is_a?(OEmbed::Response::Rich) end
def video?
def video? is_a?(OEmbed::Response::Video) end