Class: Qismo::Client

Inherits:
Object
  • Object
show all
Includes:
Api
Included in:
Api
Defined in:
lib/qismo/client.rb

Overview

Qismo ruby client

Constant Summary collapse

DEFAULT_APP_ID =

Returns:

  • (String)
ENV["QISCUS_APP_ID"]
DEFAULT_SECRET_KEY =

Returns:

  • (String)
ENV["QISCUS_SECRET_KEY"]
DEFAULT_URL =

Returns:

  • (String)
(ENV["QISCUS_OMNICHANNEL_URL"] || "https://multichannel.qiscus.com")

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Api

#add_room_tag, #allocate_agent, #allocate_and_assign_agent, #assign_agent, #create_auth_webhook, #create_new_session_webhook, #create_resolve_webhook, #create_room_info, #delete_room_tag, #disable_bot_in_room, #enable_bot_in_room, #get_agent, #get_auth_webhook, #get_new_session_webhook, #get_office_hours, #get_profile, #get_resolve_webhook, #get_room, #get_wa_broadcast_job, #get_wa_credit, #handover_room_from_bot, #handover_room_from_bot_to_division, #initiate_widget_chat, #list_agents, #list_agents_by_divisions, #list_agents_by_ids, #list_available_agents, #list_channels, #list_other_agents, #list_room_broadcasts, #list_room_info, #list_room_tags, #list_rooms, #list_wa_broadcast_jobs, #list_wa_broadcast_logs, #list_wa_broadcast_templates, #remove_agent, #resolve_room, #send_bot_message, #send_message_to_custom_channel, #send_wa_broadcast, #send_wa_outbound, #update_room_info, #upload_wa_broadcast_csv

Constructor Details

#initialize(app_id: DEFAULT_APP_ID, secret_key: DEFAULT_SECRET_KEY, url: DEFAULT_URL, logger: nil, instrumentation: nil, timeout: 5, proxy: nil, admin_email: nil) ⇒ Client

Initiate Qismo Ruby client

Parameters:

  • app_id (String) (defaults to: DEFAULT_APP_ID)

    Your account’s app id. Can be checked in Qiscus Omnichannel dashboard. We will use ENV value as default value.

  • secret_key (String) (defaults to: DEFAULT_SECRET_KEY)

    Your account’s secret key. Can be checked in Qiscus Omnichannel dashboard. We will use ENV value as default value.

  • url (String) (defaults to: DEFAULT_URL)

    Custom base url for Qiscus Omnichannel client. We will use ENV value as default value. If its not provided, we will use multichannel.qiscus.com as default value

  • logger (Logger) (defaults to: nil)

    Http client logging. Default is nil

  • instrumentation (ActiveSupport::Notifications::Instrumenter) (defaults to: nil)

    Http client instrumenter. Use ActiveSupport Instrumenter

  • timeout (Integer, Hash) (defaults to: 5)

    Http client timout. Default is 5 seconds

  • proxy (Array<String,Integer>) (defaults to: nil)

    Http client proxy

  • admin_email (String, Proc) (defaults to: nil)

    Your account’s admin email. Can be checked in Qiscus Omnichannel dashboard



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/qismo/client.rb', line 66

def initialize(
  app_id: DEFAULT_APP_ID,
  secret_key: DEFAULT_SECRET_KEY,
  url: DEFAULT_URL,
  logger: nil,
  instrumentation: nil,
  timeout: 5,
  proxy: nil,
  admin_email: nil
)
  @app_id = app_id
  @secret_key = secret_key

  # @type [Addressable::URI]
  @url = Addressable::URI.parse(url)

  @logger = logger
  @instrumentation = instrumentation
  @timeout = timeout
  @proxy = proxy

  @admin_email = (admin_email || -> { "#{@app_id}[email protected]" })
end

Instance Attribute Details

#admin_emailString, Proc

Returns:

  • (String, Proc)


42
43
44
# File 'lib/qismo/client.rb', line 42

def admin_email
  @admin_email
end

#app_idString

Returns:

  • (String)


21
22
23
# File 'lib/qismo/client.rb', line 21

def app_id
  @app_id
end

#instrumentationActiveSupport::Notifications::Instrumenter

Returns:

  • (ActiveSupport::Notifications::Instrumenter)


33
34
35
# File 'lib/qismo/client.rb', line 33

def instrumentation
  @instrumentation
end

#loggerLogger

Returns:

  • (Logger)


30
31
32
# File 'lib/qismo/client.rb', line 30

def logger
  @logger
end

#proxyArray<String,Integer>

Returns:

  • (Array<String,Integer>)


39
40
41
# File 'lib/qismo/client.rb', line 39

def proxy
  @proxy
end

#secret_keyString

Returns:

  • (String)


24
25
26
# File 'lib/qismo/client.rb', line 24

def secret_key
  @secret_key
end

#timeoutInteger, Hash

Returns:

  • (Integer, Hash)


36
37
38
# File 'lib/qismo/client.rb', line 36

def timeout
  @timeout
end

#urlAddressable::URI

Returns:

  • (Addressable::URI)


27
28
29
# File 'lib/qismo/client.rb', line 27

def url
  @url
end

Instance Method Details

#connectionHTTP::Chainable

Http connection config

Returns:

  • (HTTP::Chainable)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/qismo/client.rb', line 172

def connection
  http = HTTP
  http = http.use(logging: {logger: @logger}) if @logger.present?
  http = http.use(instrumentation: @instrumentation) if @instrumentation.present?
  http = http.via(*@proxy) if @proxy.present?

  if @timeout.present?
    http = if @timeout.is_a?(Hash)
      http.timeout(**@timeout)
    else
      http.timeout(@timeout)
    end
  end

  http.headers({
    "Qiscus-App-Id": @app_id,
    "Qiscus-Secret-Key": @secret_key,
    "User-Agent": user_agent
  })
end

#default_broadcast_nameString

Default broadcast name for using in send broadcast api

Returns:

  • (String)


212
213
214
# File 'lib/qismo/client.rb', line 212

def default_broadcast_name
  Time.current.strftime("%d/%m/%Y, %H:%I %z")
end

#delete(path, params = {}) ⇒ Qismo::ObjectifiedHash

Send HTTP request with delete method

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})

Returns:



122
123
124
# File 'lib/qismo/client.rb', line 122

def delete(path, params = {})
  request(:delete, path, params: params.compact)
end

#get(path, params = {}) ⇒ Qismo::ObjectifiedHash

Send HTTP request with get method

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})

Returns:



113
114
115
# File 'lib/qismo/client.rb', line 113

def get(path, params = {})
  request(:get, path, params: params.compact)
end

#post(path, body = {}) ⇒ Qismo::ObjectifiedHash

Send http request with post method

Parameters:

  • path (String)
  • body (Hash) (defaults to: {})

Returns:



95
96
97
# File 'lib/qismo/client.rb', line 95

def post(path, body = {})
  request(:post, path, json: body.compact)
end

#post_upload(path, body = {}) ⇒ Qismo::SingleObject

Send HTTP request with post method to upload file

Parameters:

  • path (String)
  • body (Hash) (defaults to: {})

Returns:

  • (Qismo::SingleObject)


104
105
106
# File 'lib/qismo/client.rb', line 104

def post_upload(path, body = {})
  request(:post, path, form: body.compact)
end

#request(method, path, options = {}) ⇒ Qismo::ObjectifiedHash

Send HTTP request

Parameters:

  • method (Symbol, String)
  • path (String)
  • headers (Hash)
  • params (Hash)
  • json (Hash)

Returns:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/qismo/client.rb', line 134

def request(method, path, options = {})
  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
      return res.to_s
    end
  end

  if res.status.server_error?
    raise InternalServerError.new("Qiscus Omnichannel server error", status_code: res.code, response_body: res.to_s)
  end

  if res.status.client_error?
    body = JSON.parse(res.to_s, object_class: Qismo::ObjectifiedHash)

    error = body.errors
    error = error.message if error.is_a?(Hash)

    error_klass_map = {
      400 => BadRequestError,
      401 => UnauthorizedError,
      402 => PaymentRequiredError,
      403 => ForbiddenError,
      404 => NotFoundError,
      429 => TooManyRequestError
    }

    error_klass = error_klass_map[res.code] || HTTPRequestError
    raise error_klass.new(error, status_code: res.code, response_body: res.to_s)
  end
end

#user_agentHash

Http user agent config

Returns:

  • (Hash)


196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/qismo/client.rb', line 196

def user_agent
  format = {
    lib_name: "Qismo Ruby",
    lib_version: Qismo::VERSION,
    lang: "ruby",
    lang_version: RUBY_VERSION,
    platform: RUBY_PLATFORM,
    engine: defined?(RUBY_ENGINE) ? RUBY_ENGINE : ""
  }

  "#{format[:lib_name]}/#{format[:lib_version]} (#{format[:platform]}; #{format[:lang]}; v#{format[:lang_version]}) app_id:#{@app_id}"
end