lib/survey_gizmo/rest_response.rb in survey-gizmo-ruby-4.0.0 vs lib/survey_gizmo/rest_response.rb in survey-gizmo-ruby-4.1.0

- old
+ new

@@ -1,24 +1,25 @@ # This class normalizes the response returned by Survey Gizmo, including validation. + class RestResponse attr_accessor :raw_response attr_accessor :parsed_response - def initialize(rest_response) - @raw_response = rest_response - @parsed_response = rest_response.parsed_response + def initialize(http_response) + @raw_response = http_response + @parsed_response = http_response.parsed_response if ENV['GIZMO_DEBUG'] - ap 'SG Response: ' + ap 'Parsed SurveyGizmo Response:' ap @parsed_response end - fail "Bad response: #{rest_response.inspect}" unless @parsed_response['result_ok'] && @parsed_response['result_ok'].to_s.downcase == 'true' + fail "Bad response: #{http_response.inspect}" unless @parsed_response['result_ok'] && @parsed_response['result_ok'].to_s.downcase == 'true' return unless data # Handle really crappy [] notation in SG API, so far just in SurveyResponse - (data.is_a?(Array) ? data : [data]).each do |datum| + (data.is_a?(Array) ? data : [data]).compact.each do |datum| unless datum['datesubmitted'].blank? # SurveyGizmo returns date information in EST but does not provide time zone information. # See https://surveygizmov4.helpgizmo.com/help/article/link/date-and-time-submitted datum['datesubmitted'] = datum['datesubmitted'] + ' EST' end @@ -45,15 +46,23 @@ end end # The parsed JSON data of the response def data - @_data ||= @parsed_response['data'] + @parsed_response['data'] end # The error message if there is one def message - @_message ||= @parsed_response['message'] + @parsed_response['message'] + end + + def current_page + @parsed_response['page'] + end + + def total_pages + @parsed_response['total_pages'] end private def cleanup_attribute_name(attr)