linkable 0.0.1 → 1.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 81b0239ae53b8cd795d5f25e4e2ace7b1f06537f
4
- data.tar.gz: bccf09fdf068767d4ec986e2595ceeb706a99989
3
+ metadata.gz: 59e22e738ea584602d03fa10479b11338c1c2c49
4
+ data.tar.gz: 37a8d7aff2aa8383cca8d488f80f8e164c8d8a31
5
5
  SHA512:
6
- metadata.gz: da70d07c680fcc9177bbbd02f2ae90ae352bf796d503c1f0eb7b11622e01ce546bc87004e6a5df394ed366ae5666cb8258a0a18d98072975ac6180fe78b97d34
7
- data.tar.gz: 90e1cdd6b824bccdcbc5c16f058b769a9518fda3c77592c958000aaf627aa05db3e1a98c690cbe43c29860cf27a79b60ece3aaf73656a9b87f954715ab7d895c
6
+ metadata.gz: ec5db28f2b7163e4914bed6a2214f0f2ee6775e8fcf1b7495b8094355cbda99a756aa084fe0adc03b3f9d6e115a5eea429fe0aa24e570378fffa988693ac0b39
7
+ data.tar.gz: 4abdb2b826dfef24f96609bb499663a8b6c0e30745f598daf1fd111d5ff1f0a3211699d8b3674ae971851c04dece1acfefec2c758c0d517eca67a711a5d4a511
@@ -0,0 +1,10 @@
1
+ ## 1.0.0
2
+ ### Breaking Changes
3
+ - URLs now return instances of URI::Generic instead of String
4
+
5
+ ### Features
6
+ - Manipulate urls in strings using blocks with `String#replace_urls`
7
+ - Automatically replace urls in strings with link tags using `String#link`
8
+
9
+ ## 0.0.1
10
+ - Initial release
data/README.md CHANGED
@@ -3,12 +3,29 @@ Parsing libraries been getting outdated as the web evolve. Linkable takes a scal
3
3
 
4
4
  ## Usage
5
5
 
6
+ #### Require the library
6
7
  ```ruby
7
8
  require "linkable"
9
+ ```
10
+
11
+ #### List all urls in a string
12
+ ```ruby
13
+ text = "Have you heard that github.com/zeeraw/linkable will detect your proprietary://special.snowflake.domain domain."
14
+ text.urls
15
+ # => [#<URI::Generic URL:github.com/zeeraw/linkable>, #<URI::Generic proprietary://special.snowflake.domain>]
16
+ ```
8
17
 
9
- string = "Have you heard that github.com/zeeraw/linkable will detect your proprietary://special.snowflake.domain domain."
10
- string.urls
11
- # => ["github.com/zeeraw/linkable", "proprietary://special.snowflake.domain"]
18
+ #### Replace urls with link tags
19
+ ```ruby
20
+ "Follow me on twitter http://twitter.com/zeeraw".link
21
+ # => "Follow me on twitter <a href='http://twitter.com/zeeraw'>http://twitter.com/zeeraw</a>"
22
+ ```
23
+
24
+ #### Replace urls manually
25
+ ```ruby
26
+ text = "Follow me on twitter http://twitter.com/zeeraw"
27
+ text.replace_urls { |url| "<a href=#{ url }> @#{ url.path[1..-1] }</a>" }
28
+ # => "Follow me on twitter <a href='http://twitter.com/zeeraw'>@zeeraw</a>"
12
29
  ```
13
30
 
14
31
  ## Installation
@@ -1,10 +1,31 @@
1
1
  require "linkable/version"
2
2
  require "linkable/expression"
3
+ require "uri"
3
4
 
4
5
  module Linkable
5
6
 
7
+ LINK_URI = Proc.new { |uri| "<a href='#{ uri }'>#{ uri }</a>" }.freeze
8
+
6
9
  def urls
7
- self.scan(Linkable::EXPRESSION).map(&:first)
10
+ self.scan(Linkable::EXPRESSION).map(&:first).map { |uri| URI::parse uri }
11
+ end
12
+
13
+ def replace_urls(&blk)
14
+ return unless block_given?
15
+ self.gsub(Linkable::EXPRESSION) { |uri| blk.call URI::parse uri }
16
+ end
17
+
18
+ def replace_urls!(&blk)
19
+ return unless block_given?
20
+ self.gsub!(Linkable::EXPRESSION) { |uri| blk.call URI::parse uri }
21
+ end
22
+
23
+ def link
24
+ self.gsub(Linkable::EXPRESSION, &LINK_URI)
25
+ end
26
+
27
+ def link!
28
+ self.gsub!(Linkable::EXPRESSION, &LINK_URI)
8
29
  end
9
30
 
10
31
  end
@@ -1,3 +1,3 @@
1
1
  module Linkable
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0.pre1"
3
3
  end
@@ -13,52 +13,100 @@ RSpec.describe Linkable do
13
13
  Schema relative URLs are pretty common these days, if you need it, we got it #{ schema_relative }
14
14
 
15
15
  And for the grand finale is when you put another domain in the query params #{ url_in_query_parameters } WOW!"
16
+
17
+ While something like #{ underscored } is not matched due to underscores.
16
18
  ) }
17
19
 
18
20
  describe "String#urls" do
19
21
 
20
22
  subject(:urls) { text.urls }
21
23
 
22
- let(:domain) { "www.domain.com" }
24
+ let(:domain) { URI::parse "www.google.com" }
23
25
  it "parses domain" do
24
26
  expect(urls).to include domain
25
27
  end
26
28
 
27
- let(:apex_domain) { "domain.com?foo=bar" }
29
+ let(:apex_domain) { URI::parse "loopia.se?foo=bar" }
28
30
  it "parses apex domain" do
29
31
  expect(urls).to include apex_domain
30
32
  end
31
33
 
32
- let(:subdomain) { "sub.domain.com" }
34
+ let(:subdomain) { URI::parse "api.github.com" }
33
35
  it "parses subdomain" do
34
36
  expect(urls).to include subdomain
35
37
  end
36
38
 
37
- let(:multiple_subdomains) { "second.sub.doma.in/foo/bar?baz=biz" }
39
+ let(:multiple_subdomains) { URI::parse "site.stage.internal.net/path/to?my=variable" }
38
40
  it "parses multiple subdomains" do
39
41
  expect(urls).to include multiple_subdomains
40
42
  end
41
43
 
42
- let(:complex_hostname) { "domain.w1th-d4shes-4nd-numbe.rs" }
44
+ let(:complex_hostname) { URI::parse "14m.t00-c00l-4-sch00l.ninja" }
43
45
  it "parses complex hostnames" do
44
46
  expect(urls).to include complex_hostname
45
47
  end
46
48
 
47
- let(:custom_schema) { "protocol://with.domain.com?queries=too" }
49
+ let(:custom_schema) { URI::parse "winzip://with.domain.com?queries=too" }
48
50
  it "parses custom schema" do
49
51
  expect(urls).to include custom_schema
50
52
  end
51
53
 
52
- let(:schema_relative) { "//with.domain.com?queries=too" }
54
+ let(:schema_relative) { URI::parse "//with.domain.com?queries=too" }
53
55
  it "parses schema relative" do
54
56
  expect(urls).to include schema_relative
55
57
  end
56
58
 
57
- let(:url_in_query_parameters) { "domain.w1th-d4shes-4nd-numbe.rs/foobar?is=http://www.another-domain.com" }
59
+ let(:url_in_query_parameters) { URI::parse "domain.w1th-d4shes-4nd-numbe.rs/foobar?is=http://www.another-domain.com" }
58
60
  it "parses domain in query parameters" do
59
61
  expect(urls).to include url_in_query_parameters
60
62
  end
61
63
 
64
+ let(:underscored) { URI::parse "try.under_score.app" }
65
+ it "parses underscores" do
66
+ expect(urls).to include underscored
67
+ end
68
+ end
69
+
70
+ describe "String#replace_urls" do
71
+ let(:text) { "Please follow me on twitter http://twitter.com/zeeraw" }
72
+ subject(:replaced) { text.replace_urls { |url| "<a href='#{ url }'>@#{ url.path[1..-1] }</a>" } }
73
+ it "converts urls with block" do
74
+ expect(replaced).to include "<a href='http://twitter.com/zeeraw'>@zeeraw</a>"
75
+ end
76
+ it "duplicates the string" do
77
+ expect(text.link.object_id).to_not match text.object_id
78
+ end
79
+ end
80
+
81
+ describe "String#replace_urls!" do
82
+ let(:text) { "Please follow me on twitter http://twitter.com/zeeraw" }
83
+ subject(:replaced) { text.replace_urls! { |url| "<a href='#{ url }'>@#{ url.path[1..-1] }</a>" } }
84
+ it "converts urls with block" do
85
+ expect(replaced).to include "<a href='http://twitter.com/zeeraw'>@zeeraw</a>"
86
+ end
87
+ it "manipulates the string" do
88
+ expect(text.link!.object_id).to match text.object_id
89
+ end
90
+ end
91
+
92
+ describe "String#link" do
93
+ let(:text) { "Please follow me on twitter http://twitter.com/zeeraw" }
94
+ it "converts urls to html links" do
95
+ expect(text.link).to match "Please follow me on twitter <a href='http://twitter.com/zeeraw'>http://twitter.com/zeeraw</a>"
96
+ end
97
+ it "duplicates the string" do
98
+ expect(text.link.object_id).to_not match text.object_id
99
+ end
100
+ end
101
+
102
+ describe "String#link!" do
103
+ let(:text) { "Please follow me on twitter http://twitter.com/zeeraw" }
104
+ it "converts urls to html links" do
105
+ expect(text.link!).to match "Please follow me on twitter <a href='http://twitter.com/zeeraw'>http://twitter.com/zeeraw</a>"
106
+ end
107
+ it "manipulates the string" do
108
+ expect(text.link!.object_id).to match text.object_id
109
+ end
62
110
  end
63
111
 
64
112
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linkable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-11 00:00:00.000000000 Z
11
+ date: 2014-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,6 +76,7 @@ extra_rdoc_files: []
76
76
  files:
77
77
  - ".gitignore"
78
78
  - ".rspec"
79
+ - CHANGELOG.md
79
80
  - Gemfile
80
81
  - LICENSE.txt
81
82
  - README.md
@@ -101,9 +102,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
102
  version: '0'
102
103
  required_rubygems_version: !ruby/object:Gem::Requirement
103
104
  requirements:
104
- - - ">="
105
+ - - ">"
105
106
  - !ruby/object:Gem::Version
106
- version: '0'
107
+ version: 1.3.1
107
108
  requirements: []
108
109
  rubyforge_project:
109
110
  rubygems_version: 2.2.2