Class: AzureSTT::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/azure_stt/client.rb

Overview

Client class that uses HTTParty to communicate with the API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region:, subscription_key:) ⇒ Client

Initialize the client

Parameters:

  • subscription_key (String)

    Cognitive Services API Key

  • region (String)

    The region of your resources



19
20
21
22
23
# File 'lib/azure_stt/client.rb', line 19

def initialize(region:, subscription_key:)
  @subscription_key = subscription_key
  @region = region
  self.class.base_uri "https://#{region}.api.cognitive.microsoft.com/speechtotext/v3.1"
end

Instance Attribute Details

#regionObject (readonly)

Returns the value of attribute region.



11
12
13
# File 'lib/azure_stt/client.rb', line 11

def region
  @region
end

#subscription_keyObject (readonly)

Returns the value of attribute subscription_key.



11
12
13
# File 'lib/azure_stt/client.rb', line 11

def subscription_key
  @subscription_key
end

Instance Method Details

#create_transcription(**args) ⇒ Hash

Create a transcription for a batch or a single file.

Parameters:

  • args (Hash)

Returns:

  • (Hash)

    The JSON body response, parsed by HTTParty

See Also:



34
35
36
37
38
39
40
41
# File 'lib/azure_stt/client.rb', line 34

def create_transcription(**args)
  results = post(
    '/transcriptions',
    args.to_json
  )

  results.parsed_response
end

#delete_transcription(id) ⇒ Boolean

Delete a transcription with a given ID

Parameters:

  • id (String)

    The id of the transcription in the API

Returns:

  • (Boolean)

    true if the transcription had been deleted, raises an error else



84
85
86
87
88
89
# File 'lib/azure_stt/client.rb', line 84

def delete_transcription(id)
  response = self.class.delete("/transcriptions/#{id}", headers: headers)
  handle_response(response)

  true
end

#get_file(file_url) ⇒ Hash

Read a JSON file and parse it.

Parameters:

  • file_url (String)

    The url of the content

Returns:

  • (Hash)

    the file parsed



113
114
115
116
117
118
119
# File 'lib/azure_stt/client.rb', line 113

def get_file(file_url)
  response = self.class.get(file_url)

  results = handle_response(response)

  results.parsed_response
end

#get_transcription(id) ⇒ Hash

Get a transcription by giving it's id

Parameters:

  • id (String)

    The identifier of the transcription

Returns:

  • (Hash)

    The JSON body response, parsed by HTTParty



50
51
52
53
54
# File 'lib/azure_stt/client.rb', line 50

def get_transcription(id)
  results = get("/transcriptions/#{id}")

  results.parsed_response
end

#get_transcription_files(id) ⇒ Array[Hash]

Get an array containing the files for a given transcription

Parameters:

  • id (Integer)

    The identifier of the transcription

Returns:

  • (Array[Hash])

    Array of the files of a transcription

See Also:



100
101
102
103
104
# File 'lib/azure_stt/client.rb', line 100

def get_transcription_files(id)
  results = get("/transcriptions/#{id}/files")

  results.parsed_response['values']
end

#get_transcriptions(skip: nil, top: nil) ⇒ Array[Hash]

Get an Array of all the transcriptions

are Hashes parsed by HTTParty.

Parameters:

  • skip (Integer) (defaults to: nil)

    Number of transcriptions that will be skipped (optional)

  • top (Integer) (defaults to: nil)

    Number of transcriptions that will be included (optional)

Returns:

  • (Array[Hash])

    Array of all the transcriptions. The transcriptions



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/azure_stt/client.rb', line 65

def get_transcriptions(skip: nil, top: nil)
  results = get(
    '/transcriptions',
    {
      skip: skip,
      top: top
    }.compact
  )

  results.parsed_response['values']
end