1. Prerequisites

To follow this guide, you will need to:

2. Install and initialize the CodeQR Ruby SDK

1

Install

To install the CodeQR Ruby SDK, run the following command:

bash
gem install codeqr
2

Initialize

Initialize the CodeQR Ruby SDK by creating a new instance of the CodeQR struct.

class LinksController < ApplicationController
  require 'codeqr'

  before_action :initialize_codeqr

  def initialize_codeqr
    @codeqr = ::OpenApiSDK::CodeQR.new
    @codeqr.config_security(
      ::OpenApiSDK::Shared::Security.new(
        token: ENV['CODEQR_API_KEY']
      )
    )
  end
end

Let’s create a short link using the CodeQR Ruby SDK.

links_controller.rb
def create
  req = ::OpenApiSDK::Operations::CreateLinkRequest.new(
    request_body: ::OpenApiSDK::Operations::CreateLinkRequestBody.new(
      url: 'https://google.com'
    )
  )

  res = @codeqr.links.create(req)

  render json: res.raw_response.body
end

Optionally, you can also pass an externalId field which is a unique identifier for the link in your own database to associate it with the link in CodeQR’s system.

links_controller.rb
def create
  req = ::OpenApiSDK::Operations::CreateLinkRequest.new(
    request_body: ::OpenApiSDK::Operations::CreateLinkRequestBody.new(
      url: 'https://google.com',
      external_id: '12345'
    )
  )

  res = @codeqr.links.create(req)

  render json: res.raw_response.body
end

This will let you easily update the link or retrieve analytics for it later on using the externalId instead of the CodeQR linkId.

CodeQR Ruby SDK provides a method to upsert a link – where an existing link is updated if it exists, or a new link is created if it doesn’t. so you don’t have to worry about checking if the link already exists.

links_controller.rb
def upsert
  req = ::OpenApiSDK::Operations::UpsertLinkRequest.new(
    request_body: ::OpenApiSDK::Operations::UpsertLinkRequestBody.new(
      url: "https://google.com",
    ),
  )

  res = @codeqr.links.upsert(req)

  render json: res.raw_response.body
end

This way, you won’t have to worry about checking if the link already exists when you’re creating it.

Let’s update an existing link using the CodeQR Ruby SDK.

You can do that in two ways:

  • Using the link’s linkId in CodeQR’s system.
  • Using the link’s externalId in your own database (prefixed with ext_).
links_controller.rb
def update
  req = ::OpenApiSDK::Operations::UpdateLinkRequest.new(
    link_id: 'clx1gvi9o0005hf5momm6f7hj',
    request_body: ::OpenApiSDK::Operations::UpdateLinkRequestBody.new(
      url: 'https://google.uk'
    )
  )

  res = @codeqr.links.update(req)

  render json: res.raw_response.body
end

Let’s retrieve analytics for a link using the CodeQR Ruby SDK.

index.rb
def analytics
  req = ::OpenApiSDK::Operations::RetrieveAnalyticsRequest.new(
    link_id: "clx1gvi9o0005hf5momm6f7hj",
    interval: ::OpenApiSDK::Operations::Interval::SEVEND,
    group_by: ::OpenApiSDK::Operations::GroupBy::TIMESERIES
  )

  res = @codeqr.analytics.retrieve(req)

  render json: res.raw_response.body
end

7. Examples