Class: Linguin::Client

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

Overview

Linguin::Client

Is used internally when working with Linguin directly. Can be instanciated to work with multiple clients.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil) ⇒ Client

Returns a new instance of Client.



21
22
23
# File 'lib/linguin/client.rb', line 21

def initialize(key = nil)
  configure_api_key(key)
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



19
20
21
# File 'lib/linguin/client.rb', line 19

def headers
  @headers
end

Instance Method Details

#api_key=(key) ⇒ Object



25
26
27
# File 'lib/linguin/client.rb', line 25

def api_key=(key)
  configure_api_key(key)
end

#detect_language(text) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/linguin/client.rb', line 29

def detect_language(text)
  ensure_api_key!

  return bulk_detect_language(text) if text.is_a?(Array)

  text = sanitize(text)

  if text.empty?
    return LanguageDetection.error(400,
                                   "The language of an empty text is more of a philosophical question.")
  end

  httparty_response = self.class.post("/detect/language", headers: headers, body: { q: text })
  LanguageDetection.from_httparty(response: httparty_response)
end

#detect_language!(text) ⇒ Object



45
46
47
# File 'lib/linguin/client.rb', line 45

def detect_language!(text)
  detect_language(text).raise_on_error!
end

#detect_profanity(text) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/linguin/client.rb', line 49

def detect_profanity(text)
  ensure_api_key!

  return bulk_detect_profanity(text) if text.is_a?(Array)

  text = text&.strip

  return ProfanityDetection.error(400, "Can an empty text have profanity in it? I doubt it.") if text.to_s.empty?

  httparty_response = self.class.post("/detect/profanity", headers: headers, body: { q: text })
  ProfanityDetection.from_httparty(response: httparty_response)
end

#detect_profanity!(text) ⇒ Object



62
63
64
# File 'lib/linguin/client.rb', line 62

def detect_profanity!(text)
  detect_profanity(text).raise_on_error!
end

#languagesObject



72
73
74
75
# File 'lib/linguin/client.rb', line 72

def languages
  httparty_response = self.class.get("/languages")
  Languages.from_httparty(response: httparty_response)
end

#statusObject



66
67
68
69
70
# File 'lib/linguin/client.rb', line 66

def status
  ensure_api_key!
  httparty_response = self.class.get("/status", headers: headers)
  Status.from_httparty(response: httparty_response)
end