NuID Ruby SDK Gem


The Ruby SDK gem provides an interface for interacting with NuID APIs and utilities. We encourage you to go read the gem docs and contact us if you have any questions. We also encourage you to open an issue or pull request if you have any issues or suggestions.

Installation


Install the gem from rubygems. Be sure to add your API Key to your environment configuration.

# my-rails-app/Gemfile
gem "nuid-sdk"
$ bundle install
# my-rails-app/config/application.rb
config.x.nuid.auth_api_key = ENV['NUID_API_KEY'] // '<API KEY>'

Usage


To use the gem, require it into your app and configure it. This is simplest to do in your ApplicationController:

require 'nuid/sdk'

class ApplicationController < ActionController::Base
  def nuid_api
    @nuid_api ||= ::NuID::SDK::API::Auth.new(Rails.configuration.x.nuid.auth_api_key)
  end

  def render_error(error, status)
    render(json: {errors: [error]}, status: status)
  end

  #...
end

Once you have a configured sdk client, you can use it to dispatch requests to the API.

class UsersController < ApplicationController
  def create
    credential_res = nuid_api.credential_create(params[:credential])
    unless credential_res.code == 201
      return render_error("Unable to create the credential", :bad_request)
    end

    user = User.create!({
      email: params[:email].strip.downcase,
      first_name: params[:firstName],
      last_name: params[:lastName],
      nuid: credential_res.parsed_response["nu/id"]
    })

    render(json: { user: user }, status: :created)
  rescue => exception
    render_error(exception.message, 500)
  end
end

You can see a complete code example using the Ruby SDK gem in our examples repo, or go read our Integrating with NuID guide.