1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
module OEmbed
# Contains oEmbed data about a URL, as returned by an OEmbed::Provider. The data
# stored in Response instances can be accessed by either using the field method
# _or_ by using the appropriate automatically-defined helper method.
#
# For example:
# @response.type #=> 'rich'
# @response.field('width') #=> '500'
# @response.width #=> '500'
class Response
# An Hash of data (probably from a Provider) just as it was parsed.
attr_reader :fields
# The Provider instance that generated this Response
attr_reader :provider
# The URL that was sent to the provider, that this Response contains data about.
attr_reader :request_url
# The name of the format used get this data from the Provider (e.g. 'json').
attr_reader :format
# Create a new Response instance of the correct type given raw
# which is data from the provider, about the url, in the given
# format that needs to be decoded.
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 initialize(fields, provider, url=nil, format=nil)
@fields = fields
@provider = provider
@request_url = url
@format = format
define_methods!
end
# The String value associated with this key. While you can use helper methods
# like Response#version, the field method is helpful if the Provider returns
# non-standard values that conflict with Ruby methods.
#
# For example, if the Provider returns a "clone" value of "true":
# # The following calls the Object#clone method
# @response.clone #=> #<OEmbed::Response:...
#
# # The following returns the value given by the Provider
# @response.field(:clone) #=> 'true'
def field(key)
@fields[key.to_s].to_s
end
# Returns true if this is an oEmbed video response.
def video?
is_a?(OEmbed::Response::Video)
end
# Returns true if this is an oEmbed photo response.
def photo?
is_a?(OEmbed::Response::Photo)
end
# Returns true if this is an oEmbed link response.
def link?
is_a?(OEmbed::Response::Link)
end
# Returns true if this is an oEmbed rich response.
def rich?
is_a?(OEmbed::Response::Rich)
end
private
# An Array of helper methods names define_methods! must be able to override
# when is's called. In general, define_methods! tries its best _not_ to override
# existing methods, so this Array is important if some other library has
# defined a method that uses an oEmbed name. For example: Object#version
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 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
end
end
|