lib/qismo/client.rb in qismo-0.8.7 vs lib/qismo/client.rb in qismo-0.8.8
- old
+ new
@@ -2,14 +2,20 @@
module Qismo
class Client
include Qismo::Api
+ # @return [String, NilClass]
DEFAULT_APP_ID = ENV["QISCUS_APP_ID"]
+
+ # @return [String, NilClass]
DEFAULT_SECRET_KEY = ENV["QISCUS_SECRET_KEY"]
+
+ # @return [String]
DEFAULT_URL = ENV["QISCUS_OMNICHANNEL_URL"] || "https://qismo.qiscus.com"
+ # @return [Hash]
DEFAULT_OPTIONS = {
app_id: DEFAULT_APP_ID,
secret_key: DEFAULT_SECRET_KEY,
url: DEFAULT_URL,
logger: nil,
@@ -18,32 +24,56 @@
proxy: nil,
}
attr_accessor(*DEFAULT_OPTIONS.keys)
+ # Initialize client
+ #
+ # @param **opt [Hash]
def initialize(**opt)
DEFAULT_OPTIONS.merge(opt).each do |key, value|
instance_variable_set("@#{key}", value)
end
end
+ # Send http request with post method
+ #
+ # @param path [String]
+ # @param body [Hash]
+ # @return [Qismo::DataObject]
def post(path, body = {})
request(:post, path, json: body)
end
+ # Http request with post method to upload file
+ #
+ # @param path [String]
+ # @param body [Hash]
+ # @return [Qismo::DataObject]
def post_upload(path, body = {})
request(:post, form: body)
end
+ # Send http request with get method
+ #
+ # @param path [String]
+ # @param params [Hash]
+ # @return [Qismo::DataObject]
def get(path, **params)
request(:get, path, params: params)
end
def delete(path)
request(:delete, path)
end
+ # Send http request
+ #
+ # @param method [Symbol, String]
+ # @param path [String]
+ # @param body [Hash]
+ # @return [Qismo::DataObject]
def request(method, path, **opt)
res = connection.request(method, @url + path, opt.compact)
if res.status.success?
return DataObject.new(JSON.parse(res.to_s))
end
@@ -70,10 +100,13 @@
error_klass = error_klass_map[res.code] || HTTPRequestError
raise error_klass.new(error, status_code: res.code, response_body: res.to_s)
end
end
+ # Http connection config
+ #
+ # @return [HTTP::Chainable]
def connection
http = HTTP
unless @logger.nil?
http = http.use(logging: @logger)
@@ -100,9 +133,12 @@
"Qiscus-Secret-Key": @secret_key,
"User-Agent": user_agent.to_json,
})
end
+ # Http user agent config
+ #
+ # @return [Hash]
def user_agent
{
lib_version: Qismo::VERSION,
lang: "ruby",
lang_version: RUBY_VERSION,