pageflow-oembed 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 768e67651b4278343646a683061082b75803f7af
4
- data.tar.gz: 0c9df1cbfe40cb14740b3eb57170d7cb45304dd3
3
+ metadata.gz: e3ab4e28e97a30dcb8bb9a5e62ff19140d17c125
4
+ data.tar.gz: 9f75f2259db7007baf98103977bd5fd80601e6ca
5
5
  SHA512:
6
- metadata.gz: bbc5417af0cf5e20ffed703cd89c48173625366adbe87b1fa9c0c116011151654c62301def80b64947247b303edf3965bdf30e08e10bf221517e7e003a6e5115
7
- data.tar.gz: 86ba5adcc366f271755bfd02bbf8cce48463f6787cdefb38444bded60cf8a0874baa5546ea4dcae273a3a4f43f21389d1b2043b1a38aef5b148ead6245ed1e34
6
+ metadata.gz: 304639e97cd7959dc15b706540ce94e41e3da4ab8b6687a3e381efadcbfa0828844140c60480987b1482f0f890e2b5e87d25f36f022ab8dddd677278968fa36c
7
+ data.tar.gz: e5326f68776ea1b68277a7ec7eb1d631b534da1499e98e4ac93ae4b6fe53564997ef89ad8baa32592500bfcfdc00361bfd163bc3f6ad0d5f778633ea46ca85ab
data/CHANGELOG CHANGED
@@ -1,3 +1,12 @@
1
+ Version 1.1.0—16 July 2017
2
+
3
+ Bugfix: NodeList to Array now considers empty Nodelist. Also, speed.
4
+
5
+ Added Spotify support. Uses Oembed like the makers intended, damnit!
6
+
7
+ * spotify, single track: https://open.spotify.com/track/298gs9ATwr2rD9tGYJKlQR
8
+ * spotify, artist: https://open.spotify.com/artist/0IIPgITtEO4JJfipw57KGv
9
+
1
10
  Version 1.0.1—16 July 2017
2
11
 
3
12
  Bugfix: tweet ID is now correctly parsed from the link.
@@ -5,4 +14,6 @@ Bugfix: tweet ID is now correctly parsed from the link.
5
14
  Version 1.0.0—16 July 2017
6
15
 
7
16
  Twitter support. Doesn't actually use oEmbed, because the Twitter SDK
8
- handles everything for us.
17
+ handles everything for us. Oh well.
18
+
19
+ * twitter, single tweet: https://twitter.com/scrollytelling/status/885128273239396352
data/README.md CHANGED
@@ -5,6 +5,8 @@ into embedded content when possible. For example, instead of a link to a
5
5
  [Tweet URL](https://twitter.com/scrollytelling/status/885128273239396352), people
6
6
  reading your story will see the Tweet embedded in the page.
7
7
 
8
+ [Read the introduction on Scrollytelling](https://app.scrollytelling.io/embed-a-tweet)
9
+
8
10
  It usually works using [oEmbed](http://oembed.com), the standard protocol for discovering
9
11
  and showing embedded content. Although sometimes we use a JavaScript SDK, since it
10
12
  gives better results. When we do use oEmbed, the embed-code is cached in
@@ -25,16 +27,20 @@ We work with a whitelist and will convert only some links.
25
27
 
26
28
  Currently supported:
27
29
 
28
- * twitter: https://twitter.com/scrollytelling/status/885128273239396352
30
+ * twitter, single tweet: https://twitter.com/scrollytelling/status/885128273239396352
31
+ * spotify, single track: https://open.spotify.com/track/298gs9ATwr2rD9tGYJKlQR
32
+ * spotify, artist: https://open.spotify.com/artist/0IIPgITtEO4JJfipw57KGv
29
33
 
30
34
  Note that the URL you use must look exactly like the ones above.
31
35
 
32
36
  More providers will follow.
33
37
 
34
- ### Just use JavaScript
38
+ ### Just uses JavaScript
35
39
 
36
- jQuery is not used at all. This plugin contains two polyfills to get IE on board.
37
- And if you are using an ancient browser, well, that's your choice.
40
+ No jQuery was created during the development of this plugin. It does mean we
41
+ have two polyfills to get IE on board. These don't get loaded if your browser
42
+ is up to date. And if you are using a really ancient browser, well, that's
43
+ your choice.
38
44
 
39
45
  ### Heads up: social scripts will be included in the page
40
46
 
@@ -2,19 +2,19 @@
2
2
  //
3
3
  pageflow.widgetTypes.register('pageflow_oembed', {
4
4
  enhance: function(element) {
5
+ var embedLinks = [];
5
6
  var urls = document.querySelectorAll('.contentText a');
6
- var embedLinks = Array.from(urls).filter(this.embeddable);
7
+ for(var i = urls.length; i--; embedLinks.unshift(urls[i]));
8
+
7
9
  for (var i = 0, len = embedLinks.length; i < len; i++) {
8
- if( /twitter/i.test(embedLinks[i].getAttribute('href')) )
9
- this.embedTwitter(embedLinks[i]);
10
+ var url = embedLinks[i].getAttribute('href');
11
+ if( /twitter/i.test(url) )
12
+ this.embedTwitter(embedLinks[i], url);
13
+ else if( /spotify/i.test(url) )
14
+ this.embedSpotify(embedLinks[i], url);
10
15
  };
11
16
  },
12
17
 
13
- embeddable: function(link) {
14
- url = link.getAttribute('href');
15
- return /https?:\/\/twitter.com\/\w*\/\w*\/\d*/.test(url);
16
- },
17
-
18
18
  token: function() {
19
19
  return document.querySelector("meta[name='csrf-token']").content;
20
20
  },
@@ -23,27 +23,26 @@ pageflow.widgetTypes.register('pageflow_oembed', {
23
23
  return link.closest('.page');
24
24
  },
25
25
 
26
- embedSpotify: function(link) {
26
+ embedSpotify: function(anchor, url) {
27
27
  var xhr = new XMLHttpRequest();
28
- var url = link.getAttribute('href');
29
- var data = {oembed: {url: url, locale: pageflow.seed.locale}};
28
+ var data = {oembed: {url: url}};
30
29
  xhr.open("POST", '/oembed/fetch', true);
31
30
  xhr.setRequestHeader("Content-Type", "application/json");
32
31
  xhr.setRequestHeader("X-CSRF-Token", this.token());
33
32
  xhr.onload = function() {
34
33
  oembed = JSON.parse(this.responseText);
35
- link.outerHTML = oembed.html;
34
+ anchor.insertAdjacentHTML('afterend', oembed.html);
35
+ anchor.style.display = 'none';
36
36
  };
37
37
  xhr.send(JSON.stringify(data));
38
38
  },
39
39
 
40
- embedTwitter: function(link) {
41
- var url = link.getAttribute('href');
40
+ embedTwitter: function(anchor, url) {
42
41
  var match = /(\d+)$/.exec(url);
43
42
  tweetId = match[0];
44
- var theme = this.page(link).classList.contains('invert') ? 'light' : 'dark';
43
+ var theme = this.page(anchor).classList.contains('invert') ? 'light' : 'dark';
45
44
 
46
- link.insertAdjacentHTML('beforebegin', '<span id="tweet-'+tweetId+'"></span>')
45
+ anchor.insertAdjacentHTML('beforebegin', '<span id="tweet-'+tweetId+'"></span>')
47
46
 
48
47
  var options = {omit_script: true, related: 'scrollytelling', lang: pageflow.seed.locale, theme: theme, dnt: 'true'};
49
48
  twttr.widgets.createTweet(
@@ -51,6 +50,6 @@ pageflow.widgetTypes.register('pageflow_oembed', {
51
50
  document.getElementById('tweet-'+tweetId),
52
51
  options
53
52
  );
54
- link.style.display = 'none';
53
+ anchor.style.display = 'none';
55
54
  }
56
55
  });
@@ -1,6 +1,3 @@
1
- require 'faraday'
2
- require 'faraday_middleware'
3
-
4
1
  module Pageflow
5
2
  module Oembed
6
3
  class Fetcher
@@ -16,26 +13,13 @@ module Pageflow
16
13
  end
17
14
 
18
15
  def fetch
19
- # Twitter: GET https://publish.twitter.com/oembed?url=https%3A%2F%2Ftwitter.com%2FInterior%2Fstatus%2F507185938620219395
20
- connection = Faraday.new(url: "https://publish.twitter.com") do |conn|
21
- conn.request :json
22
- conn.response :json, :content_type => /\bjson$/
23
-
24
- conn.adapter Faraday.default_adapter
16
+ if url.include?('twitter')
17
+ provider = Providers::Twitter.new(url, params)
18
+ provider.oembed_response
19
+ elsif url.include?('spotify')
20
+ provider = Providers::Spotify.new(url, params)
21
+ provider.oembed_response
25
22
  end
26
-
27
- response = connection.get "/oembed", {
28
- url: url,
29
- omit_script: true,
30
- related: 'scrollytelling',
31
- lang: params[:locale] || 'en',
32
- theme: params[:theme] || 'dark',
33
- dnt: 'true'
34
- }
35
-
36
- oembed = response.body
37
- oembed[:cache_until] = oembed[:cache_age].seconds.from_now
38
- oembed
39
23
  end
40
24
  end
41
25
  end
@@ -0,0 +1,42 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module Pageflow
5
+ module Oembed
6
+ module Providers
7
+ class Spotify
8
+ def initialize(url, params)
9
+ @url = url
10
+ @params = params
11
+ end
12
+
13
+ attr_reader :url, :params
14
+
15
+ def oembed_response
16
+ # Spotify: GET https://open.spotify.com/oembed?url=http://open.spotify.com/track/298gs9ATwr2rD9tGYJKlQR
17
+ connection = Faraday.new({
18
+ url: "https://open.spotify.com",
19
+ headers: {user_agent: user_agent}
20
+ }) do |conn|
21
+ conn.request :json
22
+ conn.response :json, :content_type => /\bjson$/
23
+
24
+ conn.adapter Faraday.default_adapter
25
+ end
26
+
27
+ response = connection.get "/oembed", {
28
+ url: url
29
+ }
30
+
31
+ response.body
32
+ end
33
+
34
+ private
35
+
36
+ def user_agent
37
+ "Pageflow Oembed #{Pageflow::Oembed::VERSION}/Faraday #{Faraday::VERSION}"
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,40 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module Pageflow
5
+ module Oembed
6
+ module Providers
7
+ class Twitter
8
+ def initialize(url, params)
9
+ @url = url
10
+ @params = params
11
+ end
12
+
13
+ attr_reader :url, :params
14
+
15
+ def oembed_response
16
+ # Twitter: GET https://publish.twitter.com/oembed?url=https%3A%2F%2Ftwitter.com%2FInterior%2Fstatus%2F507185938620219395
17
+ connection = Faraday.new(url: "https://publish.twitter.com") do |conn|
18
+ conn.request :json
19
+ conn.response :json, :content_type => /\bjson$/
20
+
21
+ conn.adapter Faraday.default_adapter
22
+ end
23
+
24
+ response = connection.get "/oembed", {
25
+ url: url,
26
+ omit_script: true,
27
+ related: 'scrollytelling',
28
+ lang: params[:locale] || 'en',
29
+ theme: params[:theme] || 'dark',
30
+ dnt: 'true'
31
+ }
32
+
33
+ oembed = response.body
34
+ oembed[:cache_until] = oembed[:cache_age].seconds.from_now
35
+ oembed
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,5 +1,5 @@
1
1
  module Pageflow
2
2
  module Oembed
3
- VERSION = "1.0.1"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pageflow-oembed
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joost Baaij
@@ -134,6 +134,8 @@ files:
134
134
  - app/models/pageflow/oembed/application_record.rb
135
135
  - app/models/pageflow/oembed/fetcher.rb
136
136
  - app/models/pageflow/oembed/oembed.rb
137
+ - app/models/pageflow/oembed/providers/spotify.rb
138
+ - app/models/pageflow/oembed/providers/twitter.rb
137
139
  - app/views/pageflow/oembed/_head.html.erb
138
140
  - app/views/pageflow/oembed/_widget.html.erb
139
141
  - bin/console