good-news 0.1.0 → 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/config/environment.rb +10 -0
- data/lib/good_news.rb +6 -0
- data/lib/good_news/article.rb +4 -0
- data/lib/good_news/cli.rb +80 -0
- data/lib/good_news/scraper.rb +40 -0
- data/lib/good_news/topic.rb +21 -0
- data/lib/good_news/version.rb +3 -0
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a86691c8160be1fcc315000c2393115fc3530f0c2e0c42eefb94c35d3fe49465
|
4
|
+
data.tar.gz: 2b691d2fffb73ced1d2f99db11d3b76b86dc822726c87072e1917f016c3bb72b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 544480be950358c4ac71fe32c9ad47899b2f998a09b96e68966dd61bbf3390b79c95fb5742e6e9031333f24f53399366dac0b6833187acd1bc2f02f856f21310
|
7
|
+
data.tar.gz: a0ac6693200aa675ca6b5a07a760126665440daf3b2c4ce0da787769fb1ab202d6de9fb7a88a0365a1c0bacee1ca5c9363e43068e09d95de906034c3c8f74596
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'pry'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'launchy'
|
5
|
+
|
6
|
+
require_relative '../lib/good_news/scraper'
|
7
|
+
require_relative '../lib/good_news/topic'
|
8
|
+
require_relative '../lib/good_news/article'
|
9
|
+
require_relative '../lib/good_news/cli'
|
10
|
+
require_relative '../lib/good_news/version'
|
data/lib/good_news.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
class GoodNews::Cli
|
2
|
+
|
3
|
+
#Class method to begin interaction with user.
|
4
|
+
#Gives a choice of topics or exit.
|
5
|
+
#Calls on #display_topics.
|
6
|
+
def self.call_user
|
7
|
+
user_input = nil
|
8
|
+
while user_input != "exit"
|
9
|
+
puts "Welcome, it looks like you are looking for some Good News!"
|
10
|
+
puts "To see Good News topics, type 'topics'."
|
11
|
+
puts "To quit, type 'exit'."
|
12
|
+
puts "Please enter your choice now."
|
13
|
+
|
14
|
+
user_input = gets.downcase.chomp
|
15
|
+
|
16
|
+
case user_input
|
17
|
+
when 'topics'
|
18
|
+
self.display_topics
|
19
|
+
when 'exit'
|
20
|
+
self.good_bye
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
#Class method to loop through topic objects and display names.
|
26
|
+
#Offers user choice of picking a topic to display articles in that topic or exit.
|
27
|
+
#Calls on #display_articles.
|
28
|
+
def self.display_topics
|
29
|
+
user_input = nil
|
30
|
+
counter = 1
|
31
|
+
GoodNews::Topic.all.each do |topic|
|
32
|
+
puts "#{counter}. #{topic.name}"
|
33
|
+
counter += 1
|
34
|
+
end
|
35
|
+
while user_input != "exit"
|
36
|
+
puts "Please enter the number of the topic to see a list of articles."
|
37
|
+
puts "Or type 'exit' to quit."
|
38
|
+
|
39
|
+
user_input = gets.chomp
|
40
|
+
|
41
|
+
if (1..GoodNews::Topic.all.length).include?(user_input.to_i)
|
42
|
+
self.display_articles(user_input)
|
43
|
+
elsif user_input == "exit"
|
44
|
+
self.good_bye
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
#Class method that loops through a topic objects articles attribute, an array of article objects.
|
50
|
+
#Offers user a choice of article to pick or to exit.
|
51
|
+
#Launches article in browser.
|
52
|
+
def self.display_articles(topic_input)
|
53
|
+
user_input = nil
|
54
|
+
topic_articles = GoodNews::Topic.all[topic_input.to_i - 1].articles
|
55
|
+
counter = 1
|
56
|
+
topic_articles.each do |article|
|
57
|
+
puts "#{counter}. #{article.title}"
|
58
|
+
counter += 1
|
59
|
+
end
|
60
|
+
while user_input != "exit"
|
61
|
+
puts "Please enter the number of the article to be taken to its page."
|
62
|
+
puts "Or type 'exit' to quit."
|
63
|
+
|
64
|
+
user_input = gets.chomp
|
65
|
+
|
66
|
+
if (1..topic_articles.length).include?(user_input.to_i)
|
67
|
+
puts "Hold onto your seat, we are sending you to some Good News!"
|
68
|
+
Launchy.open(topic_articles[user_input.to_i - 1].web_addr)
|
69
|
+
elsif user_input == "exit"
|
70
|
+
self.good_bye
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
#Class method to abstract away the exit redundancy.
|
76
|
+
def self.good_bye
|
77
|
+
puts "See you next time!"
|
78
|
+
exit
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class GoodNews::Scraper
|
2
|
+
# A constant to store the homepage.
|
3
|
+
HOMEPAGEURL = "https://www.goodnewsnetwork.org/category/news/"
|
4
|
+
|
5
|
+
#Uses open-uri and nokogiri to grab and parse the HTML.
|
6
|
+
#Returns the parsed page in a array which sets it up for a search using CSS selectors.
|
7
|
+
def self.get_page(url)
|
8
|
+
return Nokogiri::HTML(open(url))
|
9
|
+
end
|
10
|
+
|
11
|
+
#This method grabs Topics and stores them. Uses Class method #get_page and saves to doc.
|
12
|
+
#Instantiates a Topic object and stores the topic name and web address in the Topic object.
|
13
|
+
#Saves each Topic object in the Topic Class variable @@all using the #save method.
|
14
|
+
def self.get_topics
|
15
|
+
doc = self.get_page(HOMEPAGEURL)
|
16
|
+
doc.css("ul.td-category a").each do |topic|
|
17
|
+
new_topic = GoodNews::Topic.new
|
18
|
+
new_topic.name = topic.text
|
19
|
+
new_topic.web_addr = topic.attribute("href").value
|
20
|
+
new_topic.save
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
#This method is used to get and store each topic's articles.
|
25
|
+
#Calls Topic's @@all Class variable array to loop through Topic objects.
|
26
|
+
#Instantiates new Article object. Saves article's web address and title to Article object.
|
27
|
+
#Pushes Article object into the Topic object's articles attribute(an array).
|
28
|
+
def self.get_articles
|
29
|
+
GoodNews::Topic.all.each do |topic|
|
30
|
+
doc = self.get_page(topic.web_addr)
|
31
|
+
doc.css("h3.entry-title a").each do |info|
|
32
|
+
new_article = GoodNews::Article.new
|
33
|
+
new_article.web_addr = info.attribute("href").value
|
34
|
+
new_article.title = info.text
|
35
|
+
topic.articles.push(new_article)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class GoodNews::Topic
|
2
|
+
attr_accessor :name, :articles, :web_addr
|
3
|
+
|
4
|
+
#Class variable stores all topics objects.
|
5
|
+
@@all = []
|
6
|
+
|
7
|
+
#Instantiates with an article attribute to store all articles of the topic.
|
8
|
+
def initialize
|
9
|
+
@articles = []
|
10
|
+
end
|
11
|
+
|
12
|
+
#Class method to get all topics.
|
13
|
+
def self.all
|
14
|
+
@@all
|
15
|
+
end
|
16
|
+
|
17
|
+
#Method to save topic object into @@all.
|
18
|
+
def save
|
19
|
+
@@all << self
|
20
|
+
end
|
21
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: good-news
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tony Baker
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05-
|
11
|
+
date: 2019-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -88,6 +88,13 @@ extensions: []
|
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
90
|
- bin/good-news
|
91
|
+
- config/environment.rb
|
92
|
+
- lib/good_news.rb
|
93
|
+
- lib/good_news/article.rb
|
94
|
+
- lib/good_news/cli.rb
|
95
|
+
- lib/good_news/scraper.rb
|
96
|
+
- lib/good_news/topic.rb
|
97
|
+
- lib/good_news/version.rb
|
91
98
|
homepage: http://rubygems.org/gems/good-news
|
92
99
|
licenses:
|
93
100
|
- MIT
|