Class: OEmbed::Provider
- Inherits:
-
Object
- Object
- OEmbed::Provider
- Defined in:
- lib/oembed/provider.rb
Overview
An OEmbed::Provider has information about an individual oEmbed enpoint.
Instance Attribute Summary (collapse)
-
- (Object) endpoint
The String that is the http URI of the Provider's oEmbed endpoint.
-
- (Object) format
The name of the default format for all request to this Provider (e.g. 'json').
-
- (Object) name
deprecated
Deprecated.
Note: This accessor currently isn't used anywhere in the codebase.
-
- (Object) url
deprecated
Deprecated.
Note: Added in a fork of the gem, a while back. I really would like
-
- (Object) urls
An Array of all URL schemes supported by this Provider.
Instance Method Summary (collapse)
-
- (Object) <<(url)
Adds the given url scheme to this Provider instance.
-
- (Object) build(url, query = {})
deprecated
Deprecated.
Note: This method will be made private in the future.
-
- (Object) get(url, query = {})
Send a request to the Provider endpoint to get information about the given url and return the appropriate OEmbed::Response.
-
- (Boolean) include?(url)
Determine whether the given url is supported by this Provider by matching against the Provider's URL schemes.
-
- (Provider) initialize(endpoint, format = OEmbed::Formatter.default)
constructor
Construct a new OEmbed::Provider instance, pointing at a specific oEmbed endpoint.
-
- (Object) raw(url, query = {})
deprecated
Deprecated.
Note: This method will be made private in the future.
Constructor Details
- (Provider) initialize(endpoint, format = OEmbed::Formatter.default)
Construct a new OEmbed::Provider instance, pointing at a specific oEmbed endpoint.
The endpoint should be a String representing the http URI of the Provider's oEmbed endpoint. The endpoint String may also contain a format portion. In actual requests to this Provider, this string will be replaced with a String representing the request format (e.g. “json”).
If give, the format should be the name of the default format for all request to this Provider (e.g. 'json'). Defaults to OEmbed::Formatter.default
For example:
# If requests should be sent to:
# "http://my.service.com/oembed?format=#{OEmbed::Formatter.default}"
@provider = OEmbed::Provider.new("http://my.service.com/oembed")
# If requests should be sent to:
# "http://my.service.com/oembed.xml"
@xml_provider = OEmbed::Provider.new("http://my.service.com/oembed.{format}", :xml)
47 48 49 50 51 52 53 54 |
# File 'lib/oembed/provider.rb', line 47 def initialize(endpoint, format = OEmbed::Formatter.default) endpoint_uri = URI.parse(endpoint.gsub(/[\{\}]/,'')) rescue nil raise ArgumentError, "The given endpoint isn't a valid http(s) URI: #{endpoint.to_s}" unless endpoint_uri.is_a?(URI::HTTP) @endpoint = endpoint @urls = [] @format = format end |
Instance Attribute Details
- (Object) endpoint
The String that is the http URI of the Provider's oEmbed endpoint. This URL may also contain a {format} portion. In actual requests to this Provider, this string will be replaced with a string representing the request format (e.g. “json”).
10 11 12 |
# File 'lib/oembed/provider.rb', line 10 def endpoint @endpoint end |
- (Object) format
The name of the default format for all request to this Provider (e.g. 'json').
13 14 15 |
# File 'lib/oembed/provider.rb', line 13 def format @format end |
- (Object) name
Note: This accessor currently isn't used anywhere in the codebase.
The human-readable name of the Provider.
21 22 23 |
# File 'lib/oembed/provider.rb', line 21 def name @name end |
- (Object) url
Note: Added in a fork of the gem, a while back. I really would like
to get rid of it, though. –Marcos
25 26 27 |
# File 'lib/oembed/provider.rb', line 25 def url @url end |
- (Object) urls
An Array of all URL schemes supported by this Provider.
16 17 18 |
# File 'lib/oembed/provider.rb', line 16 def urls @urls end |
Instance Method Details
- (Object) <<(url)
Adds the given url scheme to this Provider instance. The url scheme can be either a String, containing wildcards specified with an asterisk, (see oembed.com/#section2.1 for details), or a Regexp.
For example:
@provider << "http://my.service.com/video/*"
@provider << "http://*.service.com/photo/*/slideshow"
@provider << %r{^http://my.service.com/((help)|(faq))/\d+[#\?].*}
65 66 67 68 69 70 71 72 73 |
# File 'lib/oembed/provider.rb', line 65 def <<(url) if !url.is_a?(Regexp) full, scheme, domain, path = *url.match(%r{([^:]*)://?([^/?]*)(.*)}) domain = Regexp.escape(domain).gsub("\\*", "(.*?)").gsub("(.*?)\\.", "([^\\.]+\\.)?") path = Regexp.escape(path).gsub("\\*", "(.*?)") url = Regexp.new("^#{Regexp.escape(scheme)}://#{domain}#{path}") end @urls << url end |
- (Object) build(url, query = {})
Note: This method will be made private in the future.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/oembed/provider.rb', line 95 def build(url, query = {}) raise OEmbed::NotFound, url unless include?(url) query = query.merge({:url => ::CGI.escape(url)}) # TODO: move this code exclusively into the get method, once build is private. this_format = (query[:format] ||= @format.to_s).to_s endpoint = @endpoint.clone if endpoint.include?("{format}") endpoint["{format}"] = this_format query.delete(:format) end base = endpoint.include?('?') ? '&' : '?' query = base + query.inject("") do |memo, (key, value)| "#{key}=#{value}&#{memo}" end.chop URI.parse(endpoint + query).instance_eval do @format = this_format def format @format end self end end |
- (Object) get(url, query = {})
Send a request to the Provider endpoint to get information about the given url and return the appropriate OEmbed::Response.
The query parameter should be a Hash of values which will be sent as query parameters in this request to the Provider endpoint. The following special cases apply to the query Hash:
:format |
overrides this Provider's default request format. |
:url |
will be ignored, replaced by the url param. |
83 84 85 86 |
# File 'lib/oembed/provider.rb', line 83 def get(url, query = {}) query[:format] ||= @format OEmbed::Response.create_for(raw(url, query), self, url, query[:format].to_s) end |
- (Boolean) include?(url)
Determine whether the given url is supported by this Provider by matching against the Provider's URL schemes.
90 91 92 |
# File 'lib/oembed/provider.rb', line 90 def include?(url) @urls.empty? || !!@urls.detect{ |u| u =~ url } end |
- (Object) raw(url, query = {})
Note: This method will be made private in the future.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/oembed/provider.rb', line 124 def raw(url, query = {}) uri = build(url, query) found = false max_redirects = 4 until found http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == 'https' http.verify_mode = OpenSSL::SSL::VERIFY_PEER %w{scheme userinfo host port registry}.each { |method| uri.send("#{method}=", nil) } res = http.request(Net::HTTP::Get.new(uri.to_s)) #res = Net::HTTP.start(uri.host, uri.port) {|http| http.get(uri.request_uri) } res.header['location'] ? uri = URI.parse(res.header['location']) : found = true if max_redirects == 0 found = true else max_redirects -= 1 end end case res when Net::HTTPNotImplemented raise OEmbed::UnknownFormat, format when Net::HTTPNotFound raise OEmbed::NotFound, url when Net::HTTPSuccess res.body else raise OEmbed::UnknownResponse, res && res.respond_to?(:code) ? res.code : 'Error' end rescue StandardError # Convert known errors into OEmbed::UnknownResponse for easy catching # up the line. This is important if given a URL that doesn't support # OEmbed. The following are known errors: # * Net::* errors like Net::HTTPBadResponse # * JSON::JSONError errors like JSON::ParserError if defined?(::JSON) && $!.is_a?(::JSON::JSONError) || $!.class.to_s =~ /\ANet::/ raise OEmbed::UnknownResponse, res && res.respond_to?(:code) ? res.code : 'Error' else raise $! end end |