Skip to content

Instantly share code, notes, and snippets.

@kalinon
Created May 28, 2018 19:57
Show Gist options
  • Save kalinon/d19a0a7d2e83ff0e2d7211d080ebe06b to your computer and use it in GitHub Desktop.
Save kalinon/d19a0a7d2e83ff0e2d7211d080ebe06b to your computer and use it in GitHub Desktop.
A basic ruby script to serve up NFC reader responses in json
#!/usr/bin/ruby
require 'json'
require 'webrick'
def get_nfc
all = Array.new
current = Hash.new
`nfc-list`.split("\n").each do |line|
if line =~ /ATQA.+:\s((:?[\da-f]{2}\s+)+)/
current = Hash.new
current['ATQA'] = $1.gsub(/\s+/,'')
elsif line =~ /UID.+:\s((:?[\da-f]{2}\s+)+)/
current['UID'] = $1.gsub(/\s+/,'')
elsif line =~ /SAK.+:\s((:?[\da-f]{2}\s+)+)/
current['SAK'] = $1.gsub(/\s+/,'')
all << current
end
end
all.to_json
end
server = WEBrick::HTTPServer.new :Port => 3000
server.mount_proc '/' do |req, res|
res.status = 200
res['Content-Type'] = 'application/json'
res.body = get_nfc
end
server.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment