Class: WtfLang::API

Inherits:
Object
  • Object
show all
Defined in:
lib/wtf_lang/api.rb

Constant Summary collapse

BASE_URL =
"http://ws.detectlanguage.com/0.2/detect"

Class Method Summary collapse

Class Method Details

.detect(text) ⇒ Object



26
27
28
29
30
31
# File 'lib/wtf_lang/api.rb', line 26

def detect(text)
  response = send(text.to_s)
  if response_data = parse(response)["data"]["detections"].first
    Language.new(response_data["language"], response_data["confidence"])
  end
end

.get_url(text) ⇒ Object



14
15
16
# File 'lib/wtf_lang/api.rb', line 14

def get_url(text)
  "#{BASE_URL}?key=#{key}&q=#{CGI.escape(text)}"
end

.keyObject



33
34
35
# File 'lib/wtf_lang/api.rb', line 33

def key
  @@key
end

.key=(value) ⇒ Object



37
38
39
# File 'lib/wtf_lang/api.rb', line 37

def key=(value)
  @@key = value
end

.parse(response) ⇒ Object



22
23
24
# File 'lib/wtf_lang/api.rb', line 22

def parse(response)
  JSON.parse(response)
end

.send(text) ⇒ Object



18
19
20
# File 'lib/wtf_lang/api.rb', line 18

def send(text)
  Net::HTTP.get(URI.parse(get_url(text)))
end