Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major changes (ResponseObject, edited response hash) #22

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions lib/responsys/api/campaign.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "responsys/api/object/all"

module Responsys
module Api
module Campaign
Expand All @@ -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
15 changes: 3 additions & 12 deletions lib/responsys/api/client.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -29,23 +25,18 @@ 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
login

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
Expand Down
2 changes: 0 additions & 2 deletions lib/responsys/api/list.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "responsys/api/object/all"

module Responsys
module Api
module List
Expand Down
91 changes: 0 additions & 91 deletions lib/responsys/helper.rb

This file was deleted.

3 changes: 3 additions & 0 deletions lib/responsys/helpers/all.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require "responsys/helpers/formatting"
require "responsys/helpers/response_object"
require "responsys/helpers/helper"
87 changes: 87 additions & 0 deletions lib/responsys/helpers/formatting.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions lib/responsys/helpers/helper.rb
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions lib/responsys/helpers/response_object.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion lib/responsys/i18n/en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
en:
responsys_api:
response:
object:
incorrect_param: The parameter is not a hash

api:
object:
field_type:
Expand All @@ -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
Loading