Class: OEmbed::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/oembed_links/response.rb

Instance Method Summary collapse

Constructor Details

#initialize(provider, url, response_object) ⇒ Response

Returns a new instance of Response.



7
8
9
10
11
12
13
# File 'lib/oembed_links/response.rb', line 7

def initialize(provider, url, response_object)
  @provider = provider
  @url = url
  @response = response_object
  @rendered_via_provider = @rendered_via_regex = @rendered_via_type = false
  @rendered = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msym, *args, &block) ⇒ Object

Provides the mechanism to allow .audio?, .video? and other .type? checking methods. The value of the method name will be compared against the “type” field from the returned server data.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/oembed_links/response.rb', line 60

def method_missing(msym, *args, &block)
  mname = msym.to_s
  if mname[mname.size - 1, mname.size] == "?"
    ts = mname[0..mname.size - 2]
    if @response["type"] == ts
      if can_render_type?(:type)
        @rendered_via_type = true
        return render_content(*args, &block)
      end
    end
  else
    raise NoMethodError.new("No such method #{msym.to_s}", msym, *args)
  end
end

Instance Method Details

#any?(*args, &block) ⇒ Boolean

Lowest priority renderer, which will execute a block regardless of conditions so long as no content has yet been rendered for this response.

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/oembed_links/response.rb', line 50

def any?(*args, &block)
  if can_render_type?
    return render_content(*args, &block)
  end      
end

#from?(provider_name, *args, &block) ⇒ Boolean

Test if this response has been returned from the given provider_name.

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
# File 'lib/oembed_links/response.rb', line 27

def from?(provider_name, *args, &block)
  if @provider.to_s === provider_name.to_s
    if can_render_type?(:provider)
      @rendered_via_provider = true
      return render_content(*args, &block)
    end
  end
end

#matches?(regex, *args, &block) ⇒ Boolean

Test if this response came from a URL that matches the given regex.

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/oembed_links/response.rb', line 38

def matches?(regex, *args, &block)
  if @url =~ regex
    if can_render_type?(:regex)
      @rendered_via_regex = true
      render_content(*args, &block)
    end
  end
end

#rendered_contentObject

If no content has been explicitly rendered for this Response, the default representation of the data will be returned.



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

def rendered_content
  @rendered || self.to_s
end

#to_sObject



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

def to_s
  @response["html"] || @response["url"]
end