nation 0.0.5 → 0.1.1
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.
- checksums.yaml +4 -4
- data/lib/nation.rb +23 -23
- data/lib/nation/basic_info.rb +20 -0
- data/lib/nation/classes/print_file.rb +15 -0
- data/lib/nation/detailed_info.rb +18 -0
- data/lib/nation/modules/get_info.rb +11 -0
- data/lib/nation/templates/html_file.rb +2 -2
- data/lib/nation/templates/json_file.rb +2 -2
- data/specs/nation_spec.rb +14 -0
- metadata +7 -5
- data/lib/nation/nation_general.rb +0 -39
- data/lib/nation/nation_images.rb +0 -0
- data/lib/nation/nation_info.rb +0 -30
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 20d614ce29d37ec18dc41ff56188d925cbff4d10dc41969c1c958b825cb3e550
|
|
4
|
+
data.tar.gz: d317a145851f9d0d131e0f9782506c499f9dc11df14b21e70979e925a7f715c7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 625c600081c285a356480bdfd39cc531543f538156af18a413fd021630ba4a5b5084530c32ca136209dab07e1c37fc967258cf5382a192f2bd5b0a9295392c7f
|
|
7
|
+
data.tar.gz: f1ec14a71e3500f247552fdea40c325b9b418d39d8b8fcb389a29f71bd039ba342186e78975a9ed9d55aa94989275eda284eb6c6b9310e679366f49e55d722fa
|
data/lib/nation.rb
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
require 'net/http'
|
|
2
|
+
require 'json'
|
|
3
|
+
require './nation/basic_info'
|
|
4
|
+
require './nation/detailed_info'
|
|
5
|
+
require_relative'./nation/classes/print_file'
|
|
2
6
|
|
|
3
|
-
|
|
4
|
-
|
|
7
|
+
class Nation
|
|
8
|
+
def initialize(nation_name)
|
|
9
|
+
@nation_info = {}
|
|
10
|
+
@nation_name = nation_name
|
|
11
|
+
@nation_code = standarize(nation_name)
|
|
5
12
|
end
|
|
6
13
|
|
|
7
|
-
def
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
nation_general = general.get_raw_data
|
|
11
|
-
|
|
12
|
-
info = NationInfo.new(nation_name)
|
|
13
|
-
info_country = info.get_raw_data
|
|
14
|
-
|
|
15
|
-
@stored_info = {:name => nation_general[0], :capital => nation_general[1], :continent => nation_general[2], :region => nation_general[3], :population => nation_general[4], :languages => nation_general[5], :about_country => info_country}
|
|
16
|
-
|
|
17
|
-
@stored_info
|
|
14
|
+
def get_info
|
|
15
|
+
basic_info = BasicInfo.new.get(@nation_code)
|
|
16
|
+
detailed_info = DetailedInfo.new.get(@nation_name)
|
|
18
17
|
|
|
18
|
+
@nation_info = basic_info.merge(detailed_info)
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def
|
|
22
|
-
|
|
21
|
+
def print_file(ext_file)
|
|
22
|
+
PrintFile.print_nation_info(@nation_info, ext_file)
|
|
23
23
|
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
def standarize(full_name)
|
|
27
|
+
uri = URI.parse("http://country.io/names.json")
|
|
28
|
+
response = Net::HTTP.get(uri)
|
|
24
29
|
|
|
25
|
-
|
|
26
|
-
HtmlFile.convert_html_file(@stored_info)
|
|
30
|
+
nation_code = JSON.parse(response).select{|k, v| v == full_name}.keys[0]
|
|
27
31
|
end
|
|
28
|
-
end
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
require './nation/nation_info'
|
|
32
|
-
require './nation/templates/json_file'
|
|
33
|
-
require './nation/templates/html_file'
|
|
33
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'net/http'
|
|
2
|
+
require 'json'
|
|
3
|
+
require_relative "modules/get_info"
|
|
4
|
+
|
|
5
|
+
# API Documentation: https://restcountries.eu/#sources
|
|
6
|
+
|
|
7
|
+
class BasicInfo
|
|
8
|
+
include GetInfo
|
|
9
|
+
|
|
10
|
+
def get(nation_code)
|
|
11
|
+
api_endpoint = "https://restcountries.eu/rest/v2/name/#{nation_code}?fullText=true"
|
|
12
|
+
response = get_api_info(api_endpoint).first
|
|
13
|
+
|
|
14
|
+
raise "Country info not avaliable" if response.class != Hash
|
|
15
|
+
|
|
16
|
+
languages = response["languages"].map{|k, v| k["name"]}
|
|
17
|
+
country_basic_info = {"name": response["name"], "capital": response["capital"], "continent": response["region"],
|
|
18
|
+
"region": response["subregion"], "population": response["population"], "languages": languages}
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require_relative '../templates/html_file'
|
|
2
|
+
require_relative '../templates/json_file'
|
|
3
|
+
|
|
4
|
+
class PrintFile
|
|
5
|
+
def self.print_nation_info(nation_object, ext_file)
|
|
6
|
+
case ext_file
|
|
7
|
+
when "html"
|
|
8
|
+
HtmlFile.convert_html_file(nation_object)
|
|
9
|
+
when "json"
|
|
10
|
+
JsonFile.convert_json_file(nation_object)
|
|
11
|
+
else
|
|
12
|
+
raise "File extention not supported yet. Extension #{ext_file}"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require 'net/http'
|
|
2
|
+
require 'json'
|
|
3
|
+
require_relative "modules/get_info"
|
|
4
|
+
|
|
5
|
+
# # API Documentation: https://www.mediawiki.org/wiki/API:Main_page
|
|
6
|
+
|
|
7
|
+
class DetailedInfo
|
|
8
|
+
include GetInfo
|
|
9
|
+
|
|
10
|
+
def get(nation_name)
|
|
11
|
+
nation_name = nation_name.downcase.split.map(&:capitalize).join("_")
|
|
12
|
+
api_endpoint = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&titles=#{nation_name}&formatversion=2&exsentences=10&exlimit=1&explaintext=1"
|
|
13
|
+
response = get_api_info(api_endpoint)
|
|
14
|
+
|
|
15
|
+
raise "Not detailed info found for country #{nation_name}" if response["query"]["pages"][0]["missing"]
|
|
16
|
+
country_detailed_info = {"detailed_info": response["query"]["pages"][0]["extract"]}
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
class
|
|
1
|
+
class HtmlFile
|
|
2
2
|
def self.convert_html_file(json_object)
|
|
3
3
|
path = "./nation_files/html"
|
|
4
4
|
file_name = "#{json_object[:name]}_info_#{Time.now.strftime("%d%m%Y%H%M")}"
|
|
5
5
|
|
|
6
|
-
Dir.mkdir(path) if Dir.exist?(path) == false
|
|
6
|
+
# Dir.mkdir(path) if Dir.exist?(path) == false
|
|
7
7
|
|
|
8
8
|
html = File.new("#{path}/#{file_name}.html", "w+")
|
|
9
9
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
require 'json'
|
|
2
2
|
|
|
3
|
-
class
|
|
3
|
+
class JsonFile
|
|
4
4
|
def self.convert_json_file(json_object)
|
|
5
5
|
|
|
6
6
|
path = "./nation_files/json"
|
|
7
7
|
file_name = "#{json_object[:name]}_info_#{Time.now.strftime("%d%m%Y%H%M")}.json"
|
|
8
8
|
Dir.mkdir(path) if Dir.exist?(path) == false
|
|
9
9
|
|
|
10
|
-
File.open("#{path}/#{file_name}", "w") do |f|
|
|
10
|
+
File.open("#{path}/#{file_name}", "w+") do |f|
|
|
11
11
|
f.write(json_object.to_json)
|
|
12
12
|
end
|
|
13
13
|
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'rspec'
|
|
2
|
+
require 'nation'
|
|
3
|
+
|
|
4
|
+
RSpec.describe Nation do
|
|
5
|
+
describe 'nation_info' do
|
|
6
|
+
it "returns a hash with the information gathering" do
|
|
7
|
+
expect(Nation.new.nation_info("Mexico").class).to eq(Hash)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "returns the correct information of the country" do
|
|
11
|
+
expect(Nation.new.nation_info("Mexico").class).to eq(JSON.parse(File.open("./spec_files/mexico.json").read).class)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: nation
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mario Martinez
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2020-02-
|
|
11
|
+
date: 2020-02-10 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A basic but useful country info extractor
|
|
14
14
|
email: mario1105mtz@gmail.com
|
|
@@ -17,11 +17,13 @@ extensions: []
|
|
|
17
17
|
extra_rdoc_files: []
|
|
18
18
|
files:
|
|
19
19
|
- lib/nation.rb
|
|
20
|
-
- lib/nation/
|
|
21
|
-
- lib/nation/
|
|
22
|
-
- lib/nation/
|
|
20
|
+
- lib/nation/basic_info.rb
|
|
21
|
+
- lib/nation/classes/print_file.rb
|
|
22
|
+
- lib/nation/detailed_info.rb
|
|
23
|
+
- lib/nation/modules/get_info.rb
|
|
23
24
|
- lib/nation/templates/html_file.rb
|
|
24
25
|
- lib/nation/templates/json_file.rb
|
|
26
|
+
- specs/nation_spec.rb
|
|
25
27
|
homepage: https://rubygems.org/gems/nation
|
|
26
28
|
licenses:
|
|
27
29
|
- MIT
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
require 'net/http'
|
|
2
|
-
require 'json'
|
|
3
|
-
|
|
4
|
-
# API Documentation: https://restcountries.eu/#sources
|
|
5
|
-
class Nation::NationGeneral
|
|
6
|
-
def initialize(nation_name)
|
|
7
|
-
@nation_name = " "
|
|
8
|
-
case nation_name
|
|
9
|
-
when "United_States", "United_States_Of_America"
|
|
10
|
-
@nation_name = "USA"
|
|
11
|
-
when "United_Kingdom"
|
|
12
|
-
@nation_name = "UK"
|
|
13
|
-
else
|
|
14
|
-
@nation_name = nation_name
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def get_raw_data
|
|
19
|
-
uri = URI.parse("https://restcountries.eu/rest/v2/name/#{@nation_name}?fullText=true")
|
|
20
|
-
raw_data = Net::HTTP.get(uri)
|
|
21
|
-
|
|
22
|
-
extract_info(raw_data)
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def extract_info(raw_data)
|
|
26
|
-
json_info = JSON.parse(raw_data)
|
|
27
|
-
to_hash = Hash[*json_info]
|
|
28
|
-
to_hash.length
|
|
29
|
-
|
|
30
|
-
raise "Country not avaliable" if to_hash.length == 1
|
|
31
|
-
|
|
32
|
-
languages = to_hash["languages"].map{|k, v| k["name"]}
|
|
33
|
-
# Structure of array [country_name, country_capital, country_continent, country_region,
|
|
34
|
-
# country_population, country_languages]
|
|
35
|
-
extracted_info = [to_hash["name"], to_hash["capital"], to_hash["region"], to_hash["subregion"], to_hash["population"], languages]
|
|
36
|
-
|
|
37
|
-
extracted_info
|
|
38
|
-
end
|
|
39
|
-
end
|
data/lib/nation/nation_images.rb
DELETED
|
File without changes
|
data/lib/nation/nation_info.rb
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
require 'net/http'
|
|
2
|
-
require 'json'
|
|
3
|
-
|
|
4
|
-
# API Documentation: https://www.mediawiki.org/wiki/API:Main_page
|
|
5
|
-
class Nation::NationInfo
|
|
6
|
-
def initialize(nation_name)
|
|
7
|
-
@nation_name = nation_name
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def get_raw_data
|
|
11
|
-
uri = URI.parse("https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&titles=#{@nation_name}&formatversion=2&exsentences=10&exlimit=1&explaintext=1")
|
|
12
|
-
raw_data = JSON.parse(Net::HTTP.get(uri))
|
|
13
|
-
|
|
14
|
-
extract_info(raw_data)
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def extract_info(raw_data)
|
|
18
|
-
hash_data = Hash[*raw_data]
|
|
19
|
-
|
|
20
|
-
# raise "Country not avaliable" if raise_error_field[0]["fromencoded"] == false
|
|
21
|
-
|
|
22
|
-
weird_hash = hash_data[["batchcomplete", true]]
|
|
23
|
-
weird_hash = weird_hash[1]["pages"] || weird_hash[0]["pages"]
|
|
24
|
-
weird_hash = Hash[*weird_hash]
|
|
25
|
-
|
|
26
|
-
country_info = weird_hash["extract"]
|
|
27
|
-
|
|
28
|
-
country_info
|
|
29
|
-
end
|
|
30
|
-
end
|