Class: MedlineplusRuby::API::ResponsePayload
- Inherits:
-
Object
- Object
- MedlineplusRuby::API::ResponsePayload
- Defined in:
- lib/medlineplus_ruby/api/response_payload.rb
Instance Method Summary collapse
-
#initialize ⇒ ResponsePayload
constructor
A new instance of ResponsePayload.
-
#respond(api_response_body) ⇒ Object
Currently only supports ‘application/json’ responses.
Constructor Details
#initialize ⇒ ResponsePayload
Returns a new instance of ResponsePayload.
8 9 |
# File 'lib/medlineplus_ruby/api/response_payload.rb', line 8 def initialize end |
Instance Method Details
#respond(api_response_body) ⇒ Object
Currently only supports ‘application/json’ responses.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/medlineplus_ruby/api/response_payload.rb', line 12 def respond(api_response_body) formatted_response = { success: false, errors: [], data_requested: nil, data: [], response_raw: api_response_body } begin parsed_body = JSON.parse api_response_body, symbolize_names: true rescue JSON::ParserError => e formatted_response[:errors] << MedlineplusRuby::API::ResponseMessage::ERROR_NO_PARSE end formatted_response.tap do |h| h[:success] = true h[:data_requested] = parsed_body.dig :feed, :subtitle, :_value parsed_body[:feed][:entry].each do |entry| h[:data] << { title: entry[:title][:_value], link: entry[:link].first[:href], description: entry[:summary][:_value] } end if !!parsed_body[:feed][:entry] end if !!parsed_body formatted_response end |