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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 919a6b7114c62d61e9f362d93f07b87ba0c2e5aa7fed0633fea60f4b73890e0f
4
- data.tar.gz: c4305c45e0294a96db4c58d31392dcb246f7e5d4853a91e765cd8ea3d33c86ff
3
+ metadata.gz: 20d614ce29d37ec18dc41ff56188d925cbff4d10dc41969c1c958b825cb3e550
4
+ data.tar.gz: d317a145851f9d0d131e0f9782506c499f9dc11df14b21e70979e925a7f715c7
5
5
  SHA512:
6
- metadata.gz: 459bcccdf3277af98693970595d38a79fdf4987bfd81a03680b19dc2f2a9cf27081bf01e4b446df9f3b4dabe3ab5194400b0f23d8057769ed89f601876c17a3a
7
- data.tar.gz: 34fbf6d7708f52f2fa061087699513a52a0d3720d644e72e58772dfa790a064d34f667ca34315ae4808c6f8cbef92c35e6de5a1a2d94ea5df37e48d5619b44f8
6
+ metadata.gz: 625c600081c285a356480bdfd39cc531543f538156af18a413fd021630ba4a5b5084530c32ca136209dab07e1c37fc967258cf5382a192f2bd5b0a9295392c7f
7
+ data.tar.gz: f1ec14a71e3500f247552fdea40c325b9b418d39d8b8fcb389a29f71bd039ba342186e78975a9ed9d55aa94989275eda284eb6c6b9310e679366f49e55d722fa
@@ -1,33 +1,33 @@
1
- class Nation
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
- def initialize
4
- @stored_info = {}
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 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
- @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 info_to_json
22
- JsonFile.convert_json_file(@stored_info)
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
- def info_to_html
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
- require './nation/nation_general'
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
@@ -0,0 +1,11 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module GetInfo
5
+ private
6
+
7
+ def get_api_info(url)
8
+ uri = URI.parse(url)
9
+ JSON.parse(Net::HTTP.get(uri))
10
+ end
11
+ end
@@ -1,9 +1,9 @@
1
- class Nation::HtmlFile
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 Nation::JsonFile
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.0.5
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-05 00:00:00.000000000 Z
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/nation_general.rb
21
- - lib/nation/nation_images.rb
22
- - lib/nation/nation_info.rb
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
File without changes
@@ -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