How to connect Ruby with OneDrive

Introduction

Connecting Ruby to Onedrive via ODBC is an excellent option for providing seamless interaction, enabling you to manage cloud files efficiently and streamline workflows. This article will show you how to install and use the ruby-odbc gem to connect to a database via ODBC, along with recommendations and solutions to typical installation and execution errors.

Prerequisites

Steps

Step 1. Installing Ruby and the ruby-odbc Gem

Some versions of Ruby may not be fully compatible with the ruby-odbc gem. To ensure proper functionality, it is recommended to use Ruby 3.1.x, as newer versions may introduce compatibility issues

  1. Download the installer from Ruby installer
  2. Install Ruby and DevKit.
  3. Open Command Prompt as an administrator
  4. Verify the version you are using with this command ruby -v
  5. install the ruby-odbc gem with the following command: gem install ruby-odbc
  6. Verify the gem installed with this command: gem list ruby-odbc
    Note: If you encounter installation issues, you may need administrative permissions

Step 2: Create an Application on the OneDrive Side

  1. Go to the Azure Portal and log in.
  2. Navigate to Azure Active Directory.
  3. Click App registrations.
  4. Select New registration.
  5. Enter an application name.
  6. Choose Accounts in this organizational directory only for the supported account type.
  7. For the Redirect URI, select Web and enter https://login.microsoftonline.com/common/oauth2/nativeclient or any valid redirect URL, like https://zappysys.com/oauth.
  8. Save your Application (client) ID, TenantID, and Secret ID.

Step 3: Create a New Driver

  1. Open ODBC Data Source by typing “ODBC” in the search box.

  2. For SQL Server Integration, go to User DSN or System DSN. Click Add.

  3. From the driver list, select ZappySys API Driver, choose OneDrive, and click Continue.

Step 4: OneDrive Connector Configuration

  1. Fill in Client ID and Client Secret and specify the necessary Scopes. Use the following URLs for Authorization and Token:
* https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/authorize
* https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token
  1. Generate the token and test the connection.

  2. Go to the Preview Tab, select a table, and view the results.

Step 5. Connecting Ruby to ODBC

Once the DSN is set up, you can use ruby-odbc to establish the connection and run queries. Create a Ruby file, e.g. OneDriveRuby.rb

require 'odbc'

# DSN name configured in ODBC
dsn = "API Driver - OneDrive"  # Replace with the correct name

begin
  # Connect to the database using DSN
  db = ODBC.connect(dsn)

  # Verify connection
  puts "✅ Successfully connected to ODBC"

  # Execute a test query
  query = "SELECT * FROM Drives"  # Adjust according to your database
  stmt = db.run(query)

  # Check if there is data
  rows = stmt.fetch_all
  if rows.empty?
    puts "⚠️ No data found in the database."
  else
    # Display results
    rows.each do |row|
      puts row.inspect
    end
  end

  # Close connection
  stmt.close
  db.disconnect

rescue ODBC::Error => e
  puts "❌ Connection error: #{e.message}"
end

When you run the code, it will call the API and retrieve data.

Common Errors and Solutions

:x: Error: cannot load such file -- dbi (LoadError)

Cause: You are trying to use the dbi gem, but it is not installed.
Solution:

gem install dbi
gem install dbd-odbc

:x: Error: ERROR: Failed to build gem native extension.

Cause: A compiler or necessary libraries are missing to compile the gem.
Solution: Install MSYS2 and ensure gcc and make are available.


:x: Error: Data source name not found, and no default driver specified

Cause: The DSN is not configured or the driver is not installed.
Solution:

  • Verify the configuration in odbcad32.exe
  • Ensure the DSN name in the code exactly matches the one configured in ODBC.

:x: Error: Issues with Ruby Version

Cause: Some Ruby versions may not be compatible with the ruby-odbc gem, especially if they are too new or too old.
Solution:

  • Check your Ruby version with:
    ruby -v
    
  • If your version is 3.2.x and you experience issues, try using an earlier version of Ruby (e.g., 2.7.x) or use RVM to switch versions:
    rvm install 2.7.8
    rvm use 2.7.8
    

Conclusion

Click here to download and connect Ruby with OneDrive API Driver in ODBC using ruby-odbc, it is a flexible and effective way to interact with databases. Following this guide, you can install, configure, and troubleshoot common issues when establishing the connection. If you encounter additional problems, review the error logs and ensure the DSN is correctly set up.

Related Links:

Contact Us

If you encounter any challenges or have specific use cases, please contact our support team via chat or ticket.