lib/qismo/client.rb in qismo-0.18.0 vs lib/qismo/client.rb in qismo-0.18.1

- old
+ new

@@ -1,45 +1,92 @@ # frozen_string_literal: true +require "logger" + module Qismo # Qismo ruby client # class Client include Qismo::Api - # @return [String, NilClass] + # @return [String] DEFAULT_APP_ID = ENV["QISCUS_APP_ID"] - # @return [String, NilClass] + # @return [String] DEFAULT_SECRET_KEY = ENV["QISCUS_SECRET_KEY"] # @return [String] - DEFAULT_URL = ENV["QISCUS_OMNICHANNEL_URL"] || "https://qismo.qiscus.com" + DEFAULT_URL = (ENV["QISCUS_OMNICHANNEL_URL"] || "https://multichannel.qiscus.com") - # @return [Hash] - DEFAULT_OPTIONS = { + # @return [String] + attr_accessor :app_id + + # @return [String] + attr_accessor :secret_key + + # @return [Addressable::URI] + attr_accessor :url + + # @return [Logger] + attr_accessor :logger + + # @return [ActiveSupport::Notifications::Instrumenter] + attr_accessor :instrumentation + + # @return [Integer,Hash] + attr_accessor :timeout + + # @return [Array<String,Integer>] + attr_accessor :proxy + + # @return [String,Proc] + attr_accessor :admin_email + + # Initiate Qismo Ruby client + # + # @param app_id [String] + # Your account's app id. Can be checked in Qiscus Omnichannel dashboard. + # We will use ENV["QISCUS_APP_ID"] value as default value. + # @param secret_key [String] + # Your account's secret key. Can be checked in Qiscus Omnichannel dashboard. + # We will use ENV["QISCUS_SECRET_KEY"] value as default value. + # @param url [String] + # Custom base url for Qiscus Omnichannel client. + # We will use ENV["QISCUS_OMNICHANNEL_URL"] value as default value. + # If its not provided, we will use https://multichannel.qiscus.com as default value + # @param logger [Logger] + # Http client logging. Default is nil + # @param instrumentation [ActiveSupport::Notifications::Instrumenter] + # Http client instrumenter. Use ActiveSupport Instrumenter + # @param timeout [Integer, Hash] + # Http client timout. Default is 5 seconds + # @param proxy [Array<String,Integer>] + # Http client proxy + # @param admin_email [String,Proc] + # Your account's admin email. Can be checked in Qiscus Omnichannel dashboard + def initialize( app_id: DEFAULT_APP_ID, secret_key: DEFAULT_SECRET_KEY, url: DEFAULT_URL, - logger: nil, + logger: Logger.new($stdout), instrumentation: nil, - timeout: nil, - proxy: nil - } + timeout: 5, + proxy: nil, + admin_email: nil + ) + @app_id = app_id + @secret_key = secret_key - attr_accessor(*DEFAULT_OPTIONS.keys) - attr_reader :admin_email + # @type [Addressable::URI] + @url = Addressable::URI.parse(url) - # Initialize client - # - # @param options [Hash] - def initialize(options = {}) - DEFAULT_OPTIONS.merge(options).each do |key, value| - instance_variable_set("@#{key}", value) - end + @logger = logger + @instrumentation = instrumentation + @timeout = timeout + @proxy = proxy - @admin_email = "#{@app_id}_admin@qismo.com" + @admin_email = (admin_email || -> { "#{@app_id}_admin@qismo.com" }) end # Send http request with post method # # @param path [String] @@ -83,11 +130,11 @@ # @param headers [Hash] # @param params [Hash] # @param json [Hash] # @return [Qismo::ObjectifiedHash] def request(method, path, options = {}) - res = connection.request(method, @url + path, options.compact) + res = connection.request(method, @url.join(path), options.compact) if res.status.success? begin return JSON.parse(res.to_s, object_class: Qismo::ObjectifiedHash) rescue JSON::ParserError @@ -122,28 +169,19 @@ # Http connection config # # @return [HTTP::Chainable] def connection http = HTTP + http = http.use(logging: @logger) if @logger.present? + http = http.use(instrumentation: @instrumentation) if @instrumentation.present? + http = http.via(*@proxy) - unless @logger.nil? - http = http.use(logging: @logger) - end - - unless @instrumentation.nil? - http = http.use(instrumentation: @instrumentation) - end - - unless @timeout.nil? + if @timeout.present? http = if @timeout.is_a?(Hash) http.timeout(**@timeout) else http.timeout(@timeout) end - end - - unless @proxy.nil? - http = http.via(*@proxy) end http.headers({ "Qiscus-App-Id": @app_id, "Qiscus-Secret-Key": @secret_key,