Class: Ip2location

Inherits:
Object
  • Object
show all
Defined in:
lib/ip2location_ruby.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_addrObject

Returns the value of attribute base_addr.



12
13
14
# File 'lib/ip2location_ruby.rb', line 12

def base_addr
  @base_addr
end

#columnsObject

Returns the value of attribute columns.



12
13
14
# File 'lib/ip2location_ruby.rb', line 12

def columns
  @columns
end

#countObject

Returns the value of attribute count.



12
13
14
# File 'lib/ip2location_ruby.rb', line 12

def count
  @count
end

#databaseObject

Returns the value of attribute database.



12
13
14
# File 'lib/ip2location_ruby.rb', line 12

def database
  @database
end

#db_indexObject

Returns the value of attribute db_index.



12
13
14
# File 'lib/ip2location_ruby.rb', line 12

def db_index
  @db_index
end

#fileObject

Returns the value of attribute file.



12
13
14
# File 'lib/ip2location_ruby.rb', line 12

def file
  @file
end

#ip_versionObject

Returns the value of attribute ip_version.



12
13
14
# File 'lib/ip2location_ruby.rb', line 12

def ip_version
  @ip_version
end

#ipnoObject

Returns the value of attribute ipno.



12
13
14
# File 'lib/ip2location_ruby.rb', line 12

def ipno
  @ipno
end

#recordObject

Returns the value of attribute record.



12
13
14
# File 'lib/ip2location_ruby.rb', line 12

def record
  @record
end

#record_classObject

Returns the value of attribute record_class.



12
13
14
# File 'lib/ip2location_ruby.rb', line 12

def record_class
  @record_class
end

#v4Object

Returns the value of attribute v4.



12
13
14
# File 'lib/ip2location_ruby.rb', line 12

def v4
  @v4
end

Instance Method Details

#bsearch(low, high, ipnum, base_addr, col_length) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ip2location_ruby.rb', line 52

def bsearch(low, high, ipnum, base_addr, col_length) 
  mid = (high + low)/2
  return nil if low == mid

  ip_from, ip_to = get_from_to(mid, base_addr, col_length)
  if ipnum < ip_from
    low = low
    high = mid
    return bsearch(low, high, ipnum, base_addr, col_length)
  elsif ipnum >= ip_to
    low = mid
    high = high
    return bsearch(low, high, ipnum, base_addr, col_length) 
  else
    from_base = ( base_addr + mid * col_length)
    file.seek(from_base)  
    return self.record_class.read(file)
  end
end

#get_all(ip) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/ip2location_ruby.rb', line 27

def get_all(ip)
  ipno = IPAddr.new(ip, Socket::AF_UNSPEC)
  self.v4 = ipno.ipv4? && self.ip_version == 4
  ipnum = ipno.to_i + 0
  mid =  self.count/2
  col_length = columns * 4
  low = 0
  high = count
  return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
end

#get_from_to(mid, base_addr, col_length) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ip2location_ruby.rb', line 38

def get_from_to(mid, base_addr, col_length)
  if v4
    from_base = ( base_addr + mid * col_length)
  else
    from_base = ( base_addr + mid * (col_length + 12))
  end
  
  file.seek(from_base)
  ip_from =  file.read(4).unpack('L')[0]
  file.seek(from_base + col_length)
  ip_to = file.read(4).unpack('L')[0]
  [ip_from, ip_to]
end

#open(url) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ip2location_ruby.rb', line 14

def open(url)
  self.file = File.open(File.expand_path url, 'rb')
  i2l = Ip2locationConfig.read(file)
  self.db_index = i2l.databasetype
  self.count = i2l.databasecount + 0
  self.base_addr = i2l.databaseaddr - 1
  self.columns = i2l.databasecolumn + 0
  self.database = DbConfig.setup_database(self.db_index)
  self.ip_version = (i2l.ipversion == 0 ? 4 : 6)
  self.record_class = (Ip2LocationRecord.init database, self.ip_version)
  self
end