Class: Linggle::Api

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

Overview

title

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApi

Returns a new instance of Api.



16
17
18
19
20
21
22
# File 'lib/linggle/api.rb', line 16

def initialize
  @conn = Faraday.new(url: API_BASE) do |f|
    f.request  :url_encoded
    f.response :logger, ::Logger.new('/dev/null')
    f.adapter  Faraday.default_adapter
  end
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



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

def conn
  @conn
end

Instance Method Details

#exampleObject



41
42
43
44
# File 'lib/linggle/api.rb', line 41

def example
 url = 'https://nlp-ultron.cs.nthu.edu.tw/coca/example'
# 'https://nlp-ultron.cs.nthu.edu.tw/coca/example/' -H 'Origin: http://linggle.com' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' -H 'Content-Type: application/json' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Referer: http://linggle.com/' -H 'Connection: keep-alive' -H 'X-CSRFToken: aCT5jTOwy93GZXnHOpPrXXmRSNIngtTN3ChZ22It0933dALsqMNnmAl9vv0J3ufG' -H 'DNT: 1' --data-binary '{"ngram":"go to school"}' --compressed
end

#query(query_str) ⇒ Object

查询返回结果示例

“go ?to schoo”, “time”: 1534167385507, “ngrams”: [[“go to schoo”, 234]], “total”: 234



29
30
31
32
33
34
35
36
37
38
# File 'lib/linggle/api.rb', line 29

def query(query_str)
   # puts "conent #{content}"
   res = conn.post '/query/', {query: query_str, time: Time.now.to_i}.to_json do |req|
     req.headers['Content-Type'] = 'application/json'
     req.headers['X-Requested-With'] = 'XMLHttpRequest'
     req['Cookie'] = "csrftoken=#{csrf_token}"
     req.headers['X-CSRFToken'] = csrf_token
   end
   JSON.parse(res.body)
end