media_embedder 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # MediaEmbedder
2
+ `media_embedder` is a simple and easily extendible solution to parsing URLs in
3
+ a string and replacing them with the proper media embed code. For example, a
4
+ link to youtube would be converted to an HTML embed of that video.
5
+
6
+ MediaEmbedder comes with the following out of the box:
7
+ * Images (png, gif, jpg)
8
+ * Youtube (both short and long formats)
9
+ * Hyperlink (URLs that don't match any other processor are turned into HTML hyperlinks)
10
+
11
+ ## Installation
12
+ ```
13
+ gem install media_embedder
14
+ ```
15
+
16
+ or add the following line to your Gemfile
17
+ ```
18
+ gem 'media_embedder'
19
+ ```
20
+
21
+ ## Usage To use MediaEmbedder
22
+ In your project simply create a new instance of
23
+ `MediaEmbedder::Parser` and call the `parse_links` method on it.
24
+
25
+ ```ruby
26
+ text = 'Check out this awesome cat picture! http://image.com/cat.gif'
27
+ parser = MediaEmbedder::Parser.new(text)
28
+ parser.parse_links
29
+ #=> 'Check out this awesome cat picture! <img src="http://image.come/cat.gif" />'
30
+ ```
31
+
32
+ ## Extending with your own processors
33
+ MediaEmbedder was designed to be exendable and it is really easy to add your
34
+ own strategy for embedding a particular type of media. You need to create a
35
+ class that does the following:
36
+ * Registers itself with `MediaEmbedder::Parser` on load
37
+ * Implements the `can_process_url?` method which returns true or false
38
+ * Implements the `to_html` method which will return the desired HTML code to embed the media
39
+
40
+ Below is a very simple processor for images:
41
+ ```ruby
42
+ module MediaEmbedder
43
+ module Resource
44
+ class Image
45
+ Parser.register(self)
46
+
47
+ def initialize(url)
48
+ @url = URI.parse url
49
+ end
50
+
51
+ def can_process_url?
52
+ @url.path.end_with? '.png', '.gif', '.jpg'
53
+ end
54
+
55
+ def to_html
56
+ "<img src=\"#{@url}\" />"
57
+ end
58
+ end
59
+ end
60
+ end
61
+ ```
62
+
63
+ ## License
64
+ This gem is distributed under the MIT license
@@ -0,0 +1,16 @@
1
+ module MediaEmbedder
2
+ class HyperlinkResource
3
+
4
+ def initialize(url)
5
+ @url = url
6
+ end
7
+
8
+ def can_process_url?
9
+ true
10
+ end
11
+
12
+ def to_html
13
+ "<a href=\"#{@url}\">#{@url}</a>"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,49 @@
1
+ module MediaEmbedder
2
+ class Parser
3
+ @resources = []
4
+
5
+ def self.register(resource)
6
+ @resources << resource
7
+ end
8
+
9
+ def self.resources
10
+ @resources.dup
11
+ end
12
+
13
+ def initialize(text)
14
+ @text = text.dup
15
+ @resources = self.class.resources << MediaEmbedder::HyperlinkResource
16
+ end
17
+
18
+ def parse_links
19
+ links.each do |link|
20
+ html_snippet = convert_to_media(link)
21
+ replace_url_with_html_embed link, html_snippet
22
+ end
23
+ @text
24
+ end
25
+
26
+ def convert_to_media(link)
27
+ resource_class = find_resource_for(link)
28
+ resource_class.new(link).to_html
29
+ end
30
+
31
+ private
32
+
33
+ def links
34
+ @links ||= URI.extract @text
35
+ end
36
+
37
+ def replace_url_with_html_embed(link, html_snippet)
38
+ @text.gsub!(link, html_snippet)
39
+ end
40
+
41
+ def find_resource_for(link)
42
+ @resources.find do |resource_class|
43
+ resource_class.new(link).can_process_url?
44
+ end
45
+ end
46
+
47
+ # Dir[File.expand_path("app/models/resources/*.rb")].each { |file| require file }
48
+ end
49
+ end
@@ -0,0 +1,19 @@
1
+ module MediaEmbedder
2
+ module Resource
3
+ class Image
4
+ Parser.register(self)
5
+
6
+ def initialize(url)
7
+ @url = URI.parse url
8
+ end
9
+
10
+ def can_process_url?
11
+ @url.path.end_with? '.png', '.gif', '.jpg'
12
+ end
13
+
14
+ def to_html
15
+ "<img src=\"#{@url}\" />"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ module MediaEmbedder
2
+ module Resource
3
+ class YoutubeRegular
4
+ Parser.register(self)
5
+
6
+ def initialize(url)
7
+ @url = URI.parse url
8
+ end
9
+
10
+ def to_html
11
+ "<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/#{video_id}\" frameborder=\"0\" allowfullscreen></iframe>"
12
+ end
13
+
14
+ def can_process_url?
15
+ @url.host.include?("youtube") && @url.query && @url.query.include?('v=')
16
+ end
17
+
18
+ private
19
+
20
+ def video_id
21
+ @url.query[2..-1]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module MediaEmbedder
2
+ module Resource
3
+ class YoutubeShort
4
+ Parser.register(self)
5
+
6
+ def initialize(url)
7
+ @url = URI.parse url
8
+ end
9
+
10
+ def to_html
11
+ "<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/#{video_id}\" frameborder=\"0\" allowfullscreen></iframe>"
12
+ end
13
+
14
+ def can_process_url?
15
+ @url.host.include? "youtu.be"
16
+ end
17
+
18
+ private
19
+
20
+ def video_id
21
+ @url.path.gsub('/', '')
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module MediaEmbedder
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'uri'
2
+ require 'media_embedder/parser'
3
+ require 'media_embedder/hyperlink_resource'
4
+ Dir[File.expand_path("lib/media_embedder/resource/*.rb")].each { |file| require file }
5
+
6
+ module MediaEmbedder
7
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :media_embedder do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: media_embedder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joël Quenneville
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: flexible, extendable library for parsing media links
15
+ email:
16
+ - joel.quen@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/media_embedder/hyperlink_resource.rb
22
+ - lib/media_embedder/parser.rb
23
+ - lib/media_embedder/resource/image.rb
24
+ - lib/media_embedder/resource/youtube_regular.rb
25
+ - lib/media_embedder/resource/youtube_short.rb
26
+ - lib/media_embedder/version.rb
27
+ - lib/media_embedder.rb
28
+ - lib/tasks/media_embedder_tasks.rake
29
+ - MIT-LICENSE
30
+ - README.md
31
+ homepage:
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: flexible, extendable library for parsing media links
55
+ test_files: []