Class: OUI_Lookup::Lookup

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

Constant Summary collapse

URL =
'http://standards.ieee.org/cgi-bin/ouisearch'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oui, silent_searching = false) ⇒ Lookup

Create an OUI_Lookup search object, given a MAC address or vendor prefix.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/oui_lookup/lookup.rb', line 12

def initialize oui, silent_searching=false
  @oui = oui.to_s.scan /\h{2}/
  validate!

  # The oui.txt file lists three-octet OUI prefixes in both
  # dash-separated and conjoined formats. The OUI with dashes seems
  # less likely to be confused with other data on the page (say, ZIP
  # codes) and makes a more reliable search term.
  @oui = @oui.take(3).join '-'
  @silent_searching = silent_searching
end

Instance Attribute Details

#ouiObject (readonly)

Returns the value of attribute oui.



8
9
10
# File 'lib/oui_lookup/lookup.rb', line 8

def oui
  @oui
end

Instance Method Details

#error_page?(html) ⇒ Boolean

Check for error pages that improperly return a 2xx status code.

Sorry

Apparently included on all error pages with a 200 status code.

Searching for nothing

POST an invalid or empty search term (e.g. x is an array).

no match for the query

No match found. Not actually an error, per se. Handled in #run.

Returns:

  • (Boolean)


50
51
52
# File 'lib/oui_lookup/lookup.rb', line 50

def error_page? html
  return true if html.include? 'Searching for nothing'
end

#lookupObject

Returns the raw HTML document from a POST search.



36
37
38
39
40
# File 'lib/oui_lookup/lookup.rb', line 36

def lookup
 printf "Searching...\n\n" unless @silent_searching
 html = RestClient.post URL, { x: @oui }
 error_page?(html) ? raise( RuntimeError, html ) : html
end

#parse(html) ⇒ Object

Return a Nokogiri nodelist.



55
56
57
# File 'lib/oui_lookup/lookup.rb', line 55

def parse html
  node_list = Nokogiri::HTML.parse html
end

#runObject

Returns the results of a successful lookup, or a “not found” error.



25
26
27
28
29
30
31
32
33
# File 'lib/oui_lookup/lookup.rb', line 25

def run
  result = vendor(parse lookup)
  if result
    puts result unless @silent_searching
  else
    warn "OUI not found: " << @oui unless result
  end
  return result
end

#vendor(node_list) ⇒ Object

Return text node containing vendor data.



60
61
62
63
# File 'lib/oui_lookup/lookup.rb', line 60

def vendor node_list
  node = node_list.at_xpath('//pre')
  node ? node.text : nil
end