covid19-cli 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,8 +19,9 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = ["covid19-cli"]
20
20
  spec.require_paths = ["lib"]
21
21
 
22
- spec.add_dependency "thor"
23
22
  spec.add_dependency "httparty"
23
+ spec.add_dependency "thor"
24
+ spec.add_dependency "text-table"
24
25
  spec.add_development_dependency "bundler", "~> 1.7"
25
26
  spec.add_development_dependency "rake", "~> 10.0"
26
27
  spec.add_development_dependency "rspec", "~> 3.0"
@@ -1,5 +1,6 @@
1
1
  require 'thor'
2
2
  require_relative 'services/covid19_data'
3
+ require_relative 'decorators/table'
3
4
 
4
5
  module Covid19
5
6
  class Client < Thor
@@ -8,23 +9,41 @@ module Covid19
8
9
  end
9
10
 
10
11
  desc 'all_continents', 'List all continents data'
12
+ option :table, required: false
11
13
  def all_continents
12
- puts Covid19::Services::Covid19Data.all_continents
14
+ locality = Covid19::Decorators::Table::LOCALITY[:continent]
15
+ result = Covid19::Services::Covid19Data.all_continents
16
+
17
+ puts options[:table] ?
18
+ Covid19::Decorators::Table.create(data: result, locality: locality) :
19
+ result
13
20
  end
14
21
 
15
22
  desc 'all countries', 'List all countries data'
23
+ option :table, required: false
16
24
  def all_countries
17
- puts Covid19::Services::Covid19Data.all_countries
25
+ locality = Covid19::Decorators::Table::LOCALITY[:country]
26
+ result = Covid19::Services::Covid19Data.all_countries
27
+
28
+ puts options[:table] ?
29
+ Covid19::Decorators::Table.create(data: result, locality: locality):
30
+ result
18
31
  end
19
32
 
20
33
  desc 'continent CONTINENT_NAME', 'List continent data'
34
+ option :table, required: false
21
35
  def continent(continent_name)
22
- puts Covid19::Services::Covid19Data.continent(continent_name)
36
+ result = Covid19::Services::Covid19Data.continent(continent_name)
37
+
38
+ puts options[:table] ? Covid19::Decorators::Table.create(data: result) : result
23
39
  end
24
40
 
25
41
  desc 'country COUNTRY_NAME', 'List country data'
42
+ option :table, required: false
26
43
  def country(country_name)
27
- puts Covid19::Services::Covid19Data.country(country_name)
44
+ result = Covid19::Services::Covid19Data.country(country_name)
45
+
46
+ puts options[:table] ? Covid19::Decorators::Table.create(data: result) : result
28
47
  end
29
48
  end
30
49
  end
@@ -0,0 +1,64 @@
1
+ require 'text-table'
2
+
3
+ module Covid19
4
+ module Decorators
5
+ class Table
6
+ LOCALITY = { country: 'country', continent: 'continent' }
7
+ DEFAULT_HEADER = %w(cases
8
+ todayCases
9
+ deaths
10
+ todayDeaths
11
+ recovered
12
+ todayRecovered
13
+ active
14
+ critical
15
+ tests
16
+ testsPerOneMillion
17
+ population)
18
+
19
+ def self.create(data:, locality: nil)
20
+ table = Text::Table.new
21
+
22
+ # Specific data
23
+ if data.is_a?(Hash)
24
+ table.head = DEFAULT_HEADER
25
+ table.rows = [align_fields(filter_data(data).values)]
26
+ end
27
+
28
+ # Data lists
29
+ if data.is_a?(Array) && !locality.empty?
30
+ table.head = DEFAULT_HEADER.unshift(locality)
31
+
32
+ data.each { |line| table.rows << align_fields(filter_data(line).values) }
33
+ end
34
+
35
+ table.to_s
36
+ end
37
+
38
+ def self.align_fields(data)
39
+ data.map { |field| { value: format_number(field), align: :right } }
40
+ end
41
+
42
+ def self.filter_data(data)
43
+ organize_fields(data).select { |key, value| DEFAULT_HEADER.include?(key) }
44
+ end
45
+
46
+ def self.format_number(number)
47
+ number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1 ")
48
+ end
49
+
50
+ def self.organize_fields(data)
51
+ if DEFAULT_HEADER.include?(LOCALITY[:continent])
52
+ continent = data.slice(LOCALITY[:continent])
53
+ data.delete(LOCALITY[:continent])
54
+
55
+ return continent.merge(data)
56
+ end
57
+
58
+ data
59
+ end
60
+
61
+ private_class_method :align_fields, :filter_data, :format_number, :organize_fields
62
+ end
63
+ end
64
+ end
@@ -1,3 +1,3 @@
1
1
  module Covid19
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,25 @@
1
+ require_relative '../../../../lib/covid19/decorators/table'
2
+
3
+ RSpec.describe 'Covid19::Decorators::Table' do
4
+ describe '.create' do
5
+ it 'returns specific data in table format' do
6
+ data = {"updated"=>1594706997629, "country"=>"Brazil", "countryInfo"=>{"_id"=>76, "iso2"=>"BR", "iso3"=>"BRA", "lat"=>-10, "long"=>-55, "flag"=>"https://disease.sh/assets/img/flags/br.png"}, "cases"=>1887959, "todayCases"=>0, "deaths"=>72921, "todayDeaths"=>0, "recovered"=>1213512, "todayRecovered"=>0, "active"=>601526, "critical"=>8318, "casesPerOneMillion"=>8880, "deathsPerOneMillion"=>343, "tests"=>4572796, "testsPerOneMillion"=>21508, "population"=>212611764, "continent"=>"South America", "oneCasePerPeople"=>113, "oneDeathPerPeople"=>2916, "oneTestPerPeople"=>46, "activePerOneMillion"=>2829.22, "recoveredPerOneMillion"=>5707.64, "criticalPerOneMillion"=>39.12}
7
+
8
+ expected_result = "+-----------+------------+--------+-------------+-----------+----------------+---------+----------+-----------+--------------------+-------------+\n| cases | todayCases | deaths | todayDeaths | recovered | todayRecovered | active | critical | tests | testsPerOneMillion | population |\n+-----------+------------+--------+-------------+-----------+----------------+---------+----------+-----------+--------------------+-------------+\n| 1 887 959 | 0 | 72 921 | 0 | 1 213 512 | 0 | 601 526 | 8 318 | 4 572 796 | 21 508 | 212 611 764 |\n+-----------+------------+--------+-------------+-----------+----------------+---------+----------+-----------+--------------------+-------------+\n"
9
+
10
+ result = Covid19::Decorators::Table.create(data: data)
11
+
12
+ expect(result).to eq(expected_result)
13
+ end
14
+
15
+ it 'returns general list in table format' do
16
+ data = [{"updated"=>1594779763760, "cases"=>4152507, "todayCases"=>7057, "deaths"=>188977, "todayDeaths"=>837, "recovered"=>1939661, "todayRecovered"=>4932, "active"=>2023869, "critical"=>19737, "casesPerOneMillion"=>7046.81, "deathsPerOneMillion"=>320.69, "tests"=>49174748, "testsPerOneMillion"=>83449.65, "population"=>589274482, "continent"=>"North America", "activePerOneMillion"=>3434.51, "recoveredPerOneMillion"=>3291.61, "criticalPerOneMillion"=>33.49, "continentInfo"=>{"lat"=>31.6768272, "long"=>-146.4707474}, "countries"=>["Anguilla", "Antigua and Barbuda", "Aruba", "Bahamas", "Barbados", "Belize", "Bermuda", "British Virgin Islands", "Canada", "Caribbean Netherlands", "Cayman Islands", "Costa Rica", "Cuba", "Curaçao", "Dominica", "Dominican Republic", "El Salvador", "Greenland", "Grenada", "Guadeloupe", "Guatemala", "Haiti", "Honduras", "Jamaica", "Martinique", "Mexico", "Montserrat", "Nicaragua", "Panama", "Saint Kitts and Nevis", "Saint Lucia", "Saint Martin", "Saint Pierre Miquelon", "Saint Vincent and the Grenadines", "Sint Maarten", "St. Barth", "Trinidad and Tobago", "Turks and Caicos Islands", "USA"]}]
17
+
18
+ expected_result = "+---------------+-----------+------------+---------+-------------+-----------+----------------+-----------+----------+------------+--------------------+-------------+\n| continent | cases | todayCases | deaths | todayDeaths | recovered | todayRecovered | active | critical | tests | testsPerOneMillion | population |\n+---------------+-----------+------------+---------+-------------+-----------+----------------+-----------+----------+------------+--------------------+-------------+\n| North America | 4 152 507 | 7 057 | 188 977 | 837 | 1 939 661 | 4 932 | 2 023 869 | 19 737 | 49 174 748 | 83 449.65 | 589 274 482 |\n+---------------+-----------+------------+---------+-------------+-----------+----------------+-----------+----------+------------+--------------------+-------------+\n"
19
+
20
+ result = Covid19::Decorators::Table.create(data: data, locality: 'continent')
21
+
22
+ expect(result).to eq(expected_result)
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  RSpec.describe Covid19::Client do
2
2
  it "has version latest number" do
3
- expect(Covid19::VERSION).to eq('0.2.0')
3
+ expect(Covid19::VERSION).to eq('0.3.0')
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: covid19-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre Carlos Martins
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-14 00:00:00.000000000 Z
11
+ date: 2020-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: thor
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -25,7 +39,7 @@ dependencies:
25
39
  - !ruby/object:Gem::Version
26
40
  version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
- name: httparty
42
+ name: text-table
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - ">="
@@ -103,8 +117,10 @@ files:
103
117
  - covid19-cli.gemspec
104
118
  - exe/covid19-cli
105
119
  - lib/covid19/client.rb
120
+ - lib/covid19/decorators/table.rb
106
121
  - lib/covid19/services/covid19_data.rb
107
122
  - lib/covid19/version.rb
123
+ - spec/lib/covid19/decorators/table_spec.rb
108
124
  - spec/lib/covid19/services/covid19_data_spec.rb
109
125
  - spec/lib/covid19/version_spec.rb
110
126
  - spec/spec_helper.rb
@@ -127,7 +143,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
143
  - !ruby/object:Gem::Version
128
144
  version: '0'
129
145
  requirements: []
130
- rubygems_version: 3.0.6
146
+ rubyforge_project:
147
+ rubygems_version: 2.7.6.2
131
148
  signing_key:
132
149
  specification_version: 4
133
150
  summary: COVID-19 Stats