Good afternoon, there are classes:
class Movement < ActiveRecord::Base belongs_to :flight scope :book_weekly, -> { where(status_code: 'BKD').where(moved_at: (Date.today..(Date.today + 1.week))) } end class Flight < ActiveRecord::Base has_many :movements end class WeeklyBriefCollector def initialize flights_ids = Movement.book_weekly.unscope(:order).map {|m| m.flight_id}.compact.uniq @flights = Flight.where(id: flights_ids, departure_time: (Date.today..(Date.today + 1.week))) end def xml flights = flights_data unless flights.empty? xml = Nokogiri::XML " <FLIGHTS DateFrom='#{Date.today}' DateTo='#{Date.today + 1.week}'> #{flights} </FLIGHTS> " xml.to_xml end end def flights_data @flights.map do |flight| unless flight.movements.empty? "<FLIGHT date='#{flight.departure_time.to_date}' no='#{flight.flight_number}' carrier='#{flight.airline}'> <leg num='1' origin='#{flight.origin_apt}' destination='#{flight.destination_apt}' Volume_Adv='#{flight.movements.sum(:volume)}' Weight_Adv='#{flight.movements.sum(:weight)}' Pieces_Adv='#{flight.movements.sum(:pieces)}'/> </FLIGHT>" end end end end
in response, I get:
#=> <?xml version="1.0"?> <FLIGHTS DateFrom="2016-07-08" DateTo="2016-07-15"> [ "<FLIGHT date="2016-07-10" no="701" carrier="U6"> <leg num="1" origin="SVX" destination="PRG" Volume_Adv="0.0" Weight_Adv="200.0" Pieces_Adv="1"/> </FLIGHT>", "<FLIGHT date="2016-07-14" no="701" carrier="U6"> <leg num="1" origin="SVX" destination="PRG" Volume_Adv="0.0" Weight_Adv="400.0" Pieces_Adv="2"/> </FLIGHT>" ] </FLIGHTS>
The bottom line is that it will automatically run WeeklyBriefCollector.new.xml
and go to a specific mailbox, for further processing by another program inside the xml, I still have the array characters, and in general I believe that I sketched all this far from the best. Please tell me how best to implement this task?
and by the total get xml of this type:
<?xml version="1.0"?> <FLIGHTS DateFrom="2016-07-08" DateTo="2016-07-15"> <FLIGHT date="2016-07-10" no="701" carrier="U6"> <leg num="1" origin="SVX" destination="PRG" Volume_Adv="0.0" Weight_Adv="200.0" Pieces_Adv="1"/> </FLIGHT> <FLIGHT date="2016-07-14" no="701" carrier="U6"> <leg num="1" origin="SVX" destination="PRG" Volume_Adv="0.0" Weight_Adv="400.0" Pieces_Adv="2"/> </FLIGHT> </FLIGHTS>