From 2b14c541a4471cd8186c0cccc01c89d5eea3663b Mon Sep 17 00:00:00 2001 From: Doug Hughes Date: Sat, 23 Jan 2021 11:01:39 -0500 Subject: [PATCH 1/3] Add attr_reads for response data --- lib/MailchimpMarketing/api_error.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/MailchimpMarketing/api_error.rb b/lib/MailchimpMarketing/api_error.rb index 70ef370..c52357b 100644 --- a/lib/MailchimpMarketing/api_error.rb +++ b/lib/MailchimpMarketing/api_error.rb @@ -14,6 +14,8 @@ module MailchimpMarketing class ApiError < StandardError attr_reader :status, :type, :title, :detail, :instance, :errors + attr_reader :response_headers, :response_body + # Usage examples: # ApiError.new # ApiError.new("message") From fc162f5ed542a369c5d03ccc565df0e592d9d0f2 Mon Sep 17 00:00:00 2001 From: Doug Hughes Date: Fri, 25 Jun 2021 18:29:39 -0400 Subject: [PATCH 2/3] include query params on post/patch/etc --- .gitignore | 2 ++ lib/MailchimpMarketing/api_client.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c021594..eead26e 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,5 @@ build/ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: .rvmrc + +.idea diff --git a/lib/MailchimpMarketing/api_client.rb b/lib/MailchimpMarketing/api_client.rb index 9cb8caa..051b8cc 100644 --- a/lib/MailchimpMarketing/api_client.rb +++ b/lib/MailchimpMarketing/api_client.rb @@ -65,7 +65,7 @@ def call_api(http_method, path, opts = {}) res = nil case http_method.to_sym.downcase when :post, :put, :patch, :delete - res = conn.request(:method => http_method, :body => opts[:body]) + res = conn.request(:method => http_method, :body => opts[:body], :query => opts[:query_params]) when :get res = conn.get(:query => opts[:query_params]) end From 05dfd3a4a4f6209881b654e476ebf73c8f61fb66 Mon Sep 17 00:00:00 2001 From: Doug Hughes Date: Sat, 26 Jun 2021 07:06:09 -0400 Subject: [PATCH 3/3] Parse response_body and flesh out attributes --- lib/MailchimpMarketing/api_error.rb | 46 ++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/lib/MailchimpMarketing/api_error.rb b/lib/MailchimpMarketing/api_error.rb index c52357b..ebf6343 100644 --- a/lib/MailchimpMarketing/api_error.rb +++ b/lib/MailchimpMarketing/api_error.rb @@ -10,6 +10,8 @@ =end +require 'json' + module MailchimpMarketing class ApiError < StandardError attr_reader :status, :type, :title, :detail, :instance, :errors @@ -21,20 +23,42 @@ class ApiError < StandardError # ApiError.new("message") # ApiError.new(:status => 500, :response_headers => {}, :response_body => "") # ApiError.new(:status => 404, :message => "Not Found") - def initialize(arg = nil) - if arg.is_a? Hash - if arg.key?(:message) || arg.key?('message') - super(arg[:message] || arg['message']) - else - super arg - end + def initialize(arguments = nil) + @arguments = arguments + return super(@arguments) unless @arguments.is_a?(Hash) + + @arguments.transform_keys!(&:to_sym) + + expand_response_body_into_arguments + + super(@arguments[:title] || @arguments[:message]) + + @arguments.each do |key, value| + instance_variable_set("@#{key}", value) + end + end + + private + + def expand_response_body_into_arguments + @arguments.merge!(parsed_response_body) unless parsed_response_body.nil? + end - arg.each do |k, v| - instance_variable_set "@#{k}", v + def parsed_response_body + @parsed_response_body ||= begin + parsed_response_body = JSON.parse(@arguments[:response_body]).transform_keys(&:to_sym) + + if parsed_response_body[:errors].is_a?(Array) + parsed_response_body[:errors].map do |error| + error.transform_keys!(&:to_sym) + end end - else - super arg + + parsed_response_body + rescue + nil end end + end end