regexy 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cbe7ab941c2a42a78cbd5b9e0ca1c7627741a967
4
+ data.tar.gz: 80c997e18a7b4c34bd4dcf6d319f02494387e9da
5
+ SHA512:
6
+ metadata.gz: c4332522ec3d526be5ff34b686c046a0279c530b593e1e47d5af9d6c8835a7738a6239c5b08e07b6044985d3df27478142e428d8b39244afec065a2865860845
7
+ data.tar.gz: 26742a07f7a0bf349b6ce4030289a78385f7860e29fa277a318440e9ba9078ab092d0af5a0ff8241244f900689e4a929f8ffeae95ba9bc7d0b548fa077876ac0
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - 2.0.0-p598
6
+ - 2.1.4
7
+ - 2.2.1
8
+ - rbx-2
9
+ - jruby
10
+
11
+ before_install: gem update bundler
12
+
13
+ script: 'bundle exec rspec'
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in regexy.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Vladimir Tikhonov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # Regexy
2
+ [![Gem Version](https://badge.fury.io/rb/regexy.svg)](http://badge.fury.io/rb/regexy)
3
+ [![Build Status](https://travis-ci.org/vladimir-tikhonov/regexy.svg?branch=master)](https://travis-ci.org/vladimir-tikhonov/regexy)
4
+ [![Dependency Status](https://gemnasium.com/vladimir-tikhonov/regexy.svg)](https://gemnasium.com/vladimir-tikhonov/regexy)
5
+ [![Code Climate](https://codeclimate.com/github/vladimir-tikhonov/regexy/badges/gpa.svg)](https://codeclimate.com/github/vladimir-tikhonov/regexy)
6
+ [![Coverage Status](https://coveralls.io/repos/vladimir-tikhonov/regexy/badge.svg)](https://coveralls.io/r/vladimir-tikhonov/regexy)
7
+
8
+ Regexy is the ruby gem that contains a lot of common-use regular expressions (such as email or ip addresses validations) and provides a friendly syntax to combine them.
9
+
10
+ ## Table of Contents
11
+
12
+ - [Installation](#installation)
13
+ - [Usage](#usage)
14
+ * [General usage](#regexyregexp)
15
+ * [Combining expressions](#combining-regular-expressions)
16
+ * [Email addresses](#regexywebemail)
17
+ * [IP addresses](#regexywebipv4)
18
+ - [Contributing](#contributing)
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ ```ruby
25
+ gem 'regexy'
26
+ ```
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install regexy
35
+
36
+ ## Usage
37
+
38
+ ### Regexy::Regexp
39
+
40
+ Wrapper around original [Regexp](http://ruby-doc.org/core-2.2.1/Regexp.html) class. You can safely use it instead of original one.
41
+
42
+ ```ruby
43
+ r1 = Regexy::Regexp.new('foo') # could be initialized from string
44
+ r2 = Regexy::Regexp.new(/foo/) # from regexp
45
+ r3 = Regexy::Regexp.new(r2) # or even from another Regexy::Regexp
46
+ r4 = Regexy::Regexp.new('foo', Regexp::IGNORECASE) # pass additional configuration
47
+ 'abcfoocde' =~ r1 # => 3
48
+ r2.match 'abcfoocde' # => #<MatchData "foo">
49
+ ```
50
+ ### Combining regular expressions
51
+
52
+ You can combine your regular expressions with `|` operator using `|` method (or `or`, which is alias for it). Note, that regexp options will be combined too.
53
+ ```ruby
54
+ Regexy::Regexp.new('foo') | Regexy::Regexp.new(/bar/) # => /foo|bar/
55
+ Regexy::Regexp.new(/foo/i) | /bar/x # => /foo|bar/ix
56
+ Regexy::Regexp.new(/foo/i).or 'bar' # => /foo|bar/i
57
+ any_ipv4 = Regexy::Web::IPv4.new(:normal) | Regexy::Web::IPv4.new(:with_port) # matches ip w\ and w\o port
58
+ ```
59
+ Also you could simply join two expressions using `+` method, or it's alias `and_then`. Note, that it will __remove__ trailing `$` from first regex and leading `^` from second regex.
60
+ ```ruby
61
+ Regexy::Regexp.new('foo') + Regexy::Regexp.new(/bar/) # => /foobar/
62
+ Regexy::Regexp.new(/foo$/i) | /bar/ # => /foobar/i
63
+ Regexy::Regexp.new(/foo/).or '^bar' # => /foobar/
64
+ Regexy::Regexp.new(/^foo$/).or '^bar$' # => /^foobar$/
65
+ ```
66
+
67
+ ### Regexy::Web::Email
68
+
69
+ Generates regular expressions for email addresses validation (with unicode support). Available options: `:relaxed` for general sanity check, `:normal` (which is default) with some additional length and ip addresses validations and `:strict` for the paranoids.
70
+
71
+ ```ruby
72
+ r1 = Regexy::Web::Email.new(:relaxed)
73
+ r2 = Regexy::Web::Email.new(:normal) # does not match 'f@s.c' and 'invalid-ip@127.0.0.1.26'
74
+ r2 = Regexy::Web::Email.new(:strict) # does not match 'hans,peter@example.com' and "partially.\"quoted\"@sld.com"
75
+ ```
76
+ ### Regexy::Web::IPv4
77
+
78
+ Generates regular expressions for matching IPv4 addresses. Available options: `:normal` (by default) for matching ip without port and `:with_port` for guess what.
79
+
80
+ ```ruby
81
+ r1 = Regexy::Web::IPv4.new # matches '127.0.0.1' but not '127.0.0.1:80'
82
+ r1 = Regexy::Web::IPv4.new(:with_port) # matches '127.0.0.1:80' but not '127.0.0.1'
83
+ any_ipv4 = Regexy::Web::IPv4.new(:normal) | Regexy::Web::IPv4.new(:with_port) # matches ip w\ and w\o port
84
+ ```
85
+ ### Regexy::Web::IPv6
86
+
87
+ Generates regular expressions for matching IPv6 addresses (standard, mixed, and compressed notation are supported). Works in `:normal` (by default) and `:with_port` modes.
88
+
89
+ ```ruby
90
+ r1 = Regexy::Web::IPv6.new # matches '::1', '2001:DB8::8:800:200C:417A' and '::FFFF:129.144.52.38'
91
+ r1 = Regexy::Web::IPv6.new(:with_port) # matches '[::1]:80' and so on
92
+ any_ipv6 = Regexy::Web::IPv6.new(:normal) | Regexy::Web::IPv6.new(:with_port) # matches ip w\ and w\o port
93
+ ```
94
+ ### Regexy::Web::Url
95
+
96
+ Generates regular expressions for matching Url addresses (with unicode support).
97
+
98
+ ```ruby
99
+ r1 = Regexy::Web::Url.new # matches 'http://foo.com', 'www.foo.com' and 'foo.com'
100
+ ```
101
+
102
+ ## Contributing
103
+ Have an idea of new regular expression? Create an [issue](https://github.com/vladimir-tikhonov/regexy/issues) (some test cases will be much appreciated) or open a [pull request](https://github.com/vladimir-tikhonov/regexy/pulls).
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,61 @@
1
+ require 'forwardable'
2
+
3
+ module Regexy
4
+ class Regexp
5
+ extend Forwardable
6
+
7
+ attr_reader :internal_regexp
8
+ def_delegators :internal_regexp, *::Regexp.public_instance_methods(false)
9
+
10
+ def initialize(regexp, *args)
11
+ regexp = normalize_regexp(regexp, *args)
12
+ @internal_regexp = ::Regexp.new(regexp, *args)
13
+ end
14
+
15
+ def | other
16
+ other = ::Regexy::Regexp.new(other)
17
+ new_regexp = "#{source}|#{other.source}"
18
+ new_options = options | other.options
19
+ ::Regexy::Regexp.new(new_regexp, new_options)
20
+ end
21
+
22
+ alias_method :or, :|
23
+
24
+ def + other
25
+ other = ::Regexy::Regexp.new(other)
26
+ new_regexp = source.chomp('$') + other.source.sub(/^\^/, '')
27
+ new_options = options | other.options
28
+ ::Regexy::Regexp.new(new_regexp, new_options)
29
+ end
30
+
31
+ alias_method :and_then, :+
32
+
33
+ protected
34
+
35
+ def normalize_regexp(regexp, *args)
36
+ case regexp
37
+ when ::Regexy::Regexp then regexp.internal_regexp
38
+ when ::Regexp then args.any? ? regexp.source : regexp # allows to pass custom options to regexp
39
+ else regexp
40
+ end
41
+ end
42
+ end
43
+
44
+ class RegexpWithMode < ::Regexy::Regexp
45
+ def initialize(mode = default_mode, *args)
46
+ regexp = regexp_for(mode.to_sym)
47
+ fail ArgumentError, "Unknown mode #{mode.to_s}" unless regexp
48
+ super(regexp, *args)
49
+ end
50
+
51
+ protected
52
+
53
+ def regexp_for(mode)
54
+ fail NotImplementedError, 'Child classes should override #regexp_for method'
55
+ end
56
+
57
+ def default_mode
58
+ :normal
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module Regexy
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: UTF-8
2
+
3
+ module Regexy
4
+ module Web
5
+ class Email < ::Regexy::RegexpWithMode
6
+ EMAIL_RELAXED = /\A\s*[^@\s]+@([^@\s]+\.)+[^@\s]+\s*\z/i.freeze
7
+ EMAIL_NORMAL = /\A\s*([^@\s]{1,64})@((?:[-\p{L}\d]+\.)+\p{L}{2,})\s*\z/i.freeze
8
+ EMAIL_STRICT = /\A\s*([-\p{L}\d+._]{1,64})@((?:[-\p{L}\d]+\.)+\p{L}{2,})\s*\z/i.freeze
9
+
10
+ protected
11
+
12
+ def regexp_for(mode)
13
+ case mode
14
+ when :relaxed then EMAIL_RELAXED
15
+ when :normal then EMAIL_NORMAL
16
+ when :strict then EMAIL_STRICT
17
+ else nil
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,43 @@
1
+ module Regexy
2
+ module Web
3
+ PORT = /:([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/i.freeze
4
+
5
+ class IPv4 < ::Regexy::RegexpWithMode
6
+ IPV4_NORMAL = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/i.freeze
7
+ IPV4_WITH_PORT = ::Regexp.new(IPV4_NORMAL.source.chomp('$') + PORT.source).freeze
8
+
9
+ protected
10
+
11
+ def regexp_for(mode)
12
+ case mode
13
+ when :normal then IPV4_NORMAL
14
+ when :with_port then IPV4_WITH_PORT
15
+ else nil
16
+ end
17
+ end
18
+ end
19
+
20
+ class IPv6 < ::Regexy::RegexpWithMode
21
+ IPV6_NORMAL = /^(?:(?:(?:[A-F0-9]{1,4}:){6}|(?=(?:[A-F0-9]{0,4}:){0,6}(?:[0-9]{1,3}\.){3}
22
+ [0-9]{1,3}(?![:.\w]))(([0-9A-F]{1,4}:){0,5}|:)((:[0-9A-F]{1,4}){1,5}:|:)
23
+ |::(?:[A-F0-9]{1,4}:){5})(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|
24
+ [1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|
25
+ (?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}|(?=(?:[A-F0-9]{0,4}:){0,7}
26
+ [A-F0-9]{0,4}(?![:.\w]))(([0-9A-F]{1,4}:){1,7}|:)((:[0-9A-F]{1,4}){1,7}
27
+ |:)|(?:[A-F0-9]{1,4}:){7}:|:(:[A-F0-9]{1,4}){7})(?![:.\w])$
28
+ /ix.freeze
29
+
30
+ IPV6_WITH_PORT = ::Regexp.new('[' + IPV6_NORMAL.source.chomp('$') + ']' + PORT.source).freeze
31
+
32
+ protected
33
+
34
+ def regexp_for(mode)
35
+ case mode
36
+ when :normal then IPV6_NORMAL
37
+ when :with_port then IPV6_WITH_PORT
38
+ else nil
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: UTF-8
2
+
3
+ module Regexy
4
+ module Web
5
+ class Url < ::Regexy::Regexp
6
+ URL = /^([a-z][a-z\d+\-.]*:(\/\/([\p{L}\d\-._~%!$&'()*+,;=]+@)?([\p{L}\d\-._~%]+|
7
+ \[[\p{L}\d:.]+\]|\[v[a-f0-9][\p{L}\d\-._~%!$&'()*+,;=:]+\])(:[0-9]+)?
8
+ (\/[\p{L}\d\-._~%!$&'()*+,;=:@]+)*\/?|(\/?[\p{L}\d\-._~%!$&'()*+,;=:@]+
9
+ (\/[\p{L}\d\-._~%!$&'()*+,;=:@]+)*\/?)?)|([\p{L}\d\-._~%!$&'()*+,;=@]+
10
+ (\/[\p{L}\d\-._~%!$&'()*+,;=:@]+)*\/?|(\/[\p{L}\d\-._~%!$&'()*+,;=:@]+)
11
+ +\/?))
12
+ (\?[\p{L}\d\-._~%!$&'()*+,;=:@\/?]*)?(\#[\p{L}\d\-._~%!$&'()*+,;=:@\/?]*)?$
13
+ /ix.freeze
14
+
15
+ def initialize(*args)
16
+ super(URL, *args)
17
+ end
18
+ end
19
+ end
20
+ end
data/lib/regexy/web.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Regexy
2
+ module Web
3
+ autoload :Email, 'regexy/web/email'
4
+ autoload :IPv4, 'regexy/web/ip'
5
+ autoload :IPv6, 'regexy/web/ip'
6
+ autoload :Url, 'regexy/web/url'
7
+ end
8
+ end
data/lib/regexy.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'regexy/version'
2
+
3
+ module Regexy
4
+ autoload :Regexp, 'regexy/regexp'
5
+ autoload :Web, 'regexy/web'
6
+ end
data/regexy.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'regexy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'regexy'
8
+ spec.version = Regexy::VERSION
9
+ spec.authors = ['Vladimir Tikhonov']
10
+ spec.email = ['vladimir@tikhonov.by']
11
+
12
+ spec.summary = 'Regexy is the collection of useful ruby regular expressions'
13
+ spec.description = 'Regexy contains a lot of common-use regular expressions and provides a friendly syntax to combine them.'
14
+ spec.homepage = 'https://github.com/vladimir-tikhonov/regexy'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'rspec', '~> 3.2'
21
+ spec.add_development_dependency 'bundler', '~> 1.8'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'coveralls', '~> 0.7'
24
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: regexy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Tikhonov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.7'
69
+ description: Regexy contains a lot of common-use regular expressions and provides
70
+ a friendly syntax to combine them.
71
+ email:
72
+ - vladimir@tikhonov.by
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".coveralls.yml"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - LICENSE
83
+ - README.md
84
+ - Rakefile
85
+ - lib/regexy.rb
86
+ - lib/regexy/regexp.rb
87
+ - lib/regexy/version.rb
88
+ - lib/regexy/web.rb
89
+ - lib/regexy/web/email.rb
90
+ - lib/regexy/web/ip.rb
91
+ - lib/regexy/web/url.rb
92
+ - regexy.gemspec
93
+ homepage: https://github.com/vladimir-tikhonov/regexy
94
+ licenses:
95
+ - MIT
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.2.2
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Regexy is the collection of useful ruby regular expressions
117
+ test_files: []