Connect to Linear Data in JRuby
JRuby is a high-performance, stable, fully threaded Java implementation of the Ruby programming language. The CData JDBC Driver for Linear makes it easy to integrate connectivity to live Linear data in JRuby. This article shows how to create a simple JRuby app that connects to Linear data, executes a query, and displays the results.
Configure a JDBC Connection to Linear Data
Before creating the app, note the installation location for the JAR file for the JDBC Driver (typically C:\Program Files\CData\CData JDBC Driver for Linear\lib).
JRuby natively supports JDBC, so you can easily connect to Linear and execute SQL queries. Initialize the JDBC connection with the getConnection function of the java.sql.DriverManager class.
You can authenticate to Linear with a personal API key or with OAuth 2.0. The API key is the simplest option for connecting with your own Linear account.
Authenticating with an API Key
Set the following connection properties:
- AuthScheme: Set this to APIKey.
- APIKey: A Linear personal API key.
To create a personal API key, log in to Linear, open Settings > Security & access > Personal API keys, select New API key, and create it. Copy the key immediately, because Linear shows it only once.
Authenticating with OAuth
OAuth requires a custom OAuth application registered in Linear (Settings > API > OAuth applications), which provides the OAuthClientId and OAuthClientSecret. Two flows are supported:
- Authorization code: Set AuthScheme to OAuth, InitiateOAuth to GETANDREFRESH, and provide OAuthClientId, OAuthClientSecret, and the CallbackURL defined in your application (e.g., http://localhost:33333). The driver opens Linear in your browser so you can grant access.
- Client credentials: Set AuthScheme to OAuthClient and provide OAuthClientId and OAuthClientSecret. This authenticates the application itself, with no browser interaction, and suits machine-to-machine integrations.
By default, the driver requests the read,write scopes. The driver refreshes the access token automatically when it expires.
Built-in Connection String Designer
For assistance in constructing the JDBC URL, use the connection string designer built into the Linear JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.linear.jar
Fill in the connection properties and copy the connection string to the clipboard.
Below is a typical JDBC connection string for Linear:
jdbc:linear:AuthScheme=APIKey;APIKey=myAPIKey;
Create a JRuby App with Connectivity to Linear Data
Create a new Ruby file (for example: LinearSelect.rb) and open it in a text editor. Copy the following code into your file:
require 'java'
require 'rubygems'
require 'C:/Program Files/CData/CData JDBC Driver for Linear 2018/lib/cdata.jdbc.linear.jar'
url = "jdbc:linear:AuthScheme=APIKey;APIKey=myAPIKey;"
conn = java.sql.DriverManager.getConnection(url)
stmt = conn.createStatement
rs = stmt.executeQuery("SELECT id, name FROM Team")
while (rs.next) do
puts rs.getString(1) + ' ' + rs.getString(2)
end
With the file completed, you are ready to display your Linear data with JRuby. To do so, simply run your file from the command line:
jruby -S LinearSelect.rb
Writing SQL-92 queries to Linear allows you to quickly and easily incorporate Linear data into your own JRuby applications. Download a free trial today!