Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added a cleaned up Soundcloud filter + tests #45

Merged
merged 5 commits into from

2 participants

@bigcurl

Hi,

this is based on the pull request from quantummachina's SHA: c9a4c7a
It is a cleaned up version and does not have any external gem dependencies.

Regards
Samuel Goebert

Samuel Goebert added some commits
@bigcurl bigcurl closed this
Samuel Goebert Added options for customizing the widget
submitted wrong url to soundcloud
catch empty responses from soundcloud eg. service is down
49570b9
@bigcurl bigcurl reopened this
Samuel Goebert added some commits
Samuel Goebert parse links without protocol e.g. www.soundcloud.com/nice
also it is possible to set the available options now
96ab57e
Samuel Goebert catch timeout and false formated url's exception e704062
@dejan dejan merged commit 5993e76 into dejan:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 14, 2012
  1. Added Soundcloud filter + tests

    Samuel Goebert authored
  2. removed a debugger statement

    Samuel Goebert authored
  3. Added options for customizing the widget

    Samuel Goebert authored
    submitted wrong url to soundcloud
    catch empty responses from soundcloud eg. service is down
  4. parse links without protocol e.g. www.soundcloud.com/nice

    Samuel Goebert authored
    also it is possible to set the available options now
Commits on May 15, 2012
  1. catch timeout and false formated url's exception

    Samuel Goebert authored
This page is out of date. Refresh to see the latest.
View
3  CHANGELOG.md
@@ -1,3 +1,6 @@
+## unreleased
+* Added Soundcloud filter
+
## 1.5.3, released 2012-05-08
* Fixed Mongoid support
View
27 lib/auto_html/filters/soundcloud.rb
@@ -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
View
29 test/unit/filters/soundcloud_test.rb
@@ -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
Something went wrong with that request. Please try again.