Rfc.rb

Path: lib/SAP/Rfc.rb
Last Update: Wed Jun 06 14:52:13 +0100 2007
Array TabArray SAPRfc Rfc Tools BaseParm Parm Tab Ticket Struct Iface Field SAP dot/f_0.png

SAP::Rfc - Ruby extension for performing RFC Function calls against an SAP R/3 System.

Overview Documentation at: www.piersharding.com/download/ruby/doc/saprfc.html

API Documentation at: www.piersharding.com/download/ruby/doc/

Project and Download at: raa.ruby-lang.org/project/saprfc

SYNOPSIS

  require "SAP/Rfc"

  rfc = SAP::Rfc.new(:ashost => "localhost",
                     :sysnr  => 18,
                     :lang   => "EN",
                     :client => 000,
                     :user   => "DEVELOPER",
                     :passwd => "19920706",
                     :trace  => 1)

 # get the connection ID
 print "connect id: ", rfc.connection, "\n"

 # test the connection
 print "is_connected: ", rfc.is_connected(), "\n"

 # get the system information
 print "sapinfo: "
 p rfc.sapinfo()

 # lookup the interface for RFC_READ_TABLE
 itab = rfc.discover("RFC_READ_TABLE")

 # set some interface parameters
 itab.query_table.value = "TRDIR"
 itab.delimiter.value = "|"
 itab.rowcount.value = 10

 # put a rows into table OPTIONS
 itab.options.value = ["NAME LIKE 'SAPL\%RFC\%'"]

 # do the RFC call
 rfc.call(itab)

 # access the results of a table
 itab.data.nextHashRow {|h| print "GOT A HASH: "; p h }

 print "close connection: ", rfc.close(), "\n"

Alternatively for registered RFC applications:

 require "lib/SAP/Rfc"
 # create a connection object that can talk to the gateway
 rfc = SAP::Rfc.new(:trace  => 1,
                    :tpname => "wibble.rfcexec",
                    :gwhost => "localhost",
                    :gwserv => "3300")

 # create an interface object that describes the RFC that SAP will call out to
 iface = SAP::Iface.new("RFC_REMOTE_PIPE")
 iface.addParm( SAP::Parm.new("COMMAND", nil, RFCIMPORT, RFCTYPE_CHAR, 256) )
 iface.addParm( SAP::Parm.new("READ", nil, RFCIMPORT, RFCTYPE_CHAR, 1) )
 iface.addParm( SAP::Tab.new("PIPEDATA", nil, 80) )

 # specify the callback
 iface.callback = Proc.new  do |iface|
   call = `#{iface.COMMAND.value()}`
   call.split(/\n/).each do |val|
     iface.PIPEDATA.value.push(val.ljust(80))
   end
 end

 # add the interface to the RFC collection
 rfc.iface(iface)

 # start off the main loop
 rfc.accept()

Getting Started

The best way to describe this package is to give a brief over view, and then launch into several examples. The SAP::Rfc Class works in concert with several other Classes that also come with same distribution, these are SAP::Iface, SAP::Parm, SAP::Tab, and SAP::Struc. These come together to give you an object oriented programming interface to performing RFC function calls to SAP from a UNIX based platform with your favourite programming language - Ruby. An SAP::Rfc object holds together one ( and only one ) connection to an SAP system at a time. The SAP::Rfc object can use one or many SAP::Iface objects, each of which equate to the definition of an RFC Function in SAP ( see trans SE37 ). Each SAP::Iface object holds one or many SAP::Parm, and/or SAP::Tab objects, corresponding to the RFC Interface definition in SAP ( SE37 ). For all SAP::Tab objects, and for complex SAP::Parm objects, a SAP::Struc object can be defined. This equates to a structure definition in the data dictionary ( see trans SE11 ). Because the manual definition of interfaces and structures is a boring and tiresome exercise, there are specific methods provided to automatically discover, and add the appropriate interface definitions for an RFC Function module to the SAP::Rfc object ( see methods discover, and structure of SAP::Rfc ).

Caching Interface and Structure discovery

In the interests of performance it is possible to cache the results of Interface and Structure discovery - each of which require RFC calls to R/3 to determine their makeup.

To switch on the caching call SAP::Rfc.useCache = true. The location of the cache repository can be controlled by calling SAP:;Rfc.cache = "/some/directory" . The default cache directory is .rfc_cache/ - which is relative to the current working directory at run time. Caching makes use of the standard ruby serialisation mechanism Marshal.

Project homepage/owner www.piersharding.com/blog/

Required files

saprfc  

Constants

RFCIMPORT = 0
RFCEXPORT = 1
RFCTABLE = 2
RFCCHANGING = 3
RFCTYPE_CHAR = 0
RFCTYPE_DATE = 1
RFCTYPE_BCD = 2
RFCTYPE_TIME = 3
RFCTYPE_BYTE = 4
RFCTYPE_NUM = 6
RFCTYPE_FLOAT = 7
RFCTYPE_INT = 8
RFCTYPE_INT2 = 9
RFCTYPE_INT1 = 10
RFCTYPE_STRING = 29
RFCTYPE_XSTRING = 30
DEFAULT_CACHE = ".rfc_cache/"

[Validate]