Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twingly::URL::Utilities.remove_scheme #21

Merged
merged 2 commits into from
Dec 11, 2014
Merged

Twingly::URL::Utilities.remove_scheme #21

merged 2 commits into from
Dec 11, 2014

Conversation

jage
Copy link
Contributor

@jage jage commented Dec 9, 2014

Test and code from Zambezi. A minor version bump is needed if merged.

Close #6

Test and code from Zambezi. A minor version bump is needed if merged.

Close #6
@walro
Copy link
Contributor

walro commented Dec 10, 2014

LGTM, extensive test suite!

@dentarg
Copy link
Collaborator

dentarg commented Dec 10, 2014

What do you think about this?

$ git diff lib
diff --git a/lib/twingly/url/utilities.rb b/lib/twingly/url/utilities.rb
index cb0c33d..8e1800a 100644
--- a/lib/twingly/url/utilities.rb
+++ b/lib/twingly/url/utilities.rb
@@ -3,10 +3,10 @@ module Twingly
     module Utilities
       module_function

-      PROTOCOL_EXPRESSION = /^https?:/i
+      PROTOCOL_EXPRESSION = /https?:/i

       def remove_scheme(url)
-        url.gsub(PROTOCOL_EXPRESSION, '')
+        url.sub(PROTOCOL_EXPRESSION, '')
       end
     end
   end

(All tests pass)

@dentarg
Copy link
Collaborator

dentarg commented Dec 10, 2014

And what do you think of these additional tests?

$ git diff test
diff --git a/test/unit/utilities_test.rb b/test/unit/utilities_test.rb
index 94d5d04..32232dc 100644
--- a/test/unit/utilities_test.rb
+++ b/test/unit/utilities_test.rb
@@ -57,5 +57,19 @@ class TestUtilities < MiniTest::Unit::TestCase
       result = Twingly::URL::Utilities.remove_scheme(url)
       assert_equal '//www.thecloset.gr/people/bloggers-pick-ιωάννα-τσιγαρίδα', result
     end
+
+    should "only remove scheme from HTTP URL" do
+      url = 'http://feedjira.herokuapp.com/?url=http://developer.twingly.com/feed.xml'
+
+      result = Twingly::URL::Utilities.remove_scheme(url)
+      assert_equal '//feedjira.herokuapp.com/?url=http://developer.twingly.com/feed.xml', result
+    end
+
+    should "only remove scheme from HTTPS URL" do
+      url = 'https://feedjira.herokuapp.com/?url=https://signalvnoise.com/posts.rss'
+
+      result = Twingly::URL::Utilities.remove_scheme(url)
+      assert_equal '//feedjira.herokuapp.com/?url=https://signalvnoise.com/posts.rss', result
+    end
   end
 end

@walro
Copy link
Contributor

walro commented Dec 10, 2014

What do you think about this?

Can't see where it'd go wrong, guess it's more performant too, which I like.

And what do you think of these additional tests?

Good ones!

@dentarg
Copy link
Collaborator

dentarg commented Dec 10, 2014

The Ruby Style Guide says that String#sub is faster than String#gsub.

@walro
Copy link
Contributor

walro commented Dec 10, 2014

PROTOCOL_EXPRESSION = /https?:/i

I thought about this again, if we have

ftp://blabla.se?url=http://blabla.se it would not work as expected right? So I think we should keep the ^ around.

@dentarg
Copy link
Collaborator

dentarg commented Dec 10, 2014

True.

@dentarg
Copy link
Collaborator

dentarg commented Dec 10, 2014

Test case for that:

should "not remove scheme from non HTTP(S) URLs with parameter" do
  url = 'ftp://ftp.example.com/?url=https://www.example.com/'
  result = Twingly::URL::Utilities.remove_scheme(url)
  assert_equal 'ftp://ftp.example.com/?url=https://www.example.com/', result
end

@jage
Copy link
Contributor Author

jage commented Dec 10, 2014

@dentarg I think the proposed changes are good! Keep the ^ in the regexp and add all the tests. If you have the code, can you commit it?

@jage
Copy link
Contributor Author

jage commented Dec 10, 2014

LGTM, extensive test suite!

The tests are from Zambezi, just replaced def test_blabla to should "blabla" do :)

We only need to replace the first occurrence.

Should make the code more clear.
May perform better.
@dentarg
Copy link
Collaborator

dentarg commented Dec 11, 2014

If you have the code, can you commit it?

Done.

@jage
Copy link
Contributor Author

jage commented Dec 11, 2014

👍

jage added a commit that referenced this pull request Dec 11, 2014
Twingly::URL::Utilities.remove_scheme
@jage jage merged commit e7eb7ff into master Dec 11, 2014
@jage jage deleted the remove-scheme branch December 11, 2014 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make gem more general
3 participants