Skip to main content
Became Hot Network Question
deleted 3 characters in body; edited tags; edited title
Source Link
toolic
  • 15.7k
  • 5
  • 29
  • 216

Ruby-script for fetching Fetching data from a REST-API, then converting to CSV

I like to have a script, which fetches data from a REST-API. Then then writes the data into a CSV-file file.

Here's what I've got so far (prototype, using ana publicly accessible dummy-API):

require 'net/http'
require 'json'
require 'csv'

uri = URI("https://swapi.dev/api/people/")
response = JSON.parse(Net::HTTP.get(uri))

response["results"].each do |person|
  CSV.open("people.csv", "a+") do |csv|
    csv << [person["name"], person["height"],
          person["mass"], person["hair_color"],
          person["skin_color"]]
  end
end

=begin
  -- File-content ----------------
  Luke Skywalker,172,77,blond,fair
  C-3PO,167,75,n/a,gold
  R2-D2,96,32,n/a,"white, blue"
  ...
=end

Are there points to improve? Would you have chosen a completely different approach?

Ruby-script for fetching data from a REST-API, then converting to CSV

I like to have a script, which fetches data from a REST-API. Then writes the data into a CSV-file.

Here's what I've got so far (prototype, using an publicly accessible dummy-API):

require 'net/http'
require 'json'
require 'csv'

uri = URI("https://swapi.dev/api/people/")
response = JSON.parse(Net::HTTP.get(uri))

response["results"].each do |person|
  CSV.open("people.csv", "a+") do |csv|
    csv << [person["name"], person["height"],
          person["mass"], person["hair_color"],
          person["skin_color"]]
  end
end

=begin
  -- File-content ----------------
  Luke Skywalker,172,77,blond,fair
  C-3PO,167,75,n/a,gold
  R2-D2,96,32,n/a,"white, blue"
  ...
=end

Are there points to improve? Would you have chosen a completely different approach?

Fetching data from a REST-API then converting to CSV

I like to have a script which fetches data from a REST-API then writes the data into a CSV file.

Here's what I've got so far (prototype, using a publicly accessible dummy-API):

require 'net/http'
require 'json'
require 'csv'

uri = URI("https://swapi.dev/api/people/")
response = JSON.parse(Net::HTTP.get(uri))

response["results"].each do |person|
  CSV.open("people.csv", "a+") do |csv|
    csv << [person["name"], person["height"],
          person["mass"], person["hair_color"],
          person["skin_color"]]
  end
end

=begin
  -- File-content ----------------
  Luke Skywalker,172,77,blond,fair
  C-3PO,167,75,n/a,gold
  R2-D2,96,32,n/a,"white, blue"
  ...
=end

Are there points to improve? Would you have chosen a completely different approach?

Source Link
michael.zech
  • 5k
  • 8
  • 34
  • 58

Ruby-script for fetching data from a REST-API, then converting to CSV

I like to have a script, which fetches data from a REST-API. Then writes the data into a CSV-file.

Here's what I've got so far (prototype, using an publicly accessible dummy-API):

require 'net/http'
require 'json'
require 'csv'

uri = URI("https://swapi.dev/api/people/")
response = JSON.parse(Net::HTTP.get(uri))

response["results"].each do |person|
  CSV.open("people.csv", "a+") do |csv|
    csv << [person["name"], person["height"],
          person["mass"], person["hair_color"],
          person["skin_color"]]
  end
end

=begin
  -- File-content ----------------
  Luke Skywalker,172,77,blond,fair
  C-3PO,167,75,n/a,gold
  R2-D2,96,32,n/a,"white, blue"
  ...
=end

Are there points to improve? Would you have chosen a completely different approach?