Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit 78b5cf8

Browse files
authored
Merge pull request #22 from ctompkinson/ctompkinson/linting
Lint Netgeo and add Rubocop ignores
2 parents 72d68ff + ecbf2d0 commit 78b5cf8

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

netgeo/.rubocop.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Rubocop Configuration
2+
Metrics/AbcSize:
3+
Max: 100
4+
5+
Metrics/BlockLength:
6+
Max: 60
7+
8+
Metrics/CyclomaticComplexity:
9+
Max: 15
10+
11+
Metrics/LineLength:
12+
Max: 100
13+
14+
Metrics/MethodLength:
15+
Max: 80
16+
17+
Metrics/PerceivedComplexity:
18+
Max: 20

netgeo/netgeo.rb

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,38 @@
66
# to make all your piping dreams come true.
77
#
88

9-
109
require 'slop'
1110
require 'rest-client'
1211
require 'json'
1312
require 'os'
1413
require 'socket'
1514

16-
1715
# Contains option parsing logic
1816
def netgeo
1917
opts = Slop.parse suppress_errors: true do |o|
2018
o.banner = 'Usage: netgeo [flag]'
2119

2220
# Gets WAN address
2321
o.on '-w', '--wan', 'Returns WAN IP' do
24-
url = "https://api.ipify.org"
22+
url = 'https://api.ipify.org'
2523
response = RestClient.get(url)
2624
puts response
2725
end
2826

2927
# Gets LAN(s)
3028
o.on '-l', '--lan', 'Returns LAN IP(s)' do
31-
lans = Socket.ip_address_list.select{|intf| intf.ipv4_private?}.map { |intf| intf.ip_address }
29+
lans = Socket.ip_address_list.select(&:ipv4_private?).map(&:ip_address)
3230
puts lans.join(', ')
3331
end
3432

3533
# Gets Router IP
3634
o.on '-r', '--router', 'Returns Router IP' do
3735
if OS.linux?
38-
router = %x[ip route | grep default | head -1 | awk '{print$3}']
36+
router = `ip route | grep default | head -1 | awk '{print$3}'`
3937
elsif OS.mac?
40-
router = %x[netstat -rn | grep default | head -1 | awk '{print$2}']
38+
router = `netstat -rn | grep default | head -1 | awk '{print$2}'`
4139
else
42-
puts "OS not supported!"
40+
puts 'OS not supported!'
4341
exit(1)
4442
end
4543
puts router
@@ -48,21 +46,20 @@ def netgeo
4846
# Gets DNS nameserver
4947
o.on '-d', '--dns', 'Returns DNS Nameserver' do
5048
if OS.linux?
51-
dns = %x[cat /etc/resolv.conf | grep nameserver | head -1 | awk '{print$2}']
49+
dns = `cat /etc/resolv.conf | grep nameserver | head -1 | awk '{print$2}'`
5250
elsif OS.mac?
53-
dns = %x[scutil --dns | grep nameserver | head -1 | awk '{print$3}']
51+
dns = `scutil --dns | grep nameserver | head -1 | awk '{print$3}'`
5452
else
55-
puts "OS not supported!"
53+
puts 'OS not supported!'
5654
exit(1)
5755
end
5856
puts dns
5957
end
6058

6159
o.string '-m', '--mac [interface]', 'Returns MAC address for interface. Ex. eth0'
6260

63-
6461
o.on '-g', '--geo', 'Returns Current IP Geodata' do
65-
url = "http://ip-api.com/json/"
62+
url = 'http://ip-api.com/json/'
6663
response = RestClient.get(url)
6764
info = JSON.parse(response)
6865
puts info['query']
@@ -83,7 +80,6 @@ def netgeo
8380
end
8481
end
8582

86-
8783
# Logic for specific geodata
8884
options = opts[:s]
8985
ip = opts.arguments || ''
@@ -107,12 +103,10 @@ def netgeo
107103
puts info['isp'] if options.include? 'isp'
108104
end
109105

110-
111106
# Logic for specific MAC address
112107
options = opts[:m]
113-
unless options == nil
114-
puts %x[ifconfig #{options} | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}']
115-
end
108+
return if options.nil?
109+
puts `ifconfig #{options} | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'`
116110
end
117111

118112
netgeo

0 commit comments

Comments
 (0)