Skip to content

Commit

Permalink
Adding a section to the QUnit post per Twitter feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjvantoll committed Sep 1, 2012
1 parent 6928da9 commit ff60953
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,6 +4,7 @@
.gist-cache
.pygments-cache
.preview-mode
.tweet-cache
_deploy
public
sass.old
Expand Down
95 changes: 95 additions & 0 deletions plugins/tweet_tag.rb
@@ -0,0 +1,95 @@
# A Liquid tag for Jekyll sites that allows embedding tweets using Twitter's
# oEmbed API, and showing the tweet as a blockquote for non-JavaScript enabled
# browsers and readers.
#
# Author: Scott W. Bradley
# Source URL: https://github.com/scottwb/jekyll-tweet-tag
#
# Example usage:
# {% tweet https://twitter.com/DEVOPS_BORAT/status/159849628819402752 %}
#
# Documentation:
# https://github.com/scottwb/jekyll-tweet-tag/blob/master/README.md
#
require 'json'

module Jekyll
class TweetTag < Liquid::Tag

TWITTER_OEMBED_URL = "https://api.twitter.com/1/statuses/oembed.json"

def initialize(tag_name, text, tokens)
super
@text = text
@cache_disabled = false
@cache_folder = File.expand_path "../.tweet-cache", File.dirname(__FILE__)
FileUtils.mkdir_p @cache_folder
end

def render(context)
args = @text.split(/\s+/).map(&:strip)
api_params = {'url' => args.shift}

args.each do |arg|
k,v = arg.split('=').map(&:strip)
if k && v
if v =~ /^'(.*)'$/
v = $1
end
api_params[k] = v
end
end

html_output_for(api_params)
end

def html_output_for(api_params)
body = "Tweet could not be processed"
if response = cached_response(api_params) || live_response(api_params)
body = response['html'] || response['error'] || body
end
"<div class='embed tweet'>#{body}</div>"
end

def cache(api_params, data)
cache_file = cache_file_for(api_params)
File.open(cache_file, "w") do |f|
f.write(data)
end
end

def cached_response(api_params)
return nil if @cache_disabled
cache_file = cache_file_for(api_params)
JSON.parse(File.read(cache_file)) if File.exist?(cache_file)
end

def url_params_for(api_params)
api_params.keys.sort.map do |k|
"#{CGI::escape(k)}=#{CGI::escape(api_params[k])}"
end.join('&')
end

def cache_file_for(api_params)
filename = "#{Digest::MD5.hexdigest(url_params_for(api_params))}.cache"
File.join(@cache_folder, filename)
end

def live_response(api_params)
api_uri = URI.parse(TWITTER_OEMBED_URL + "?#{url_params_for(api_params)}")
response = Net::HTTP.get(api_uri.host, api_uri.request_uri)
cache(api_params, response) unless @cache_disabled
JSON.parse(response)
end
end

class TweetTagNoCache < TweetTag
def initialize(tag_name, text, token)
super
@cache_disabled = true
end
end
end

Liquid::Template.register_tag('tweet', Jekyll::TweetTag)
Liquid::Template.register_tag('tweetnocache', Jekyll::TweetTagNoCache)
Expand Up @@ -201,3 +201,13 @@ This might be a bit excessive for some but I like being able to quickly see info
### TAP Format

If you want to output the test results in [TAP format](http://en.wikipedia.org/wiki/Test_Anything_Protocol) the [QUnit-tap](https://github.com/twada/qunit-tap) plugin provides an excellent implementation using the same approach described above.

### Update (August 31st, 2012)

{% tweet https://twitter.com/qunitjs/status/240049283997503488 %}

QUnit provides its own [test runner for use with PhantomJS](https://github.com/jquery/qunit/tree/master/addons/phantomjs) which logs information on the test run using its own APIs.

One difference with doing the logging in the runner rather than in the HTML is that you will not get the console logging when you run the tests in the browser, which is usually what you want.

Either approach can be used and customized to provide the logging you'd like.

0 comments on commit ff60953

Please sign in to comment.