Skip to content

Commit

Permalink
Solve iso code table bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
phlegx committed Jul 3, 2017
1 parent 0bb23f5 commit 3bc3615
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Rails users can create a file `macker.rb` in `config/initializers` to load the o

```ruby
Maker.configure do |config|
config.oui_full_url = 'http://standards-oui.ieee.org/oui.txt', # Full URL of OUI text file
config.oui_full_url = 'http://linuxnet.ca/ieee/oui.txt', # Full URL of OUI text file
config.user_agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0', # A common user agent
config.ttl_in_seconds = 86_400, # Will expire the vendors in one day
config.cache = File.expand_path(File.dirname(__FILE__) + '/../../data/oui_*.txt'), # Can be a string, pathname or proc
Expand Down Expand Up @@ -84,6 +84,9 @@ mac.to_s

mac = Macker.generate(iso_code: 'US')
# => #<Macker::Address:0x000000046b86f0 @val=161304050786, @name="The Weather Channel", @address=["Mail Stop 500", "Atlanta Ga 30339", "Us"], @iso_code="US">

# Raise an exception
Macker.generate!(iso_code: 'HELLO')
```

### Lookup MAC address
Expand All @@ -99,6 +102,7 @@ Macker.lookup(mac)
Macker.lookup('64-E6-82-E5-CC-58')
Macker.lookup('64E682E5CC58')
Macker.lookup(110941201353816)
Macker.lookup!(110941201353816)
```

### MAC address
Expand Down Expand Up @@ -129,6 +133,46 @@ mymac.prefix
mymac.full_address
```

### Cache control

```ruby
# Update OUI from cache (careful)
Macker.update
# => 2017-07-03 13:03:00 +0200

# Update OUI from remote (straight)
Macker.update(true)
# => 2017-07-03 13:04:00 +0200

# Vendor table with all base16 MAC prefixes as keys
Macker.prefix_table
# => "F8DA0C"=>{:name=>"Hon Hai..."}, ...

# Vendor table with all country iso codes as keys
Macker.iso_code_table
# => "CN"=>[{:name=>"Hon Hai..."} ... ]

# Vendor table with all country vendor names as keys
Macker.vendor_table
# => "Apple, Inc."=>[{:prefix=>...} ... ]

Macker.expire!
# => false

Macker.expired?
# => false

Macker.stale?
# => false

Macker.vendors_expiration
# => 2017-07-04 13:04:00 +0200

# Get configuration of Macker
Macker.config
# => #<Macker::Config:0x0000000124ff30 @config=#...>>
```

## Contributors

* Inspired by MacVendor [github.com/uceem/mac_vendor](https://github.com/uceem/mac_vendor).
Expand Down
5 changes: 3 additions & 2 deletions lib/macker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def lookup!(mac)
# @return [Address] MAC address with data
def generate(opts = {})
expire! if config.auto_expiration
return generate_by_iso_code(opts.delete(:iso_code), opts) if opts[:iso_code] && opts[:iso_code].length == 2
return generate_by_iso_code(opts.delete(:iso_code), opts) if opts[:iso_code]
vendor = opts.delete(:vendor)
case vendor
when nil, false
Expand Down Expand Up @@ -257,6 +257,7 @@ def vendor_list(straight = false)
base16_fields = vendor.strip.split("\n")[1].split("\t")
mac_prefix = Address.new(base16_fields[0].strip[0..5]).prefix
address = vendor.strip.delete("\t").split("\n")
iso_code = address[-1].strip
next unless @prefix_table[mac_prefix].nil?
@prefix_table[mac_prefix] = { name: base16_fields[-1]
.strip
Expand All @@ -273,7 +274,7 @@ def vendor_list(straight = false)
.map(&:capitalize)
.join(' ')
},
iso_code: address[-1].strip.upcase
iso_code: iso_code.length == 2 ? iso_code.upcase : nil
}
end
@iso_code_table = @prefix_table.each_with_object({}) { |(key, value), out| (out[value[:iso_code]] ||= []) << value.merge(prefix: key) }
Expand Down
2 changes: 1 addition & 1 deletion lib/macker/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# Macker namespace
module Macker
# Macker version
VERSION = '0.1.0'.freeze
VERSION = '0.1.1'.freeze
end

0 comments on commit 3bc3615

Please sign in to comment.