Method: Miasma::Types::Api#format_response
- Defined in:
- lib/miasma/types/api.rb
#format_response(result, extract_body = true) ⇒ Smash
Makes best attempt at formatting response
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/miasma/types/api.rb', line 183 def format_response(result, extract_body=true) extracted_headers = Smash[result.headers.map{|k,v| [Utils.snake(k), v]}] if(extract_body) body_content = result.body.to_s body_content.encode!('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '' ) if(extracted_headers[:content_type].to_s.include?('json')) extracted_body = from_json(body_content) || body_content elsif(extracted_headers[:content_type].to_s.include?('xml')) extracted_body = from_xml(body_content) || body_content else extracted_body = from_json(body_content) || from_xml(body_content) || body_content end end unless(extracted_body) # @note if body is over 100KB, do not extract if(extracted_headers[:content_length].to_i < 102400) extracted_body = result.body.to_s else extracted_body = result.body end end Smash.new( :response => result, :headers => extracted_headers, :body => extracted_body ) end |