chalk-ip 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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZDQ0NTNmNjFlMzM3NzVhMzhlOWUzYTM2YmFjMDkwMDZlNWFkZmE0OA==
5
+ data.tar.gz: !binary |-
6
+ ZjU1OTIyNGFkNjBhMmMwOTcwOWRkZWVkMWFhN2ExZGJmMDNlYjllOQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ Yzk4ZWI4NjkxMmVmMWY0NDE1NmY3NzE0Zjk1MDgzZjA2MTkxMjIyMWM5ZGZl
10
+ MGNkOWMzZTRiZTczZmU0MjY4OGEwNjlkNzNlZWYwYWNkN2MzOTk0ZDJlNmQ0
11
+ MWQxZTAyNzEyMjI1ZWRiZGNiMGY3YTE4Yzk4MTE1OWJkNTZmNTI=
12
+ data.tar.gz: !binary |-
13
+ ZDljYThlMGEyZTBkZDI2NjUzZWI1OWM2NTJkZGM2NTc0NjRmYjZjYzBkZTRh
14
+ NGVjZjFiMDQxNjc1NDUwM2YzNWE2YjQ2MGEyNjIxY2FiZjdiZDYwYmEyNjVh
15
+ ZDYzZjZmYmU5YzRlNGI1MDhiODI1OGUzYTdkNzgzZjVkN2M0ODc=
@@ -0,0 +1,2 @@
1
+ /Gemfile.lock
2
+ /pkg/
@@ -0,0 +1,11 @@
1
+ Style/Documentation:
2
+ Enabled: false
3
+
4
+ Style/BracesAroundHashParameters:
5
+ Enabled: false
6
+
7
+ Style/EmptyLines:
8
+ Enabled: false
9
+
10
+ Style/EmptyLinesAroundClassBody:
11
+ Enabled: false
@@ -0,0 +1,17 @@
1
+ inherit_from:
2
+ - .rubocop-disables.yml
3
+
4
+ Style/SpaceAroundEqualsInParameterDefault:
5
+ EnforcedStyle: no_space
6
+
7
+ Style/TrailingComma:
8
+ EnforcedStyleForMultiline: comma
9
+
10
+ Style/SignalException:
11
+ EnforcedStyle: only_raise
12
+
13
+ AllCops:
14
+ Include:
15
+ - '**/Rakefile'
16
+ Exclude:
17
+ - Gemfile
@@ -0,0 +1 @@
1
+ 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # Execute bundler hook if present
2
+ ['~/.', '/etc/'].any? do |file|
3
+ File.lstat(path = File.expand_path(file + 'bundle-gemfile-hook')) rescue next
4
+ eval(File.read(path), binding, path); break true
5
+ end || source('https://rubygems.org/')
6
+
7
+ gemspec
@@ -0,0 +1,12 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'bundler/setup'
3
+ # require 'rspec/core/rake_task'
4
+
5
+ task :default do
6
+ sh 'rake -T'
7
+ end
8
+
9
+ def alias_task(alias_task, original)
10
+ desc "Alias for rake #{original}"
11
+ task alias_task, Rake.application[original].arg_names => original
12
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path('../lib/chalk-ip/version', __FILE__)
2
+
3
+ def self.list_files
4
+ Dir.chdir(File.dirname(__FILE__)) do
5
+ `git ls-files`.split($\)
6
+ end
7
+ end
8
+
9
+ # Find files under bin/ that include "ruby" in the first line (shebang).
10
+ def self.find_binaries
11
+ list_files.grep(%r{^bin/[^/]+$}).find_all { |name|
12
+ File.open(name, 'r') do |f|
13
+ f.readline.include?('ruby')
14
+ end
15
+ }.map {|f| File.basename(f)}
16
+ end
17
+
18
+ Gem::Specification.new do |gem|
19
+ gem.authors = ["Andy Brody"]
20
+ gem.email = ["oss@stripe.com"]
21
+ gem.summary = "Chalk::IP manipulates IP addresses"
22
+ gem.description = <<-EOM
23
+ Chalk::IP contains a few utilities for manipulating IPAddr objects that
24
+ really ought to be in the ruby standard library.
25
+ EOM
26
+ gem.homepage = "https://github.com/stripe/chalk-ip/"
27
+
28
+ gem.files = list_files
29
+ gem.executables = find_binaries
30
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
31
+ gem.name = "chalk-ip"
32
+ gem.require_paths = ["lib"]
33
+
34
+ gem.version = Chalk::IP::VERSION
35
+
36
+ gem.add_development_dependency 'pry'
37
+ gem.add_development_dependency 'rake'
38
+ gem.add_development_dependency 'rubocop'
39
+
40
+ gem.required_ruby_version = '>= 1.9.3'
41
+ end
@@ -0,0 +1,115 @@
1
+ require 'ipaddr'
2
+
3
+ module Chalk; module IP
4
+ # Given netmask, return the number of leading 1 bits (appropriate for use
5
+ # with CIDR notation).
6
+ #
7
+ # I can't believe this isn't built in to the IPAddr class, but whatever.
8
+ #
9
+ # @example
10
+ #
11
+ # >> netmask_to_cidr_bits('255.255.0.0')
12
+ # => 16
13
+ #
14
+ # >> mask = IPAddr.new('255.255.255.255/8')
15
+ # => #<IPAddr: IPv4:255.0.0.0/255.0.0.0>
16
+ # >> netmask_to_cidr_bits(mask)
17
+ # => 8
18
+ #
19
+ # >> netmask_to_cidr_bits('ffff:ffff:ffff:ffff::')
20
+ # => 64
21
+ #
22
+ # >> netmask_to_cidr_bits('ffff::')
23
+ # => 16
24
+ #
25
+ # @param addr [String, IPAddr]
26
+ #
27
+ # @return Fixnum
28
+ #
29
+ def self.netmask_to_cidr_bits(addr)
30
+ case addr
31
+ when IPAddr
32
+ addr_obj = addr
33
+ when String
34
+ addr_obj = IPAddr.new(addr)
35
+ else
36
+ raise ArgumentError.new("Unexpected address: #{addr.inspect}")
37
+ end
38
+
39
+ if addr_obj.ipv4?
40
+ total = 32
41
+ xor = IPAddr::IN4MASK
42
+ elsif addr_obj.ipv6?
43
+ total = 128
44
+ xor = IPAddr::IN6MASK
45
+ else
46
+ raise "#{addr_obj.inspect} is neither IPv4 nor IPv6??"
47
+ end
48
+
49
+ Integer(total - Math.log2((addr_obj.to_i ^ xor) + 1))
50
+ end
51
+
52
+ # Given IPAddr, return the CIDR netmask bits.
53
+ #
54
+ # There doesn't seem to be any native way to do this in Rubby.
55
+ #
56
+ # @param addr [IPAddr]
57
+ #
58
+ # @return Fixnum
59
+ #
60
+ def self.addr_to_cidr_bits(addr)
61
+ mask = IPAddr.new(addr.instance_variable_get(:@mask_addr), addr.family)
62
+
63
+ netmask_to_cidr_bits(mask)
64
+ end
65
+
66
+ # Given an IP address, return it as a string in CIDR notation.
67
+ #
68
+ # I really can't believe there's no way to do this natively. Rubby.
69
+ #
70
+ # @param addr [IPAddr, String]
71
+ #
72
+ # @return String
73
+ #
74
+ def self.addr_to_s_cidr(addr)
75
+ if addr.is_a?(IPAddr)
76
+ addr_obj = addr
77
+ else
78
+ addr_obj = IPAddr.new(addr)
79
+ end
80
+
81
+ "#{addr_obj}/#{addr_to_cidr_bits(addr_obj)}"
82
+ end
83
+
84
+ # Given an enumerable of addresses, find the smallest CIDR block that will
85
+ # cover those addresses.
86
+ #
87
+ # @param [Enumerable<IPAddr>] addresses
88
+ #
89
+ # @return [IPAddr] A network range covering the addresses.
90
+ #
91
+ def self.cidr_block_covering(addresses)
92
+ # start with the first address as our range
93
+ net = addresses.find { true }
94
+ if net.nil?
95
+ raise ArgumentError.new('addresses cannot be empty')
96
+ end
97
+
98
+ addresses.each do |addr|
99
+ # make sure we don't mix IPv4 and IPv6 addresses
100
+ if net.family != addr.family
101
+ raise ArgumentError.new('Cannot mix addresses of differing family')
102
+ end
103
+
104
+ # expand net until it includes addr
105
+ until net.include?(addr)
106
+ mask_bits = addr_to_cidr_bits(net)
107
+ net = IPAddr.new("#{net.to_string}/#{mask_bits - 1}")
108
+ end
109
+ end
110
+
111
+ net
112
+ end
113
+ end; end
114
+
115
+ require_relative './chalk-ip/version'
@@ -0,0 +1,5 @@
1
+ module Chalk
2
+ module IP
3
+ VERSION = '0.0.1' unless defined?(self::VERSION)
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chalk-ip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andy Brody
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: ! " Chalk::IP contains a few utilities for manipulating IPAddr objects
56
+ that\n really ought to be in the ruby standard library.\n"
57
+ email:
58
+ - oss@stripe.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - .rubocop-disables.yml
65
+ - .rubocop.yml
66
+ - .ruby-version
67
+ - Gemfile
68
+ - Rakefile
69
+ - chalk-ip.gemspec
70
+ - lib/chalk-ip.rb
71
+ - lib/chalk-ip/version.rb
72
+ homepage: https://github.com/stripe/chalk-ip/
73
+ licenses: []
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: 1.9.3
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.3.0
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Chalk::IP manipulates IP addresses
95
+ test_files: []