Permalink
Browse files

Merge pull request #45 from bigcurl/soundcloud

Added a cleaned up Soundcloud filter + tests
  • Loading branch information...
2 parents e859d44 + e704062 commit 5993e7685a2c794d62e1ff1056f4c7f50aec11f6 @dejan committed Jun 4, 2012
Showing with 59 additions and 0 deletions.
  1. +3 −0 CHANGELOG.md
  2. +27 −0 lib/auto_html/filters/soundcloud.rb
  3. +29 −0 test/unit/filters/soundcloud_test.rb
View
@@ -1,3 +1,6 @@
+## unreleased
+* Added Soundcloud filter
+
## 1.5.3, released 2012-05-08
* Fixed Mongoid support
@@ -0,0 +1,27 @@
+require 'uri'
+require 'net/http'
+
+AutoHtml.add_filter(:soundcloud).with({}) do |text, options|
+ # set these options
+ # :maxwidth => '', :maxheight => '', :auto_play => false, :show_comments => false
+ text.gsub(/(https?:\/\/)?(www.)?soundcloud\.com\/.*/) do |match|
+ begin
+ new_uri = match.to_s
+ new_uri = (new_uri =~ /^https?\:\/\/.*/) ? URI(new_uri) : URI("http://#{new_uri}")
+ new_uri.normalize!
+
+ uri = URI("http://soundcloud.com/oembed")
+ params = {:format => 'json', :url => new_uri}
+ params = params.merge options
+ uri.query = URI.encode_www_form(params)
+ response = Net::HTTP.get(uri)
+ rescue
+ end
+
+ if !response.blank?
+ JSON.parse(response)["html"]
+ else
+ match
+ end
+ end
+end
@@ -0,0 +1,29 @@
+require File.expand_path('../../unit_test_helper', __FILE__)
+
+class SoundcloudTest < Test::Unit::TestCase
+
+ def test_transform_url_with_www
+ result = auto_html('http://www.soundcloud.com/forss/flickermood') { soundcloud }
+ assert_equal '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F293&show_artwork=true"></iframe>', result
+ end
+
+ def test_transform_url_without_www
+ result = auto_html('http://soundcloud.com/forss/flickermood') { soundcloud }
+ assert_equal '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F293&show_artwork=true"></iframe>', result
+ end
+
+ def test_transform_url_without_protocol
+ result = auto_html('soundcloud.com/forss/flickermood') { soundcloud }
+ assert_equal '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F293&show_artwork=true"></iframe>', result
+ end
+
+ def test_transform_url_with_ssl
+ result = auto_html('https://soundcloud.com/forss/flickermood') { soundcloud }
+ assert_equal '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F293&show_artwork=true"></iframe>', result
+ end
+
+ def test_transform_url_with_options
+ result = auto_html('http://www.soundcloud.com/forss/flickermood') { soundcloud(:maxwidth => '100', :maxheight => '100', :auto_play => false, :show_comments => false) }
+ assert_equal '<iframe width="100" height="100" scrolling="no" frameborder="no" src="http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F293&show_artwork=true&maxwidth=100&maxheight=100&auto_play=false&show_comments=false"></iframe>', result
+ end
+end

0 comments on commit 5993e76

Please sign in to comment.