Class: MmReader::Query

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(postal, city, state) ⇒ Query

Returns a new instance of Query.



5
6
7
8
9
# File 'lib/mm_reader/query.rb', line 5

def initialize postal, city, state
  @postal = postal
  @city = city
  @state = state
end

Instance Attribute Details

#cityObject

Returns the value of attribute city.



3
4
5
# File 'lib/mm_reader/query.rb', line 3

def city
  @city
end

#postalObject

Returns the value of attribute postal.



3
4
5
# File 'lib/mm_reader/query.rb', line 3

def postal
  @postal
end

#stateObject

Returns the value of attribute state.



3
4
5
# File 'lib/mm_reader/query.rb', line 3

def state
  @state
end

Instance Method Details

#query_resultObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mm_reader/query.rb', line 11

def query_result
  db = MmReader::Connection.get
  result = db.execute('SELECT DISTINCT network FROM ip4_index WHERE postal_code = ?', [@postal])
  return result if result.size > 0

  result = db.execute('SELECT DISTINCT network FROM ip4_index WHERE city = ? and state = ?', [@city, @state])
  return result if result.size > 0

  result = db.execute('SELECT DISTINCT network FROM ip4_index WHERE city = ?', [@city])
  return result if result.size > 0

  result = db.execute('SELECT DISTINCT network FROM ip4_index WHERE state = ?', [@state])
  return result if result.size > 0

  return []
end

#resultObject



27
28
29
# File 'lib/mm_reader/query.rb', line 27

def result
  query_result.flatten.map{ |mask| IPAddr.new(mask).to_range.map(&:to_s) }.flatten
end