network_utils 0.1.2 → 0.1.3

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
  SHA256:
3
- metadata.gz: 6a623edd8bf786902727640bbddb21d8c31e215aca5f50d745b92b1026cd34d3
4
- data.tar.gz: 8fb3e2c6b95c70e2cdb5cb29f364cba3fa9b674b2f50bdf0d30220a0cc506f7d
3
+ metadata.gz: d72ff1687c23b9699d0e45838f3530153b2583043beece802807a8a611b504f6
4
+ data.tar.gz: d2bb6f5bab2872ba4173af0482b39f9aea86a14d801bcd69052776bd5ff72204
5
5
  SHA512:
6
- metadata.gz: 9a65b5d09e44263da26d47501a3f1e015c5b97d7c4702cb897be81e4dabe10d2e6177d938b96b8dc1ae59ff0ef8d3dfb3b1370f5d8332d0d05afd7b637d89667
7
- data.tar.gz: 878e09a9433dea297e8f692e532b92cad63829389229eb8a82b69556251bd87616d8d0dc6f3fb2b5f1f0d6716bdaffadd8ed602dd56ce4f9bf178938e82f819d
6
+ metadata.gz: 184ab1705999597e786fbfc9972207d8f6524f245f412dc04b7e43c7c5391196455201890c4706e83451ace42382da3681eb4f4a6ec0bc651d1e3d8cba59af13
7
+ data.tar.gz: a24a0373043314f6bd066654b041b5117af68364946f08432d4f814e4b85a63be0ee965ce16782c0f35a7ec37337b6266174506bc33ea31912ba15b4227b1a04
data/.gitignore CHANGED
@@ -7,6 +7,8 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .vscode
11
+ /log/
10
12
 
11
13
  # rspec failure tracking
12
14
  .rspec_status
@@ -86,6 +88,3 @@ Session.vim
86
88
  *~
87
89
  # auto-generated tag files
88
90
  tags
89
-
90
- ### VSCode ###
91
- .vscode
@@ -1,5 +1,9 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.4.1
5
- before_install: gem install bundler -v 1.16.0
4
+ - 2.5
5
+ - 2.7
6
+ before_install:
7
+ - sudo apt-get update
8
+ - sudo apt-get -y install netbase
9
+ - gem install bundler -v 2.1
data/README.md CHANGED
@@ -4,7 +4,8 @@
4
4
  <img width="360" title="logo" src ="./img/network_utils.png" />
5
5
  </p>
6
6
 
7
- > A set of convenient network utils to get URL info before downloading a resource, work with ports, etc.
7
+ > A set of convenient network utils utils to get URL info before downloading a resource, work with ports, etc.
8
+ > [![Build Status](https://travis-ci.org/smileart/network_utils.svg?branch=master)](https://travis-ci.org/smileart/network_utils)
8
9
 
9
10
  ## Installation
10
11
 
@@ -35,20 +36,26 @@ info.is?([:text, 'text/html', 'application/xml', 'text/csv'])
35
36
  info.content_type # => 'text/html'
36
37
  info.size # => 100500
37
38
  info.is?('text/html') # => true
38
- info.is?(:image) # => false
39
+ info.is?(:image). # => false
39
40
  ```
40
41
 
41
42
  ```ruby
42
43
  # NetworkUtils::Port
43
44
 
44
- NetworkUtils::Port.random # => 50200
45
- NetworkUtils::Port.random_free # => 65000
45
+ NetworkUtils::Port.random # => 50200
46
+ NetworkUtils::Port.random_free # => 65000
46
47
 
47
48
  NetworkUtils::Port.available?(65000) # => true
48
49
  NetworkUtils::Port.free?(50200) # => false
49
50
 
50
51
  NetworkUtils::Port.opened?(50200) # => true
51
52
  NetworkUtils::Port.occupied?(65000) # => false
53
+
54
+ NetworkUtils::Port.name(8080) # => ["http-alt"]
55
+ NetworkUtils::Port.service(8080) # => [
56
+ # {:name=>"http-alt", :port=>8080, :protocol=>:udp, :description=>"HTTP Alternate (see port 80)"},
57
+ # {:name=>"http-alt", :port=>8080, :protocol=>:tcp, :description=>"HTTP Alternate (see port 80)"}
58
+ # ]
52
59
  ```
53
60
 
54
61
  ## Development
File without changes
data/bin/setup CHANGED
File without changes
@@ -3,6 +3,8 @@
3
3
  require 'socket'
4
4
  require 'timeout'
5
5
 
6
+ require_relative '../tapp_printer' if ENV['DEBUG']
7
+
6
8
  ##
7
9
  # Namespace Module to wrap self-written Network tools
8
10
  module NetworkUtils
@@ -17,6 +19,10 @@ module NetworkUtils
17
19
  # Internet Assigned Numbers Authority suggested range
18
20
  IANA_PORT_RANGE = (49_152..65_535).freeze
19
21
 
22
+ # Current system's IANA port assignments file
23
+ # Cound be changed using SERVICES_FILE_PATH ENV variable
24
+ SERVICES_FILE_PATH = '/etc/services'
25
+
20
26
  # Checks if the port is available (free) on the host
21
27
  #
22
28
  # @example
@@ -89,6 +95,59 @@ module NetworkUtils
89
95
  nil
90
96
  end
91
97
 
98
+ # Checks the IANA port assignments file for port info
99
+ #
100
+ # @example
101
+ # NetworkUtils::Port.service(8080)
102
+ # => [
103
+ # {:name=>"http-alt", :port=>8080, :protocol=>:udp, :description=>"HTTP Alternate (see port 80)"},
104
+ # {:name=>"http-alt", :port=>8080, :protocol=>:tcp, :description=>"HTTP Alternate (see port 80)"}
105
+ # ]
106
+ #
107
+ # @note When looking just for a short name, use ::name
108
+ #
109
+ # @param [Integer] port the port we want to check out
110
+ #
111
+ # @return [Array] port services info with the :name, :port, :protocol, :description
112
+ def self.service(port)
113
+ # check the IANA port assignments file (default or custom)
114
+ services_file = ENV['SERVICES_FILE_PATH'] || SERVICES_FILE_PATH
115
+ return nil unless File.exist?(services_file)
116
+
117
+ # read the file and extract info (ony lines matching "bacnet 47808/tcp # Building Automation and Control Networks" format)
118
+ services = File.read(services_file).lines.map do |line|
119
+ line_elements = line.split(/\s+/)
120
+
121
+ next unless line_elements[1] =~ /\d+\//
122
+
123
+ known_port, known_protocol = line_elements[1].split('/')
124
+
125
+ {
126
+ name: line_elements[0],
127
+ port: known_port.to_i,
128
+ protocol: known_protocol.to_sym,
129
+ description: line_elements[3..-1]&.join(' ')
130
+ }
131
+ end
132
+
133
+ # extract infor about the requested port
134
+ Array.wrap(services.compact.find_all { |s| s[:port] == port })
135
+ end
136
+
137
+ # Checks the IANA port assignments file and returns possible service name
138
+ #
139
+ # @example
140
+ # NetworkUtils::Port.name(8080) => ["http-alt"]
141
+ #
142
+ # @note Just a convinience method over ::service
143
+ #
144
+ # @param [Integer] port the port we want to check out
145
+ #
146
+ # @return [Array] port's possible serivce names
147
+ def self.name(port)
148
+ self.service(port).map { |s| s.fetch(:name) }.uniq
149
+ end
150
+
92
151
  # Add a few nice aliases
93
152
  class << self
94
153
  # opened? → occupied?
@@ -6,9 +6,15 @@ require 'addressable/uri'
6
6
  require 'url_regex'
7
7
  require 'active_support/core_ext/array/wrap'
8
8
 
9
+ require_relative '../tapp_printer' if ENV['DEBUG']
10
+
9
11
  ##
10
12
  # Namespace Module to wrap self-written Network tools
11
13
  module NetworkUtils
14
+ # Additional time to add to Ruby timeout as a workound
15
+ # for HTTParty timeout issue when with the same timeout Ruby fails earlier
16
+ CODE_TIMEOUT_EXTRA = 3
17
+
12
18
  ##
13
19
  # Simple class to get URL info (validation/existance, headers, content-type)
14
20
  # Allows to get all this stuff without actually downloading huge files like
@@ -18,10 +24,10 @@ module NetworkUtils
18
24
  # Initialise a UrlInfo for a particular URL
19
25
  #
20
26
  # @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
27
+ # @param [Integer] request_timeout Max time to wait for headers from the server (seconds)
22
28
  #
23
29
  def initialize(url, request_timeout = 10)
24
- @url = url.dup.to_s.force_encoding('UTF-8')
30
+ @url = String.new(url.to_s).force_encoding('UTF-8')
25
31
  @request_timeout = request_timeout
26
32
  end
27
33
 
@@ -74,28 +80,24 @@ module NetworkUtils
74
80
  #
75
81
  # @return [Hash, nil] remote resource HTTP headers list or nil
76
82
  def headers
77
- @headers ||= request_headers
78
- end
79
-
80
- private
81
-
82
- def request_headers
83
83
  return nil if @url.to_s.empty?
84
84
  return nil unless (encoded_url = encode(@url))
85
85
 
86
- Timeout.timeout(@request_timeout) do
86
+ Timeout.timeout(@request_timeout + CODE_TIMEOUT_EXTRA) do
87
87
  response = HTTParty.head(encoded_url, timeout: @request_timeout)
88
-
89
88
  raise response.response if response.response.is_a?(Net::HTTPServerError) ||
90
89
  response.response.is_a?(Net::HTTPClientError)
91
90
 
92
- return response.headers
91
+ @headers ||= response.headers
93
92
  end
94
- rescue SocketError, ThreadError, Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL, Timeout::Error, TypeError,
95
- Net::HTTPServerError, Net::HTTPClientError, HTTParty::ResponseError
93
+ rescue SocketError, ThreadError, Errno::ENETUNREACH, Errno::ECONNREFUSED,
94
+ Errno::EADDRNOTAVAIL, Timeout::Error, TypeError,
95
+ Net::HTTPServerError, Net::HTTPClientError, Net::OpenTimeout
96
96
  nil
97
97
  end
98
98
 
99
+ private
100
+
99
101
  def encode(url)
100
102
  Addressable::URI.encode(url)
101
103
  end
@@ -3,5 +3,5 @@
3
3
  # Library namespace and current version
4
4
  module NetworkUtils
5
5
  # Current Library Version
6
- VERSION = '0.1.2'
6
+ VERSION = '0.1.3'
7
7
  end
@@ -0,0 +1,27 @@
1
+ require 'tapp'
2
+ require 'tapp/printer'
3
+ require 'amazing_print'
4
+
5
+ # Module to define own Tapp printer
6
+ module Tapp::Printer
7
+ # Custom AmazingPrint based printer for Tapp
8
+ class AmazingPrint < Base
9
+ # Prints the object using custom (AmazingPrint) printer
10
+ #
11
+ # @param [Object, Array] *args arbitrary Object to print
12
+ #
13
+ # @return [nil] prints to IO, returns nothing
14
+ def print(*args)
15
+ ap *args
16
+ end
17
+ end
18
+
19
+ register :amazing_print, AmazingPrint
20
+ end
21
+
22
+ # Configure Tapp to use our custom printer and report caller
23
+ Tapp.configure do |config|
24
+ config.report_caller = true
25
+ config.default_printer = :amazing_print
26
+ end
27
+
@@ -22,21 +22,22 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ['lib']
24
24
 
25
- spec.add_development_dependency 'bundler', '~> 1.16'
26
- spec.add_development_dependency 'byebug', '~> 9.1'
25
+ spec.add_development_dependency 'amazing_print', '~> 1.2'
26
+ spec.add_development_dependency 'bundler', '~> 2.1'
27
+ spec.add_development_dependency 'byebug', '~> 11.1'
27
28
  spec.add_development_dependency 'inch', '~> 0.8'
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'
29
+ spec.add_development_dependency 'tapp', '~> 1.5'
30
+ spec.add_development_dependency 'rake', '~> 13.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.9'
32
+ spec.add_development_dependency 'rubocop', '~> 0.91'
32
33
  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'
34
+ spec.add_development_dependency 'simplecov', '~> 0.19'
35
+ spec.add_development_dependency 'vcr', '~> 6.0'
36
+ spec.add_development_dependency 'webmock', '~> 3.9'
36
37
  spec.add_development_dependency 'yard', '~> 0.9'
37
38
 
38
- spec.add_dependency 'activesupport', '~> 5.1'
39
- spec.add_dependency 'addressable', '~> 2.5'
40
- spec.add_dependency 'httparty', '~> 0.15'
39
+ spec.add_dependency 'activesupport', '~> 6.0'
40
+ spec.add_dependency 'addressable', '~> 2.7'
41
+ spec.add_dependency 'httparty', '~> 0.18'
41
42
  spec.add_dependency 'url_regex', '~> 0.0.3'
42
43
  end
metadata CHANGED
@@ -1,43 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: network_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - smileart
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-26 00:00:00.000000000 Z
11
+ date: 2020-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: amazing_print
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - "~>"
18
32
  - !ruby/object:Gem::Version
19
- version: '1.16'
33
+ version: '2.1'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - "~>"
25
39
  - !ruby/object:Gem::Version
26
- version: '1.16'
40
+ version: '2.1'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: byebug
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '9.1'
47
+ version: '11.1'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '9.1'
54
+ version: '11.1'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: inch
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -53,61 +67,61 @@ dependencies:
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0.8'
55
69
  - !ruby/object:Gem::Dependency
56
- name: letters
70
+ name: tapp
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - "~>"
60
74
  - !ruby/object:Gem::Version
61
- version: '0.4'
75
+ version: '1.5'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
- version: '0.4'
82
+ version: '1.5'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rake
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - "~>"
74
88
  - !ruby/object:Gem::Version
75
- version: '12.2'
89
+ version: '13.0'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
- version: '12.2'
96
+ version: '13.0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rspec
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
101
  - - "~>"
88
102
  - !ruby/object:Gem::Version
89
- version: '3.7'
103
+ version: '3.9'
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
108
  - - "~>"
95
109
  - !ruby/object:Gem::Version
96
- version: '3.7'
110
+ version: '3.9'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rubocop
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
115
  - - "~>"
102
116
  - !ruby/object:Gem::Version
103
- version: '0.51'
117
+ version: '0.91'
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
122
  - - "~>"
109
123
  - !ruby/object:Gem::Version
110
- version: '0.51'
124
+ version: '0.91'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: rubygems-tasks
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -128,42 +142,42 @@ dependencies:
128
142
  requirements:
129
143
  - - "~>"
130
144
  - !ruby/object:Gem::Version
131
- version: '0.15'
145
+ version: '0.19'
132
146
  type: :development
133
147
  prerelease: false
134
148
  version_requirements: !ruby/object:Gem::Requirement
135
149
  requirements:
136
150
  - - "~>"
137
151
  - !ruby/object:Gem::Version
138
- version: '0.15'
152
+ version: '0.19'
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: vcr
141
155
  requirement: !ruby/object:Gem::Requirement
142
156
  requirements:
143
157
  - - "~>"
144
158
  - !ruby/object:Gem::Version
145
- version: '3.0'
159
+ version: '6.0'
146
160
  type: :development
147
161
  prerelease: false
148
162
  version_requirements: !ruby/object:Gem::Requirement
149
163
  requirements:
150
164
  - - "~>"
151
165
  - !ruby/object:Gem::Version
152
- version: '3.0'
166
+ version: '6.0'
153
167
  - !ruby/object:Gem::Dependency
154
168
  name: webmock
155
169
  requirement: !ruby/object:Gem::Requirement
156
170
  requirements:
157
171
  - - "~>"
158
172
  - !ruby/object:Gem::Version
159
- version: '3.1'
173
+ version: '3.9'
160
174
  type: :development
161
175
  prerelease: false
162
176
  version_requirements: !ruby/object:Gem::Requirement
163
177
  requirements:
164
178
  - - "~>"
165
179
  - !ruby/object:Gem::Version
166
- version: '3.1'
180
+ version: '3.9'
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: yard
169
183
  requirement: !ruby/object:Gem::Requirement
@@ -184,42 +198,42 @@ dependencies:
184
198
  requirements:
185
199
  - - "~>"
186
200
  - !ruby/object:Gem::Version
187
- version: '5.1'
201
+ version: '6.0'
188
202
  type: :runtime
189
203
  prerelease: false
190
204
  version_requirements: !ruby/object:Gem::Requirement
191
205
  requirements:
192
206
  - - "~>"
193
207
  - !ruby/object:Gem::Version
194
- version: '5.1'
208
+ version: '6.0'
195
209
  - !ruby/object:Gem::Dependency
196
210
  name: addressable
197
211
  requirement: !ruby/object:Gem::Requirement
198
212
  requirements:
199
213
  - - "~>"
200
214
  - !ruby/object:Gem::Version
201
- version: '2.5'
215
+ version: '2.7'
202
216
  type: :runtime
203
217
  prerelease: false
204
218
  version_requirements: !ruby/object:Gem::Requirement
205
219
  requirements:
206
220
  - - "~>"
207
221
  - !ruby/object:Gem::Version
208
- version: '2.5'
222
+ version: '2.7'
209
223
  - !ruby/object:Gem::Dependency
210
224
  name: httparty
211
225
  requirement: !ruby/object:Gem::Requirement
212
226
  requirements:
213
227
  - - "~>"
214
228
  - !ruby/object:Gem::Version
215
- version: '0.15'
229
+ version: '0.18'
216
230
  type: :runtime
217
231
  prerelease: false
218
232
  version_requirements: !ruby/object:Gem::Requirement
219
233
  requirements:
220
234
  - - "~>"
221
235
  - !ruby/object:Gem::Version
222
- version: '0.15'
236
+ version: '0.18'
223
237
  - !ruby/object:Gem::Dependency
224
238
  name: url_regex
225
239
  requirement: !ruby/object:Gem::Requirement
@@ -259,6 +273,7 @@ files:
259
273
  - lib/network_utils/port.rb
260
274
  - lib/network_utils/url_info.rb
261
275
  - lib/network_utils/version.rb
276
+ - lib/tapp_printer.rb
262
277
  - network_utils.gemspec
263
278
  homepage: http://github.com/smileart/network_utils
264
279
  licenses:
@@ -279,8 +294,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
279
294
  - !ruby/object:Gem::Version
280
295
  version: '0'
281
296
  requirements: []
282
- rubyforge_project:
283
- rubygems_version: 2.7.8
297
+ rubygems_version: 3.1.2
284
298
  signing_key:
285
299
  specification_version: 4
286
300
  summary: A set of convenient network utils