Forum: Ruby nfc 1.0.0 Released

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aaron P. (Guest)
on 2009-06-02 09:26
(Received via mailing list)
nfc version 1.0.0 has been released!

* <http://seattlerb.rubyforge.org>

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:

* A USB RFID reader.  I'm using the touchatag[http://touchatag.com].
* ffi
* libnfc

## 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

* 1 major enhancement

  * Birthday!

* <http://seattlerb.rubyforge.org>
Suraj K. (Guest)
on 2009-06-02 21: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
Brian C. (Guest)
on 2009-06-02 21:15
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. (Guest)
on 2009-06-03 00:31
(Received via mailing list)
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.
Suraj K. (Guest)
on 2009-06-03 03:18
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
Eric H. (Guest)
on 2009-06-03 03:30
(Received via mailing list)
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.
Suraj K. (Guest)
on 2009-06-03 08:16
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.
Ryan D. (Guest)
on 2009-06-03 12:54
(Received via mailing list)
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.
Suraj K. (Guest)
on 2009-06-03 20:01
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.  :-(
Ryan D. (Guest)
on 2009-06-04 00:54
(Received via mailing list)
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.
Reverend Ted (Guest)
on 2009-07-04 11:00
(Received via mailing list)
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:in `gem_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: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?

Thanks,
--T
Ryan D. (Guest)
on 2009-07-04 11:21
(Received via mailing list)
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?).
Aaron P. (Guest)
on 2009-07-04 11:26
(Received via mailing list)
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:

  http://github.com/tenderlove/nfc/tree/master

You need to have libnfc installed, and make sure that the dylib (or so)
is in your LD_LIBRARY_PATH.
Reverend Ted (Guest)
on 2009-07-04 14:11
(Received via mailing list)
On Jul 4, 9:20 am, Ryan D. <removed_email_address@domain.invalid> 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
Reverend Ted (Guest)
on 2009-07-04 14:30
(Received via mailing list)
On Jul 4, 9:25 am, Aaron P. <removed_email_address@domain.invalid> wrote:
> Make sure to read the section under INSTALL:
>
>  http://github.com/tenderlove/nfc/tree/master
>
> 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
Aaron P. (Guest)
on 2009-07-05 00:00
(Received via mailing list)
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!  :-)
This topic is locked and can not be replied to.