Class: OEmbed::Response
- Inherits:
-
Object
- Object
- OEmbed::Response
- Defined in:
- lib/oembed_links/response.rb
Instance Method Summary collapse
-
#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.
-
#from?(provider_name, *args, &block) ⇒ Boolean
Test if this response has been returned from the given provider_name.
- #has_rendered? ⇒ Boolean
-
#initialize(provider, url, response_object) ⇒ Response
constructor
A new instance of Response.
-
#matches?(regex, *args, &block) ⇒ Boolean
Test if this response came from a URL that matches the given regex.
-
#method_missing(msym, *args, &block) ⇒ Object
Provides the mechanism to allow .audio?, .video? and other .type? checking methods.
-
#none?(*args, &block) ⇒ Boolean
Case where url has not matched at all.
-
#rendered_content ⇒ Object
If no content has been explicitly rendered for this Response, the default representation of the data will be returned.
- #to_s ⇒ Object
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.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/oembed_links/response.rb', line 68 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.
58 59 60 61 62 |
# File 'lib/oembed_links/response.rb', line 58 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.
35 36 37 38 39 40 41 42 |
# File 'lib/oembed_links/response.rb', line 35 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 |
#has_rendered? ⇒ Boolean
83 84 85 |
# File 'lib/oembed_links/response.rb', line 83 def has_rendered? !@rendered.nil? end |
#matches?(regex, *args, &block) ⇒ Boolean
Test if this response came from a URL that matches the given regex.
46 47 48 49 50 51 52 53 |
# File 'lib/oembed_links/response.rb', line 46 def matches?(regex, *args, &block) if @url =~ regex if can_render_type?(:regex) @rendered_via_regex = true render_content(*args, &block) end end end |
#none?(*args, &block) ⇒ Boolean
Case where url has not matched at all
27 28 29 30 31 |
# File 'lib/oembed_links/response.rb', line 27 def none?(*args, &block) if @response.keys.empty? && !has_rendered? return render_content(*args, &block) end end |
#rendered_content ⇒ Object
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_s ⇒ Object
15 16 17 |
# File 'lib/oembed_links/response.rb', line 15 def to_s @response["html"] || @response["url"] end |