jekyll-embedly-plugin 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jekyll-embedly-plugin.rb +154 -0
  3. metadata +100 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 37503478e64c99b4bd919a82d10f8e80a441e7e1
4
+ data.tar.gz: 4c377f7e19e30b0137bf3ac6724026c24ac8495f
5
+ SHA512:
6
+ metadata.gz: a97687d92cfd774aade1ba8bd85411b923af020b47bd3c99f6903f5e6eae654404893e80d57470b5988b7dae084fe1ae6e7f728bc0aac22f48bc12d0f651aeb5
7
+ data.tar.gz: 04681e76338b6d77bbca11b274cdc73fd473021cc4c7e175ed5ca8924e47344084a515986be57d0970d3522267b7a27836ca8b2e853e829bbe42e955191d1f77
@@ -0,0 +1,154 @@
1
+ require "net/https"
2
+ require "uri"
3
+ require "json"
4
+ require "domainatrix"
5
+ require "compose_url"
6
+
7
+ module JekyllEmbedlyPlugin
8
+
9
+ class FileCache
10
+ def initialize
11
+ @cache_folder = File.expand_path(".embedly-cache")
12
+ FileUtils.mkdir_p @cache_folder
13
+ end
14
+
15
+ def read(key)
16
+ file_to_read = cache_file(cache_filename(key))
17
+ File.read(file_to_read) if File.exist?(file_to_read)
18
+ end
19
+
20
+ def write(key, response)
21
+ file_to_write = cache_file(cache_filename(key))
22
+
23
+ File.open(file_to_write, 'w') do |f|
24
+ f.write(response)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def cache_file(filename)
31
+ File.join(@cache_folder, filename)
32
+ end
33
+
34
+ def cache_filename(cache_key)
35
+ "#{cache_key}.cache"
36
+ end
37
+ end
38
+
39
+ class NullCache
40
+ def read(_key); end
41
+
42
+ def write(_key, _data); end
43
+ end
44
+
45
+ module Cacheable
46
+ def cache_key(url)
47
+ Digest::MD5.hexdigest("#{url}")
48
+ end
49
+ end
50
+
51
+ class EmbedlyTag < Liquid::Tag
52
+ include JekyllEmbedlyPlugin::Cacheable
53
+
54
+ def initialize(tag_name, text, tokens)
55
+ super
56
+
57
+ tokens = text.split /\,\s/
58
+ @url = tokens[0]
59
+ @parameters = {}
60
+ @cache = FileCache.new
61
+
62
+ tokens[1..-1].each do |arg|
63
+ key, value = arg.split /:/
64
+ value ||= "1"
65
+ @parameters[key.strip] = value.strip
66
+ end
67
+ end
68
+
69
+ def render(context)
70
+ @config = context.registers[:site].config["embedly"]
71
+ @api_key = @config["api_key"]
72
+
73
+ if @api_key.nil?
74
+ raise EmbedlyTagError "You must provide embed.ly api key."
75
+ end
76
+
77
+ embed @url
78
+ end
79
+
80
+ private
81
+
82
+ def live_response(url)
83
+ domain = Domainatrix.parse(url).domain
84
+ params = (@config[domain] || {}).merge @parameters
85
+
86
+ embedly_url = ComposeURL.new("http://api.embed.ly/1/oembed")
87
+
88
+ if domain == "youtube" && params["rel"] == "0"
89
+ youtube_url = ComposeURL.new(url)
90
+ youtube_url.add_param("rel", "0")
91
+ url = youtube_url.to_s
92
+ params.delete "rel"
93
+ end
94
+
95
+ embedly_url.add_param("key", @api_key)
96
+ embedly_url.add_param("url", url)
97
+ params.each { |k, v| embedly_url.add_param(k, v) }
98
+
99
+ response = compose JSON.parse resolve embedly_url.to_s
100
+ @cache.write(cache_key(url), response)
101
+ response
102
+ end
103
+
104
+ def cached_response(url)
105
+ @cache.read(cache_key(url))
106
+ end
107
+
108
+ def embed(url)
109
+ cached_response(url) || live_response(url)
110
+ end
111
+
112
+ def compose(json_rep)
113
+ type = json_rep["type"]
114
+ provider = json_rep["provider_name"].downcase
115
+
116
+ if type == "photo"
117
+ url = json_rep["url"]
118
+ width = json_rep["width"]
119
+ height = json_rep["height"]
120
+ desc = CGI::escapeHTML json_rep["description"]
121
+ html = "<img src=\"#{url}\" alt=\"#{desc}\" width=\"#{width}\" height=\"#{height}\" />"
122
+ elsif type == "link"
123
+ url = json_rep["url"]
124
+ title = json_rep["title"]
125
+ html = "<a class=\"embedly-card\" href=\"#{url}\">#{title}</a><script async src=\"//cdn.embedly.com/widgets/platform.js\" charset=\"UTF-8\"></script>"
126
+ else
127
+ html = json_rep["html"]
128
+ end
129
+
130
+ "<div class=\"embed #{type} #{provider}\">#{html}</div>"
131
+ end
132
+
133
+ def resolve(url)
134
+ response = Net::HTTP.get_response(URI(url))
135
+
136
+ unless response["location"].nil? and response["location"].nil?
137
+ resolve URI.parse(response["location"]) or
138
+ URI.parse(response["location"])
139
+ else
140
+ response.body
141
+ end
142
+ end
143
+
144
+ end
145
+
146
+ class EmbedlyTagError < StandardError
147
+ def initialize(msg)
148
+ super(msg)
149
+ end
150
+ end
151
+
152
+ end
153
+
154
+ Liquid::Template.register_tag("embedly", JekyllEmbedlyPlugin::EmbedlyTag)
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-embedly-plugin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Burnette
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.3.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.3.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: compose_url
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: domainatrix
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.11
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.11
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.8.1
69
+ description:
70
+ email: ryan.burnette@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - lib/jekyll-embedly-plugin.rb
76
+ homepage: https://github.com/ryanburnette/jekyll-embedly-plugin
77
+ licenses:
78
+ - Apache2
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: An embed.ly Liquid tag for Jekyll.
100
+ test_files: []