diff --git a/lib/responsys/api/campaign.rb b/lib/responsys/api/campaign.rb index 3a57f44..7c8338a 100644 --- a/lib/responsys/api/campaign.rb +++ b/lib/responsys/api/campaign.rb @@ -1,5 +1,3 @@ -require "responsys/api/object/all" - module Responsys module Api module Campaign @@ -24,14 +22,6 @@ def trigger_message(campaign, recipients) } api_method(:trigger_campaign_message, message) end - - def check_failures(outcome, recipients) - if outcome.respond_to?(:each_index) - outcome.each_index { |i| puts "failed:\n" + recipients[i][:recipient].to_s unless outcome[i][:success] } - else - puts "failed:\n" + recipients[:recipient].to_s unless outcome[:success] - end - end end end end diff --git a/lib/responsys/api/client.rb b/lib/responsys/api/client.rb index 7bee52f..8b25393 100644 --- a/lib/responsys/api/client.rb +++ b/lib/responsys/api/client.rb @@ -1,8 +1,4 @@ -require "responsys/configuration" require "savon" -require "responsys/helper" -require "responsys/api/all" -require "responsys/api/object/all" require "singleton" module Responsys @@ -29,7 +25,7 @@ def initialize end end - def api_method(action, message = nil, response_type = :hash) + def api_method(action, message = nil) raise Responsys::Helper.get_message("api.client.api_method.wrong_action_#{action.to_s}") if action.to_sym == :login || action.to_sym == :logout begin @@ -37,15 +33,10 @@ def api_method(action, message = nil, response_type = :hash) response = run_with_credentials(action, message, jsession_id, header) - case response_type - when :result - Responsys::Helper.format_response_result(response, action) - when :hash - Responsys::Helper.format_response_hash(response, action) - end + Responsys::Helper.format(action: action, response: response) rescue Exception => e - Responsys::Helper.format_response_with_errors(e) + Responsys::Helper.format(error: e) ensure logout end diff --git a/lib/responsys/api/list.rb b/lib/responsys/api/list.rb index 2b282f1..a1d625d 100644 --- a/lib/responsys/api/list.rb +++ b/lib/responsys/api/list.rb @@ -1,5 +1,3 @@ -require "responsys/api/object/all" - module Responsys module Api module List diff --git a/lib/responsys/helper.rb b/lib/responsys/helper.rb deleted file mode 100644 index 7406ff9..0000000 --- a/lib/responsys/helper.rb +++ /dev/null @@ -1,91 +0,0 @@ -module Responsys - module Helper - def self.format_response_result(response, action) - response.body[("#{action}_response").to_sym][:result] - end - - def self.format_response_hash(response, action) - formatted_response = { status: "ok" } - - return formatted_response unless response.body.has_key? "#{action}_response".to_sym - - if response.body["#{action}_response".to_sym].has_key?(:result) and response.body["#{action}_response".to_sym][:result].is_a? Hash - formatted_response[:data] = handle_response_types(response.body["#{action}_response".to_sym]) - elsif response.body["#{action}_response".to_sym].has_key?(:result) - formatted_response[:result] = response.body["#{action}_response".to_sym][:result] - elsif response.body["#{action}_response".to_sym].is_a? Hash and !response.body["#{action}_response".to_sym].empty? - formatted_response[:result] = response.body["#{action}_response".to_sym].values[0] - end - - formatted_response - end - - def self.format_record_data(record_data) - field_names = record_data[:field_names] - records = record_data[:records] - - data = [] - if records.is_a? Array - records.each do |record| - data << format_field_values(record, field_names) - end - else - data << format_field_values(records, field_names) - end - - data - end - - def self.format_field_values(record, field_names) - values = {} - - if record.is_a? Hash and record[:field_values].is_a? Array - record[:field_values].each_with_index do |value, index| - values[field_names[index].to_sym] = value - end - elsif record.is_a? Hash - values[field_names.to_sym] = record[:field_values] - end - - values - end - - def self.handle_response_types(response_body) - data = [] - - if response_body[:result].has_key? :record_data - data = format_record_data(response_body[:result][:record_data]) - else - data << response_body - end - - data - end - - def self.format_response_with_errors(error) - error_response = { status: "failure" } - - if error.to_hash[:fault].has_key?(:detail) and !error.to_hash[:fault][:detail].nil? - key = error.to_hash[:fault][:detail].keys[0] - error_response[:error] = { http_status_code: error.http.code, code: error.to_hash[:fault][:detail][key][:exception_code], message: error.to_hash[:fault][:detail][key][:exception_message] } - error_response[:error][:trace] = error.to_hash[:fault][:detail][:source] if error.to_hash[:fault][:detail].has_key?(:source) - else - error_response[:error] = { http_status_code: error.http.code, code: error.to_hash[:fault][:faultcode], message: error.to_hash[:fault][:faultstring] } - end - - error_response - end - - def self.format_response_with_message(i18n_key) - { status: "failure", error: { http_status_code: "", code: i18n_key.split('.')[-1], message: get_message(i18n_key) } } - end - - def self.get_message(key) - begin - I18n.t(key, scope: :responsys_api, locale: I18n.locale, raise: true) - rescue I18n::MissingTranslationData - I18n.t(key, scope: :responsys_api, locale: :en) - end - end - end -end diff --git a/lib/responsys/helpers/all.rb b/lib/responsys/helpers/all.rb new file mode 100644 index 0000000..3cec750 --- /dev/null +++ b/lib/responsys/helpers/all.rb @@ -0,0 +1,3 @@ +require "responsys/helpers/formatting" +require "responsys/helpers/response_object" +require "responsys/helpers/helper" \ No newline at end of file diff --git a/lib/responsys/helpers/formatting.rb b/lib/responsys/helpers/formatting.rb new file mode 100644 index 0000000..daa8430 --- /dev/null +++ b/lib/responsys/helpers/formatting.rb @@ -0,0 +1,87 @@ +module Responsys + module Helper + module Formatting + + def self.format_response_with_errors(error) + error_response = { success: false } + + if error.to_hash[:fault].has_key?(:detail) and !error.to_hash[:fault][:detail].nil? + key = error.to_hash[:fault][:detail].keys[0] + error_response[:error] = { http_status_code: error.http.code, code: error.to_hash[:fault][:detail][key][:exception_code], message: error.to_hash[:fault][:detail][key][:exception_message] } + error_response[:error][:trace] = error.to_hash[:fault][:detail][:source] if error.to_hash[:fault][:detail].has_key?(:source) + else + error_response[:error] = { http_status_code: error.http.code, code: error.to_hash[:fault][:faultcode], message: error.to_hash[:fault][:faultstring] } + end + + error_response + end + + def self.format_response_object_from_hash(hash) + return hash if hash.nil? + ResponseObject.new(hash) + end + + def self.format_response_hash(response, action) + formatted_response = { success: true } + + return formatted_response unless response.body.has_key? "#{action}_response".to_sym + + if response.body["#{action}_response".to_sym].has_key?(:result) and response.body["#{action}_response".to_sym][:result].is_a? Hash + formatted_response[:data] = handle_response_types(response.body["#{action}_response".to_sym]) + elsif response.body["#{action}_response".to_sym].has_key?(:result) + formatted_response[:result] = response.body["#{action}_response".to_sym][:result] + elsif response.body["#{action}_response".to_sym].is_a? Hash and !response.body["#{action}_response".to_sym].empty? + formatted_response[:result] = response.body["#{action}_response".to_sym].values[0] + end + + formatted_response + end + + def self.handle_response_types(response_body) + data = [] + + if response_body[:result].has_key? :record_data + data = format_record_data(response_body[:result][:record_data]) + else + data << response_body + end + + data + end + + def self.format_record_data(record_data) + field_names = record_data[:field_names] + records = record_data[:records] + + data = [] + if records.is_a? Array + records.each do |record| + data << format_field_values(record, field_names) + end + else + data << format_field_values(records, field_names) + end + + data + end + + def self.format_field_values(record, field_names) + values = {} + + if record.is_a? Hash and record[:field_values].is_a? Array + record[:field_values].each_with_index do |value, index| + values[field_names[index].to_sym] = value + end + elsif record.is_a? Hash + values[field_names.to_sym] = record[:field_values] + end + + values + end + + def self.format_response_with_message(i18n_key) + { success: false, error: { http_status_code: "", code: i18n_key.split('.')[-1], message: I18n.t(i18n_key) } } + end + end + end +end \ No newline at end of file diff --git a/lib/responsys/helpers/helper.rb b/lib/responsys/helpers/helper.rb new file mode 100644 index 0000000..17e7542 --- /dev/null +++ b/lib/responsys/helpers/helper.rb @@ -0,0 +1,29 @@ +module Responsys + module Helper + + def self.format(params = {}) + result_hash = nil + + if params.has_key?(:response) && params.has_key?(:action) + result_hash = Formatting::format_response_hash(params[:response], params[:action]) + elsif params.has_key?(:error) + result_hash = Formatting::format_response_with_errors(params[:error]) + end + + Formatting::format_response_object_from_hash(result_hash) + end + + def self.build_custom_error_response(message_key) + hash = { success: false, error: { http_status_code: "", code: message_key.split('.')[-1], message: get_message(message_key) } } + ResponseObject.new(hash) + end + + def self.get_message(key) + begin + I18n.t(key, scope: :responsys_api, locale: I18n.locale, raise: true) + rescue I18n::MissingTranslationData + I18n.t(key, scope: :responsys_api, locale: :en) + end + end + end +end \ No newline at end of file diff --git a/lib/responsys/helpers/response_object.rb b/lib/responsys/helpers/response_object.rb new file mode 100644 index 0000000..47b5b4a --- /dev/null +++ b/lib/responsys/helpers/response_object.rb @@ -0,0 +1,44 @@ +module Responsys + class ResponseObject + attr_accessor :response_hash + + def initialize(response) + raise ParameterException, "The parameter is not a hash" unless response.is_a?(Hash) + @response_hash = response + end + + def success? + response_hash[:success] == true + end + + def error? + response_hash[:success] == false + end + + def error_code + return nil unless error? + + "#{response_hash[:error][:code]}" + end + + def error_message + return nil unless error? + + "#{response_hash[:error][:message]}" + end + + def error_desc + "#{error_code} - #{error_message}" + end + + def data + if @response_hash.has_key?(:data) + @response_hash[:data] + elsif @response_hash.has_key?(:result) + @response_hash[:result] + else + nil + end + end + end +end \ No newline at end of file diff --git a/lib/responsys/i18n/en.yml b/lib/responsys/i18n/en.yml index 2ce2481..3cad83f 100644 --- a/lib/responsys/i18n/en.yml +++ b/lib/responsys/i18n/en.yml @@ -1,5 +1,9 @@ en: responsys_api: + response: + object: + incorrect_param: The parameter is not a hash + api: object: field_type: @@ -25,4 +29,4 @@ en: incorrect_recipients_type: Recipients parameter must be an array member: riid_missing: Variable riid is not provided to the member - record_not_found: The member has not been found in the list + record_not_found: The member has not been found in the list \ No newline at end of file diff --git a/lib/responsys/member.rb b/lib/responsys/member.rb index e581d15..f07a79e 100644 --- a/lib/responsys/member.rb +++ b/lib/responsys/member.rb @@ -1,6 +1,3 @@ -require "responsys/api/all" -require "responsys/api/object/all" - module Responsys class Member include Responsys::Api @@ -33,8 +30,8 @@ def add_to_list(list, subscribe = false, details = {}, update_record = false) end def retrieve_profile_extension(profile_extension, fields) - return Responsys::Helper.format_response_with_message("member.riid_missing") if @user_riid.nil? - return Responsys::Helper.format_response_with_message("member.record_not_found") unless present_in_profile?(profile_extension) + return Responsys::Helper.build_custom_error_response("member.riid_missing") if @user_riid.nil? + return Responsys::Helper.build_custom_error_response("member.record_not_found") unless present_in_profile?(profile_extension) @client.retrieve_profile_extension_records(profile_extension, QueryColumn.new("RIID"), fields, [@user_riid]) end @@ -48,18 +45,18 @@ def unsubscribe(list) end def update(list, data) - return Responsys::Helper.format_response_with_message("member.record_not_found") unless present_in_list?(list) + return Responsys::Helper.build_custom_error_response("member.record_not_found") unless present_in_list?(list) record = RecordData.new(data) @client.merge_list_members(list, record) end def present_in_profile?(list) - return Responsys::Helper.format_response_with_message("member.riid_missing") if @user_riid.nil? + return Responsys::Helper.build_custom_error_response("member.riid_missing") if @user_riid.nil? response = lookup_profile_via_key(list, "RIID", @user_riid) - !(response[:status] == "failure" && response[:error][:code] == "RECORD_NOT_FOUND") + !(response.error? && response.error_code == "RECORD_NOT_FOUND") end def present_in_list?(list) @@ -69,14 +66,14 @@ def present_in_list?(list) response = lookup_list_via_key(list, "EMAIL_ADDRESS", @email) end - !(response[:status] == "failure" && response[:error][:code] == "RECORD_NOT_FOUND") + !(response.error? && response.error_code == "RECORD_NOT_FOUND") end def subscribed?(list) - return Responsys::Helper.format_response_with_message("member.record_not_found") unless present_in_list?(list) + return Responsys::Helper.build_custom_error_response("member.record_not_found") unless present_in_list?(list) - response = @client.retrieve_list_members(list, QueryColumn.new("EMAIL_ADDRESS"), %w(EMAIL_PERMISSION_STATUS_), [@email]) - response[:data][0][:EMAIL_PERMISSION_STATUS_] == "I" + object = @client.retrieve_list_members(list, QueryColumn.new("EMAIL_ADDRESS"), %w(EMAIL_PERMISSION_STATUS_), [@email]) + object.data[0][:EMAIL_PERMISSION_STATUS_] == "I" end private diff --git a/lib/responsys_api.rb b/lib/responsys_api.rb index 889bb41..67c3adf 100644 --- a/lib/responsys_api.rb +++ b/lib/responsys_api.rb @@ -2,8 +2,14 @@ I18n.load_path.concat Dir.glob( File.dirname(__FILE__) + "/responsys/i18n/*.yml" ) -require "responsys/exceptions/all" -require "responsys/helper" require "responsys/configuration" + +require "responsys/helpers/all" + +require "responsys/exceptions/all" + +require "responsys/api/object/all" +require "responsys/api/all" + require "responsys/api/client" -require "responsys/member" +require "responsys/member" \ No newline at end of file diff --git a/spec/api/campaign_spec.rb b/spec/api/campaign_spec.rb index 7a85f06..f22d68f 100644 --- a/spec/api/campaign_spec.rb +++ b/spec/api/campaign_spec.rb @@ -18,14 +18,14 @@ it "should return a status of ok when triggering a message for one recipient" do VCR.use_cassette("api/campaign/trigger_message_1") do response = Responsys::Api::Client.instance.trigger_message(campaign, [recipientData1]) - expect(response[:status]).to eq("ok") + expect(response.success?).to be_truthy end end it "should return a status of ok when triggering a message for more than one recipient" do VCR.use_cassette("api/campaign/trigger_message_2") do response = Responsys::Api::Client.instance.trigger_message(campaign, [recipientData1,recipientData2]) - expect(response[:status]).to eq("ok") + expect(response.success?).to be_truthy end end @@ -44,14 +44,14 @@ it "should return a status of ok when triggering a custom event for one recipient" do VCR.use_cassette("api/campaign/trigger_custom_event_1") do response = Responsys::Api::Client.instance.trigger_custom_event(custom_event, [recipientData1]) - expect(response[:status]).to eq("ok") + expect(response.success?).to be_truthy end end it "should return a status of ok when triggering a custom event for more than one recipient" do VCR.use_cassette("api/campaign/trigger_custom_event_2") do response = Responsys::Api::Client.instance.trigger_custom_event(custom_event, [recipientData1,recipientData2]) - expect(response[:status]).to eq("ok") + expect(response.success?).to be_truthy end end diff --git a/spec/api/list_spec.rb b/spec/api/list_spec.rb index 6c18da9..2b1bd5b 100644 --- a/spec/api/list_spec.rb +++ b/spec/api/list_spec.rb @@ -16,8 +16,7 @@ it "should get a ok status" do VCR.use_cassette("api/list/retrieve") do response = Responsys::Api::Client.instance.retrieve_list_members(@list, @query_column_email, %w(EMAIL_ADDRESS_ MOBILE_NUMBER_), @multiple_users_email) - - expect(response[:status]).to eq("ok") + expect(response.success?).to be_truthy end end @@ -25,7 +24,7 @@ VCR.use_cassette("api/list/retrieve") do response = Responsys::Api::Client.instance.retrieve_list_members(@list, @query_column_email, %w(EMAIL_ADDRESS_ MOBILE_NUMBER_), @multiple_users_email) - expect(response[:data][0][:MOBILE_NUMBER_]).to eq(@user1_mobile) + expect(response.data[0][:MOBILE_NUMBER_]).to eq(@user1_mobile) end end @@ -33,7 +32,7 @@ VCR.use_cassette("api/list/retrieve") do response = Responsys::Api::Client.instance.retrieve_list_members(@list, @query_column_email, %w(EMAIL_ADDRESS_ MOBILE_NUMBER_), @multiple_users_email) - expect(response[:data][1][:MOBILE_NUMBER_]).to be(nil) + expect(response.data[1][:MOBILE_NUMBER_]).to be(nil) end end @@ -41,7 +40,7 @@ VCR.use_cassette("api/list/retrieve_single") do response = Responsys::Api::Client.instance.retrieve_list_members(@list, @query_column_email, %w(EMAIL_ADDRESS_ MOBILE_NUMBER_), [@user1_email]) - expect(response[:data][0][:EMAIL_ADDRESS_]).to eq(@user1_email) + expect(response.data[0][:EMAIL_ADDRESS_]).to eq(@user1_email) end end @@ -49,7 +48,7 @@ VCR.use_cassette("api/list/retrieve_single_single") do response = Responsys::Api::Client.instance.retrieve_list_members(@list, @query_column_email, %w(EMAIL_ADDRESS_), [@user1_email]) - expect(response[:data][0][:EMAIL_ADDRESS_]).to eq(@user1_email) + expect(response.data[0][:EMAIL_ADDRESS_]).to eq(@user1_email) end end end @@ -60,7 +59,7 @@ data = [{ Email_Address_: @user1_email, Mobile_Number_: @user1_mobile }] response = Responsys::Api::Client.instance.merge_list_members(@list, Responsys::Api::Object::RecordData.new(data), merge_rule = Responsys::Api::Object::ListMergeRule.new(matchColumnName1: "Mobile_Number_")) - expect(response[:data][0][:result][:rejected_count].to_i).to eq(0) + expect(response.data[0][:result][:rejected_count].to_i).to eq(0) end end end diff --git a/spec/api/table_spec.rb b/spec/api/table_spec.rb index 03165c3..e9e5e5b 100644 --- a/spec/api/table_spec.rb +++ b/spec/api/table_spec.rb @@ -46,7 +46,7 @@ ] response = Responsys::Api::Client.instance.create_table_with_pk(@table_with_pk, fields, %w(field1)) - expect(response[:result]).to be(true) + expect(response.success?).to be_truthy end end @@ -54,7 +54,7 @@ VCR.use_cassette("api/table/delete_with_pk") do response = Responsys::Api::Client.instance.delete_table(@table_with_pk) - expect(response[:result]).to be(true) + expect(response.success?).to be_truthy end end end @@ -65,7 +65,7 @@ VCR.use_cassette("api/profile_extension/retrieve_profile_extension_records") do response = Responsys::Api::Client.instance.retrieve_profile_extension_records(@profile_extension, @query_column_riid, @fields, %W(#{@user_riid})) - expect(response[:status]).to eq("ok") + expect(response.success?).to be_truthy end end @@ -73,7 +73,7 @@ VCR.use_cassette("api/profile_extension/retrieve_profile_extension_records") do response = Responsys::Api::Client.instance.retrieve_profile_extension_records(@profile_extension, @query_column_riid, @fields, %W(#{@user_riid})) - expect(response[:data].length).to eq(1) + expect(response.data.length).to eq(1) end end @@ -81,7 +81,7 @@ VCR.use_cassette("api/profile_extension/retrieve_profile_extension_records") do response = Responsys::Api::Client.instance.retrieve_profile_extension_records(@profile_extension, @query_column_riid, @fields, %W(#{@user_riid})) - expect(response[:data][0].length).to eq(2) + expect(response.data[0].length).to eq(2) end end @@ -90,7 +90,7 @@ record_data = Responsys::Api::Object::RecordData.new([{RIID_: @user_riid, MONTHLY_PURCH: @user_purch}]) response = Responsys::Api::Client.instance.merge_into_profile_extension(@profile_extension, record_data, "RIID", true) - expect(response[:status]).to eq("ok") + expect(response.success?).to be_truthy end end @@ -98,7 +98,7 @@ VCR.use_cassette("api/profile_extension/delete_profile_extension_records") do response = Responsys::Api::Client.instance.delete_profile_extension_members(@profile_extension, @query_column_riid, %W{#{@user_riid}}) - expect(response[:status]).to eq("ok") + expect(response.success?).to be_truthy end end diff --git a/spec/helper_spec.rb b/spec/helpers/helper_spec.rb similarity index 100% rename from spec/helper_spec.rb rename to spec/helpers/helper_spec.rb diff --git a/spec/member_spec.rb b/spec/member_spec.rb index 0d2aacc..993d957 100644 --- a/spec/member_spec.rb +++ b/spec/member_spec.rb @@ -32,9 +32,9 @@ end it "should check the user has subscribed" do - response_expected = { status: "ok", data: [{ EMAIL_PERMISSION_STATUS_: "I" }] } + response_expected = { success: true, data: [{ EMAIL_PERMISSION_STATUS_: "I" }] } - allow(connection).to receive(:retrieve_list_members).with(@list, kind_of(Responsys::Api::Object::QueryColumn), %w(EMAIL_PERMISSION_STATUS_), %W(#{@member.email})).and_return(response_expected) + allow(connection).to receive(:retrieve_list_members).with(@list, kind_of(Responsys::Api::Object::QueryColumn), %w(EMAIL_PERMISSION_STATUS_), %W(#{@member.email})).and_return(Responsys::ResponseObject.new(response_expected)) expect(@member.subscribed?(@list)).to eq(true) end @@ -84,7 +84,7 @@ member_with_fake_riid = Responsys::Member.new(@email, "000001") response = member_with_fake_riid.subscribed?(@list) - expect(response[:error][:code]).to eq("record_not_found") + expect(response.error_code).to eq("record_not_found") end end @@ -99,11 +99,11 @@ @fields = %w(RIID_ MONTHLY_PURCH) end - it "should set the status to failure if no riid provided" do + it "should set the status to error if no riid provided" do VCR.use_cassette("member/retrieve_profile_extension_fail") do response = @member_without_riid.retrieve_profile_extension(@profile_extension, @fields) - expect(response[:status]).to eq("failure") + expect(response.error?).to be_truthy end end @@ -111,7 +111,7 @@ VCR.use_cassette("member/retrieve_profile_extension_fail") do response = @member_without_riid.retrieve_profile_extension(@profile_extension, @fields) - expect(response[:error][:message]).to eq(Responsys::Helper.get_message("member.riid_missing")) + expect(response.error_message).to eq(Responsys::Helper.get_message("member.riid_missing")) end end @@ -119,7 +119,7 @@ VCR.use_cassette("member/retrieve_profile_extension") do response = @member_with_riid.retrieve_profile_extension(@profile_extension, @fields) - expect(response[:status]).to eq("ok") + expect(response.success?).to be_truthy end end