Class: Udger::IpParser
- Inherits:
-
BaseParser
- Object
- BaseParser
- Udger::IpParser
- Defined in:
- lib/udger/ip_parser.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
Returns the value of attribute db.
-
#ip ⇒ Object
Returns the value of attribute ip.
-
#object ⇒ Object
Returns the value of attribute object.
Instance Method Summary collapse
- #fetch_ip ⇒ Object
-
#initialize(db, ip) ⇒ IpParser
constructor
A new instance of IpParser.
- #ip4_format(ip_object) ⇒ Object
- #ip6_format(ip_object) ⇒ Object
- #parse ⇒ Object
- #parse_ip_object ⇒ Object
Constructor Details
#initialize(db, ip) ⇒ IpParser
Returns a new instance of IpParser.
6 7 8 9 10 |
# File 'lib/udger/ip_parser.rb', line 6 def initialize(db, ip) super(db) @ip = ip @object = IpAddress.new end |
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
4 5 6 |
# File 'lib/udger/ip_parser.rb', line 4 def db @db end |
#ip ⇒ Object
Returns the value of attribute ip.
4 5 6 |
# File 'lib/udger/ip_parser.rb', line 4 def ip @ip end |
#object ⇒ Object
Returns the value of attribute object.
4 5 6 |
# File 'lib/udger/ip_parser.rb', line 4 def object @object end |
Instance Method Details
#fetch_ip ⇒ Object
105 106 107 108 109 |
# File 'lib/udger/ip_parser.rb', line 105 def fetch_ip IPAddr.new @ip rescue StandardError nil end |
#ip4_format(ip_object) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/udger/ip_parser.rb', line 70 def ip4_format(ip_object) query = 'SELECT name,name_code,homepage FROM udger_datacenter_range JOIN udger_datacenter_list ON udger_datacenter_range.datacenter_id=udger_datacenter_list.id WHERE iplong_from <= ? AND iplong_to >= ?' ip_int = ip_object.to_i data = db.execute(query, [ip_int, ip_int]) return if data.empty? result = data[0] object.datacenter_name = result['name'] object.datacenter_name_code = result['name_code'] object.datacenter_homepage = result['homepage'] end |
#ip6_format(ip_object) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/udger/ip_parser.rb', line 85 def ip6_format(ip_object) ip_range = ip_object.to_string.split(':').map { |x| x.to_i(16) } query = 'SELECT name,name_code,homepage FROM udger_datacenter_range6 JOIN udger_datacenter_list ON udger_datacenter_range6.datacenter_id=udger_datacenter_list.id WHERE ' ip_range.each_with_index do |value, index| query += " iplong_from#{index} <= #{value} AND iplong_to#{index} >= #{value}" query += ' AND ' if index < 7 end data = db.execute(query) return if data.empty? result = data[0] object.datacenter_name = result['name'] object.datacenter_name_code = result['name_code'] object.datacenter_homepage = result['homepage'] end |
#parse ⇒ Object
12 13 14 15 16 17 |
# File 'lib/udger/ip_parser.rb', line 12 def parse return unless @ip object.ip = @ip parse_ip_object end |
#parse_ip_object ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/udger/ip_parser.rb', line 19 def parse_ip_object ip_object = fetch_ip return if ip_object.nil? object.ip_ver = ip_object.ipv4? ? 4 : 6 query = "SELECT udger_crawler_list.id as botid,ip_last_seen,ip_hostname,ip_country,ip_city,ip_country_code,ip_classification,ip_classification_code, name,ver,ver_major,last_seen,respect_robotstxt,family,family_code,family_homepage,family_icon,vendor,vendor_code,vendor_homepage,crawler_classification,crawler_classification_code FROM udger_ip_list JOIN udger_ip_class ON udger_ip_class.id=udger_ip_list.class_id LEFT JOIN udger_crawler_list ON udger_crawler_list.id=udger_ip_list.crawler_id LEFT JOIN udger_crawler_class ON udger_crawler_class.id=udger_crawler_list.class_id WHERE ip=? ORDER BY sequence" data = db.execute(query, ip_object.to_s) if data.empty? object.ip_classification = 'Unrecognized' object.ip_classification_code = 'Unrecognized' else result = data[0] object.ip_classification = result['ip_classification'] object.ip_classification_code = result['ip_classification_code'] object.ip_last_seen = result['ip_last_seen'] object.ip_hostname = result['ip_hostname'] object.ip_country = result['ip_country'] object.ip_country_code = result['ip_country_code'] object.ip_city = result['ip_city'] object.crawler_name = result['name'] object.crawler_ver = result['ver'] object.crawler_ver_major = result['ver_major'] object.crawler_family = result['family'] object.crawler_family_code = result['family_code'] object.crawler_family_homepage = result['family_homepage'] object.crawler_family_vendor = result['vendor'] object.crawler_family_vendor_code = result['vendor_code'] object.crawler_family_vendor_homepage = result['vendor_homepage'] object.crawler_family_icon = result['family_icon'] if result['ip_classification_code'] == 'crawler' object.crawler_family_info_url = "https://udger.com/resources/ua-list/bot-detail?bot=#{result['family']}#id#{result['botid']}" end object.crawler_last_seen = result['last_seen'] object.crawler_category = result['crawler_classification'] object.crawler_category_code = result['crawler_classification_code'] object.crawler_respect_robotstxt = result['respect_robotstxt'] end if object.ip_ver == 4 ip4_format(ip_object) else ip6_format(ip_object) end end |