bypass 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDBiMzE5MzA3NjVlMDg2MjE4NDdiMzM1MmIwNjg4N2RhNjcwODRiNQ==
5
+ data.tar.gz: !binary |-
6
+ OWJlMGQyMjliODRmOGM3YTExZjk5NmIwZThiMmI3ZTgyNDhiYTU2OQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OGY3NTZmMzc1ZGRhYzhlZWU2NWJlZDY2OWFmZWQ4YTJmZTkyNjZiZjY1ZWY3
10
+ OWE3NmFlOWJmYzJhMTg5ZWU4MjVhNTE4ZmE5YWM1OWMzMjU3M2FiNThmMzE2
11
+ Nzc1OGUzNDc4MWUxODI3YzA4NjVhOTE1NjFhYzM5ZjEwYjJhMWU=
12
+ data.tar.gz: !binary |-
13
+ M2Y0MTVkZTA1ZmI1YjFkNTNiOGI1NTg5YjM3YWQ2MmZmNDBkNDI1YTIyNjA2
14
+ MDlmNTFmNGJhOGM3ZjU4YzUyOWIwYTcxOGM1YzM2MWI1ZmI2NWY3MDUyOTky
15
+ M2M1ZWY4ZmI3OGMyOGY4OWMzN2Q4NzcxY2M1MDUwMjM2ZTA4MWU=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .DS_Store
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - jruby-18mode
6
+ - jruby-19mode
7
+ - rbx-2.1.1
8
+ - ruby-head
9
+ - jruby-head
10
+ - 1.8.7
11
+ - ree
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'rake'
5
+ end
6
+
7
+ # Specify your gem's dependencies in bypass.gemspec
8
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Derrick Reimer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Bypass
2
+
3
+ [![Build Status](https://travis-ci.org/djreimer/bypass.png?branch=master)](https://travis-ci.org/djreimer/bypass)
4
+ [![Code Climate](https://codeclimate.com/github/djreimer/bypass.png)](https://codeclimate.com/github/djreimer/bypass)
5
+
6
+ Bypass is a Ruby gem that scans plain text or HTML documents for URLs and
7
+ hyperlinks and allows you to mutate or replace them with ease. This library
8
+ was originally designed for appending tracking data and shortening link
9
+ URLs in HTML and plain text emails.
10
+
11
+ Extracted from [Drip](http://www.getdrip.com/).
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'bypass'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install bypass
26
+
27
+ ## Usage
28
+
29
+ There are URL filters available for both plain text and HTML documents.
30
+
31
+ ### Plain Text
32
+
33
+ To replace all URLs in a plain text document:
34
+
35
+ ```ruby
36
+ text = "Visit our website: http://www.getdrip.com"
37
+ filter = Bypass::TextFilter.new(text)
38
+
39
+ filter.replace do |url|
40
+ url.append_to_query_values(:id => 123)
41
+ end
42
+
43
+ filter.content
44
+ #=> "Visit our website: http://www.getdrip.com?id=123"
45
+ ```
46
+
47
+ ### HTML
48
+
49
+ To replace all `href` attributes in `a` tags in an HTML document:
50
+
51
+ ```ruby
52
+ text = "Visit our website: <a href='http://www.getdrip.com'>Drip</a>"
53
+ filter = Bypass::HTMLFilter.new(text)
54
+
55
+ filter.replace do |url|
56
+ url.append_to_query_values(:id => 123)
57
+ end
58
+
59
+ filter.content
60
+ #=> "Visit our website: <a href='http://www.getdrip.com?id=123'>Drip</a>"
61
+ ```
62
+
63
+ To convert all non-hyperlinked URLs to hyperlinks:
64
+
65
+ ```ruby
66
+ text = "Lets auto link this: http://www.google.com"
67
+ filter = Bypass::HTMLFilter.new(text)
68
+ filter.auto_link
69
+
70
+ filter.content
71
+ #=> "Lets auto link this: <a href='http://www.google.com'>http://www.google.com</a>"
72
+ ```
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "lib"
7
+ t.pattern = "test/**/*_test.rb"
8
+ t.verbose = true
9
+ end
10
+
11
+ desc "Run tests"
12
+ task :default => :test
data/bypass.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bypass/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "bypass"
8
+ gem.version = Bypass::VERSION
9
+ gem.authors = ["Derrick Reimer"]
10
+ gem.email = ["derrickreimer@gmail.com"]
11
+ gem.description = %q{Mutate URLs and hyperlinks in HTML and plain text documents with ease}
12
+ gem.summary = %q{Mutate URLs and hyperlinks in HTML and plain text documents with ease}
13
+ gem.homepage = "https://github.com/djreimer/bypass"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "nokogiri", "~> 1.5"
21
+ gem.add_dependency "addressable", "~> 2.0"
22
+ gem.add_development_dependency "shoulda-context", "~> 1.0"
23
+ gem.add_development_dependency "mocha"
24
+ end
@@ -0,0 +1,29 @@
1
+ module Bypass
2
+ class Filter
3
+ attr_reader :content
4
+
5
+ URL_PATTERN = /\bhttps?:\/\/
6
+ [a-zA-Z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;=%]+
7
+ [a-zA-Z0-9\-_~:\/\?#\[\]@!$&\*\+;=%]/x
8
+
9
+ def initialize(content)
10
+ @content = content.to_s
11
+ end
12
+
13
+ def replace
14
+ raise NotImplementedError
15
+ end
16
+
17
+ def to_s
18
+ content
19
+ end
20
+
21
+ private
22
+
23
+ def gsub_urls(text, &block)
24
+ text.gsub(URL_PATTERN) do |match|
25
+ yield(match.to_s)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,54 @@
1
+ require "nokogiri"
2
+
3
+ module Bypass
4
+ class HTMLFilter < Filter
5
+
6
+ def replace(&block)
7
+ parsed_content.traverse do |node|
8
+ if node.name == "a" && href = node.get_attribute("href")
9
+ url = yield(Bypass::URI.parse(href))
10
+ node.set_attribute("href", url.to_s)
11
+ end
12
+ end
13
+ end
14
+
15
+ def auto_link(options = {})
16
+ each_text_node do |node|
17
+ text = gsub_urls(node.text.to_s) do |url|
18
+ build_anchor_tag(url, url, options)
19
+ end
20
+ node.replace(parse_html_fragment(text))
21
+ end
22
+ end
23
+
24
+ def build_anchor_tag(text, href, options = {})
25
+ attributes = []
26
+ options.each { |k, v| attributes << " #{k}=\"#{v}\"" }
27
+ "<a href=\"#{href}\"#{attributes.join(' ')}>#{text}</a>"
28
+ end
29
+
30
+ def content
31
+ parsed_content.to_s
32
+ end
33
+
34
+ private
35
+
36
+ def each_text_node(&block)
37
+ parsed_content.traverse do |node|
38
+ if node.name == "text"
39
+ unless node.path.split("/").include?("a")
40
+ yield(node)
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ def parsed_content
47
+ @parsed_content ||= parse_html_fragment(@content)
48
+ end
49
+
50
+ def parse_html_fragment(html)
51
+ Nokogiri::HTML::DocumentFragment.parse(html)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,12 @@
1
+ module Bypass
2
+ class TextFilter < Filter
3
+
4
+ def replace(&block)
5
+ @content = gsub_urls(content) do |url|
6
+ yield(Bypass::URI.parse(url)).to_s
7
+ end
8
+ end
9
+
10
+ def auto_link(options = {}); end
11
+ end
12
+ end
data/lib/bypass/uri.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "addressable/uri"
2
+
3
+ module Bypass
4
+ class URI < Addressable::URI
5
+ def append_to_query_values(params = {})
6
+ self.query_values = (query_values || {}).merge(params)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Bypass
2
+ VERSION = "0.0.2"
3
+ end
data/lib/bypass.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "bypass/version"
2
+ require "bypass/uri"
3
+ require "bypass/filter"
4
+ require "bypass/html_filter"
5
+ require "bypass/text_filter"
6
+
7
+ module Bypass
8
+ end
@@ -0,0 +1,11 @@
1
+ require 'addressable/uri'
2
+ require File.dirname(__FILE__) + '/../test_helper.rb'
3
+
4
+ class Bypass::FilterTest < Test::Unit::TestCase
5
+ context "#to_s" do
6
+ should "return content" do
7
+ filter = Bypass::Filter.new("foo")
8
+ assert_equal "foo", filter.to_s
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,58 @@
1
+ require 'addressable/uri'
2
+ require File.dirname(__FILE__) + '/../test_helper.rb'
3
+
4
+ class Bypass::HTMLFilterTest < Test::Unit::TestCase
5
+ context "#replace" do
6
+ should "replace hrefs" do
7
+ text = "<a href=\"http://yahoo.com\">Yahoo</a>"
8
+ filter = Bypass::HTMLFilter.new(text)
9
+ filter.replace { "foo" }
10
+ assert_equal "<a href=\"foo\">Yahoo</a>", filter.content
11
+ end
12
+
13
+ should "handle cases when there is no href attribute" do
14
+ text = "<a>Yahoo</a>"
15
+ filter = Bypass::HTMLFilter.new(text)
16
+ filter.replace { "foo" }
17
+ assert_equal "<a>Yahoo</a>", filter.content
18
+ end
19
+ end
20
+
21
+ context "#auto_link" do
22
+ should "convert plain URLs into links" do
23
+ text = "Hello http://www.google.com."
24
+ filter = Bypass::HTMLFilter.new(text)
25
+ filter.auto_link
26
+ assert_equal "Hello <a href=\"http://www.google.com\">http://www.google.com</a>.", filter.content
27
+ end
28
+
29
+ should "not replace urls inside a-tags" do
30
+ text = "<a href=\"http://yahoo.com\">http://yahoo.com</a>"
31
+ filter = Bypass::HTMLFilter.new(text)
32
+ filter.auto_link
33
+ assert_equal text, filter.content
34
+ end
35
+ end
36
+
37
+ context "#build_anchor_tag" do
38
+ should "use the right text and href" do
39
+ filter = Bypass::HTMLFilter.new("")
40
+ tag = "<a href=\"http://www.google.com\">Google</a>"
41
+ assert_equal tag, filter.build_anchor_tag("Google", "http://www.google.com")
42
+ end
43
+
44
+ should "include additional attributes" do
45
+ filter = Bypass::HTMLFilter.new("")
46
+ tag = "<a href=\"http://www.google.com\" class=\"button\">Google</a>"
47
+ assert_equal tag, filter.build_anchor_tag("Google", "http://www.google.com", :class => "button")
48
+ end
49
+ end
50
+
51
+ context "#content" do
52
+ should "cast parsed content to string" do
53
+ filter = Bypass::HTMLFilter.new("")
54
+ filter.expects(:parsed_content).returns(stub(:to_s => "bar"))
55
+ assert_equal "bar", filter.content
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,34 @@
1
+ require 'addressable/uri'
2
+ require File.dirname(__FILE__) + '/../test_helper.rb'
3
+
4
+ class Bypass::TextFilterTest < Test::Unit::TestCase
5
+ context "#replace" do
6
+ should "replace URLs" do
7
+ text = "http://yahoo.com"
8
+ filter = Bypass::TextFilter.new(text)
9
+ filter.replace { "foo" }
10
+ assert_equal "foo", filter.content
11
+ end
12
+
13
+ should "not include trailing periods in urls" do
14
+ text = "http://yahoo.com."
15
+ filter = Bypass::TextFilter.new(text)
16
+ filter.replace { "foo" }
17
+ assert_equal "foo.", filter.content
18
+ end
19
+
20
+ should "not include parenthesis in urls" do
21
+ text = "(http://yahoo.com)"
22
+ filter = Bypass::TextFilter.new(text)
23
+ filter.replace { "foo" }
24
+ assert_equal "(foo)", filter.content
25
+ end
26
+
27
+ should "not include trailing commas in urls" do
28
+ text = "http://yahoo.com,"
29
+ filter = Bypass::TextFilter.new(text)
30
+ filter.replace { "foo" }
31
+ assert_equal "foo,", filter.content
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ class Bypass::URITest < Test::Unit::TestCase
4
+ context "#append_to_query_values" do
5
+ should "add values to the query string if it starts empty" do
6
+ uri = Bypass::URI.parse("http://www.getdrip.com")
7
+ uri.append_to_query_values(:foo => "bar")
8
+ assert_equal "http://www.getdrip.com?foo=bar", uri.to_s
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require "bypass"
4
+ require "minitest/autorun"
5
+ require "shoulda-context"
6
+ require "mocha/setup"
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bypass
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Derrick Reimer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: addressable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: shoulda-context
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Mutate URLs and hyperlinks in HTML and plain text documents with ease
70
+ email:
71
+ - derrickreimer@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bypass.gemspec
83
+ - lib/bypass.rb
84
+ - lib/bypass/filter.rb
85
+ - lib/bypass/html_filter.rb
86
+ - lib/bypass/text_filter.rb
87
+ - lib/bypass/uri.rb
88
+ - lib/bypass/version.rb
89
+ - test/detour/filter_test.rb
90
+ - test/detour/html_filter_test.rb
91
+ - test/detour/text_filter_test.rb
92
+ - test/detour/uri_test.rb
93
+ - test/test_helper.rb
94
+ homepage: https://github.com/djreimer/bypass
95
+ licenses: []
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.1.10
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Mutate URLs and hyperlinks in HTML and plain text documents with ease
117
+ test_files:
118
+ - test/detour/filter_test.rb
119
+ - test/detour/html_filter_test.rb
120
+ - test/detour/text_filter_test.rb
121
+ - test/detour/uri_test.rb
122
+ - test/test_helper.rb