Ready to get started?

Learn more about the CData ODBC Driver for SAP Netweaver Gateway or download a free trial:

Download Now

Connect to SAP Netweaver Gateway Data in Ruby

Connect to SAP Netweaver Gateway data in Ruby with ruby-dbi, dbd-odbc, and ruby-odbc.

The CData ODBC Driver for SAP Netweaver Gateway makes it easy to integrate connectivity to live SAP Netweaver Gateway data in Ruby. This article shows how to create a simple Ruby app that connects to SAP Netweaver Gateway data, executes a query, and displays the results.

Create an ODBC Connection to SAP Netweaver Gateway Data

If you have not already, first specify connection properties in an ODBC DSN (data source name). This is the last step of the driver installation. You can use the Microsoft ODBC Data Source Administrator to create and configure ODBC DSNs.

SAP Gateway allows both basic and OAuth 2.0 authentication. You can use basic authentication to connect to your own account, or you can use OAuth to enable other users to retrieve data from your service with their accounts. In addition to authenticating, set the following connection properties to access SAP Gateway tables.

  • Url: Set this to the URL of your environment, or to the full URL of the service. For example, the full URL might appear as: https://sapes5.sapdevcenter.com/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/. In this example, the environment URL would just be: https://sapes5.sapdevcenter.com.
  • Namespace: Set the appropriate Service Namespace. In the example above, IWBEP is the namespace. It is optional if the full URL to the service is specified.
  • Service: Set this to the service you want to retrieve data from. In the example above, the service is GWSAMPLE_BASIC. It is not required if the full URL is specified.

Authenticate via Basic Authentication

In basic authentication, you use your login credentials to connect. Set the following properties:

  • User: This is the username you use to log in to SAP Gateway.
  • Password: This is the password you use to log in to SAP Gateway.

Authenticate via OAuth Authentication

You can connect to SAP Gateway using the embedded OAuth connectivity (without setting any additional authentication connection properties). When you connect, the OAuth endpoint opens in your browser. Log in and grant permissions to complete the OAuth process. See the OAuth section in the online Help documentation for more information on other OAuth authentication flows.

Installing Ruby and Necessary Gems

If you do not have Ruby installed, refer to the Ruby installation page. With Ruby installed, you will need to install the ruby-dbi, dbd-odbc, and ruby-odbc gems:

gem install dbi gem install dbd-odbc gem install ruby-odbc

Create a Ruby App with Connectivity to SAP Netweaver Gateway Data

Create a new Ruby file (for example: SAPGatewaySelect.rb) and open it in a text editor. Copy the following code into your file:

#connect to the DSN require 'DBI' cnxn = DBI.connect('DBI:ODBC:CData SAPGateway Source','','') #execute a SELECT query and store the result set resultSet = cnxn.execute("SELECT ProductID, Quantity FROM SalesOrderLineItems WHERE Quantity < 15") #display the names of the columns resultSet.column_names.each do |name| print name, "\t" end puts #display the results while row = resultSet.fetch do (0..resultSet.column_names.size - 1).each do |n| print row[n], "\t" end puts end resultSet.finish #close the connection cnxn.disconnect if cnxn

With the file completed, you are ready to display your SAP Netweaver Gateway data with Ruby. To do so, simply run your file from the command line:

ruby SAPGatewaySelect.rb Writing SQL-92 queries to SAP Netweaver Gateway allows you to quickly and easily incorporate SAP Netweaver Gateway data into your own Ruby applications. Download a free trial today!