lib/qismo/client.rb in qismo-0.13.0 vs lib/qismo/client.rb in qismo-0.14.0

- old
+ new

@@ -40,70 +40,74 @@ # Send http request with post method # # @param path [String] # @param body [Hash] - # @return [Qismo::SingleObject] + # @return [Qismo::ObjectifiedHash] def post(path, body = {}) - request(:post, path, json: body) + puts body.to_json + request(:post, path, json: body.compact) end # Send HTTP request with post method to upload file # # @param path [String] # @param body [Hash] # @return [Qismo::SingleObject] def post_upload(path, body = {}) - request(:post, form: body) + request(:post, path, form: body.compact) end # Send HTTP request with get method # # @param path [String] # @param params [Hash] - # @return [Qismo::SingleObject] + # @return [Qismo::ObjectifiedHash] def get(path, params = {}) - request(:get, path, params: params) + request(:get, path, params: params.compact) end # Send HTTP request with delete method # # @param path [String] # @param params [Hash] - # @return [Qismo::SingleObject] + # @return [Qismo::ObjectifiedHash] def delete(path, params = {}) - request(:delete, path, params: params) + request(:delete, path, params: params.compact) end # Send HTTP request # # @param method [Symbol, String] # @param path [String] # @param headers [Hash] # @param params [Hash] # @param json [Hash] - # @return [Qismo::SingleObject] - def request(method, path, headers: {}, params: {}, json: {}) - res = connection.request(method, @url + path, { - headers: headers, - params: params, - json: json - }) + # @return [Qismo::ObjectifiedHash] + def request(method, path, options = {}) + res = connection.request(method, @url + path, options.compact) + # require "byebug" + # byebug + if res.status.success? - return SingleObject.new(JSON.parse(res.to_s)) + 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 = SingleObject.new(JSON.parse(res.to_s)) + body = JSON.parse(res.to_s, object_class: Qismo::ObjectifiedHash) error = body.errors - error = error.message if error.is_a?(SingleObject) + error = error.message if error.is_a?(Hash) error_klass_map = { 400 => BadRequestError, 401 => UnauthorizedError, 402 => PaymentRequiredError, @@ -144,23 +148,30 @@ end http.headers({ "Qiscus-App-Id": @app_id, "Qiscus-Secret-Key": @secret_key, - "User-Agent": user_agent.to_json + "User-Agent": user_agent }) end # Http user agent config # # @return [Hash] 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 + + def default_broadcast_name + Time.current.strftime("%d/%m/%Y, %H:%I %z") end end end