ohembedr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/LICENSE +22 -0
  2. data/lib/ohembedr.rb +184 -0
  3. metadata +56 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 Ben McRedmond
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/lib/ohembedr.rb ADDED
@@ -0,0 +1,184 @@
1
+ # OhEmbedr is an easy to use ruby library for using OEmbed services
2
+ # in your applications. See http://oembed.com for information on
3
+ # the OEmbed standard. This library supports the following services
4
+ # by default:
5
+ # * YouTube
6
+ # * Vimeo
7
+ # * Flickr
8
+ # * Qik
9
+ # * Revision 3
10
+ # * Viddler
11
+ # * Hulu
12
+ #
13
+ # See the OhEmbedr class page for usage details.
14
+ #
15
+ # *Author*:: Ben McRedmond
16
+ # *Copyright*:: Copyright (c) 2009 Ben McRedmond
17
+ # *License*:: Licensed under the MIT license
18
+
19
+ require 'rubygems'
20
+ require 'net/http'
21
+ require 'cgi'
22
+
23
+ module OhEmbedr
24
+ # Standard error raised for errors with OhEmbedr
25
+ class Error < RuntimeError; end
26
+
27
+ # This error is raised when OhEmbedr comes across something that
28
+ # either it or the specified provider doesn't support.
29
+ class UnsupportedError < Error; end
30
+
31
+ # == Using OhEmbedr
32
+ # Using OhEmbedr is super simple. Below is an example
33
+ # begin
34
+ # o = OhEmbedr::OhEmbedr.new(:url => "http://vimeo.com/6382511", :maxwidth => 600)
35
+ # embed_data = o.gets
36
+ # rescue OhEmbedr::UnsupportedError => error
37
+ # # URL not supported
38
+ # end
39
+ #
40
+ # Wrapping in a <em>begin rescue</em> block allows us to pass any url to OhEmbedr if an UnsupportedError exception is raised then we know the url is not supported and can continue
41
+ class OhEmbedr
42
+ # The providers supported by default in this class
43
+ @@default_providers = {"youtube.com" => {:base => "http://www.youtube.com/oembed", :dot_format => false},
44
+ "vimeo.com" => {:base => "http://vimeo.com/api/oembed", :dot_format => true },
45
+ "flickr.com" => {:base => "http://www.flickr.com/services/oembed", :dot_format => false},
46
+ "qik.com" => {:base => "http://qik.com/api/oembed", :dot_format => true },
47
+ "revision3.com" => {:base => "http://revision3.com/api/oembed", :dot_format => false},
48
+ "viddler.com" => {:base => "http://lab.viddler.com/services/oembed", :dot_format => false},
49
+ "hulu.com" => {:base => "http://www.hulu.com/api/oembed", :dot_format => true}}
50
+
51
+ # A mapping of supported formats to the methods which parse them
52
+ @@formats = {"xml" => {:require => "xmlsimple", :oembed_parser => "parse_xml_oembed" },
53
+ "json" => {:require => "json", :oembed_parser => "parse_json_oembed"}}
54
+
55
+ # Format we will try first if none is specified
56
+ @@default_format = 'json'
57
+
58
+ # Options can be any of the following:
59
+ # * <tt>:url</tt> - The url we want to embed, or attempt to embed (required)
60
+ # * <tt>:providers</tt> - A hash of providers, if not provided the default list is used. Must be in the format. <tt>:base</tt> is the base url for the oembed service provider and <tt>:dot_format</tt> is true if the format is specified like <tt>http://example.com/api/oembed.format</tt> opposed to a GET paramater:
61
+ #
62
+ # {"example.com" => {:base => "http://example.com/api/oembed", :dot_format => true}}
63
+ #
64
+ # * <tt>:format</tt> - The format you want to make the request in, the default is json. You may also use xml. If you're using json you need to have the json gem installed and you need the xml-simple gem for xml.
65
+ # * Any other items in the options hash are appended to the oembed url as GET paramaters. Can be used to specify maxwidth, maxheight, etc.
66
+ def initialize(options)
67
+ raise ArgumentError, "No url provided in options hash" if options[:url].nil?
68
+
69
+ if !options[:format].nil?
70
+ load_format(options[:format])
71
+ else
72
+ load_format
73
+ end
74
+
75
+ @providers = options[:providers] || @@default_providers
76
+
77
+ # Split the url up and check it's an ok protocol
78
+ broken_url = options[:url].split("/")
79
+ raise UnsupportedError, "Unspported protocol" if broken_url.count == 1 || (broken_url[0] != "http:" && broken_url[0] != "https:")
80
+
81
+ @domain = broken_url[2].sub("www.", "")
82
+ raise UnsupportedError, "Unsupported provider" if @providers[@domain] == nil
83
+
84
+ @url = options[:url]
85
+
86
+ # Delete the options we used, the rest get passed to oembed provider
87
+ options.delete(:url)
88
+ options.delete(:providers) unless options[:providers].nil?
89
+ options.delete(:format) unless options[:format].nil?
90
+
91
+ @url_params = options
92
+ @request_url = url_for_request
93
+ @hash = {}
94
+ end
95
+
96
+ attr_reader :url, :request_url, :hash, :format
97
+
98
+ # Calling +gets+ returns a hash containing the oembed data.
99
+ def gets
100
+ begin
101
+ data = make_http_request
102
+ @hash = send(@@formats[@format][:oembed_parser], data) # Call the method specified in default_formats hash above
103
+ rescue RuntimeError => e
104
+ if e.message == "401"
105
+ # Embedding disabled
106
+ return nil
107
+ elsif e.message == "501"
108
+ # Format not supported
109
+ raise UnsupportedError, "#{@format} not supported by #{@domain}"
110
+ elsif e.message == "404"
111
+ # Not found
112
+ raise Error, "#{@url} not found"
113
+ end
114
+ end
115
+ end
116
+
117
+ private
118
+ def url_for_request
119
+ base = @providers[@domain][:base]
120
+ if @providers[@domain][:dot_format]
121
+ url = "#{base}.#{@format}?url=#{CGI::escape(@url)}"
122
+ else
123
+ url = "#{base}?url=#{CGI::escape(@url)}&format=#{@format}"
124
+ end
125
+
126
+ unless @url_params.empty?
127
+ @url_params.each do |key,value|
128
+ url += "&#{CGI::escape(key.to_s)}=#{CGI::escape(value.to_s)}"
129
+ end
130
+ end
131
+
132
+ url
133
+ end
134
+
135
+ def make_http_request
136
+ response = Net::HTTP.get_response(URI.parse(@request_url))
137
+ raise response.code if response.code != "200"
138
+ return response.body
139
+ end
140
+
141
+ def parse_json_oembed data
142
+ JSON.parse(data)
143
+ end
144
+
145
+ def parse_xml_oembed data
146
+ XmlSimple.xml_in(data, 'ForceArray' => false)
147
+ end
148
+
149
+ # Call with no argument to use default
150
+ def load_format requested_format = nil
151
+ raise ArgumentError, "Requested format not supported" if !requested_format.nil? && @@formats[requested_format].nil?
152
+ @format = requested_format || @@default_format
153
+ attempted_formats = ""
154
+
155
+ begin
156
+ attempted_formats += @@formats[@format][:require]
157
+ require @@formats[@format][:require]
158
+ return true
159
+ rescue LoadError
160
+ # If the user requested the format and it failed. Just give up!
161
+ raise Error "Please install: '#{@@formats[@format][:require]}' to use #{@format} format." unless requested_format.nil?
162
+
163
+ # Otherwise lets exhaust all our other options first
164
+ available_formats = @@formats
165
+ available_formats.delete(@format)
166
+
167
+ available_formats.each_key do |format|
168
+ begin
169
+ attempted_formats += ", "
170
+ require @@formats[format][:require]
171
+
172
+ @format = format
173
+ return true
174
+ rescue LoadError
175
+ attempted_formats += @@formats[format][:require]
176
+ end
177
+ end
178
+
179
+ raise Error, "Could not find any suitable library to parse response with, tried: #{attempted_formats}. Please install one of these to use OEmbed."
180
+ end
181
+ end
182
+
183
+ end
184
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ohembedr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben McRedmond
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-17 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: OhEmbedr is a super simple ruby OEmbed library.
17
+ email: ben+ohembedr@benmcredmond.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - LICENSE
26
+ - lib/ohembedr.rb
27
+ has_rdoc: true
28
+ homepage: http://github.com/benofsky/ohembedr/
29
+ licenses: []
30
+
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.3.5
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: OhEmbedr is a super simple ruby OEmbed library.
55
+ test_files: []
56
+