Integrating with Rails (Ruby Rack)

The API code used in this tutorial is based on our Building a RESTful API with Rails tutorial. Please reference the tutorial if you need more context on the example code below or help setting up your project.

Ruby on Rails is an extremely popular way to quickly and securely build RESTful APIs. Moesif is a great addition to your Ruby API stack to cover everything from analytics to helping you set up metered billing.

Building your API is just the first step. Once built, you need to keep customers and services using the API happy. Moesif is the tool to help you do exactly that.

Integrating Moesif with your Rails API can be done in a few simple steps. In this blog I’ll show you how to:

  • Create a Moesif Account
  • Integrate Moesif into your API code
  • Verify the integration
  • Be ready to jump into docs and get started!

Step 1: Create your Moesif Account

If you don’t already have a Moesif account, You’ll want to sign up. Once you’re registered you’ll need to add a few details.

<img class="lazyload blur-up" data-src="/docs/images/guides/2200/1-get-started-screen.png" width="75%" alt="Moesif getting started screen">

These include:

  • Organization Name
  • the name of your organization or company
  • App Name
  • the name of the app you are integrating with Moesif
  • What best matches your role?
  • Here you can describe what your current role is at your organization. This helps us to assist you with relevant hints and tips in the future.
  • What are you hoping to achieve?
  • Here you can let us know how you'll use Moesif to help your organization or project.

Once you’ve filled out these details, click Next. After this, you’ll be presented with a brief overview of Moesif.

<img class="lazyload blur-up" data-src="/docs/images/guides/2200/2-overview-screen.png" width="75%" alt="Moesif platform overview screen">

Click Next once again.

Step 2: Add the Moesif integration code

After the Overview screen, you’ll be presented with the Quick Install screen. Here you can pick from any different server integrations and plugins. For Rails, we want to make sure Ruby Rack is selected in the left pane and that the Ruby Rack walkthrough is displayed on the right.

<img class="lazyload blur-up" data-src="/docs/images/guides/2200/3-choose-sdk-screen.png" width="75%" alt="moesif installation screen">

You’ll also see the message which shows “No data received yet”. This shows that Moesif is now listening for events to come in through the SDK that we are about to install in our Rails app.

Once selected, follow Step 1 on the screen and add the Moesif Middleware to your project. To do this, in your terminal (ensure that you are in your project’s root directory) run the following command to install the Moesif gem.

gem install moesif_rack

As an alternative, if you’re using Bundler, add the gem to your Gemfile by adding the line below.

gem 'moesif_rack'

To actually install the gem, run the bundle install command in the command line.

Bundle install

When the command completes, the SDK will be added to your project.

Next, you’ll want to add the integration code to your app. To do this, in your config/application.rb we will add the code to configure the Moesif middleware.

    moesif_options = {
      'application_id' => 'Your Moesif Application Id'
    }

    config.middleware.use MoesifRack::MoesifMiddleware, moesif_options

The Your Moesif Application Id in the code above should contain your unique Moesif application Id

The completed file should look something like this:

require_relative "boot"

require "rails/all"

Bundler.require(*Rails.groups)

module MoesifApiProject
 class Application < Rails::Application
   config.load_defaults 7.0
   config.api_only = true

   @moesif_options = {
     'application_id' => 'Your Moesif Application Id'
   }

   config.middleware.use MoesifRack::MoesifMiddleware, @moesif_options
 end
end

Since Moesif Rack is a logging middleware, the ordering of middleware matters. The best place for MoesifRack::MoesifMidleware is near the top (so it captures the data closest to the wire), but after any body parsers or authentication middleware. Typically, right above the default logger of Rails apps, “Rails::Rack::Logger” is a good spot. Or if you want to be as close to the wire as possible, put it before “ActionDispatch::Static” To insert the Moesif middleware before Rails::Rack::Logger, you can use the insert_before method instead of the use method that is used in the example above.

class Application < Rails::Application
    # snip

    config.middleware.insert_before Rails::Rack::Logger, MoesifRack::MoesifMiddleware, moesif_options

    # snip
  end

If you are using Rack::Deflater or another compression middleware, make sure Moesif is after it, so it can capture the uncompressed data.

The above lines of code configure the Moesif middleware component in your Rails app. There are lots of other options you can add to the configuration, this just sticks to the basics. To learn more about configuration options for the middleware, check out our Ruby Rack integration guide.

Step 3: Fire off your first request

Once your API code is back up and running, it’s time to send a request to your endpoint. You can use Postman, Insomnia, or an equivalent platform to send the GET request to your endpoints.

Regardless of how the request is sent, the Moesif integration into your application isn’t complete until the first request is sent and detected by Moesif. In the Moesif setup screen, before you send off the request, you should see a dialog at the bottom of the screen showing “No data received yet.” mentioned earlier.

Step 4: Confirm your integration

After the request is sent, Moesif should receive the event and data. This may take a few moments for the first request to reflect in the system, once sent. Once received, Moesif will confirm that you are ready to move forward. The previous prompt will turn green and show “Data Received!”.

<img class="lazyload blur-up" data-src="/docs/images/guides/2200/4-data-received.png" width="75%" alt="moesif integration screen with data received">

Lastly, in the green notification bar at bottom of the screen, click Next.

Step 5: Invite your team

The final step in the setup is to invite your team. The next screen is where you can do it quickly and easily.

<img class="lazyload blur-up" data-src="/docs/images/guides/2200/5-add-team.png" width="75%" alt="moesif add team member screen">

If you’d like to add a team member, simply add their email and click Send Invite. If you don’t have any team members yet or want to skip this step, you can always add team members later.

Once you’ve added all the team members you want, click Finish.

Explore Moesif!

You’ve now successfully integrated your API application with Moesif. You should now see the Moesif dashboard in the browser, ready to explore.

<img class="lazyload blur-up" data-src="/docs/images/guides/2200/6-live-event-log.png" width="75%" alt="moesif live event log screen with API call">

Now that your API is integrated and moving data into Moesif you can start to explore. Check out our docs to find everything from API analytics, monitoring and alerts, and more!

From Moesif

Updated: