Class: MmReader::Query
- Inherits:
-
Object
- Object
- MmReader::Query
- Defined in:
- lib/mm_reader/query.rb
Instance Attribute Summary collapse
-
#city ⇒ Object
Returns the value of attribute city.
-
#postal ⇒ Object
Returns the value of attribute postal.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
-
#initialize(postal, city, state) ⇒ Query
constructor
A new instance of Query.
- #query_result ⇒ Object
- #result ⇒ Object
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
#city ⇒ Object
Returns the value of attribute city.
3 4 5 |
# File 'lib/mm_reader/query.rb', line 3 def city @city end |
#postal ⇒ Object
Returns the value of attribute postal.
3 4 5 |
# File 'lib/mm_reader/query.rb', line 3 def postal @postal end |
#state ⇒ Object
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_result ⇒ Object
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 |
#result ⇒ Object
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 |