nation 0.0.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 +7 -0
- data/lib/nation/nation_general.rb +39 -0
- data/lib/nation/nation_images.rb +0 -0
- data/lib/nation/nation_info.rb +30 -0
- data/lib/nation/templates/html_file.rb +30 -0
- data/lib/nation/templates/json_file.rb +14 -0
- data/lib/nation.rb +31 -0
- metadata +48 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: fb8ab3bed94178ab765de84fde972c0179cd4f8cd22b76043df82d44328d6b66
|
|
4
|
+
data.tar.gz: 391976ef021023ecf6361a893ca07f6f0bbf654f11a1c9b1088c2b398142982d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3f0fb3fc88da3e455caa245ef1fafd79fc1269fa9950665ce604d10b95912cc406fc0f6edfcf94c570c02aa864e8d20a09c4b65bb1bb288aec11b554157048df
|
|
7
|
+
data.tar.gz: ea243788c10b7620a983d46ed00b92e92c0586c83ae3db92e8e0468b79444c2cd0e0792902ce5e45eba287c722ca263d60c0da60bc330314df4f2201cfa4de99
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
class Nation::HtmlFile
|
|
2
|
+
def self.convert_html_file(json_object)
|
|
3
|
+
path = "./nation_files/html"
|
|
4
|
+
file_name = "#{json_object[:name]}_info_#{Time.now.strftime("%d%m%Y%H%M")}"
|
|
5
|
+
|
|
6
|
+
Dir.mkdir(path) if Dir.exist?(path) == false
|
|
7
|
+
|
|
8
|
+
html = File.new("#{path}/#{file_name}.html", "w+")
|
|
9
|
+
|
|
10
|
+
html.puts "<!DOCTYPE html>"
|
|
11
|
+
html.puts "<html>"
|
|
12
|
+
html.puts "<head>"
|
|
13
|
+
html.puts "<title>#{file_name}</title>"
|
|
14
|
+
html.puts "</head>"
|
|
15
|
+
|
|
16
|
+
html.puts "<body>"
|
|
17
|
+
json_object.each do |k, v|
|
|
18
|
+
if k == "name"
|
|
19
|
+
html.puts "<h1>#{v.upcase}</h1"
|
|
20
|
+
else
|
|
21
|
+
html.puts "<div><h3>#{k}</h3> <p>#{v}</p></div>"
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
html.puts "</body>"
|
|
26
|
+
html.puts "</html>"
|
|
27
|
+
|
|
28
|
+
html.close()
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
class Nation::JsonFile
|
|
4
|
+
def self.convert_json_file(json_object)
|
|
5
|
+
|
|
6
|
+
path = "./nation_files/json"
|
|
7
|
+
file_name = "#{json_object[:name]}_info_#{Time.now.strftime("%d%m%Y%H%M")}.json"
|
|
8
|
+
Dir.mkdir(path) if Dir.exist?(path) == false
|
|
9
|
+
|
|
10
|
+
File.open("#{path}/#{file_name}", "w") do |f|
|
|
11
|
+
f.write(json_object.to_json)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
data/lib/nation.rb
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
class Nation
|
|
2
|
+
|
|
3
|
+
def initialize
|
|
4
|
+
@stored_info = {}
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def nation_info(nation_name)
|
|
8
|
+
nation_name = nation_name.downcase.split.map(&:capitalize).join("_")
|
|
9
|
+
general = NationGeneral.new(nation_name)
|
|
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
|
+
end
|
|
18
|
+
|
|
19
|
+
def info_to_json
|
|
20
|
+
JsonFile.convert_json_file(@stored_info)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def info_to_html
|
|
24
|
+
HtmlFile.convert_html_file(@stored_info)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
require './nation/nation_general'
|
|
29
|
+
require './nation/nation_info'
|
|
30
|
+
require './nation/templates/json_file'
|
|
31
|
+
require './nation/templates/html_file'
|
metadata
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: nation
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Mario Martinez
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-02-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: A nation info extractor
|
|
14
|
+
email: mario1105mtz@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/nation.rb
|
|
20
|
+
- lib/nation/nation_general.rb
|
|
21
|
+
- lib/nation/nation_images.rb
|
|
22
|
+
- lib/nation/nation_info.rb
|
|
23
|
+
- lib/nation/templates/html_file.rb
|
|
24
|
+
- lib/nation/templates/json_file.rb
|
|
25
|
+
homepage: https://rubygems.org/gems/nation
|
|
26
|
+
licenses:
|
|
27
|
+
- MIT
|
|
28
|
+
metadata: {}
|
|
29
|
+
post_install_message:
|
|
30
|
+
rdoc_options: []
|
|
31
|
+
require_paths:
|
|
32
|
+
- lib
|
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: '0'
|
|
43
|
+
requirements: []
|
|
44
|
+
rubygems_version: 3.1.2
|
|
45
|
+
signing_key:
|
|
46
|
+
specification_version: 4
|
|
47
|
+
summary: Nation
|
|
48
|
+
test_files: []
|