I'm learning Ruby and i need help with CSVs files. I'm not finding the correct way to print output to a CSV file but i get recursivity and i don't want that. Thats the code:
require "httparty"
require "csv"
class ConnexioEPPO
include HTTParty
base_uri 'https://data.eppo.int/api/rest/1.0'
@@authtoken = "a9505d2ab257987580641d1a56de1f6c"
def pests(eppocode)
request = self.class.get("/taxon/#{eppocode}/pests?authtoken=#{@@authtoken}")
result = request.parsed_response
CSV.open("data.csv", "w", headers: result["Host"].first.keys) do |csv|
result["Host"].each do |h|
requestTax = self.class.get("/taxon/#{h["eppocode"]}/taxonomy?authtoken=#{@@authtoken}")
resultTax = requestTax.parsed_response
puts h["eppocode"]
resultTax.each do |tax|
puts "#{tax["eppocode"]} #{tax["prefname"]}"
planta = h.values << tax["eppocode"] ", " tax["prefname"]
csv << planta
end
end
end
end
end
connexioEPPO = ConnexioEPPO.new
puts connexioEPPO.pests('1ULMG')
As you can see in the first part of the code im requesting (pests) a information as a Hash what contains(this is just few lines):
{"eppocode"=>"ANIDMA", "idclass"=>9, "labelclass"=>"Host", "fullname"=>"Anisandrus maiche"}
{"eppocode"=>"ANOLGL", "idclass"=>9, "labelclass"=>"Host", "fullname"=>"Anoplophora glabripennis"}
{"eppocode"=>"APRIGE", "idclass"=>9, "labelclass"=>"Host", "fullname"=>"Apriona germari"}
{"eppocode"=>"PHYPUL", "idclass"=>9, "labelclass"=>"Host", "fullname"=>"'Candidatus Phytoplasma ulmi'"}
And then I'm requesting (taxonomy) information again and what I'm doing is getting information where the field eppocode is equal to eppocode in the new requests (the idea is just get the taxonomy of the previously requested fields by eppocode)
And the idea is to get printed out in a CSV the first headers
{"eppocode"=>"ANIDMA", "idclass"=>9, "labelclass"=>"Host", "fullname"=>"Anisandrus maiche"}
and adding the taxonomy of the corresponding field
Wished output(example with one eppocode):
ANIDMA,9,Host,Anisandrus maiche,"1ANIMK, Animalia","1ARTHP, Arthropoda","1INSEC, Insecta","1COLEO, Coleoptera","1CURCF, Curculionidae","1SCOLS, Scolytinae","1ANIDG, Anisandrus","ANIDMA, Anisandrus maiche"
Actual output(example with one eppocode):
ANIDMA,9,Host,Anisandrus maiche,"1ANIMK, Animalia"
ANIDMA,9,Host,Anisandrus maiche,"1ARTHP, Arthropoda"
ANIDMA,9,Host,Anisandrus maiche,"1HEXAQ, Hexapoda"
ANIDMA,9,Host,Anisandrus maiche,"1INSEC, Insecta"
ANIDMA,9,Host,Anisandrus maiche,"1COLEO, Coleoptera"
ANIDMA,9,Host,Anisandrus maiche,"1CURCF, Curculionidae"
ANIDMA,9,Host,Anisandrus maiche,"1SCOLS, Scolytinae"
ANIDMA,9,Host,Anisandrus maiche,"1ANIDG, Anisandrus"
ANIDMA,9,Host,Anisandrus maiche,"ANIDMA, Anisandrus maiche"
And what I want is avoid that bunch of fields with the same information. I hope you understand my questions Thanks!
CodePudding user response:
I believe that in order to fix your code with the minimal changes, you need to change the posting to the CSV to be outside of your resultTax loop, so something like the following might work?:
planta = h.values
resultTax.each do |tax|
puts "#{tax["eppocode"]} #{tax["prefname"]}"
planta << tax["eppocode"] ", " tax["prefname"]
end
csv << planta
(Please excuse any typos, posting from my phone)
