has_ip_address 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +48 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/lib/has_ip_address.rb +40 -0
- data/lib/has_ip_address/extensions.rb +34 -0
- data/spec/has_ip_address_spec.rb +59 -0
- data/spec/spec_helper.rb +10 -0
- metadata +95 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Joshua Nichols
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= has_ip_address
|
2
|
+
|
3
|
+
An extension to ActiveRecord to store IP Addresses in the database as integers, in part to IPAddr.
|
4
|
+
|
5
|
+
class Visit < ActiveRecord::Base
|
6
|
+
has_ip_address
|
7
|
+
end
|
8
|
+
|
9
|
+
Here's an example of using it:
|
10
|
+
|
11
|
+
>> visit = Visit.new
|
12
|
+
=> #<Visit id: nil, ip_address: nil, created_at: nil, updated_at: nil>
|
13
|
+
>> visit.ip_address = "127.0.0.1"
|
14
|
+
=> "127.0.0.1"
|
15
|
+
>> visit.ip_address
|
16
|
+
=> #<IPAddr: IPv4:127.0.0.1/255.255.255.255>
|
17
|
+
>> visit.ip_address.to_i
|
18
|
+
=> 2130706433
|
19
|
+
>> visit.ip_address.to_s
|
20
|
+
=> "127.0.0.1"
|
21
|
+
|
22
|
+
This assumes you are using a column named :ip_address. To change this behavior, pass in a parameter with the column you care to use:
|
23
|
+
|
24
|
+
|
25
|
+
class Visit < ActiveRecord::Base
|
26
|
+
has_ip_address :visit_ip
|
27
|
+
end
|
28
|
+
|
29
|
+
== Known issues
|
30
|
+
|
31
|
+
* Setting ip_address to an invalid IP address will fail with an ArgumentError from IPAddr.
|
32
|
+
* Ideally, it should let you pass any value in without error, but invalid IP addresses will add a validation error during validation
|
33
|
+
* In the mean time, you probably want to only this when the IP Address will definitely be correct, ie from a controller's `request.remote_ip`.
|
34
|
+
|
35
|
+
== Note on Patches/Pull Requests
|
36
|
+
|
37
|
+
* Fork the project.
|
38
|
+
* Make your feature addition or bug fix.
|
39
|
+
* Add tests for it. This is important so I don't break it in a
|
40
|
+
future version unintentionally.
|
41
|
+
* Commit, do not mess with rakefile, version, or history.
|
42
|
+
(if you want to have your own version, that is fine but
|
43
|
+
bump version in a commit by itself I can ignore when I pull)
|
44
|
+
* Send me a pull request. Bonus points for topic branches.
|
45
|
+
|
46
|
+
== Copyright
|
47
|
+
|
48
|
+
Copyright (c) 2009 Joshua Nichols. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "has_ip_address"
|
8
|
+
gem.summary = %Q{An extension to ActiveRecord to store IP Addresses in the database as integers}
|
9
|
+
gem.description = %Q{An extension to ActiveRecord to store IP Addresses in the database as integers}
|
10
|
+
gem.email = "josh@technicalpickles.com"
|
11
|
+
gem.homepage = "http://github.com/technicalpickles/has_ip_address"
|
12
|
+
gem.authors = ["Joshua Nichols"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
gem.add_development_dependency "yard"
|
15
|
+
gem.add_development_dependency "acts_as_fu"
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'spec/rake/spectask'
|
24
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
25
|
+
spec.libs << 'lib' << 'spec'
|
26
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
27
|
+
end
|
28
|
+
|
29
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
|
+
spec.libs << 'lib' << 'spec'
|
31
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :spec => :check_dependencies
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
begin
|
40
|
+
require 'yard'
|
41
|
+
YARD::Rake::YardocTask.new
|
42
|
+
rescue LoadError
|
43
|
+
task :yardoc do
|
44
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
45
|
+
end
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'ipaddr'
|
2
|
+
require 'has_ip_address/extensions'
|
3
|
+
|
4
|
+
module HasIpAddress
|
5
|
+
|
6
|
+
def self.included(other)
|
7
|
+
other.class_eval do
|
8
|
+
extend ClassMethods
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def has_ip_address(column = :ip_address)
|
14
|
+
define_method "#{column}=" do |address|
|
15
|
+
ipaddr = address.to_ipaddr
|
16
|
+
write_attribute column, ipaddr
|
17
|
+
|
18
|
+
ipaddr
|
19
|
+
end
|
20
|
+
|
21
|
+
define_method column do
|
22
|
+
integer = read_attribute column
|
23
|
+
if integer.present?
|
24
|
+
IPAddr.new(i_to_ipaddr(integer))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
unless self.instance_methods.include?('i_to_ipaddr')
|
29
|
+
define_method :i_to_ipaddr do |i|
|
30
|
+
[24, 16, 8, 0].collect {|b| (i >> b) & 255}.join('.')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
require 'active_record'
|
38
|
+
ActiveRecord::Base.class_eval do
|
39
|
+
include HasIpAddress
|
40
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
IPAddr.class_eval do
|
2
|
+
# hack so == doesn't blow up when passed false
|
3
|
+
def ==(other)
|
4
|
+
return false if other.kind_of?(TrueClass) || other.kind_of?(FalseClass)
|
5
|
+
|
6
|
+
# copied from original IPAddr#==
|
7
|
+
if other.kind_of?(IPAddr) && @family != other.family
|
8
|
+
return false
|
9
|
+
end
|
10
|
+
return (@addr == other.to_i)
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_ipaddr
|
14
|
+
self
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Bignum
|
19
|
+
def to_ipaddr
|
20
|
+
[24, 16, 8, 0].collect {|b| (self >> b) & 255}.join('.').to_ipaddr
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Fixnum
|
25
|
+
def to_ipaddr
|
26
|
+
[24, 16, 8, 0].collect {|b| (self >> b) & 255}.join('.').to_ipaddr
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Object
|
31
|
+
def to_ipaddr
|
32
|
+
IPAddr.new(self)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "has_ip_address" do
|
4
|
+
describe "with default column" do
|
5
|
+
before do
|
6
|
+
build_model :visits do
|
7
|
+
integer :ip_address
|
8
|
+
has_ip_address
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "and setting and getting" do
|
13
|
+
before do
|
14
|
+
visit = Visit.new
|
15
|
+
|
16
|
+
visit.ip_address = "127.0.0.1"
|
17
|
+
|
18
|
+
@ip_address = visit.ip_address
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should be a IPAddr" do
|
22
|
+
@ip_address.should be_kind_of(IPAddr)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should return the set value when got" do
|
26
|
+
@ip_address.should == IPAddr.new("127.0.0.1")
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "with default column" do
|
33
|
+
before do
|
34
|
+
build_model :visits do
|
35
|
+
integer :visitor_ip
|
36
|
+
has_ip_address :visitor_ip
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "and setting and getting" do
|
41
|
+
before do
|
42
|
+
visit = Visit.new
|
43
|
+
|
44
|
+
visit.visitor_ip = "192.168.0.1"
|
45
|
+
|
46
|
+
@ip_address = visit.visitor_ip
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should be a IPAddr" do
|
50
|
+
@ip_address.should be_kind_of(IPAddr)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should return the set value when got" do
|
54
|
+
@ip_address.should == IPAddr.new("192.168.0.1")
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'has_ip_address'
|
4
|
+
require 'acts_as_fu'
|
5
|
+
require 'spec'
|
6
|
+
require 'spec/autorun'
|
7
|
+
|
8
|
+
Spec::Runner.configure do |config|
|
9
|
+
config.include ActsAsFu
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_ip_address
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joshua Nichols
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-28 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: yard
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: acts_as_fu
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
description: An extension to ActiveRecord to store IP Addresses in the database as integers
|
46
|
+
email: josh@technicalpickles.com
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- LICENSE
|
53
|
+
- README.rdoc
|
54
|
+
files:
|
55
|
+
- .document
|
56
|
+
- .gitignore
|
57
|
+
- LICENSE
|
58
|
+
- README.rdoc
|
59
|
+
- Rakefile
|
60
|
+
- VERSION
|
61
|
+
- lib/has_ip_address.rb
|
62
|
+
- lib/has_ip_address/extensions.rb
|
63
|
+
- spec/has_ip_address_spec.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
has_rdoc: true
|
66
|
+
homepage: http://github.com/technicalpickles/has_ip_address
|
67
|
+
licenses: []
|
68
|
+
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options:
|
71
|
+
- --charset=UTF-8
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
version:
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: "0"
|
85
|
+
version:
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.3.5
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: An extension to ActiveRecord to store IP Addresses in the database as integers
|
93
|
+
test_files:
|
94
|
+
- spec/has_ip_address_spec.rb
|
95
|
+
- spec/spec_helper.rb
|