externallink 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8bd46ca3958d62bc101d1270763992cdf259d6e2
4
- data.tar.gz: 442c4c5e96d31d5834148c1175eff5da5e875c89
3
+ metadata.gz: 087377f62b27385ffaaf1e13b1a710f3adb4fabd
4
+ data.tar.gz: eeda78989c34e51e86fd413fccefa1c75390547a
5
5
  SHA512:
6
- metadata.gz: 20bea68e341fa1c413a818f22515ed2b1eb37fa7a1239433593de5a889bb219eac8aca81ee4190d400d26219c0be400dc4efd1273be5ac5cc00d22a1134252bf
7
- data.tar.gz: 744c07f5db5f7311b3547303586541594b0626d821d262a5d4f756cfc51a3442fd286ea9c8c680ea5808ea2e8b3825ad686bbf2aa4cdfc0cfcb997acf4299b6b
6
+ metadata.gz: 12e384141ba413a86cf131f66f99a682135fcafa05ed632dc0a6f8da9161d21d264b9a4837fff394b90ebe55c3ac12b1369c9abcc34d3ed8e1731d68f4c77933
7
+ data.tar.gz: 09655674305099e9165adf04e29387582b98656b95db01c8bcba177cb5b6f56dec37c004ffc859919503bd68e7b71f87853d20caae308e47ea6e82c037a2a8e4
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in externallink.gemspec
4
3
  gemspec
4
+
5
+ gem 'pry'
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Externallink
2
2
 
3
- TODO: Write a gem description
3
+ Helper to detect external urls.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,11 +20,27 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ This gem works out of the box for Rails applications and provides the following helpers.
24
+
25
+ ### `external_url?`
26
+
27
+ Checks if the given URL is external or not.
28
+ Example (when the app domain is example.com):
29
+
30
+ ```ruby
31
+ external_url?("http://www.google.com") # => true
32
+ external_url?("/foo") # => false
33
+ external_url?("http://example.com/foo") # => false
34
+ ```
35
+
36
+ ### `internal_url?`
37
+
38
+ The opposite behavior of `external_url?`
39
+
24
40
 
25
41
  ## Contributing
26
42
 
27
- 1. Fork it ( https://github.com/[my-github-username]/externallink/fork )
43
+ 1. Fork it ( https://github.com/xarsh/externallink/fork )
28
44
  2. Create your feature branch (`git checkout -b my-new-feature`)
29
45
  3. Commit your changes (`git commit -am 'Add some feature'`)
30
46
  4. Push to the branch (`git push origin my-new-feature`)
data/lib/externallink.rb CHANGED
@@ -1,3 +1,7 @@
1
- require 'externallink/railtie' if defined?(Rails)
2
- require "externallink/version"
3
- require "URI"
1
+ require 'uri'
2
+
3
+ %w(version view_helpers).each do |m|
4
+ require_relative "externallink/#{m}"
5
+ end
6
+
7
+ require_relative 'externallink/railtie' if defined?(Rails)
@@ -1,3 +1,3 @@
1
1
  module Externallink
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,11 +1,12 @@
1
1
  module Externallink
2
2
  module ViewHelpers
3
3
  def external_url?(url)
4
- request.host != URI(url).host
4
+ !internal_url?(url)
5
5
  end
6
6
 
7
7
  def internal_url?(url)
8
- request.host == URI(url).host
8
+ url_host = URI(url).host
9
+ url_host.nil? || request.host == url_host
9
10
  end
10
11
  end
11
12
  end
@@ -0,0 +1,41 @@
1
+ describe Externallink::ViewHelpers do
2
+ subject(:helpers) { Class.new { extend Externallink::ViewHelpers } }
3
+
4
+ before(:each) do
5
+ allow(helpers).to receive(:request).and_return(FakeRequest.new)
6
+ end
7
+
8
+ describe '#internal_url?' do
9
+ it 'should detect:key => "value", external URLs' do
10
+ expect(helpers.internal_url?('http://www.google.com')).to eq(false)
11
+ end
12
+
13
+ it 'should ignore protocol' do
14
+ expect(helpers.internal_url?('https://google.com')).to eq(false)
15
+ end
16
+
17
+ it 'should detect path only URLs as internal' do
18
+ expect(helpers.internal_url?('/foo')).to eq(true)
19
+ end
20
+
21
+ it 'should detect internal URLs with domain' do
22
+ expect(helpers.internal_url?('http://example.com')).to eq(true)
23
+ end
24
+
25
+ it 'should ignore path' do
26
+ expect(helpers.internal_url?('https://example.com/foo')).to eq(true)
27
+ end
28
+ end
29
+
30
+ describe '#external_url?' do
31
+ it 'should have the opposite behavior of external_url?' do
32
+ %w(http://wwww.google.com https://google.com).each do |url|
33
+ expect(helpers.external_url?(url)).to eq(true)
34
+ end
35
+
36
+ %w(/foo http://example.com https://example.com/foo).each do |url|
37
+ expect(helpers.external_url?(url)).to eq(false)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'externallink'
4
+ require 'pry'
5
+
6
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each do |s|
7
+ require s
8
+ end
9
+
10
+ RSpec.configure do |config|
11
+ config.expect_with :rspec do |c|
12
+ c.include_chain_clauses_in_custom_matcher_descriptions = true
13
+ c.syntax = :expect
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ class FakeRequest
2
+ def host
3
+ "example.com"
4
+ end
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: externallink
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takao Naito
@@ -46,6 +46,7 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
+ - ".rspec"
49
50
  - Gemfile
50
51
  - LICENSE.txt
51
52
  - README.md
@@ -55,6 +56,9 @@ files:
55
56
  - lib/externallink/railtie.rb
56
57
  - lib/externallink/version.rb
57
58
  - lib/externallink/view_helpers.rb
59
+ - spec/lib/externallink/view_helpers_spec.rb
60
+ - spec/spec_helper.rb
61
+ - spec/support/fake_request.rb
58
62
  homepage: https://github.com/xarsh/externallink
59
63
  licenses:
60
64
  - MIT
@@ -79,5 +83,8 @@ rubygems_version: 2.4.5
79
83
  signing_key:
80
84
  specification_version: 4
81
85
  summary: Helper to detect external urls.
82
- test_files: []
86
+ test_files:
87
+ - spec/lib/externallink/view_helpers_spec.rb
88
+ - spec/spec_helper.rb
89
+ - spec/support/fake_request.rb
83
90
  has_rdoc: