Class: OEmbed::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/oembed/response.rb,
lib/oembed/response/link.rb,
lib/oembed/response/rich.rb,
lib/oembed/response/photo.rb,
lib/oembed/response/video.rb

Overview

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'

Direct Known Subclasses

Link, Photo, Rich, Video

Defined Under Namespace

Classes: Link, Photo, Rich, Video

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields, provider, url = nil, format = nil) ⇒ Response

Returns a new instance of Response.



40
41
42
43
44
45
46
# File 'lib/oembed/response.rb', line 40

def initialize(fields, provider, url=nil, format=nil)
  @fields = fields
  @provider = provider
  @request_url = url
  @format = format
  define_methods!
end

Instance Attribute Details

#fieldsObject (readonly)

An Hash of data (probably from a Provider) just as it was parsed.



12
13
14
# File 'lib/oembed/response.rb', line 12

def fields
  @fields
end

#formatObject (readonly)

The name of the format used get this data from the Provider (e.g. ‘json’).



21
22
23
# File 'lib/oembed/response.rb', line 21

def format
  @format
end

#providerObject (readonly)

The Provider instance that generated this Response



15
16
17
# File 'lib/oembed/response.rb', line 15

def provider
  @provider
end

#request_urlObject (readonly)

The URL that was sent to the provider, that this Response contains data about.



18
19
20
# File 'lib/oembed/response.rb', line 18

def request_url
  @request_url
end

Class Method Details

.create_for(raw, provider, url, format) ⇒ Object

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.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/oembed/response.rb', line 26

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

Instance Method Details

#field(key) ⇒ Object

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'


58
59
60
# File 'lib/oembed/response.rb', line 58

def field(key)
  @fields[key.to_s].to_s
end

#link?Boolean

Returns true if this is an oEmbed link response.

Returns:

  • (Boolean)


73
74
75
# File 'lib/oembed/response.rb', line 73

def link?
  is_a?(OEmbed::Response::Link)
end

#photo?Boolean

Returns true if this is an oEmbed photo response.

Returns:

  • (Boolean)


68
69
70
# File 'lib/oembed/response.rb', line 68

def photo?
  is_a?(OEmbed::Response::Photo)
end

#rich?Boolean

Returns true if this is an oEmbed rich response.

Returns:

  • (Boolean)


78
79
80
# File 'lib/oembed/response.rb', line 78

def rich?
  is_a?(OEmbed::Response::Rich)
end

#video?Boolean

Returns true if this is an oEmbed video response.

Returns:

  • (Boolean)


63
64
65
# File 'lib/oembed/response.rb', line 63

def video?
  is_a?(OEmbed::Response::Video)
end