Class: Qismo::Client
- Inherits:
-
Object
- Object
- Qismo::Client
- Includes:
- Api
- Included in:
- Api
- Defined in:
- lib/qismo/client.rb
Overview
Qismo ruby client
Constant Summary collapse
- DEFAULT_APP_ID =
ENV["QISCUS_APP_ID"]
- DEFAULT_SECRET_KEY =
ENV["QISCUS_SECRET_KEY"]
- DEFAULT_URL =
(ENV["QISCUS_OMNICHANNEL_URL"] || "https://multichannel.qiscus.com")
Instance Attribute Summary collapse
- #admin_email ⇒ String, Proc
- #app_id ⇒ String
- #instrumentation ⇒ ActiveSupport::Notifications::Instrumenter
- #logger ⇒ Logger
- #proxy ⇒ Array<String,Integer>
- #secret_key ⇒ String
- #timeout ⇒ Integer, Hash
- #url ⇒ Addressable::URI
Instance Method Summary collapse
-
#connection ⇒ HTTP::Chainable
Http connection config.
-
#default_broadcast_name ⇒ String
Default broadcast name for using in send broadcast api.
-
#delete(path, params = {}) ⇒ Qismo::ObjectifiedHash
Send HTTP request with delete method.
-
#get(path, params = {}) ⇒ Qismo::ObjectifiedHash
Send HTTP request with get method.
-
#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
constructor
Initiate Qismo Ruby client.
-
#post(path, body = {}) ⇒ Qismo::ObjectifiedHash
Send http request with post method.
-
#post_upload(path, body = {}) ⇒ Qismo::SingleObject
Send HTTP request with post method to upload file.
-
#request(method, path, options = {}) ⇒ Qismo::ObjectifiedHash
Send HTTP request.
-
#user_agent ⇒ Hash
Http user agent config.
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
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_email ⇒ String, Proc
42 43 44 |
# File 'lib/qismo/client.rb', line 42 def admin_email @admin_email end |
#app_id ⇒ String
21 22 23 |
# File 'lib/qismo/client.rb', line 21 def app_id @app_id end |
#instrumentation ⇒ ActiveSupport::Notifications::Instrumenter
33 34 35 |
# File 'lib/qismo/client.rb', line 33 def instrumentation @instrumentation end |
#logger ⇒ Logger
30 31 32 |
# File 'lib/qismo/client.rb', line 30 def logger @logger end |
#proxy ⇒ Array<String,Integer>
39 40 41 |
# File 'lib/qismo/client.rb', line 39 def proxy @proxy end |
#secret_key ⇒ String
24 25 26 |
# File 'lib/qismo/client.rb', line 24 def secret_key @secret_key end |
#timeout ⇒ Integer, Hash
36 37 38 |
# File 'lib/qismo/client.rb', line 36 def timeout @timeout end |
#url ⇒ Addressable::URI
27 28 29 |
# File 'lib/qismo/client.rb', line 27 def url @url end |
Instance Method Details
#connection ⇒ HTTP::Chainable
Http connection config
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_name ⇒ String
Default broadcast name for using in send broadcast api
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
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
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
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
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
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, = {}) res = connection.request(method, @url.join(path), .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. 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_agent ⇒ Hash
Http user agent config
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 |