TheChamp helped me out to sort out with the array of hashes issue. Thanks. Now I would like to improve on that. It is really messy in the views
- Move the code to model, so only calling the instance variable will show the desired result. Like one variable for flight types, the departure times, price structure, etc.
- Have the result sorted in the model itself, based on some conditions. Like for price, default to lowest first.
- It takes a lot of time to get the API response. What are the various options to cache the response, so results are instantaneous. Also, what values need to be checked to ensure the cache is fresh.
This is my code base. (for ur consideration, a portion of API response - http://jsfiddle.net/PP9N5/)
Model
class Cleartrip
include HTTParty
debug_output $stdout
base_uri "api.staging.cleartrip.com/air/1.0/search"
headers 'X-CT-API-KEY' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
format :xml
def self.get_flight
response = get("?from=DEL&to=BLR&depart-date=2014-08-10&adults=1&children=0&infants=0")
if response.success?
response
else
raise response.message
end
end
Controller
# This does nothing
expires_in 20.minutes, public: true
@flight = Cleartrip.get_flight
Views
<table>
<tbody>
<% @flight["air_search_result"]["onward_solutions"]["solution"].each do |h| %>
<tr>
<td> # This gets the flight info
<% Array.wrap(h["flights"]["flight"]["segments"]["segment"]).each do |segment| %>
<strong><%= segment['airline'] %></strong> <br>
<%= segment['departure_airport'] %> - <%= segment['departure_date_time'] %> ||
<%= segment['arrival_airport'] %> - <%= segment['arrival_date_time'] %> <br>
<% end %>
<hr>
</td>
<td> # this gets the type of fare, Refundable/ Non-refundable
<%= h["pax_pricing_info_list"]["pax_pricing_info"]["pricing_info_list"]["pricing_info"]["fare_type"] %>
</td>
<td> # This gets the fare structure
<% h["pax_pricing_info_list"]["pax_pricing_info"]["pricing_info_list"]["pricing_info"]["pricing_elements"]["pricing_element"].each do |f| %>
<%= f['category']%> - <%= f['amount'] %> <br>
<% end %>
</td>
<td> # The pricing summary
<strong><%=h["pricing_summary"]["total_fare"] %></strong>
</td>
</tr>
<% end %>
</tbody>
</table>
Appreciate general guidelines.
Thanks.