Class: Udger::Parser

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

Constant Summary collapse

DB_FILENAME =
'udgerdb_v3.dat'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_dir = nil, lru_cache_size: 10_000, cache: true, ua_services: []) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
13
14
15
16
# File 'lib/udger/parser.rb', line 9

def initialize(data_dir = nil, lru_cache_size: 10_000, cache: true, ua_services: [])
  @data_dir = data_dir || __dir__
  @db = SQLite3::Database.new "#{@data_dir}/#{DB_FILENAME}"
  @db.results_as_hash = true
  @cache = cache
  parse_ua_params(ua_services)
  @lru_cache = lru_cache_size.zero? && !cache ? {} : LruRedux::Cache.new(lru_cache_size)
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



7
8
9
# File 'lib/udger/parser.rb', line 7

def cache
  @cache
end

#data_dirObject (readonly)

Returns the value of attribute data_dir.



6
7
8
# File 'lib/udger/parser.rb', line 6

def data_dir
  @data_dir
end

#dbObject (readonly)

Returns the value of attribute db.



6
7
8
# File 'lib/udger/parser.rb', line 6

def db
  @db
end

#lru_cache_sizeObject

Returns the value of attribute lru_cache_size.



7
8
9
# File 'lib/udger/parser.rb', line 7

def lru_cache_size
  @lru_cache_size
end

Instance Method Details

#parse_ip(ip) ⇒ Object



32
33
34
35
36
# File 'lib/udger/parser.rb', line 32

def parse_ip(ip)
  parser = IpParser.new @db, ip
  parser.parse
  parser.object
end

#parse_ua(ua_string) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/udger/parser.rb', line 18

def parse_ua(ua_string)
  if @cache
    cache_string = ua_string.hash
    cache_value = @lru_cache[cache_string]
    return cache_value if cache_value

    result = parse_ua_no_cache(ua_string)
    @lru_cache[cache_string] = result
    result
  else
    parse_ua_no_cache(ua_string)
  end
end