embedrb 0.1.0

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/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # embedrb
2
+
3
+ embedrb is a ruby port of [EmbedJS](http://riteshkr.com/embed.js/)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'embedrb'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install embedrb
20
+
21
+
22
+ ## Simple Usage
23
+
24
+ ```ruby
25
+ input = <<EOF
26
+ I recomended this youtube video https://www.youtube.com/watch?v=wF_3Rp8oe1M
27
+ http://www.google.com/
28
+ the following is geocode
29
+ @(東京駅)
30
+ EOF
31
+ opts = {
32
+ # :openGraphEndpoint => 'http://opengraph.io/api/1.0/site/',
33
+ :gmap => true,
34
+ :gmapOptions => {
35
+ :googleAuthKey => 'google-access-key'
36
+ }
37
+ }
38
+ embed = EmbedRb::Embed.new(opts, input)
39
+ print(embed.process)
40
+ ```
41
+
42
+ ## Supported Service
43
+
44
+ - Supprted
45
+ - LINK
46
+ - OPENGRAPH
47
+ - MAP
48
+ - image
49
+ - BASICIMAGE
50
+ - FLICKR
51
+ - INSTAGRAM
52
+ - video
53
+ - BASICVIDEO
54
+ - TED
55
+ - YOUTUBE
56
+ - embed only(no detail description)
57
+ - USTREAM
58
+ - not working well
59
+ - audio
60
+ - BASICAUDIO
61
+ - Will Supported
62
+ - SMILEY
63
+ - HIGHLIGHTCODE
64
+ - IDEONE
65
+ - PLUNKER
66
+ - JSBIN
67
+ - CODEPEN
68
+ - JSFIDDLE
69
+ - GIST
70
+ - DAILYMOTION
71
+ - LIVELEAK
72
+ - VINE
73
+ - VIMEO
74
+ - GITHUB
75
+ - SOUNDCLOUD
76
+ - SPOTIFY
77
+ - SLIDESHARE
78
+
79
+ - Not Supported
80
+ - MARKDOWN
81
+ - EMOJI
82
+
83
+ ## Development
84
+
85
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
86
+
87
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/liris/embedrb.
92
+
93
+ == Copyright
94
+
95
+ Copyright (c) 2016 liris. See LICENSE.txt for
96
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ lib = File.expand_path('../lib', __FILE__)
15
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
16
+ require 'embedrb/version'
17
+ require 'jeweler'
18
+ Jeweler::Tasks.new do |gem|
19
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
20
+ gem.name = "embedrb"
21
+ gem.version = EmbedRb::VERSION
22
+ gem.homepage = "http://github.com/liris/embedrb"
23
+ gem.license = "MIT"
24
+ gem.summary = %Q{a ruby port of Embed.js}
25
+ gem.description = %Q{a ruby port of Embed.js}
26
+ gem.email = "liris.pp@gmail.com"
27
+ gem.authors = ["liris"]
28
+ # dependencies defined in Gemfile
29
+ end
30
+ Jeweler::RubygemsDotOrgTasks.new
31
+
32
+ require 'rake/testtask'
33
+ Rake::TestTask.new(:test) do |test|
34
+ test.libs << 'lib' << 'test'
35
+ test.pattern = 'test/**/test_*.rb'
36
+ test.verbose = true
37
+ end
38
+
39
+ desc "Code coverage detail"
40
+ task :simplecov do
41
+ ENV['COVERAGE'] = "true"
42
+ Rake::Task['test'].execute
43
+ end
44
+
45
+ task :default => :test
46
+
47
+ require 'rdoc/task'
48
+ Rake::RDocTask.new do |rdoc|
49
+ # version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+ version = EmbedRb::VERSION
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "embedrb #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/embedrb.gemspec ADDED
@@ -0,0 +1,79 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: embedrb 0.1.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "embedrb"
9
+ s.version = "0.1.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["liris"]
14
+ s.date = "2016-02-12"
15
+ s.description = "a ruby port of Embed.js"
16
+ s.email = "liris.pp@gmail.com"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "LICENSE.txt",
20
+ "README.md"
21
+ ]
22
+ s.files = [
23
+ ".document",
24
+ "Gemfile",
25
+ "Gemfile.lock",
26
+ "LICENSE",
27
+ "LICENSE.txt",
28
+ "README.md",
29
+ "Rakefile",
30
+ "embedrb.gemspec",
31
+ "lib/embedrb.rb",
32
+ "lib/embedrb/audio/embed_audio.rb",
33
+ "lib/embedrb/base.rb",
34
+ "lib/embedrb/embed.rb",
35
+ "lib/embedrb/embed_gmap.rb",
36
+ "lib/embedrb/embed_open_graph.rb",
37
+ "lib/embedrb/embed_url.rb",
38
+ "lib/embedrb/image/embed_flickr.rb",
39
+ "lib/embedrb/image/embed_images.rb",
40
+ "lib/embedrb/image/embed_instagram.rb",
41
+ "lib/embedrb/utils.rb",
42
+ "lib/embedrb/version.rb",
43
+ "lib/embedrb/video/embed_ted.rb",
44
+ "lib/embedrb/video/embed_ustream.rb",
45
+ "lib/embedrb/video/embed_video.rb",
46
+ "lib/embedrb/video/embed_youtube.rb",
47
+ "test/helper.rb",
48
+ "test/test_embedrb.rb"
49
+ ]
50
+ s.homepage = "http://github.com/liris/embedrb"
51
+ s.licenses = ["MIT"]
52
+ s.rubygems_version = "2.4.5"
53
+ s.summary = "a ruby port of Embed.js"
54
+
55
+ if s.respond_to? :specification_version then
56
+ s.specification_version = 4
57
+
58
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
59
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
60
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
61
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
62
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
63
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
64
+ else
65
+ s.add_dependency(%q<shoulda>, [">= 0"])
66
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
67
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
68
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
69
+ s.add_dependency(%q<simplecov>, [">= 0"])
70
+ end
71
+ else
72
+ s.add_dependency(%q<shoulda>, [">= 0"])
73
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
74
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
75
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
76
+ s.add_dependency(%q<simplecov>, [">= 0"])
77
+ end
78
+ end
79
+
@@ -0,0 +1,25 @@
1
+ require_relative '../utils'
2
+ require_relative '../base.rb'
3
+
4
+ module EmbedRb
5
+ class BasicAudio
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*\.(?:wav|mp3|ogg))/mi
14
+ end
15
+
16
+ private
17
+ def render(url)
18
+ return <<EOF
19
+ <div class="ejs-audio ejs-embed">
20
+ <audio src="#{url}" controls class="video-js ejs-video-js" />
21
+ </div>
22
+ EOF
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,34 @@
1
+
2
+ module EmbedRb
3
+ module Base
4
+ def process()
5
+ embed
6
+ @output
7
+ end
8
+
9
+ def embed()
10
+ @input.scan(@regex) {|match|
11
+ url = match[0]
12
+ if !@options[:served].include? url
13
+ text = url_to_text match
14
+ if text
15
+ @options[:served] << url
16
+ @embeds << {
17
+ :key => url,
18
+ :text => text
19
+ }
20
+ end
21
+ end
22
+ }
23
+ end
24
+
25
+ def url_to_text(matched)
26
+ url = matched[0]
27
+ return render url
28
+ end
29
+
30
+ def shorten(url)
31
+ url.gsub(/http(s?):\/\/(www\.|m\.|)/, '')
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,87 @@
1
+ require_relative 'embed_url'
2
+ require_relative 'embed_gmap'
3
+ require_relative 'embed_open_graph'
4
+ require_relative 'image/embed_images.rb'
5
+ require_relative 'image/embed_flickr.rb'
6
+ require_relative 'image/embed_instagram.rb'
7
+ require_relative 'video/embed_video'
8
+ require_relative 'video/embed_ted'
9
+ require_relative 'video/embed_ustream'
10
+ require_relative 'video/embed_youtube'
11
+ require_relative 'audio/embed_audio'
12
+ require_relative 'utils'
13
+
14
+ PROCESSORS = {
15
+ :link => EmbedRb::Url,
16
+ :gmap => EmbedRb::Gmap,
17
+ :video => EmbedRb::BasicVideo,
18
+ :youtube => EmbedRb::YouTube,
19
+ :ted => EmbedRb::Ted,
20
+ :ustream => EmbedRb::Ustream,
21
+ :image => EmbedRb::BasicImage,
22
+ :flickr => EmbedRb::Flickr,
23
+ :instagram => EmbedRb::Instagram,
24
+ :audio => EmbedRb::BasicAudio,
25
+ :openGraphEndpoint => EmbedRb::OpenGraph,
26
+ }
27
+
28
+ DEFAULT_OPTIONS = {
29
+ :link => true,
30
+ :linkOptions => {
31
+ :target => 'self',
32
+ :exclude => ['pdf'],
33
+ :rel => '',
34
+ },
35
+
36
+ :openGraphEndpoint => nil, # http://opengraph.io/api/1.0/site/${url_encoded_site_url}
37
+ :openGraphOptions => {
38
+ :apiKey => nil,
39
+ :excluded_regex => nil,
40
+ },
41
+
42
+ :gmap => false,
43
+ :gmapOptions => {
44
+ :mode => 'place',
45
+ :googleAuthKey => nil,
46
+ },
47
+
48
+ :video => true,
49
+
50
+ :youtube => true,
51
+ :youtubeOptions => {
52
+ },
53
+
54
+ :ted => true,
55
+ :ustream => false,
56
+
57
+ :image => true,
58
+ :flickr => true,
59
+ :instagram => true,
60
+
61
+ :audio => true,
62
+
63
+ }
64
+
65
+ module EmbedRb
66
+ class Embed
67
+ def initialize(options, input)
68
+ @options = EmbedRb.deep_merge(DEFAULT_OPTIONS, options)
69
+ @options[:served] = []
70
+ @input = input
71
+ end
72
+
73
+ def process()
74
+ input = @input
75
+ embeds = []
76
+ output = ''
77
+
78
+ PROCESSORS.each {|key, klass|
79
+ if @options[key]
80
+ output = klass.new(input, output, @options, embeds).process()
81
+ end
82
+ }
83
+
84
+ EmbedRb.create_text(output, embeds)
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,61 @@
1
+ require 'erb'
2
+ require 'json'
3
+ require_relative 'utils'
4
+
5
+ module EmbedRb
6
+ class Gmap
7
+ def initialize(input, output, options, embeds)
8
+ @input = input
9
+ @output = output
10
+ @embeds = embeds
11
+ @options = options
12
+ @regex = /@\((.+)\)/i
13
+ end
14
+
15
+ def process()
16
+ if @options[:gmapOptions][:googleAuthKey]
17
+ @input.scan(@regex) {|match|
18
+ location = match[0]
19
+ @embeds << {
20
+ key: match,
21
+ text: render(location)
22
+ }
23
+ }
24
+ end
25
+
26
+ @output
27
+ end
28
+
29
+ private
30
+ def get_coordinate(location)
31
+ url = "http://maps.googleapis.com/maps/api/geocode/json?address=#{ERB::Util.url_encode(location)}&sensor=false"
32
+ data = JSON.parse EmbedRb.get_response(url)
33
+ geometry = data["results"][0]["geometry"]["location"]
34
+ [geometry["lat"], geometry["lng"]]
35
+ end
36
+
37
+ def render(location)
38
+ config = @options[:gmapOptions]
39
+ dims = EmbedRb.get_dimensions(@options)
40
+ case config[:mode]
41
+ when 'streetview'
42
+ lat, lng = get_coordinate(location)
43
+ format('streetview', config, dims, "location=#{lat},#{lng}&heading=210&pitch=10&fov=35")
44
+ when 'view'
45
+ lat, lng = get_coordinate(location)
46
+ format('view', config, dims, "center=#{lat},#{lng}&zoom=18&maptype=satellite")
47
+ else
48
+ format('place', config, dims, "q=#{ERB::Util.url_encode(location)}")
49
+ end
50
+ end
51
+
52
+ def format(mode, config, dims, params)
53
+ url = "https://www.google.com/maps/embed/v1/#{mode}?key=#{config[:googleAuthKey]}&#{params}"
54
+ template = <<EOF
55
+ <div class="ejs-embed ejs-map">
56
+ <iframe width="#{dims[:width]}" height="#{dims[:height]}" src="#{url}" />
57
+ </div>
58
+ EOF
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,78 @@
1
+ require 'erb'
2
+ require 'json'
3
+ require_relative 'base'
4
+ require_relative 'utils'
5
+
6
+ module EmbedRb
7
+ class OpenGraph
8
+ include EmbedRb::Base
9
+
10
+ def initialize(input, output, options, embeds)
11
+ @input = input
12
+ @output = output
13
+ @options = options
14
+ @embeds = embeds
15
+ @url_regex = EmbedRb.url_regex
16
+ pattern = ['flickr.com|youtube.com|youtu.be|.mp4|.ogv|.webm|.mp3|.wav|.gif|.pdf|.doc|.ppt|.docx|.jpg|.jpeg|.tiff|.png|.svg|.webp|.ogg']
17
+ openGraphOptions = options[:openGraphOptions]
18
+ if openGraphOptions[:excluded_regex]
19
+ pattern[1] = openGraphOptions[:excluded_regex]
20
+ end
21
+ @excluded_regex = Regexp.new(pattern.join("|"), Regexp::IGNORECASE)
22
+ end
23
+
24
+ def process()
25
+ @input.scan(@url_regex) {|match|
26
+ url = match[2]
27
+ short_url = shorten(url)
28
+ if !exclude?(url) && !@options[:served].include?(short_url)
29
+ p short_url
30
+ data = fetch(url)
31
+ if data
32
+ @options[:served] << short_url
33
+ @embeds << {
34
+ :key => url,
35
+ :text => render(data)
36
+ }
37
+ end
38
+ end
39
+ }
40
+
41
+ @output
42
+ end
43
+
44
+ private
45
+ def fetch(url)
46
+ endpoint = @options[:openGraphEndpoint]
47
+ api = endpoint + ERB::Util.url_encode(url)
48
+ begin
49
+ data = JSON.parse EmbedRb.get_response(api)
50
+ if data && !data["error"]
51
+ return data
52
+ end
53
+ rescue
54
+ # error handling
55
+ end
56
+ end
57
+
58
+ def render(data)
59
+ return <<EOF
60
+ <div class="ejs-embed ejs-ogp">
61
+ <div class="ejs-ogp-thumb" style="background-image:url(#{data["hybridGraph"]['image']})" />
62
+ <div class="ejs-ogp-details">
63
+ <div class="ejs-ogp-title">
64
+ <a href="#{data['url']}" target="#{@options[:linkOptions][:target]}">
65
+ #{data["hybridGraph"]['title']}
66
+ </a>
67
+ </div>
68
+ <div class="ejs-ogb-details">#{data["hybridGraph"]['description']}</div>
69
+ </div>
70
+ </div>
71
+ EOF
72
+ end
73
+
74
+ def exclude?(url)
75
+ url.match(@excluded_regex) ? true : false
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'utils'
2
+
3
+ module EmbedRb
4
+ # input = "hoge http://www.youtubue.com/watch?a=b done\n https://hoge.fug/d"
5
+ # url = Embed::Url.new(input, {:linkOptions => {:target => '_blank'}})
6
+ # print(url.process)
7
+ class Url
8
+ def initialize(input, output, options, embeds)
9
+ @input = input
10
+ @options = options
11
+ @url_regex = EmbedRb.url_regex
12
+ end
13
+
14
+ def process()
15
+ config = EmbedRb.option(@options, :linkOptions)
16
+ @input.gsub(@url_regex) {|match|
17
+ extention = nil
18
+ if match.include?(".")
19
+ extention = match[match.rindex(".")+1, match.length].downcase
20
+ end
21
+ if config[:exclude].include? extention
22
+ match
23
+ else
24
+ "<a href=\"#{EmbedRb.to_url(match)}\" rel=\"#{config[:rel]}\" target=\"#{config[:target]}\">#{match}</a>"
25
+ end
26
+ }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ require_relative '../utils'
2
+ require_relative '../base.rb'
3
+
4
+ module EmbedRb
5
+ class Flickr
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 = /(flickr.com\/[a-z]+\/[a-zA-Z0-9@_\-]+\/[\d]+)/mi
14
+ end
15
+
16
+ private
17
+ def render(url)
18
+ dims = EmbedRb.get_dimensions(@options)
19
+ return <<EOF
20
+ <div class="ejs-embed">
21
+ <div class="ne-image-wrapper">
22
+ <iframe src="#{EmbedRb.to_url(url.split('/?')[0])}/player/" width="#{dims[:width]}" height="#{dims[:height]}" />
23
+ </div>
24
+ </div>
25
+ EOF
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ require_relative '../utils'
2
+ require_relative '../base.rb'
3
+
4
+ module EmbedRb
5
+ class BasicImage
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*\.(?:gif|jpg|jpeg|tiff|png|svg|webp))/mi
14
+ end
15
+
16
+ private
17
+ def render(url)
18
+ return <<EOF
19
+ <div class="ejs-image ejs-embed">
20
+ <div class="ne-image-wrapper">
21
+ <img src="#{url}"/>
22
+ </div>
23
+ </div>
24
+ EOF
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ require_relative '../utils'
2
+ require_relative '../base.rb'
3
+
4
+ module EmbedRb
5
+ class Instagram
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 = /(instagram.com\/p\/[a-zA-Z0-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-instagram">
21
+ <div class="ne-image-wrapper">
22
+ <iframe src="#{EmbedRb.to_url(url.split('/?')[0])}/embed/" width="#{dims[:width]}" height="#{dims[:height]}" />
23
+ </div>
24
+ </div>
25
+ EOF
26
+ end
27
+ end
28
+ end