The Azure Code Samples are currently available in English

Manage Azure Cognitive Services with Ruby

Última atualização: 30/09/2016
Editar no GitHub

This sample demonstrates how to manage your Azure cognitive services account using a ruby client.

On this page

1. If you don't already have it, install Ruby and the Ruby DevKit.

  1. If you don't have bundler, install it.

    gem install bundler
  2. Clone the repository.

    git clone
  3. Install the dependencies using bundle.

    cd cognitive-services-ruby-create-account
    bundle install
  4. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  5. Set the following environment variables using the information from the service principle that you created.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client id}
    export AZURE_CLIENT_SECRET={your client secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}

    [AZURE.NOTE] On Windows, use set instead of export.

  6. Run the sample.

    bundle exec ruby example.rb

What does example.rb do?

This sample starts by setting up a ResourceManagementClient object using your subscription and credentials.

subscription_id = ENV['AZURE_SUBSCRIPTION_ID'] || '11111111-1111-1111-1111-111111111111' # your Azure Subscription Id
provider =
credentials =
cs_client =
cs_client.long_running_operation_retry_timeout = ENV.fetch('RETRY_TIMEOUT', 30).to_i
resource_client =
resource_client.subscription_id = cs_client.subscription_id = subscription_id
resource_client.long_running_operation_retry_timeout = ENV.fetch('RETRY_TIMEOUT', 30).to_i

The sample then sets up a resource group.

resource_group_params = do |rg|
  rg.location = WEST_US


resource_client.resource_groups.create_or_update(GROUP_NAME, resource_group_params)

Create a cognitive services account

cs_acc_params =
sku = = Azure::ARM::CognitiveServices::Models::SkuName::F0
cs_acc_params.sku = sku
cs_acc_params.kind = Azure::ARM::CognitiveServices::Models::Kind::TextAnalytics
cs_acc_params.location = 'westus' = {:prop1 => 'prop1'}
cs_account = cs_client.cognitive_services_accounts.create(GROUP_NAME, ACCOUNT_NAME, cs_acc_params)

Delete a cognitive services account

cs_client.cognitive_services_accounts.delete(GROUP_NAME, ACCOUNT_NAME)

More information

Please refer to Azure SDK for Ruby for more information.

Mais amostras de Veronica Giaudrone