librariesio-url-parser 1.0.0

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
+ SHA256:
3
+ metadata.gz: 4095a2003d55e594f33b981cb4fa104b37ab2ffba7f6f3988b905a901afb9785
4
+ data.tar.gz: 8dfbab7448f15692cd1064eb119cd55101d5fceab25dd806930780c48f3a65cd
5
+ SHA512:
6
+ metadata.gz: 5aa7f74fcbaa9d1dc7774d61eedcbf349028a4e3bef26dc4a9e3b07e3856cdc2559feff970a36becf6e2c4e10eb4be77b68969470f814cd422a47e4f800e940e
7
+ data.tar.gz: f3dfc1047f3141e6a4f9ea39f7ac7514bbb607bd5f2577ac0809903614b3e008d7bb48916a01cb537886f5be0bad4fb793b9130e43d212d85fc0061bf1fb8161
@@ -0,0 +1,22 @@
1
+ version: 2.1
2
+ orbs:
3
+ ruby: circleci/ruby@1.8.0
4
+
5
+ jobs:
6
+ test:
7
+ docker:
8
+ - image: cimg/ruby:2.6.5
9
+ executor: ruby/default
10
+ steps:
11
+ - checkout
12
+ - run:
13
+ name: Which bundler?
14
+ command: bundle -v
15
+ - ruby/install-deps
16
+ - ruby/rspec-test
17
+
18
+ workflows:
19
+ version: 2.1
20
+ test:
21
+ jobs:
22
+ - test
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.5
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+ ruby "2.6.5"
3
+
4
+ # Specify your gem's dependencies in librariesio-url-parser.gemspec
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ librariesio-url-parser (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.5.0)
10
+ rake (12.3.3)
11
+ rspec (3.11.0)
12
+ rspec-core (~> 3.11.0)
13
+ rspec-expectations (~> 3.11.0)
14
+ rspec-mocks (~> 3.11.0)
15
+ rspec-core (3.11.0)
16
+ rspec-support (~> 3.11.0)
17
+ rspec-expectations (3.11.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.11.0)
20
+ rspec-mocks (3.11.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.11.0)
23
+ rspec-support (3.11.0)
24
+ rspec_junit_formatter (0.5.1)
25
+ rspec-core (>= 2, < 4, != 2.12.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ librariesio-url-parser!
32
+ rake (~> 12.0)
33
+ rspec (~> 3.0)
34
+ rspec_junit_formatter (~> 0.5)
35
+
36
+ RUBY VERSION
37
+ ruby 2.6.5p114
38
+
39
+ BUNDLED WITH
40
+ 2.1.4
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Libraries.io URL Parser
2
+
3
+ Repository URL parsing library for https://libraries.io
4
+
5
+ ## Usage
6
+
7
+ Parse a org/repo string from an input string:
8
+
9
+ ```ruby
10
+ GithubURLParser.parse("https://github.com/rails/rails/") #=> rails/rails
11
+ GithubURLParser.parse("git@github.com:rails/rails.git") #=> rails/rails
12
+ GithubURLParser.parse("https://github.com/rails/rails.git") #=> rails/rails
13
+ GithubURLParser.parse("https://github.com") #=> nil
14
+ ```
15
+
16
+ Parse a full url from an input string:
17
+
18
+ ```ruby
19
+ GithubURLParser.parse_to_full_url("https://github.com/rails/rails/") #=> https://github.com/rails/rails
20
+ GithubURLParser.parse_to_full_url("git@github.com:rails/rails.git") #=> https://github.com/rails/rails
21
+ GithubURLParser.parse_to_full_url("https://github.com/rails/rails.git") #=> https://github.com/rails/rails
22
+ GithubURLParser.parse_to_full_url("https://github.com") #=> nil
23
+ ```
24
+
25
+ Parse an org/user url from an input string:
26
+
27
+ ```ruby
28
+ GithubURLParser.parse_to_full_user_url("https://github.com/rails/rails/") #=> nil
29
+ GithubURLParser.parse_to_full_user_url("git@github.com:rails/rails.git") #=> nil
30
+ GithubURLParser.parse_to_full_user_url("https://github.com/rails/rails.git") #=> nil
31
+ GithubURLParser.parse_to_full_user_url("https://github.com") #=> nil
32
+ GithubURLParser.parse_to_full_user_url("https://github.com/rails") #=> https://github.com/rails
33
+ ```
34
+
35
+ Parse a full url from an input string where the repository is unknown
36
+
37
+ ```ruby
38
+ URLParser.try_all("git@github.com:rails/rails.git") #=> https://github.com/rails/rails
39
+ URLParser.try_all("git@gitlab.com:inkscape/inkscape.git") #=> "https://gitlab.com/inkscape/inkscape"
40
+ URLParser.try_all("git@bitbucket.org:tildeslash/monit.git") #=> "https://bitbucket.org/tildeslash/monit"
41
+ ```
42
+
43
+ ## Supported repositories
44
+
45
+ - GitHub
46
+ - GitLab
47
+ - Bitbucket
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests.
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/librariesio/librariesio-url-parser. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
56
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ class BitbucketURLParser < URLParser
3
+ private
4
+
5
+ def full_domain
6
+ 'https://bitbucket.org'
7
+ end
8
+
9
+ def tlds
10
+ %w(com org)
11
+ end
12
+
13
+ def domain
14
+ 'bitbucket'
15
+ end
16
+
17
+ def remove_domain
18
+ url.gsub!(/(bitbucket.com|bitbucket.org)+?(:|\/)?/i, '')
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ class GithubURLParser < URLParser
3
+ private
4
+
5
+ def full_domain
6
+ 'https://github.com'
7
+ end
8
+
9
+ def tlds
10
+ %w(io com org)
11
+ end
12
+
13
+ def domain
14
+ 'github'
15
+ end
16
+
17
+ def remove_domain
18
+ url.gsub!(/(github.io|github.com|github.org|raw.githubusercontent.com)+?(:|\/)?/i, '')
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ class GitlabURLParser < URLParser
3
+ private
4
+
5
+ def full_domain
6
+ 'https://gitlab.com'
7
+ end
8
+
9
+ def tlds
10
+ %w(com)
11
+ end
12
+
13
+ def domain
14
+ 'gitlab'
15
+ end
16
+
17
+ def remove_domain
18
+ url.gsub!(/(gitlab.com)+?(:|\/)?/i, '')
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "url_parser"
4
+ require_relative "bitbucket_url_parser"
5
+ require_relative "github_url_parser"
6
+ require_relative "gitlab_url_parser"
7
+
8
+ module LibrariesioURLParser
9
+ VERSION = "1.0.0"
10
+ end
data/lib/url_parser.rb ADDED
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: true
2
+ class URLParser
3
+ def self.parse(url)
4
+ new(url).parse
5
+ end
6
+
7
+ def initialize(url)
8
+ @url = url.to_s.dup
9
+ end
10
+
11
+ def parse
12
+ return nil unless parseable?
13
+
14
+ if url = extractable_early?
15
+ url
16
+ else
17
+ clean_url
18
+ format_url
19
+ end
20
+ end
21
+
22
+ def self.parse_to_full_url(url)
23
+ new(url).parse_to_full_url
24
+ end
25
+
26
+ def self.parse_to_full_user_url(url)
27
+ new(url).parse_to_full_user_url
28
+ end
29
+
30
+ def self.try_all(url)
31
+ GithubURLParser.parse_to_full_url(url) ||
32
+ GitlabURLParser.parse_to_full_url(url) ||
33
+ BitbucketURLParser.parse_to_full_url(url)
34
+ end
35
+
36
+ def parse_to_full_url
37
+ path = parse
38
+ return nil unless path.present?
39
+ [full_domain, path].join('/')
40
+ end
41
+
42
+ def parse_to_full_user_url
43
+ return nil unless parseable?
44
+ path = clean_url
45
+ return nil unless path.length == 1
46
+ [full_domain, path].join('/')
47
+ end
48
+
49
+ private
50
+
51
+ attr_accessor :url
52
+
53
+ def clean_url
54
+ remove_whitespace
55
+ remove_brackets
56
+ remove_anchors
57
+ remove_querystring
58
+ remove_auth_user
59
+ remove_equals_sign
60
+ remove_scheme
61
+ return nil unless includes_domain?
62
+ remove_subdomain
63
+ remove_domain
64
+ remove_git_extension
65
+ remove_git_scheme
66
+ remove_extra_segments
67
+ end
68
+
69
+ def format_url
70
+ return nil unless url.length == 2
71
+ url.join('/')
72
+ end
73
+
74
+ def parseable?
75
+ !url.nil? && url.include?(domain)
76
+ end
77
+
78
+ def tlds
79
+ raise NotImplementedError
80
+ end
81
+
82
+ def domain
83
+ raise NotImplementedError
84
+ end
85
+
86
+ def includes_domain?
87
+ raise NotImplementedError
88
+ end
89
+
90
+ def extractable_early?
91
+ raise NotImplementedError
92
+ end
93
+
94
+ def domain_regex
95
+ "#{domain}\.(#{tlds.join('|')})"
96
+ end
97
+
98
+ def website_url?
99
+ url.match(/www\.#{domain_regex}/i)
100
+ end
101
+
102
+ def includes_domain?
103
+ url.match(/#{domain_regex}/i)
104
+ end
105
+
106
+ def extractable_early?
107
+ return false if website_url?
108
+
109
+ match = url.match(/([\w\.@\:\-_~]+)\.#{domain_regex}\/([\w\.@\:\-\_\~]+)/i)
110
+ if match && match.length == 4
111
+ return "#{match[1]}/#{match[3]}"
112
+ end
113
+
114
+ nil
115
+ end
116
+
117
+ def remove_anchors
118
+ url.gsub!(/(#\S*)$/i, '')
119
+ end
120
+
121
+ def remove_auth_user
122
+ self.url = url.split('@')[-1]
123
+ end
124
+
125
+ def remove_domain
126
+ raise NotImplementedError
127
+ end
128
+
129
+ def remove_brackets
130
+ url.gsub!(/>|<|\(|\)|\[|\]/, '')
131
+ end
132
+
133
+ def remove_equals_sign
134
+ self.url = url.split('=')[-1]
135
+ end
136
+
137
+ def remove_extra_segments
138
+ self.url = url.split('/').reject{ |s| s.strip.empty? }[0..1]
139
+ end
140
+
141
+ def remove_git_extension
142
+ url.gsub!(/(\.git|\/)$/i, '')
143
+ end
144
+
145
+ def remove_git_scheme
146
+ url.gsub!(/git\/\//i, '')
147
+ end
148
+
149
+ def remove_querystring
150
+ url.gsub!(/(\?\S*)$/i, '')
151
+ end
152
+
153
+ def remove_scheme
154
+ url.gsub!(/(((git\+https|git|ssh|hg|svn|scm|http|https)+?:)+?)/i, '')
155
+ end
156
+
157
+ def remove_subdomain
158
+ url.gsub!(/(www|ssh|raw|git|wiki)+?\./i, '')
159
+ end
160
+
161
+ def remove_whitespace
162
+ url.gsub!(/\s/, '')
163
+ end
164
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "librariesio-url-parser"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "librariesio-url-parser"
8
+ spec.version = LibrariesioURLParser::VERSION
9
+ spec.authors = ["Matt Pace"]
10
+ spec.email = ["matt.pace@tidelift.com"]
11
+
12
+ spec.summary = "Parse the URL for various repositories tracked by libraries.io"
13
+ spec.homepage = "https://github.com/librariesio/librariesio-url-parser"
14
+ spec.license = "AGPL-3.0"
15
+ spec.metadata = { "rubygems_mfa_required" => "true" }
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 "rake", "~> 12.0"
21
+ spec.add_development_dependency "rspec", "~> 3.0"
22
+ spec.add_development_dependency "rspec_junit_formatter", "~> 0.5"
23
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: librariesio-url-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Pace
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-06-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '12.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '12.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec_junit_formatter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.5'
55
+ description:
56
+ email:
57
+ - matt.pace@tidelift.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".circleci/config.yml"
63
+ - ".ruby-version"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - README.md
67
+ - Rakefile
68
+ - lib/bitbucket_url_parser.rb
69
+ - lib/github_url_parser.rb
70
+ - lib/gitlab_url_parser.rb
71
+ - lib/librariesio-url-parser.rb
72
+ - lib/url_parser.rb
73
+ - librariesio-url-parser.gemspec
74
+ homepage: https://github.com/librariesio/librariesio-url-parser
75
+ licenses:
76
+ - AGPL-3.0
77
+ metadata:
78
+ rubygems_mfa_required: 'true'
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 3.0.9
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Parse the URL for various repositories tracked by libraries.io
98
+ test_files: []