class Linguin::Client
Linguin::Client
¶ ↑
Is used internally when working with Linguin
directly. Can be instanciated to work with multiple clients.
Attributes
headers[RW]
Public Class Methods
new(key = nil)
click to toggle source
# File lib/linguin/client.rb, line 21 def initialize(key = nil) configure_api_key(key) end
Public Instance Methods
api_key=(key)
click to toggle source
# File lib/linguin/client.rb, line 25 def api_key=(key) configure_api_key(key) end
detect_language(text)
click to toggle source
# 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)
click to toggle source
# File lib/linguin/client.rb, line 45 def detect_language!(text) detect_language(text).raise_on_error! end
detect_profanity(text)
click to toggle source
# 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)
click to toggle source
# File lib/linguin/client.rb, line 62 def detect_profanity!(text) detect_profanity(text).raise_on_error! end
languages()
click to toggle source
# File lib/linguin/client.rb, line 72 def languages httparty_response = self.class.get("/languages") Languages.from_httparty(response: httparty_response) end
status()
click to toggle source
# 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
Private Instance Methods
bulk_detect_language(texts)
click to toggle source
# File lib/linguin/client.rb, line 79 def bulk_detect_language(texts) texts = texts.map { |text| sanitize(text) } return BulkLanguageDetection.error(400, "At least one of the texts provided was empty.") if texts.any?(&:empty?) httparty_response = self.class.post("/bulk_detect/language", headers: headers, body: { q: texts }) BulkLanguageDetection.from_httparty(response: httparty_response) end
bulk_detect_profanity(texts)
click to toggle source
# File lib/linguin/client.rb, line 88 def bulk_detect_profanity(texts) texts.map! { |text| text.to_s.strip } return BulkProfanityDetection.error(400, "At least one of the texts provided was empty.") if texts.any?(&:empty?) httparty_response = self.class.post("/bulk_detect/profanity", headers: headers, body: { q: texts }) BulkProfanityDetection.from_httparty(response: httparty_response) end
configure_api_key(key)
click to toggle source
# File lib/linguin/client.rb, line 97 def configure_api_key(key) @api_key = key self.headers = { "Authorization" => "Bearer #{key}" } end
ensure_api_key!()
click to toggle source
# File lib/linguin/client.rb, line 102 def ensure_api_key! raise Linguin::AuthenticationError, "Seems like you forgot to set Linguin.api_key" unless @api_key end
sanitize(text)
click to toggle source
# File lib/linguin/client.rb, line 106 def sanitize(text) text ||= "" text.to_s.strip end