Class: RecastAI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/recastai/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token = nil, language = nil) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/recastai/client.rb', line 7

def initialize(token = nil, language = nil)
  @token = token
  @language = language
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



5
6
7
# File 'lib/recastai/client.rb', line 5

def token
  @token
end

Instance Method Details

#file_request(file, options = {}) ⇒ Object

Perform a voice file request to Recast.AI

  • Args :

    • file - String or File, the voice file to process

    • options - Hash, request’s options

  • Returns :

    • An instance of Response

  • Throws :

    • RecastError

Raises:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/recastai/client.rb', line 50

def file_request(file, options = {})
  token = options[:token] || @token
  raise(RecastError.new('Token is missing')) if token.nil?

  language = options[:language] || @language

  body = { 'voice' => File.new(file) }
  body['language'] = language unless language.nil?
  response = HTTMultiParty.post(
    Utils::API_ENDPOINT,
    body: body,
    headers: { 'Authorization' => "Token #{token}" }
  )
  raise(RecastError.new(response.message)) if response.code != 200

  Response.new(response.body)
end

#text_request(text, options = {}) ⇒ Object

Perform a text request to Recast.AI

  • Args :

    • text - String, the text to process

    • options - Hash, request’s options

  • Returns :

    • An instance of Response

  • Throws :

    • RecastError

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/recastai/client.rb', line 22

def text_request(text, options = {})
  token = options[:token] || @token
  raise(RecastError.new('Token is missing')) if token.nil?

  language = options[:language] || @language

  body = { 'text' => text }
  body['language'] = language unless language.nil?
  response = HTTParty.post(
    Utils::API_ENDPOINT,
    body: body,
    headers: { 'Authorization' => "Token #{token}" }
  )
  raise(RecastError.new(response.message)) if response.code != 200

  Response.new(response.body)
end