Sha256: d92b8dd8422f17f0c49cbeb88011935a9568216e4f86aea3e72e1fc919fe73e4

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require 'rest-client'
require 'json'

module Ltp
  module Cloud
    module Sdk
      class Client
        attr_accessor :api_key

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

        def initialize(api_key)
          self.api_key = api_key
        end

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

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

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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ltp-cloud-sdk-0.1.1 lib/ltp/cloud/sdk/client.rb