Module: Responsys::Helper
- Defined in:
- lib/responsys/helper.rb
Class Method Summary collapse
- .format_field_values(record, field_names) ⇒ Object
- .format_record_data(record_data) ⇒ Object
- .format_response_hash(response, action) ⇒ Object
- .format_response_result(response, action) ⇒ Object
- .format_response_with_errors(error) ⇒ Object
- .format_response_with_message(i18n_key) ⇒ Object
- .get_message(key) ⇒ Object
- .handle_response_types(response_body) ⇒ Object
Class Method Details
.format_field_values(record, field_names) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/responsys/helper.rb', line 39 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 |
.format_record_data(record_data) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/responsys/helper.rb', line 23 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 |
.format_response_hash(response, action) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/responsys/helper.rb', line 7 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 |
.format_response_result(response, action) ⇒ Object
3 4 5 |
# File 'lib/responsys/helper.rb', line 3 def self.format_response_result(response, action) response.body[("#{action}_response").to_sym][:result] end |
.format_response_with_errors(error) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/responsys/helper.rb', line 65 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 |
.format_response_with_message(i18n_key) ⇒ Object
79 80 81 |
# File 'lib/responsys/helper.rb', line 79 def self.(i18n_key) { status: "failure", error: { http_status_code: "", code: i18n_key.split('.')[-1], message: (i18n_key) } } end |
.get_message(key) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/responsys/helper.rb', line 83 def self.(key) begin I18n.t(key, scope: :responsys_api, locale: I18n.locale, raise: true) rescue I18n::MissingTranslationData I18n.t(key, scope: :responsys_api, locale: :en, default: "Responsys - Unknown message '#{key}'") end end |
.handle_response_types(response_body) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/responsys/helper.rb', line 53 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 |