Class: Sterlet::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



3
4
5
# File 'lib/sterlet/client.rb', line 3

def initialize(api_key)
  @raw_client = RawClient.new(api_key)
end

Instance Method Details

#detect(text, hints: []) ⇒ Object



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

def detect(text, hints: [])
  response = @raw_client.detect(text, hints: hints)
  response['lang'].to_sym
end

#dirsObject



18
19
20
# File 'lib/sterlet/client.rb', line 18

def dirs
  @raw_client.get_langs['dirs'].map { |x| x.split('-').map(&:to_sym) }
end

#langs(ui: :en) ⇒ Object

rubocop:disable Naming/UncommunicativeMethodParamName



23
24
25
# File 'lib/sterlet/client.rb', line 23

def langs(ui: :en)
  @raw_client.get_langs(ui: ui)['langs'].map { |k, v| [k.to_sym, v] }.to_h.freeze
end

#translate(text, to:, from: nil, format: :plain) ⇒ Object



12
13
14
15
16
# File 'lib/sterlet/client.rb', line 12

def translate(text, to:, from: nil, format: :plain)
  lang = from ? [from, to].join('-') : to
  response = @raw_client.translate(text, lang: lang, format: format)
  response['text'].join(' ')
end