Class: Ltp::Cloud::Sdk::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ltp/cloud/sdk/client.rb

Constant Summary collapse

END_POINT =
'https://api.ltp-cloud.com/'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



12
13
14
# File 'lib/ltp/cloud/sdk/client.rb', line 12

def initialize(api_key)
  self.api_key = api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/ltp/cloud/sdk/client.rb', line 8

def api_key
  @api_key
end

Class Method Details

.with(api_key) ⇒ Object



16
17
18
# File 'lib/ltp/cloud/sdk/client.rb', line 16

def self.with(api_key)
  new(api_key)
end

Instance Method Details

#analysis(text, timeout = 5) ⇒ Object



40
41
42
43
44
# File 'lib/ltp/cloud/sdk/client.rb', line 40

def analysis(text, timeout = 5)
  payload = request_payload({ text: CGI::escape(text), pattern: 'ws' })
  res     = request 'analysis', payload, timeout
  res.split(' ')
end

#request(service, payload, timeout) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/ltp/cloud/sdk/client.rb', line 27

def request(service, payload, timeout)
  res = RestClient::Request.execute(method:  :get,
                                    url:     uri(service),
                                    timeout: timeout,
                                    headers: { params: payload }
  )
  res.body
end

#request_payload(data) ⇒ Object



20
21
22
23
24
25
# File 'lib/ltp/cloud/sdk/client.rb', line 20

def request_payload(data)
  {
    api_key: api_key,
    format:  'plain'
  }.merge data
end

#uri(service) ⇒ Object



36
37
38
# File 'lib/ltp/cloud/sdk/client.rb', line 36

def uri(service)
  "#{END_POINT}#{service}"
end