Class: Deepgram::Listen::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/deepgram/listen.rb

Overview

The Response class encapsulates the response from the Deepgram Listen API, providing methods to access various parts of the response data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, body:, headers:) ⇒ Response

Initializes a new Response object with the given status, body, and headers.

Parameters:

  • status (Integer)

    The HTTP status code of the response.

  • body (String)

    The body of the response, expected to be in JSON format.

  • headers (Hash)

    The HTTP headers of the response.



85
86
87
88
89
# File 'lib/deepgram/listen.rb', line 85

def initialize(status:, body:, headers:)
  @status = status
  @body = body
  @headers = headers
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



78
79
80
# File 'lib/deepgram/listen.rb', line 78

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



78
79
80
# File 'lib/deepgram/listen.rb', line 78

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



78
79
80
# File 'lib/deepgram/listen.rb', line 78

def status
  @status
end

Instance Method Details

#confidenceFloat?

Extracts and returns the confidence score from the first channel and alternative in the response data.

Returns:

  • (Float, nil)

    The confidence score, if present.



115
116
117
# File 'lib/deepgram/listen.rb', line 115

def confidence
  raw.dig('results', 'channels', 0, 'alternatives', 0, 'confidence')
end

#metadataHash?

Extracts and returns the metadata from the response data.

Returns:

  • (Hash, nil)

    The metadata from the response, if present.



94
95
96
# File 'lib/deepgram/listen.rb', line 94

def 
  raw['metadata']
end

#rawHash

Parses the response body as JSON and returns the raw data.

Returns:

  • (Hash)

    The parsed JSON from the response body.



122
123
124
# File 'lib/deepgram/listen.rb', line 122

def raw
  JSON.parse(@body)
end

#transcriptString?

Extracts and returns the transcript text from the first channel and alternative in the response data.

Returns:

  • (String, nil)

    The transcript text, if present.



101
102
103
# File 'lib/deepgram/listen.rb', line 101

def transcript
  raw.dig('results', 'channels', 0, 'alternatives', 0, 'transcript')
end

#wordsArray?

Extracts and returns the words array from the first channel and alternative in the response data.

Returns:

  • (Array, nil)

    The words array, if present.



108
109
110
# File 'lib/deepgram/listen.rb', line 108

def words
  raw.dig('results', 'channels', 0, 'alternatives', 0, 'words')
end