embedrb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module EmbedRb
5
+ module_function
6
+
7
+ def deep_merge(h1, h2)
8
+ result = h1.dup
9
+
10
+ result.each {|k, v|
11
+ if v.kind_of? Hash
12
+ result[k] = v.dup
13
+ end
14
+ }
15
+
16
+ h2.each {|k, v|
17
+ if v.kind_of? Hash
18
+ result[k] = deep_merge(result[k], v)
19
+ else
20
+ result[k] = v
21
+ end
22
+ }
23
+
24
+ result
25
+ end
26
+
27
+ def option(options, key)
28
+ options[key] ? options[key] : {}
29
+ end
30
+
31
+ def create_text(str, embeds)
32
+ # TODO: sort embeds
33
+ embeds_str = embeds.map {|embed|
34
+ embed[:text]
35
+ }.join("\n")
36
+ str + "\n" + embeds_str
37
+ end
38
+
39
+ def to_url(input)
40
+ input.include?('//') ? input : ('//' + input)
41
+ end
42
+
43
+ def get_dimensions(options)
44
+ width = options[:embedWidth]
45
+ height = options[:embedHeight]
46
+
47
+ if width && height
48
+ return {:width => width, :height => height}
49
+ elsif height
50
+ width = (height / 3) * 4
51
+ return {:width => width, :height => height}
52
+ elsif width
53
+ height = (width / 4) * 3
54
+ return {:width => width, :height => height}
55
+ else
56
+ return {:width => 800, :height => 600}
57
+ end
58
+ end
59
+
60
+ # s = "hoge http://www.youtubue.com/watch?a=b done https://hoge.fug/d"
61
+ # print s.scan(EmbedUtils.url_regex())
62
+ def url_regex()
63
+ /((href|src)=["']|)(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])|(?:https?:\/\/)?(?:(?:0rz\.tw)|(?:1link\.in)|(?:1url\.com)|(?:2\.gp)|(?:2big\.at)|(?:2tu\.us)|(?:3\.ly)|(?:307\.to)|(?:4ms\.me)|(?:4sq\.com)|(?:4url\.cc)|(?:6url\.com)|(?:7\.ly)|(?:a\.gg)|(?:a\.nf)|(?:aa\.cx)|(?:abcurl\.net)|(?:ad\.vu)|(?:adf\.ly)|(?:adjix\.com)|(?:afx\.cc)|(?:all\.fuseurl.com)|(?:alturl\.com)|(?:amzn\.to)|(?:ar\.gy)|(?:arst\.ch)|(?:atu\.ca)|(?:azc\.cc)|(?:b23\.ru)|(?:b2l\.me)|(?:bacn\.me)|(?:bcool\.bz)|(?:binged\.it)|(?:bit\.ly)|(?:buff\.ly)|(?:bizj\.us)|(?:bloat\.me)|(?:bravo\.ly)|(?:bsa\.ly)|(?:budurl\.com)|(?:canurl\.com)|(?:chilp\.it)|(?:chzb\.gr)|(?:cl\.lk)|(?:cl\.ly)|(?:clck\.ru)|(?:cli\.gs)|(?:cliccami\.info)|(?:clickthru\.ca)|(?:clop\.in)|(?:conta\.cc)|(?:cort\.as)|(?:cot\.ag)|(?:crks\.me)|(?:ctvr\.us)|(?:cutt\.us)|(?:dai\.ly)|(?:decenturl\.com)|(?:dfl8\.me)|(?:digbig\.com)|(?:digg\.com)|(?:disq\.us)|(?:dld\.bz)|(?:dlvr\.it)|(?:do\.my)|(?:doiop\.com)|(?:dopen\.us)|(?:easyuri\.com)|(?:easyurl\.net)|(?:eepurl\.com)|(?:eweri\.com)|(?:fa\.by)|(?:fav\.me)|(?:fb\.me)|(?:fbshare\.me)|(?:ff\.im)|(?:fff\.to)|(?:fire\.to)|(?:firsturl\.de)|(?:firsturl\.net)|(?:flic\.kr)|(?:flq\.us)|(?:fly2\.ws)|(?:fon\.gs)|(?:freak\.to)|(?:fuseurl\.com)|(?:fuzzy\.to)|(?:fwd4\.me)|(?:fwib\.net)|(?:g\.ro.lt)|(?:gizmo\.do)|(?:gl\.am)|(?:go\.9nl.com)|(?:go\.ign.com)|(?:go\.usa.gov)|(?:goo\.gl)|(?:goshrink\.com)|(?:gurl\.es)|(?:hex\.io)|(?:hiderefer\.com)|(?:hmm\.ph)|(?:href\.in)|(?:hsblinks\.com)|(?:htxt\.it)|(?:huff\.to)|(?:hulu\.com)|(?:hurl\.me)|(?:hurl\.ws)|(?:icanhaz\.com)|(?:idek\.net)|(?:ilix\.in)|(?:is\.gd)|(?:its\.my)|(?:ix\.lt)|(?:j\.mp)|(?:jijr\.com)|(?:kl\.am)|(?:klck\.me)|(?:korta\.nu)|(?:krunchd\.com)|(?:l9k\.net)|(?:lat\.ms)|(?:liip\.to)|(?:liltext\.com)|(?:linkbee\.com)|(?:linkbun\.ch)|(?:liurl\.cn)|(?:ln-s\.net)|(?:ln-s\.ru)|(?:lnk\.gd)|(?:lnk\.ms)|(?:lnkd\.in)|(?:lnkurl\.com)|(?:lru\.jp)|(?:lt\.tl)|(?:lurl\.no)|(?:macte\.ch)|(?:mash\.to)|(?:merky\.de)|(?:migre\.me)|(?:miniurl\.com)|(?:minurl\.fr)|(?:mke\.me)|(?:moby\.to)|(?:moourl\.com)|(?:mrte\.ch)|(?:myloc\.me)|(?:myurl\.in)|(?:n\.pr)|(?:nbc\.co)|(?:nblo\.gs)|(?:nn\.nf)|(?:not\.my)|(?:notlong\.com)|(?:nsfw\.in)|(?:nutshellurl\.com)|(?:nxy\.in)|(?:nyti\.ms)|(?:o-x\.fr)|(?:oc1\.us)|(?:om\.ly)|(?:omf\.gd)|(?:omoikane\.net)|(?:on\.cnn.com)|(?:on\.mktw.net)|(?:onforb\.es)|(?:orz\.se)|(?:ow\.ly)|(?:ping\.fm)|(?:pli\.gs)|(?:pnt\.me)|(?:politi\.co)|(?:post\.ly)|(?:pp\.gg)|(?:profile\.to)|(?:ptiturl\.com)|(?:pub\.vitrue.com)|(?:qlnk\.net)|(?:qte\.me)|(?:qu\.tc)|(?:qy\.fi)|(?:r\.im)|(?:rb6\.me)|(?:read\.bi)|(?:readthis\.ca)|(?:reallytinyurl\.com)|(?:redir\.ec)|(?:redirects\.ca)|(?:redirx\.com)|(?:retwt\.me)|(?:ri\.ms)|(?:rickroll\.it)|(?:riz\.gd)|(?:rt\.nu)|(?:ru\.ly)|(?:rubyurl\.com)|(?:rurl\.org)|(?:rww\.tw)|(?:s4c\.in)|(?:s7y\.us)|(?:safe\.mn)|(?:sameurl\.com)|(?:sdut\.us)|(?:shar\.es)|(?:shink\.de)|(?:shorl\.com)|(?:short\.ie)|(?:short\.to)|(?:shortlinks\.co.uk)|(?:shorturl\.com)|(?:shout\.to)|(?:show\.my)|(?:shrinkify\.com)|(?:shrinkr\.com)|(?:shrt\.fr)|(?:shrt\.st)|(?:shrten\.com)|(?:shrunkin\.com)|(?:simurl\.com)|(?:slate\.me)|(?:smallr\.com)|(?:smsh\.me)|(?:smurl\.name)|(?:sn\.im)|(?:snipr\.com)|(?:snipurl\.com)|(?:snurl\.com)|(?:sp2\.ro)|(?:spedr\.com)|(?:srnk\.net)|(?:srs\.li)|(?:starturl\.com)|(?:su\.pr)|(?:surl\.co.uk)|(?:surl\.hu)|(?:t\.cn)|(?:t\.co)|(?:t\.lh.com)|(?:ta\.gd)|(?:tbd\.ly)|(?:tcrn\.ch)|(?:tgr\.me)|(?:tgr\.ph)|(?:tighturl\.com)|(?:tiniuri\.com)|(?:tiny\.cc)|(?:tiny\.ly)|(?:tiny\.pl)|(?:tinylink\.in)|(?:tinyuri\.ca)|(?:tinyurl\.com)|(?:tl\.gd)|(?:tmi\.me)|(?:tnij\.org)|(?:tnw\.to)|(?:tny\.com)|(?:to\.ly)|(?:togoto\.us)|(?:totc\.us)|(?:toysr\.us)|(?:tpm\.ly)|(?:tr\.im)|(?:tra\.kz)|(?:trunc\.it)|(?:twhub\.com)|(?:twirl\.at)|(?:twitclicks\.com)|(?:twitterurl\.net)|(?:twitterurl\.org)|(?:twiturl\.de)|(?:twurl\.cc)|(?:twurl\.nl)|(?:u\.mavrev.com)|(?:u\.nu)|(?:u76\.org)|(?:ub0\.cc)|(?:ulu\.lu)|(?:updating\.me)|(?:ur1\.ca)|(?:url\.az)|(?:url\.co.uk)|(?:url\.ie)|(?:url360\.me)|(?:url4\.eu)|(?:urlborg\.com)|(?:urlbrief\.com)|(?:urlcover\.com)|(?:urlcut\.com)|(?:urlenco\.de)|(?:urli\.nl)|(?:urls\.im)|(?:urlshorteningservicefortwitter\.com)|(?:urlx\.ie)|(?:urlzen\.com)|(?:usat\.ly)|(?:use\.my)|(?:vb\.ly)|(?:vgn\.am)|(?:vl\.am)|(?:vm\.lc)|(?:w55\.de)|(?:wapo\.st)|(?:wapurl\.co.uk)|(?:wipi\.es)|(?:wp\.me)|(?:x\.vu)|(?:xr\.com)|(?:xrl\.in)|(?:xrl\.us)|(?:xurl\.es)|(?:xurl\.jp)|(?:y\.ahoo.it)|(?:yatuc\.com)|(?:ye\.pe)|(?:yep\.it)|(?:yfrog\.com)|(?:yhoo\.it)|(?:yiyd\.com)|(?:youtu\.be)|(?:yuarel\.com)|(?:z0p\.de)|(?:zi\.ma)|(?:zi\.mu)|(?:zipmyurl\.com)|(?:zud\.me)|(?:zurl\.ws)|(?:zz\.gd)|(?:zzang\.kr)|(?:›\.ws)|(?:✩\.ws)|(?:✿\.ws)|(?:❥\.ws)|(?:➔\.ws)|(?:➞\.ws)|(?:➡\.ws)|(?:➨\.ws)|(?:➯\.ws)|(?:➹\.ws)|(?:➽\.ws))\/[a-z0-9]*/im;
64
+ end
65
+
66
+ def get_response(url)
67
+ response = Net::HTTP.get_response URI.parse(url)
68
+ response.body
69
+ end
70
+ end
@@ -0,0 +1,4 @@
1
+
2
+ module EmbedRb
3
+ VERSION="0.1.0"
4
+ end
@@ -0,0 +1,31 @@
1
+ require_relative '../utils'
2
+ require_relative '../base.rb'
3
+
4
+ module EmbedRb
5
+ class Ted
6
+ include EmbedRb::Base
7
+
8
+ def initialize(input, output, options, embeds)
9
+ @input = input
10
+ @output = output
11
+ @embeds = embeds
12
+ @options = options
13
+ @regex = /(ted.com\/talks\/([a-zA-Z0-9_]+))/mi
14
+ end
15
+
16
+ private
17
+ def url_to_text(matched)
18
+ _id = matched[1]
19
+ return render _id
20
+ end
21
+
22
+ def render(_id)
23
+ dims = EmbedRb.get_dimensions(@options)
24
+ return <<EOF
25
+ <div class="ejs-embed ejs-ted">
26
+ <iframe src="http://embed.ted.com/talks/#{_id}.html" height="#{dims[:height]}" width="#{dims[:width]}" />
27
+ </div>
28
+ EOF
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../utils'
2
+ require_relative '../base.rb'
3
+
4
+ module EmbedRb
5
+ class Ustream
6
+ include EmbedRb::Base
7
+
8
+ def initialize(input, output, options, embeds)
9
+ @input = input
10
+ @output = output
11
+ @embeds = embeds
12
+ @options = options
13
+ @regex = /(ustream.tv\/([a-z\/0-9%]*))/mi
14
+ end
15
+
16
+ private
17
+ def render(url)
18
+ dims = EmbedRb.get_dimensions(@options)
19
+ return <<EOF
20
+ <div class="ejs-embed ejs-ustream">
21
+ <iframe src="//www.#{url}" height="#{dims[:height]}" width="#{dims[:width]} />
22
+ </div>
23
+ EOF
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ require_relative '../utils'
2
+ require_relative '../base.rb'
3
+
4
+ module EmbedRb
5
+ class BasicVideo
6
+ include EmbedRb::Base
7
+
8
+ def initialize(input, output, options, embeds)
9
+ @input = input
10
+ @output = output
11
+ @embeds = embeds
12
+ @options = options
13
+ @regex = /((?:https?):\/\/\S*\.(?:ogv|webm|mp4))/mi
14
+ end
15
+
16
+ private
17
+ def render(url)
18
+ return <<EOF
19
+ <div class="ejs-video ejs-embed">
20
+ <div class="ejs-video-player">
21
+ <div class="ejs-player">
22
+ <video src="#{url}" class="ejs-video-js video-js" controls></video>
23
+ </div>
24
+ </div>
25
+ </div>
26
+ EOF
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ require_relative '../utils'
2
+ require_relative '../base.rb'
3
+
4
+ module EmbedRb
5
+ class YouTube
6
+ include EmbedRb::Base
7
+
8
+ def initialize(input, output, options, embeds)
9
+ @input = input
10
+ @output = output
11
+ @embeds = embeds
12
+ @options = options
13
+ @regex = /(https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[?=&+%\w-]*)/mi
14
+ end
15
+
16
+ private
17
+ def url_to_text(matched)
18
+ url = "https://www.youtube.com/embed/#{matched[1]}"
19
+ return render url
20
+ end
21
+
22
+ def render(url)
23
+ dims = EmbedRb.get_dimensions(@options)
24
+ return <<EOF
25
+ <div class="ejs-video-player ejs-embed">
26
+ <iframe src="#{url}" frameBorder="0" width="#{dims[:width]}" height="#{dims[:height]}" />
27
+ </div>
28
+ EOF
29
+ end
30
+ end
31
+ end
data/lib/embedrb.rb ADDED
@@ -0,0 +1,35 @@
1
+ $LOAD_PATH << File.dirname(__FILE__) unless $LOAD_PATH.include?(File.dirname(__FILE__))
2
+ require 'embedrb/embed'
3
+
4
+ if $0 == __FILE__
5
+ input = <<EOF
6
+ hoge https://www.youtube.com/watch?v=wF_3Rp8oe1M done
7
+ http://www.google.com/
8
+ the following is geocode
9
+ @(東京駅)
10
+ http://hoge.fuga.com/xxx/yyy.png
11
+ https://www.flickr.com/photos/parismadrid/5573484679/in/photolist-9uvyPr-7TD5uZ-jYwbo8-qeZLRe-odzeYC-n8x6Xn-dzq2Ws-oRNMGp-68DxGi-c4DxV7-bnmwjN-nqZDVD-kF39se-rXJWT4-nTTR4S-7YLgdX-apPuvi-q1DhYc-kFWFjG-a2c9kr-aPGmhK-oqB5cQ-ziBGRr-oBkGxp-pEFgd8-jY9diK-mDTbpD-jzSaXQ-cuCWwN-pzK88U-9HvHVt-dBPTZV-nKdYRM-oEDGkL-bTygzn-o3CxjL-rhtanH-oXpW1i-s4Jdat-p8N1RJ-fhpghZ-dX2tPC-v8R2LX-x7M7j1-xFACNe-cAWLSf-A6FvqE-adcNyU-t2N6Hh-j1TaxV
12
+
13
+ https://www.instagram.com/p/BBmsYJesuYH/?tagged=beach
14
+ https://hoge.fuga.com/video.mp4
15
+
16
+ https://ted.com/talks/sometalk
17
+ http://www.ustream.tv/channel/%E3%81%AC%E3%81%93%E3%81%AE%E3%81%93
18
+ http://www.ustream.tv/embed/5832768?html5ui
19
+
20
+ http://www.example.com/a.mp3
21
+
22
+ ---------------------
23
+ EOF
24
+
25
+ opts = {
26
+ :openGraphEndpoint => 'http://opengraph.io/api/1.0/site/',
27
+ :gmap => true,
28
+ :gmapOptions => {
29
+ :googleAuthKey => ENV['GOOGLE_AUTH_KEY'],
30
+ :mode => 'streetview'
31
+ },
32
+ }
33
+ embed = EmbedRb::Embed.new(opts, input)
34
+ print(embed.process)
35
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'simplecov'
2
+
3
+ module SimpleCov::Configuration
4
+ def clean_filters
5
+ @filters = []
6
+ end
7
+ end
8
+
9
+ SimpleCov.configure do
10
+ clean_filters
11
+ load_adapter 'test_frameworks'
12
+ end
13
+
14
+ ENV["COVERAGE"] && SimpleCov.start do
15
+ add_filter "/.rvm/"
16
+ end
17
+ require 'rubygems'
18
+ require 'bundler'
19
+ begin
20
+ Bundler.setup(:default, :development)
21
+ rescue Bundler::BundlerError => e
22
+ $stderr.puts e.message
23
+ $stderr.puts "Run `bundle install` to install missing gems"
24
+ exit e.status_code
25
+ end
26
+ require 'test/unit'
27
+ require 'shoulda'
28
+
29
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
30
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
31
+ require 'embedrb'
32
+
33
+ class Test::Unit::TestCase
34
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestEmbedrb < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: embedrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - liris
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shoulda
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jeweler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: a ruby port of Embed.js
84
+ email: liris.pp@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - LICENSE
89
+ - LICENSE.txt
90
+ - README.md
91
+ files:
92
+ - ".document"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - embedrb.gemspec
100
+ - lib/embedrb.rb
101
+ - lib/embedrb/audio/embed_audio.rb
102
+ - lib/embedrb/base.rb
103
+ - lib/embedrb/embed.rb
104
+ - lib/embedrb/embed_gmap.rb
105
+ - lib/embedrb/embed_open_graph.rb
106
+ - lib/embedrb/embed_url.rb
107
+ - lib/embedrb/image/embed_flickr.rb
108
+ - lib/embedrb/image/embed_images.rb
109
+ - lib/embedrb/image/embed_instagram.rb
110
+ - lib/embedrb/utils.rb
111
+ - lib/embedrb/version.rb
112
+ - lib/embedrb/video/embed_ted.rb
113
+ - lib/embedrb/video/embed_ustream.rb
114
+ - lib/embedrb/video/embed_video.rb
115
+ - lib/embedrb/video/embed_youtube.rb
116
+ - test/helper.rb
117
+ - test/test_embedrb.rb
118
+ homepage: http://github.com/liris/embedrb
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.4.5
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: a ruby port of Embed.js
142
+ test_files: []