Sha256: 6fa389848cbad9486c8e306267806b86dd640ddabc636d68ba4052c8f5a24d0f
Contents?: true
Size: 1.84 KB
Versions: 1
Compression:
Stored size: 1.84 KB
Contents
module ExceptionNotificationLarkNotifier class Http def initialize(options) @uri = options.fetch(:uri) @options = options.fetch(:options) end def post(options) request(:post, options) end private def request(verb, options) begin response = http(@options).request(verb, @uri, options) rescue HTTP::Error => e raise ExceptionNotificationLarkNotifier::Exceptions::HttpError, e.message end parse_response(response) do |parse_as, result| case parse_as when :json if result.key?("code") break result if result["code"] == 0 raise ExceptionNotificationLarkNotifier::Exceptions::APIError, result["msg"] else break result if result["StatusCode"] == 0 raise ExceptionNotificationLarkNotifier::Exceptions::APIError, result["StatusMessage"] end else result end end end def http(options) HTTP::Client.new(HTTP::Options.new(options)) end def parse_response(response) content_type = response.headers[:content_type] parse_as = { %r{^application/json} => :json, %r{^text/plain} => :plain }.each_with_object([]) { |match, memo| memo << match[1] if content_type&.match?(match[0]) }.first || :plain if parse_as == :plain result = begin parse_json_response(response) rescue nil end if result return yield(:json, result) else return yield(:plain, response.body) end end result = case parse_as when :json parse_json_response(response) else response.body end yield(parse_as, result) end def parse_json_response(response) JSON.parse(response.body.to_s) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
exception_notification-lark-notifier-1.0.2 | lib/exception_notification-lark-notifier/http.rb |