ip_address_serializer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +36 -0
- data/Rakefile +8 -0
- data/ip_address_serializer.gemspec +26 -0
- data/lib/ip_address_serializer.rb +34 -0
- data/lib/ip_address_serializer/version.rb +3 -0
- data/test/ipv4_test.rb +23 -0
- data/test/ipv6_test.rb +22 -0
- data/test/test_helper.rb +5 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 605708a4418dbd7a80a9d66f45a598e7913c551f
|
4
|
+
data.tar.gz: ae2393ba7f8fe90eec550d6ae8fba67da91cc964
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3c72468518c64efafc8eb20afaece20991be4979a05fdc34ade897dd575ac1abd8f211d9c180c3ff9c4d2d0be52a00d114e3a8b11a1be2ce6c22f34cecfbb519
|
7
|
+
data.tar.gz: b67165c8919bb6eb1577c5cafe955f9a5442fd3a758dcd1c2b348725b7cea767e70e1e960ea35c96e6d7f5a6a98b9fa7f85a05fea314c2d50898a4dff78b4a53
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
The MIT License (MIT)
|
3
|
+
|
4
|
+
Copyright (c) 2014 Mike Simkins
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# IpAddressSerializer
|
2
|
+
|
3
|
+
This gem providers a custom Serializer for ActiveRecord to allow the storage and retrieval of an
|
4
|
+
IPv4 or IPv6 Address in a String Field
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'ip_address_serializer'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install ip_address_serializer
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Once the Gem has been added to your Gemfile, in any model that is required to hold an IP Address, add the
|
25
|
+
following line to your model
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
serialize <field name symbol>, IPAddressSerializer
|
29
|
+
```
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it ( https://github.com/msimkins/ip_address_serializer/fork )
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ip_address_serializer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ip_address_serializer"
|
8
|
+
spec.version = IpAddressSerializer::VERSION
|
9
|
+
spec.authors = ["Mike Simkins"]
|
10
|
+
spec.email = ["software@g7obs.com"]
|
11
|
+
spec.summary = %q{Gem to enable serialized storage of an IP Address in a string}
|
12
|
+
spec.description = %q{ActiveRecord Serializer to be allow the storage of a CIDR IP Address in a String Field}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "minitest", "~> 5.4"
|
24
|
+
|
25
|
+
spec.add_dependency "ruby-ip", "~> 0.9"
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'ip'
|
2
|
+
require "ip_address_serializer/version"
|
3
|
+
|
4
|
+
DELIMITER = '^'
|
5
|
+
|
6
|
+
module IPAddressSerializer
|
7
|
+
|
8
|
+
# Load the value and return the IP Object.
|
9
|
+
# @param [String] value String representation of the IP Object
|
10
|
+
# @return [IP] The IP Object (from ruby-ip)
|
11
|
+
def self.load(value)
|
12
|
+
if value.nil?
|
13
|
+
return nil
|
14
|
+
end
|
15
|
+
if value.is_a?(String) && value.index(DELIMITER) > 0
|
16
|
+
return IP.new(value.split(DELIMITER))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Save the value, which converts either a String, or an IP Object to the String representation for storage
|
21
|
+
# @param [String, IP, IP::V4, IP::V6] String representation, or internal IP representation of an IP Address
|
22
|
+
# @return [String] The encoded string value representing the IP address object
|
23
|
+
def self.dump(value)
|
24
|
+
if value.nil?
|
25
|
+
return nil
|
26
|
+
end
|
27
|
+
if value.is_a?(String)
|
28
|
+
ip = IP.new(value)
|
29
|
+
elsif (value.is_a?(IP) || value.is_a?(IP::V4) || value.is_a?(IP::V6))
|
30
|
+
ip = value
|
31
|
+
end
|
32
|
+
return [ip.proto, ip.to_hex, ip.pfxlen].join(DELIMITER)
|
33
|
+
end
|
34
|
+
end
|
data/test/ipv4_test.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class IPV4_dump < MiniTest::Test
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@ip4 = IP.new("192.168.1.1")
|
7
|
+
@ip4s = "192.168.1.1"
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_ipv4_dump_from_ip
|
11
|
+
assert_equal "v4^c0a80101^32", IPAddressSerializer.dump(@ip4)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_ipv4_load_to_ip
|
15
|
+
assert_equal IP.new("192.168.1.1"), IPAddressSerializer.load("v4^c0a80101^32")
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_ipv4_dump_from_string
|
19
|
+
assert_equal "v4^c0a80101^32", IPAddressSerializer.dump(@ip4s)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
data/test/ipv6_test.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class IPV6_dump < MiniTest::Test
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@ip6 = IP.new("2001:db8:102::10/120")
|
7
|
+
@ip6s = "2001:db8:102::10/120"
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_ipv6_dump_from_ip
|
11
|
+
assert_equal "v6^20010db8010200000000000000000010^120", IPAddressSerializer.dump(@ip6)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_ipv6_load_to_ip
|
15
|
+
assert_equal IP.new("2001:db8:102::10/120"), IPAddressSerializer.load("v6^20010db8010200000000000000000010^120")
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_ipv6_dump_from_string
|
19
|
+
assert_equal "v6^20010db8010200000000000000000010^120", IPAddressSerializer.dump(@ip6)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ip_address_serializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Simkins
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-22 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: ruby-ip
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.9'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.9'
|
69
|
+
description: ActiveRecord Serializer to be allow the storage of a CIDR IP Address
|
70
|
+
in a String Field
|
71
|
+
email:
|
72
|
+
- software@g7obs.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- ip_address_serializer.gemspec
|
83
|
+
- lib/ip_address_serializer.rb
|
84
|
+
- lib/ip_address_serializer/version.rb
|
85
|
+
- test/ipv4_test.rb
|
86
|
+
- test/ipv6_test.rb
|
87
|
+
- test/test_helper.rb
|
88
|
+
homepage: ''
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.2.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Gem to enable serialized storage of an IP Address in a string
|
112
|
+
test_files:
|
113
|
+
- test/ipv4_test.rb
|
114
|
+
- test/ipv6_test.rb
|
115
|
+
- test/test_helper.rb
|