network_utils 0.1.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 +7 -0
- data/.editorconfig +12 -0
- data/.gitignore +88 -0
- data/.rspec +3 -0
- data/.rubocop.yml +34 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +70 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/img/network_utils.png +0 -0
- data/lib/network_utils/port.rb +101 -0
- data/lib/network_utils/url_info.rb +98 -0
- data/lib/network_utils/version.rb +7 -0
- data/lib/network_utils.rb +5 -0
- data/network_utils.gemspec +42 -0
- metadata +287 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 74ef9537c6f2145e0c4ea57ab0363643a9506b65
|
|
4
|
+
data.tar.gz: 4af84ef8727e5db694017bdeb02e67ada0b2a753
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 667ecbe24cd465348ab82fb8c07e53bfd6dba56888ced38c0193eaafd30f6e9f5767cc02b1162d5a901d34c60abccdc803790c7ee17e645cfbab4a2804fe992c
|
|
7
|
+
data.tar.gz: 9070ca698aba7150c08d2382f6568d3f0766e45a9e6746ed67270c5585de0dce44e49ad3caed6b1b74606ee7742a9e4f4060f491ccd37d97d89d07ee979aaa27
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/.bundle/
|
|
2
|
+
/.yardoc
|
|
3
|
+
/Gemfile.lock
|
|
4
|
+
/_yardoc/
|
|
5
|
+
/coverage/
|
|
6
|
+
/doc/
|
|
7
|
+
/pkg/
|
|
8
|
+
/spec/reports/
|
|
9
|
+
/tmp/
|
|
10
|
+
|
|
11
|
+
# rspec failure tracking
|
|
12
|
+
.rspec_status
|
|
13
|
+
|
|
14
|
+
### Ruby ###
|
|
15
|
+
.byebug_history
|
|
16
|
+
*.gem
|
|
17
|
+
|
|
18
|
+
### macOS ###
|
|
19
|
+
*.DS_Store
|
|
20
|
+
.AppleDouble
|
|
21
|
+
.LSOverride
|
|
22
|
+
|
|
23
|
+
# Icon must end with two \r
|
|
24
|
+
Icon
|
|
25
|
+
|
|
26
|
+
# Thumbnails
|
|
27
|
+
._*
|
|
28
|
+
|
|
29
|
+
# Files that might appear in the root of a volume
|
|
30
|
+
.DocumentRevisions-V100
|
|
31
|
+
.fseventsd
|
|
32
|
+
.Spotlight-V100
|
|
33
|
+
.TemporaryItems
|
|
34
|
+
.Trashes
|
|
35
|
+
.VolumeIcon.icns
|
|
36
|
+
.com.apple.timemachine.donotpresent
|
|
37
|
+
|
|
38
|
+
# Directories potentially created on remote AFP share
|
|
39
|
+
.AppleDB
|
|
40
|
+
.AppleDesktop
|
|
41
|
+
Network Trash Folder
|
|
42
|
+
Temporary Items
|
|
43
|
+
.apdisk
|
|
44
|
+
|
|
45
|
+
### Config ###
|
|
46
|
+
/config/*
|
|
47
|
+
!/config/*.sample
|
|
48
|
+
|
|
49
|
+
# Used by dotenv library to load environment variables.
|
|
50
|
+
.env
|
|
51
|
+
.env.*
|
|
52
|
+
.env.local
|
|
53
|
+
.env.local.*
|
|
54
|
+
!.env.local.test
|
|
55
|
+
|
|
56
|
+
## Documentation cache and generated files:
|
|
57
|
+
/.yardoc/
|
|
58
|
+
/_yardoc/
|
|
59
|
+
/doc/
|
|
60
|
+
/rdoc/
|
|
61
|
+
|
|
62
|
+
## Environment normalization:
|
|
63
|
+
/.bundle/
|
|
64
|
+
/vendor/bundle
|
|
65
|
+
/lib/bundler/man/
|
|
66
|
+
|
|
67
|
+
# for a library or gem, you might want to ignore these files since the code is
|
|
68
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
69
|
+
Gemfile.lock
|
|
70
|
+
.ruby-version
|
|
71
|
+
.ruby-gemset
|
|
72
|
+
|
|
73
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
|
74
|
+
.rvmrc
|
|
75
|
+
|
|
76
|
+
### Vim ###
|
|
77
|
+
# swap
|
|
78
|
+
[._]*.s[a-v][a-z]
|
|
79
|
+
[._]*.sw[a-p]
|
|
80
|
+
[._]s[a-v][a-z]
|
|
81
|
+
[._]sw[a-p]
|
|
82
|
+
# session
|
|
83
|
+
Session.vim
|
|
84
|
+
# temporary
|
|
85
|
+
.netrwhist
|
|
86
|
+
*~
|
|
87
|
+
# auto-generated tag files
|
|
88
|
+
tags
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
DisabledByDefault: false
|
|
3
|
+
TargetRubyVersion: 2.4
|
|
4
|
+
Exclude:
|
|
5
|
+
- 'spec/fixtures/**/*'
|
|
6
|
+
|
|
7
|
+
Metrics/LineLength:
|
|
8
|
+
Max: 120
|
|
9
|
+
# To make it possible to copy or click on URIs in the code, we allow lines
|
|
10
|
+
# containing a URI to be longer than Max.
|
|
11
|
+
AllowHeredoc: true
|
|
12
|
+
AllowURI: true
|
|
13
|
+
URISchemes:
|
|
14
|
+
- http
|
|
15
|
+
- https
|
|
16
|
+
# The IgnoreCopDirectives option causes the LineLength rule to ignore cop
|
|
17
|
+
# directives like '# rubocop: enable ...' when calculating a line's length.
|
|
18
|
+
IgnoreCopDirectives: false
|
|
19
|
+
# The IgnoredPatterns option is a list of !ruby/regexp and/or string
|
|
20
|
+
# elements. Strings will be converted to Regexp objects. A line that matches
|
|
21
|
+
# any regular expression listed in this option will be ignored by LineLength.
|
|
22
|
+
IgnoredPatterns: []
|
|
23
|
+
|
|
24
|
+
Metrics/BlockLength:
|
|
25
|
+
Enabled: false
|
|
26
|
+
|
|
27
|
+
Style/AsciiComments:
|
|
28
|
+
Enabled: false
|
|
29
|
+
|
|
30
|
+
Metrics/MethodLength:
|
|
31
|
+
Max: 25
|
|
32
|
+
|
|
33
|
+
Metrics/AbcSize:
|
|
34
|
+
Max: 20
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at smileart21@gmail.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 smileart
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# 🛠 NetworkUtils
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img width="360" title="logo" src ="./img/network_utils.png" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
> A set of convenient network utils utils to get URL info before downloading a resource, work with ports, etc.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Add this line to your application's Gemfile:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem 'network_utils'
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
And then execute:
|
|
18
|
+
|
|
19
|
+
$ bundle
|
|
20
|
+
|
|
21
|
+
Or install it yourself as:
|
|
22
|
+
|
|
23
|
+
$ gem install network_utils
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
# NetworkUtils::UrlInfo
|
|
29
|
+
|
|
30
|
+
info = NetworkUtils::UrlInfo.new('https://www.wikipedia.org')
|
|
31
|
+
info.valid? # => true
|
|
32
|
+
info.valid_online? # => true
|
|
33
|
+
info.headers # { 'content-type': 100500, … }
|
|
34
|
+
info.is?([:text, 'text/html', 'application/xml', 'text/csv'])
|
|
35
|
+
info.content_type # => 'text/html'
|
|
36
|
+
info.size # => 100500
|
|
37
|
+
info.is?('text/html') # => true
|
|
38
|
+
info.is?(:image). # => false
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
# NetworkUtils::Port
|
|
43
|
+
|
|
44
|
+
NetworkUtils::Port.random # => 50200
|
|
45
|
+
NetworkUtils::Port.random_free # => 65000
|
|
46
|
+
|
|
47
|
+
NetworkUtils::Port.available?(65000) # => true
|
|
48
|
+
NetworkUtils::Port.free?(50200) # => false
|
|
49
|
+
|
|
50
|
+
NetworkUtils::Port.opened?(50200) # => true
|
|
51
|
+
NetworkUtils::Port.occupied?(65000) # => false
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Development
|
|
55
|
+
|
|
56
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
57
|
+
|
|
58
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
59
|
+
|
|
60
|
+
## Contributing
|
|
61
|
+
|
|
62
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/smileart/network_utils. 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.
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
|
|
66
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
67
|
+
|
|
68
|
+
## Code of Conduct
|
|
69
|
+
|
|
70
|
+
Everyone interacting in the NetworkUtils project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/smileart/network_utils/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
require 'network_utils'
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
11
|
+
# require "pry"
|
|
12
|
+
# Pry.start
|
|
13
|
+
|
|
14
|
+
require 'irb'
|
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
Binary file
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'socket'
|
|
4
|
+
require 'timeout'
|
|
5
|
+
|
|
6
|
+
##
|
|
7
|
+
# Namespace Module to wrap self-written Network tools
|
|
8
|
+
module NetworkUtils
|
|
9
|
+
##
|
|
10
|
+
# Simple class to work with ports
|
|
11
|
+
# Allows to get random port number, check availability, etc.
|
|
12
|
+
#
|
|
13
|
+
class Port
|
|
14
|
+
# The max limit for port lookup retries
|
|
15
|
+
PORT_LOOKUP_RETRY_LIMIT = 50
|
|
16
|
+
|
|
17
|
+
# Internet Assigned Numbers Authority suggested range
|
|
18
|
+
IANA_PORT_RANGE = (49_152..65_535).freeze
|
|
19
|
+
|
|
20
|
+
# Checks if the port is available (free) on the host
|
|
21
|
+
#
|
|
22
|
+
# @example
|
|
23
|
+
# NetworkUtils::Port.available?(9292)
|
|
24
|
+
# NetworkUtils::Port.available?(80, 'google.com', 100)
|
|
25
|
+
# NetworkUtils::Port.free?(80, 'google.com', 100)
|
|
26
|
+
# NetworkUtils::Port.free?(80, 'google.com', 100)
|
|
27
|
+
#
|
|
28
|
+
# @param [Integer] port the port we want to check availability of
|
|
29
|
+
# @param [String] host the host we want to check on (default: 127.0.0.1)
|
|
30
|
+
# @param [Timeout] timeout the time (seconds) we ready to wait (default: 1)
|
|
31
|
+
#
|
|
32
|
+
# @return [Boolean] result of the check (true — port is free to use, false — the port is occupied)
|
|
33
|
+
def self.available?(port, host = '127.0.0.1', timeout = 1)
|
|
34
|
+
return false unless port && host && timeout && timeout.positive?
|
|
35
|
+
|
|
36
|
+
Timeout.timeout(timeout) do
|
|
37
|
+
TCPSocket.new(host, port).close
|
|
38
|
+
false
|
|
39
|
+
end
|
|
40
|
+
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
|
|
41
|
+
true
|
|
42
|
+
rescue SocketError, Timeout::Error, Errno::EADDRNOTAVAIL
|
|
43
|
+
false
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Checks if the port is opened (occupied / being listened) on the host
|
|
47
|
+
#
|
|
48
|
+
# @example
|
|
49
|
+
# NetworkUtils::Port.opened?(443, 'google.com')
|
|
50
|
+
# NetworkUtils::Port.opened?(80, 'google.com', 1)
|
|
51
|
+
# NetworkUtils::Port.occupied?(80, 'google.com', 1)
|
|
52
|
+
# NetworkUtils::Port.occupied?(80, 'google.com', 1)
|
|
53
|
+
#
|
|
54
|
+
# @note Just the opposite of `available?`
|
|
55
|
+
#
|
|
56
|
+
# @param [Integer] port the port we want to check availability of
|
|
57
|
+
# @param [String] host the host we want to check on (default: 127.0.0.1)
|
|
58
|
+
# @param [Timeout] timeout the time (seconds) we ready to wait (default: 1)
|
|
59
|
+
#
|
|
60
|
+
# @return [Boolean] result of the check (true — the port is being listened, false — the port is free)
|
|
61
|
+
def self.opened?(port, host = '127.0.0.1', timeout = 1)
|
|
62
|
+
!available?(port, host, timeout)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Generates random port from IANA recommended range
|
|
66
|
+
#
|
|
67
|
+
# @note
|
|
68
|
+
# The Internet Assigned Numbers Authority (IANA) suggests the
|
|
69
|
+
# range 49152 to 65535 (215+214 to 216−1) for dynamic or private ports.
|
|
70
|
+
#
|
|
71
|
+
# @return [Boolean] port the port from the IANA suggested range
|
|
72
|
+
def self.random
|
|
73
|
+
rand(IANA_PORT_RANGE)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Generates random port from IANA recommended range which is free on the localhost
|
|
77
|
+
#
|
|
78
|
+
# @note
|
|
79
|
+
# The Internet Assigned Numbers Authority (IANA) suggests the
|
|
80
|
+
# range 49152 to 65535 (215+214 to 216−1) for dynamic or private ports.
|
|
81
|
+
#
|
|
82
|
+
# @return [Boolean] port the port from the IANA suggested range which is also free on the current machine
|
|
83
|
+
def self.random_free
|
|
84
|
+
PORT_LOOKUP_RETRY_LIMIT.times do
|
|
85
|
+
port = random
|
|
86
|
+
return port if available?(port)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
nil
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Add a few nice aliases
|
|
93
|
+
class << self
|
|
94
|
+
# opened? → occupied?
|
|
95
|
+
alias occupied? opened?
|
|
96
|
+
|
|
97
|
+
# available? → free?
|
|
98
|
+
alias free? available?
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'uri'
|
|
4
|
+
require 'httparty'
|
|
5
|
+
require 'addressable/uri'
|
|
6
|
+
require 'url_regex'
|
|
7
|
+
require 'active_support/core_ext/array/wrap'
|
|
8
|
+
|
|
9
|
+
##
|
|
10
|
+
# Namespace Module to wrap self-written Network tools
|
|
11
|
+
module NetworkUtils
|
|
12
|
+
##
|
|
13
|
+
# Simple class to get URL info (validation/existance, headers, content-type)
|
|
14
|
+
# Allows to get all this stuff without actually downloading huge files like
|
|
15
|
+
# CSVs, images, videos, etc.
|
|
16
|
+
#
|
|
17
|
+
class UrlInfo
|
|
18
|
+
# Initialise a UrlInfo for a particular URL
|
|
19
|
+
#
|
|
20
|
+
# @param [String] url the URL you want to get info about
|
|
21
|
+
# @param [Integer] request_timeout Max time to wait for headers from the server
|
|
22
|
+
#
|
|
23
|
+
def initialize(url, request_timeout = 10)
|
|
24
|
+
@url = url.dup.to_s.force_encoding('UTF-8')
|
|
25
|
+
@request_timeout = request_timeout
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Check the Content-Type of the resource
|
|
29
|
+
#
|
|
30
|
+
# @param [String, Symbol, Array] type the prefix (before "/") or full Content-Type content
|
|
31
|
+
# @return [Boolean] true if Content-Type matches something from the types list
|
|
32
|
+
def is?(type)
|
|
33
|
+
return false if type.to_s.empty?
|
|
34
|
+
|
|
35
|
+
expected_types = Array.wrap(type).map(&:to_s)
|
|
36
|
+
content_type && expected_types.select do |t|
|
|
37
|
+
content_type.select { |ct| ct.start_with?(t) }
|
|
38
|
+
end.any?
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Check offline URL validity
|
|
42
|
+
#
|
|
43
|
+
# @return [Boolean] true if the URL is valid from the point of view of the standard
|
|
44
|
+
def valid?
|
|
45
|
+
@url.match?(UrlRegex.get(mode: :validation))
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Check online URL validity (& format validity as well)
|
|
49
|
+
#
|
|
50
|
+
# @return [Boolean] true if the URL is valid from the point of view of the
|
|
51
|
+
# standard & exists (has headers)
|
|
52
|
+
def valid_online?
|
|
53
|
+
valid? && headers
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# A shortcut method to get the remote resource size
|
|
57
|
+
#
|
|
58
|
+
# @return [Integer] remote resource size (bytes), 0 if there's nothing
|
|
59
|
+
def size
|
|
60
|
+
headers&.fetch('content-length', 0).to_i
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# A shortcut method to get the Content-Type of the remote resource
|
|
64
|
+
#
|
|
65
|
+
# @return [String] remote resource Content-Type Header content
|
|
66
|
+
def content_type
|
|
67
|
+
headers&.fetch('content-type', nil)
|
|
68
|
+
&.split(/,\s/)
|
|
69
|
+
&.map { |ct| ct.split(/;\s/).first }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# A method to get the remote resource HTTP headers
|
|
73
|
+
# Caches the result and returns memoised version
|
|
74
|
+
#
|
|
75
|
+
# @return [Hash, nil] remote resource HTTP headers list or nil
|
|
76
|
+
def headers
|
|
77
|
+
return nil if @url.to_s.empty?
|
|
78
|
+
return nil unless (encoded_url = encode(@url))
|
|
79
|
+
|
|
80
|
+
Timeout.timeout(@request_timeout) do
|
|
81
|
+
response = HTTParty.head(encoded_url, timeout: @request_timeout)
|
|
82
|
+
raise response.response if response.response.is_a?(Net::HTTPServerError) ||
|
|
83
|
+
response.response.is_a?(Net::HTTPClientError)
|
|
84
|
+
|
|
85
|
+
@headers ||= response.headers
|
|
86
|
+
end
|
|
87
|
+
rescue SocketError, ThreadError, Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL, Timeout::Error, TypeError,
|
|
88
|
+
Net::HTTPServerError, Net::HTTPClientError
|
|
89
|
+
nil
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
private
|
|
93
|
+
|
|
94
|
+
def encode(url)
|
|
95
|
+
Addressable::URI.encode(url)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
require 'network_utils/version'
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.name = 'network_utils'
|
|
9
|
+
spec.version = NetworkUtils::VERSION
|
|
10
|
+
spec.authors = ['smileart']
|
|
11
|
+
spec.email = ['smileart21@gmail.com']
|
|
12
|
+
|
|
13
|
+
spec.summary = 'A set of convenient network utils'
|
|
14
|
+
spec.description = 'A set of utils to get URL info before downloading a resource, work with ports, etc.'
|
|
15
|
+
spec.homepage = 'http://github.com/smileart/network_utils'
|
|
16
|
+
spec.license = 'MIT'
|
|
17
|
+
|
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
|
20
|
+
end
|
|
21
|
+
spec.bindir = 'exe'
|
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
23
|
+
spec.require_paths = ['lib']
|
|
24
|
+
|
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
|
26
|
+
spec.add_development_dependency 'byebug', '~> 9.1'
|
|
27
|
+
spec.add_development_dependency 'inch', '~> 0.7'
|
|
28
|
+
spec.add_development_dependency 'letters', '~> 0.4'
|
|
29
|
+
spec.add_development_dependency 'rake', '~> 12.2'
|
|
30
|
+
spec.add_development_dependency 'rspec', '~> 3.7'
|
|
31
|
+
spec.add_development_dependency 'rubocop', '~> 0.51'
|
|
32
|
+
spec.add_development_dependency 'rubygems-tasks', '~> 0.2'
|
|
33
|
+
spec.add_development_dependency 'simplecov', '~> 0.15'
|
|
34
|
+
spec.add_development_dependency 'vcr', '~> 3.0'
|
|
35
|
+
spec.add_development_dependency 'webmock', '~> 3.1'
|
|
36
|
+
spec.add_development_dependency 'yard', '~> 0.8'
|
|
37
|
+
|
|
38
|
+
spec.add_dependency 'activesupport', '~> 5.1'
|
|
39
|
+
spec.add_dependency 'addressable', '~> 2.5'
|
|
40
|
+
spec.add_dependency 'httparty', '~> 0.15'
|
|
41
|
+
spec.add_dependency 'url_regex', '~> 0.0.3'
|
|
42
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: network_utils
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- smileart
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-12-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.16'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.16'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: byebug
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '9.1'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '9.1'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: inch
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0.7'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0.7'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: letters
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0.4'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0.4'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rake
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '12.2'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '12.2'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: rspec
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '3.7'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '3.7'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rubocop
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - "~>"
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '0.51'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - "~>"
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0.51'
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: rubygems-tasks
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - "~>"
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0.2'
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - "~>"
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: '0.2'
|
|
125
|
+
- !ruby/object:Gem::Dependency
|
|
126
|
+
name: simplecov
|
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - "~>"
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '0.15'
|
|
132
|
+
type: :development
|
|
133
|
+
prerelease: false
|
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - "~>"
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: '0.15'
|
|
139
|
+
- !ruby/object:Gem::Dependency
|
|
140
|
+
name: vcr
|
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
|
142
|
+
requirements:
|
|
143
|
+
- - "~>"
|
|
144
|
+
- !ruby/object:Gem::Version
|
|
145
|
+
version: '3.0'
|
|
146
|
+
type: :development
|
|
147
|
+
prerelease: false
|
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
149
|
+
requirements:
|
|
150
|
+
- - "~>"
|
|
151
|
+
- !ruby/object:Gem::Version
|
|
152
|
+
version: '3.0'
|
|
153
|
+
- !ruby/object:Gem::Dependency
|
|
154
|
+
name: webmock
|
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
|
156
|
+
requirements:
|
|
157
|
+
- - "~>"
|
|
158
|
+
- !ruby/object:Gem::Version
|
|
159
|
+
version: '3.1'
|
|
160
|
+
type: :development
|
|
161
|
+
prerelease: false
|
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
163
|
+
requirements:
|
|
164
|
+
- - "~>"
|
|
165
|
+
- !ruby/object:Gem::Version
|
|
166
|
+
version: '3.1'
|
|
167
|
+
- !ruby/object:Gem::Dependency
|
|
168
|
+
name: yard
|
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
|
170
|
+
requirements:
|
|
171
|
+
- - "~>"
|
|
172
|
+
- !ruby/object:Gem::Version
|
|
173
|
+
version: '0.8'
|
|
174
|
+
type: :development
|
|
175
|
+
prerelease: false
|
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
177
|
+
requirements:
|
|
178
|
+
- - "~>"
|
|
179
|
+
- !ruby/object:Gem::Version
|
|
180
|
+
version: '0.8'
|
|
181
|
+
- !ruby/object:Gem::Dependency
|
|
182
|
+
name: activesupport
|
|
183
|
+
requirement: !ruby/object:Gem::Requirement
|
|
184
|
+
requirements:
|
|
185
|
+
- - "~>"
|
|
186
|
+
- !ruby/object:Gem::Version
|
|
187
|
+
version: '5.1'
|
|
188
|
+
type: :runtime
|
|
189
|
+
prerelease: false
|
|
190
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
191
|
+
requirements:
|
|
192
|
+
- - "~>"
|
|
193
|
+
- !ruby/object:Gem::Version
|
|
194
|
+
version: '5.1'
|
|
195
|
+
- !ruby/object:Gem::Dependency
|
|
196
|
+
name: addressable
|
|
197
|
+
requirement: !ruby/object:Gem::Requirement
|
|
198
|
+
requirements:
|
|
199
|
+
- - "~>"
|
|
200
|
+
- !ruby/object:Gem::Version
|
|
201
|
+
version: '2.5'
|
|
202
|
+
type: :runtime
|
|
203
|
+
prerelease: false
|
|
204
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
205
|
+
requirements:
|
|
206
|
+
- - "~>"
|
|
207
|
+
- !ruby/object:Gem::Version
|
|
208
|
+
version: '2.5'
|
|
209
|
+
- !ruby/object:Gem::Dependency
|
|
210
|
+
name: httparty
|
|
211
|
+
requirement: !ruby/object:Gem::Requirement
|
|
212
|
+
requirements:
|
|
213
|
+
- - "~>"
|
|
214
|
+
- !ruby/object:Gem::Version
|
|
215
|
+
version: '0.15'
|
|
216
|
+
type: :runtime
|
|
217
|
+
prerelease: false
|
|
218
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
219
|
+
requirements:
|
|
220
|
+
- - "~>"
|
|
221
|
+
- !ruby/object:Gem::Version
|
|
222
|
+
version: '0.15'
|
|
223
|
+
- !ruby/object:Gem::Dependency
|
|
224
|
+
name: url_regex
|
|
225
|
+
requirement: !ruby/object:Gem::Requirement
|
|
226
|
+
requirements:
|
|
227
|
+
- - "~>"
|
|
228
|
+
- !ruby/object:Gem::Version
|
|
229
|
+
version: 0.0.3
|
|
230
|
+
type: :runtime
|
|
231
|
+
prerelease: false
|
|
232
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
233
|
+
requirements:
|
|
234
|
+
- - "~>"
|
|
235
|
+
- !ruby/object:Gem::Version
|
|
236
|
+
version: 0.0.3
|
|
237
|
+
description: A set of utils to get URL info before downloading a resource, work with
|
|
238
|
+
ports, etc.
|
|
239
|
+
email:
|
|
240
|
+
- smileart21@gmail.com
|
|
241
|
+
executables: []
|
|
242
|
+
extensions: []
|
|
243
|
+
extra_rdoc_files: []
|
|
244
|
+
files:
|
|
245
|
+
- ".editorconfig"
|
|
246
|
+
- ".gitignore"
|
|
247
|
+
- ".rspec"
|
|
248
|
+
- ".rubocop.yml"
|
|
249
|
+
- ".travis.yml"
|
|
250
|
+
- CODE_OF_CONDUCT.md
|
|
251
|
+
- Gemfile
|
|
252
|
+
- LICENSE.txt
|
|
253
|
+
- README.md
|
|
254
|
+
- Rakefile
|
|
255
|
+
- bin/console
|
|
256
|
+
- bin/setup
|
|
257
|
+
- img/network_utils.png
|
|
258
|
+
- lib/network_utils.rb
|
|
259
|
+
- lib/network_utils/port.rb
|
|
260
|
+
- lib/network_utils/url_info.rb
|
|
261
|
+
- lib/network_utils/version.rb
|
|
262
|
+
- network_utils.gemspec
|
|
263
|
+
homepage: http://github.com/smileart/network_utils
|
|
264
|
+
licenses:
|
|
265
|
+
- MIT
|
|
266
|
+
metadata: {}
|
|
267
|
+
post_install_message:
|
|
268
|
+
rdoc_options: []
|
|
269
|
+
require_paths:
|
|
270
|
+
- lib
|
|
271
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
272
|
+
requirements:
|
|
273
|
+
- - ">="
|
|
274
|
+
- !ruby/object:Gem::Version
|
|
275
|
+
version: '0'
|
|
276
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
277
|
+
requirements:
|
|
278
|
+
- - ">="
|
|
279
|
+
- !ruby/object:Gem::Version
|
|
280
|
+
version: '0'
|
|
281
|
+
requirements: []
|
|
282
|
+
rubyforge_project:
|
|
283
|
+
rubygems_version: 2.6.14
|
|
284
|
+
signing_key:
|
|
285
|
+
specification_version: 4
|
|
286
|
+
summary: A set of convenient network utils
|
|
287
|
+
test_files: []
|