From 06ed501d6bf805de9cb24c32b1ee425e3351060e Mon Sep 17 00:00:00 2001 From: Sadayuki Furuhashi Date: Wed, 1 Jul 2020 12:37:18 -0700 Subject: [PATCH] Add covid19 example A simple example of calling web API written in Ruby. --- examples/covid19/covid19.rb | 54 ++++++++++++++++++++++++++++++++++++ examples/covid19/example.yml | 12 ++++++++ 2 files changed, 66 insertions(+) create mode 100755 examples/covid19/covid19.rb create mode 100644 examples/covid19/example.yml diff --git a/examples/covid19/covid19.rb b/examples/covid19/covid19.rb new file mode 100755 index 0000000..54dd439 --- /dev/null +++ b/examples/covid19/covid19.rb @@ -0,0 +1,54 @@ +#!/usr/bin/env ruby + +# COVID-19 statistics from https://thevirustracker.com/ +# +# How to use: +# 1. install ruby +# 2. install embulk +# 4. install embulk-input-script $ embulk gem install embulk-input-script +# 5. install embulk-output-postgresql $ embulk gem install embulk-output-postgresql +# 5. run $ embulk run config.yml + +require 'open-uri' +require 'json' +require 'yaml' +require 'csv' +require 'time' + +case ARGV[0] +when "setup" # covid19.rb setup config.yml setup.yml + config = { + "tasks" => 1, + "columns" => [ + {"name" => "countrycode", "type" => "string"}, + {"name" => "date", "type" => "timestamp", "format" => "%m/%d/%y"}, + {"name" => "cases", "type" => "long"}, + {"name" => "deaths", "type" => "long"}, + {"name" => "recovered", "type" => "long"}, + ] + } + File.write(ARGV[2], config.to_yaml) + +when "run" # covid19.rb run setup.yml output.csv 0 + uri = URI.parse('https://thevirustracker.com/timeline/map-data.json') + data = JSON.parse(uri.read) + + def or_zero(val) + val.to_s.empty? ? "0" : val + end + + CSV.open(ARGV[2], 'wb') do |csv| + data["data"].each do |row| + csv << [ + row["countrycode"], + row["date"], + or_zero(row["cases"]), + or_zero(row["deaths"]), + or_zero(row["recovered"]), + ] + end + end + +when "finish" # covid19.rb finish setup.yml + +end diff --git a/examples/covid19/example.yml b/examples/covid19/example.yml new file mode 100644 index 0000000..4901026 --- /dev/null +++ b/examples/covid19/example.yml @@ -0,0 +1,12 @@ +in: + type: script + run: ruby covid19.rb + try_named_pipe: false +out: + type: postgresql + host: localhost # Set PostgreSQL hostname + user: pg # Set PostgreSQL username + password: "" # Set PostgreSQL password + database: embulk_example # Set PostgreSQL database name + table: covid19 # Set destination table name + mode: replace