Nfc 1.0.0 Released

nfc version 1.0.0 has been released!

NFC is a ruby wrapper for the Near Field Communication library. The
Near
Field Communication library works with many USB RFID readers, so this
gem
lets you read RFID tags.

FEATURES/PROBLEMS:

  • Only supports ISO1443A (MIFARE) tags right now.

SYNOPSIS:

require ‘nfc’

Read your tag and print the info

p NFC.instance.find

REQUIREMENTS:

INSTALL:

  • First install libnfc[http://libnfc.org/]
  • Make sure libnfc.dylib or libnfc.so is in your library path
  • gem install nfc

Changes:

1.0.0 / 2009-06-01

Aaron P. wrote:

require ‘nfc’

Read your tag and print the info

p NFC.instance.find

Seems like the NFC class includes Singleton, correct?

require ‘singleton’
class NFC
include Singleton
# …
end

You could simplify your public API (by not requiring users to type
.instance) like this:

require ‘singleton’
NFC = Class.new do
include Singleton
# …
end.instance

Suraj K. wrote:

You could simplify your public API (by not requiring users to type
.instance) like this:

require ‘singleton’
NFC = Class.new do
include Singleton
# …
end.instance

Module functions are good for this too.

module NFC
def find(*args)
puts *args
end
module_function :find
end

NFC.find(“123”)

class Foo
include NFC
def bar
find(“456”)
end
end

Foo.new.bar

Aaron P. wrote:

On Wed, Jun 03, 2009 at 02:01:45AM +0900, Suraj K. wrote:

include Singleton

end.instance

That would create a new instance of the object as soon as the gem is
required, and that might not be desired. Especially when dealing with a
piece of hardware.

In that case, use Kernel#autoload to load your library only when the NFC
constant is actually used:

autoload :NFC, ‘nfc’

Then your users can simply write:

NFC.find

On Jun 2, 2009, at 16:18, Suraj K. wrote:

In that case, use Kernel#autoload to load your library only when the
NFC
constant is actually used:

autoload :NFC, ‘nfc’

Then your users can simply write:

NFC.find

You have to require ‘nfc’ to get the autoload, so this won’t work.
Adding two files to do this is far lamer than Singleton.

If you’re really dead set on it, why don’t just fork it? I’m quite
certain Aaron won’t care.

Eric H. wrote:

You have to require ‘nfc’ to get the autoload, so this won’t work.

I beg to differ; you need not require() before using autoload():

sun@yantram ~/tmp> cat nfc.rb
NFC = Class.new do
require ‘singleton’
include Singleton

def find(*args)
  p :find => args
end

end.instance

p :loaded_nfc

sun@yantram ~/tmp> cat run_this.rb
autoload :NFC, ‘nfc’
NFC.find(‘123’)

sun@yantram ~/tmp> multiruby run_this.rb

VERSION = 1.8.7-p72
CMD = ~/.multiruby/install/1.8.7-p72/bin/ruby run_this.rb

:loaded_nfc
{:find=>[“123”]}

RESULT = 0

VERSION = 1.8.6-p368
CMD = ~/.multiruby/install/1.8.6-p368/bin/ruby run_this.rb

:loaded_nfc
{:find=>[“123”]}

RESULT = 0

VERSION = 1.9.1-p0
CMD = ~/.multiruby/install/1.9.1-p0/bin/ruby run_this.rb

:loaded_nfc
{:find=>[“123”]}

RESULT = 0

VERSION = jruby-1.2.0
CMD = ~/.multiruby/install/jruby-1.2.0/bin/ruby run_this.rb

:loaded_nfc
{:find=>[“123”]}

RESULT = 0

TOTAL RESULT = 0 failures out of 4

Passed: 1.8.6-p368, 1.8.7-p72, jruby-1.2.0, 1.9.1-p0
Failed:

Adding two files to do this is far lamer than Singleton.

Why would you need two files? Because NFC is a gem?

Since RubyGems overrides Kernel#require() and Kernel#autoload() uses
Kernel#require(), the autoload approach should work even if loading NFC
from a gem.

On Wed, Jun 03, 2009 at 02:01:45AM +0900, Suraj K. wrote:

include Singleton

end.instance
That would create a new instance of the object as soon as the gem is
required, and that might not be desired. Especially when dealing with a
piece of hardware.

On Jun 2, 2009, at 21:16 , Suraj K. wrote:

Eric H. wrote:

You have to require ‘nfc’ to get the autoload, so this won’t work.

I beg to differ; you need not require() before using autoload():

yes… but you have to put that autoload SOMEWHERE. Otherwise, what’s
the point? You should have been able to intuit that eric understood
this by stating “adding two files to do this is far lamer than
Singleton”.

I believe it is fair to say at this point that nobody involved with
nfc cares about “improving” the API as you’re suggesting. There isn’t
anything wrong with typing “instance”. As Eric stated, if you’ve got
such a boner for autoload, you’re welcome to fork.

Ryan D. wrote:

On Jun 2, 2009, at 21:16 , Suraj K. wrote:

Eric H. wrote:

You have to require ‘nfc’ to get the autoload, so this won’t work.

I beg to differ; you need not require() before using autoload():

yes… but you have to put that autoload SOMEWHERE. Otherwise, what’s
the point? You should have been able to intuit that eric understood
this by stating “adding two files to do this is far lamer than
Singleton”.

Ah, I see now.

I believe it is fair to say at this point that nobody involved with
nfc cares about “improving” the API as you’re suggesting. There isn’t
anything wrong with typing “instance”. As Eric stated,

Alright.

if you’ve got such a boner for autoload, you’re welcome to fork.

Such harsh words! You could have stopped at the previous sentence; your
point has already been made. This remark only adds hostility; a poison
coating for your arrows.

Please remember MINSWAN. :frowning:

I’m really intrigued by this gem, and have been trying to get it to
work. Unfortunately, I keep running aground with the following error
(s):

/opt/local/lib/ruby/gems/1.8/gems/ffi-0.3.5/lib/ffi/library.rb:
18:in ffi_lib': Could not open any of [nfc] (LoadError) from /opt/local/lib/ruby/gems/1.8/gems/nfc-1.0.0/lib/nfc/lib_nfc.rb:6 from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb: 31:ingem_original_require’
from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:
31:in require' from /opt/local/lib/ruby/gems/1.8/gems/nfc-1.0.0/lib/nfc.rb:2 from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb: 36:ingem_original_require’
from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:
36:in `require’
from nfctest.rb:2

Perhaps it’s obvious to more experienced devs, but after much Googling
and reading to figure it out, I’m stumped. Anyone?

Thanks,
–T

On Jul 4, 2009, at 00:00 , Reverend Ted wrote:

I’m really intrigued by this gem, and have been trying to get it to
work. Unfortunately, I keep running aground with the following error
(s):

/opt/local/lib/ruby/gems/1.8/gems/ffi-0.3.5/lib/ffi/library.rb:
18:in `ffi_lib’: Could not open any of [nfc] (LoadError)

nfc (the gem) uses ffi to attach to a C library of the same name. You
need this installed if it doesn’t ship with the gem (which it prolly
won’t, otherwise why bother with ffi?).

On Sat, Jul 04, 2009 at 04:00:06PM +0900, Reverend Ted wrote:

31:in require' from /opt/local/lib/ruby/gems/1.8/gems/nfc-1.0.0/lib/nfc.rb:2 from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb: 36:in gem_original_require’
from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:
36:in `require’
from nfctest.rb:2

Perhaps it’s obvious to more experienced devs, but after much Googling
and reading to figure it out, I’m stumped. Anyone?

Make sure to read the section under INSTALL:

GitHub - tenderlove/nfc: NFC is a ruby wrapper for the Near Field Communication library.

You need to have libnfc installed, and make sure that the dylib (or so)
is in your LD_LIBRARY_PATH.

On Jun 3, 2009, at 09:01 , Suraj K. wrote:

if you’ve got such a boner for autoload, you’re welcome to fork.

Such harsh words! You could have stopped at the previous sentence;
your
point has already been made. This remark only adds hostility; a
poison
coating for your arrows.

I assure you that hostility would be offering you MY boner if you
didn’t like the current implementation.

On Jul 4, 9:20 am, Ryan D. [email protected] wrote:

On Jul 4, 2009, at 00:00 , Reverend Ted wrote:

nfc (the gem) uses ffi to attach to a C library of the same name. You
need this installed if it doesn’t ship with the gem (which it prolly
won’t, otherwise why bother with ffi?).

Thanks, Ryan. I had the C library in place. Aaron P.'s PATH
leads me to the fix.
T

On Jul 4, 9:25 am, Aaron P. [email protected] wrote:

Make sure to read the section under INSTALL:

GitHub - tenderlove/nfc: NFC is a ruby wrapper for the Near Field Communication library.

You need to have libnfc installed, and make sure that the dylib (or so)
is in your LD_LIBRARY_PATH.

Aaron:
Thanks for the reply. That gave me enough to get there.

I document the solution for anyone coming along looking for it:

MacOS uses a different environment setting from LD_LIBRARY_PATH.
Instead it uses DYLD_LIBRARY_PATH. (Try ‘man dyld’ at the command line
to learn more about this.)

To enable the nfc gem to find libnfc, you must ‘export
DYLD_LIBRARY_PATH=/path/to/libnfc-1.1.0:$DYLD_LIBRARY_PATH’

That makes it work for a given Terminal session. You can add the same
command to your ~/.profile file.

Thanks both to you and Ryan for the quick replies.

Aaron: mail me at ted /at/ touchatag so I can send you a thank you
gift.

–Ted

On Sat, Jul 04, 2009 at 07:30:05PM +0900, Reverend Ted wrote:

I document the solution for anyone coming along looking for it:

MacOS uses a different environment setting from LD_LIBRARY_PATH.
Instead it uses DYLD_LIBRARY_PATH. (Try ‘man dyld’ at the command line
to learn more about this.)

To enable the nfc gem to find libnfc, you must ‘export
DYLD_LIBRARY_PATH=/path/to/libnfc-1.1.0:$DYLD_LIBRARY_PATH’

Hrm. ffi uses dlopen for loading the dynamic library. The man page for
dlopen under the searching section indicates that it searches
LD_LIBRARY_PATH, DYLD_LIBRARY_PATH, then DYLD_FALLBACK_LIBRARY_PATH.

So either should work (though I haven’t tried).

That makes it work for a given Terminal session. You can add the same
command to your ~/.profile file.

Thanks both to you and Ryan for the quick replies.

No problem!

Aaron: mail me at ted /at/ touchatag so I can send you a thank you
gift.

Sent. Thank you! :slight_smile: