class Sterlet::RawClient

Public Class Methods

new(api_key) click to toggle source
# File lib/sterlet/raw_client.rb, line 3
def initialize(api_key)
  @api_key = api_key
end

Public Instance Methods

detect(text, hints: []) click to toggle source

rubocop:enable Naming/UncommunicativeMethodParamName

# File lib/sterlet/raw_client.rb, line 13
def detect(text, hints: [])
  perform_post_request('/detect', text: text, hints: hints)
end
get_langs(ui: :en) click to toggle source

rubocop:disable Naming/UncommunicativeMethodParamName

# File lib/sterlet/raw_client.rb, line 8
def get_langs(ui: :en)
  perform_post_request('/getLangs', ui: ui)
end
translate(text, lang:, format: :plain, options: 0) click to toggle source
# File lib/sterlet/raw_client.rb, line 17
def translate(text, lang:, format: :plain, options: 0)
  params = { text: text, lang: lang, format: format, options: options }
  perform_post_request('/translate', params)
end

Private Instance Methods

default_params() click to toggle source
# File lib/sterlet/raw_client.rb, line 24
def default_params
  { key: @api_key }
end
perform_post_request(endpoint, params) click to toggle source
# File lib/sterlet/raw_client.rb, line 28
def perform_post_request(endpoint, params)
  response =
    HTTP.post(API_URI_BASE + endpoint, form: default_params.merge(params))
  return JSON.parse(response.to_s) if response.code == 200

  raise Error, JSON.parse(response.to_s)['message']
end