Back

Step by Step Guide to Building a Firebase Admin SDK API Integration in Ruby

Aug 3, 20245 minute read

Introduction

Hey there, fellow Ruby enthusiast! Ready to supercharge your app with Firebase? You're in the right place. We're going to walk through integrating the Firebase Admin SDK into your Ruby project. It's powerful, it's flexible, and trust me, it's going to make your life a whole lot easier.

Prerequisites

Before we dive in, make sure you've got:

  • Ruby 2.5 or later (come on, you're not still using 1.9, right?)
  • A Firebase project (if you don't have one, hop over to the Firebase console and set one up)
  • Your favorite text editor or IDE

Installation

First things first, let's get that Firebase Admin SDK gem into your project. Add this line to your Gemfile:

gem 'firebase-admin-sdk'

Then run:

bundle install

Easy peasy, right?

Authentication

Now, let's get you authenticated. Head over to your Firebase console, navigate to Project Settings > Service Accounts, and generate a new private key. Download that JSON file – it's your golden ticket.

In your Ruby code, initialize the SDK like this:

require 'firebase-admin-sdk' Firebase::Admin.configure do |config| config.credentials = 'path/to/your/service-account-key.json' end

Boom! You're in.

Basic Operations

Let's get our hands dirty with some CRUD operations:

Reading Data

firestore = Firebase::Admin::Firestore.client doc = firestore.col('users').doc('johndoe').get puts doc.data

Writing Data

firestore.col('users').doc('janedoe').set( name: 'Jane Doe', age: 30 )

Updating Data

firestore.col('users').doc('johndoe').update( age: 31 )

Deleting Data

firestore.col('users').doc('johndoe').delete

See? Firebase makes it almost too easy.

Advanced Features

User Management

Create a user:

auth = Firebase::Admin::Auth.client user = auth.create_user( email: '[email protected]', password: 'secretpassword' )

Custom Claims

Set custom claims for user roles:

auth.set_custom_user_claims(user.uid, { admin: true })

Firebase Cloud Messaging (FCM)

Send a message to a device:

messaging = Firebase::Admin::Messaging.client message = Firebase::Admin::Messaging::Message.new( token: 'device_token', notification: { title: 'Hello', body: 'World' } ) messaging.send(message)

Error Handling and Best Practices

Always wrap your Firebase calls in begin/rescue blocks:

begin # Your Firebase operation here rescue Firebase::Admin::Error => e puts "Oops! #{e.message}" end

And remember, keep your service account key secret. Never, ever commit it to version control. Use environment variables in production.

Testing

Mocking Firebase services in your tests is crucial. Check out libraries like webmock or vcr to stub out Firebase API calls.

Deployment Considerations

When deploying, use environment variables to manage your Firebase configuration. In your production code, you might do something like:

Firebase::Admin.configure do |config| config.credentials = JSON.parse(ENV['FIREBASE_CREDENTIALS']) end

Conclusion

And there you have it! You're now armed and dangerous with Firebase Admin SDK in your Ruby arsenal. Remember, the official Firebase docs are your best friend for diving deeper. Now go forth and build something awesome!

Happy coding, Rubyist! 🚀