npr_cli_news_reader 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da71f6df65e63af78bf564427c36a4c603061489a952ac62ef2246681314a0da
4
- data.tar.gz: 424b403cfb52c5565c9b5dc21df9a4ae6a4a68c927d4f1c6e1d767543f2a97b5
3
+ metadata.gz: a6bc48b3aa6d5410a0a6bd8ceab34c05615646322a0e7d3e63b2871d9ba44d9f
4
+ data.tar.gz: caa3b3ca9ae015d245156380ae3eb7c01086b08eedb2fae0808590db80e1299e
5
5
  SHA512:
6
- metadata.gz: e2affc610e2be4c98577ab38872f7c3185e87268202d60fdc72d01bb7df46135c690dd64173487e7f7e53341eeadfd7df60d3439a7bbe137c2cc8c3e3c199d35
7
- data.tar.gz: 249f6bccc02c1e7a03ed522f07ee6758e5db7a05a899e89b3806788f0d7886796f3146cd1174d195eb419c73b6eab358764d4ca14cc498a434d9494488382810
6
+ metadata.gz: 24e3a943d90ad23e31097b7764b73b20731db83bc987871ac4711788b65458d4d16b54dc177f73cbcbcd57321b7f8873a906dbcb9ced69e05e06b6fa23a9e1cf
7
+ data.tar.gz: 57fa73b4db9f2990b6ae13d2cd204e15af1b94799f0b52e602f7bde9c34a1790bfacf12f64225290a3aeb8145796795e16aad48d9f05156ef3b4b069e5132976
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- npr_cli_news_reader (0.1.6)
4
+ npr_cli_news_reader (0.2.0)
5
5
  nokogiri
6
6
  rainbow
7
7
 
data/README.md CHANGED
@@ -23,6 +23,7 @@ Or install it yourself as:
23
23
  In Terminal enter once installed:
24
24
 
25
25
  irb
26
+ > require 'npr_cli_news_reader'
26
27
  > NprCliNewsReader::Run.start
27
28
 
28
29
  This command will start up the CLI application
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "../lib/npr_cli_news_reader"
4
4
 
5
+ NprCliNewsReader::CLI.new.call
5
6
 
6
7
 
7
8
 
@@ -1,11 +1,12 @@
1
- require_relative './npr_cli_news_reader/version'
2
- require_relative './npr_cli_news_reader/article'
3
- require_relative './npr_cli_news_reader/cli'
4
- require_relative './npr_cli_news_reader/run'
5
- require_relative'./npr_cli_news_reader/scraper'
6
1
  require 'open-uri'
7
2
  require 'nokogiri'
8
3
  require 'rainbow'
4
+ require 'pry'
5
+ require_relative './npr_cli_news_reader/version'
6
+ require_relative './npr_cli_news_reader/article'
7
+ require_relative'./npr_cli_news_reader/scraper'
8
+ require_relative './npr_cli_news_reader/cli'
9
+
9
10
 
10
11
 
11
12
  module NprCliNewsReader
@@ -1,6 +1,6 @@
1
1
  class NprCliNewsReader::Article
2
2
 
3
- attr_accessor :category, :title, :short_description, :full_article, :article_url
3
+ attr_accessor :category, :title, :teaser, :full_article, :article_url, :author, :date
4
4
 
5
5
  @@all = []
6
6
 
@@ -13,22 +13,12 @@ class NprCliNewsReader::Article
13
13
  @@all
14
14
  end
15
15
 
16
- def self.find_by_category(filter)
17
- self.all.detect {|article| article.category == filter}
16
+ def add_full_article(full_article)
17
+ self.full_article = full_article
18
18
  end
19
19
 
20
- def self.find_or_create_articles(filter)
21
- if self.find_by_category(filter) == nil
22
- NprCliNewsReader::Scraper.get_articles("/sections/#{filter}")
23
- else
24
- self.sort_articles(filter)
25
- end
20
+ def self.sort_articles_by_category(filter)
21
+ self.all.select {|article| article.category == filter.downcase}
26
22
  end
27
23
 
28
- def self.sort_articles(filter)
29
- self.all.select {|article| article.category == filter}
30
- end
31
-
32
-
33
-
34
24
  end
@@ -1,25 +1,35 @@
1
1
  class NprCliNewsReader::CLI
2
2
 
3
- attr_accessor :done, :selected_category
4
-
5
- @sorted_articles = []
3
+ attr_accessor :selected_category
6
4
 
7
5
  def initialize
8
6
  @categories = ["National", "World", "Politics", "Business", "Health", "Science", "Technology", "Race & Culture"]
9
- @done = false
10
7
  end
11
8
 
12
- def greeting
13
- puts
14
- puts "--------Hello fellow fact finder!--------"
15
- puts
16
- puts "---Welcome to the NPR CLI News Reader----"
17
- puts
9
+ def call
10
+ greet_user
11
+ present_categories
12
+ handle_category_input
13
+ handle_article_selection
14
+ end
15
+
16
+ def greet_user
17
+ puts Rainbow("Hello fellow fact finder...").bright
18
+ puts Rainbow("Welcome to the Npr Cli News Reader.").bright.fg(:green)
19
+ puts "--------------------"
20
+ sleep(1.5)
18
21
  end
19
22
 
20
- def display_article_categories
21
- puts "----Article Categories----"
22
- @categories.each_with_index {|category, i| puts "#{i + 1}.) #{Rainbow(category).bright}"}
23
+ def present_categories
24
+ puts Rainbow("Categories").bright
25
+ puts "--------------------"
26
+ @categories.each_with_index do |category, i|
27
+ if i.even?
28
+ puts Rainbow("#{i + 1}). #{category}").bright
29
+ else
30
+ puts Rainbow("#{i + 1}). #{category}").bright.fg(:green)
31
+ end
32
+ end
23
33
  end
24
34
 
25
35
  def valid_input?(user_input, collection)
@@ -30,55 +40,75 @@ class NprCliNewsReader::CLI
30
40
  end
31
41
  end
32
42
 
33
- def handle_category_input(user_input)
34
- if valid_input?(user_input, @categories)
35
- categories_index = user_input.to_i - 1
36
- @selected_category = @categories[categories_index].downcase
37
- NprCliNewsReader::Article.find_or_create_articles(@selected_category)
38
- @sorted_articles = NprCliNewsReader::Article.sort_articles(@selected_category)
39
- display_articles
40
- end
43
+ def format_teaser(teaser_string)
44
+ formatted_teaser = teaser_string.split(/\A.+[\u0095]\s/)[1]
45
+ formatted_teaser
41
46
  end
42
47
 
43
- def display_articles
44
- puts "----- #{Rainbow(@sorted_articles.first.category.capitalize).bright} Category -----"
45
- @sorted_articles.each_with_index do |article, i|
46
- sleep(0.5)
47
- puts "#{i + 1}). #{Rainbow(article.title).bright}"
48
- puts "#{article.short_description}"
49
- puts "-------------------"
48
+ def handle_category_input
49
+ puts Rainbow("Which Category would you like to see articles for?").bright
50
+ puts "Use the numbers associated with the categories."
51
+ category_input = gets.strip
52
+ while !valid_input?(category_input, @categories)
53
+ puts "Sorry, the input received is not valid to select a category"
54
+ puts "Please enter a number 1-#{@categories.size}:"
55
+ category_input = gets.strip
50
56
  end
57
+ @selected_category = @categories[category_input.to_i - 1].downcase
58
+ puts Rainbow("#{@selected_category.capitalize} Category").bright
59
+ puts "--------------------"
60
+ NprCliNewsReader::Scraper.scrape_articles_for_category(@selected_category)
61
+ display_category_articles
51
62
  end
52
63
 
53
- def handle_article_input(user_input)
54
- if valid_input?(user_input, @sorted_articles)
55
- article_index = user_input.to_i - 1
56
- selected_article = @sorted_articles[article_index]
57
- puts 'Loading...'
58
- full_article = NprCliNewsReader::Scraper.get_full_article(selected_article.article_url)
59
- selected_article.full_article = full_article
60
- sleep(2)
61
- display_selected_article(selected_article)
64
+ def display_category_articles
65
+ NprCliNewsReader::Article.sort_articles_by_category(@selected_category).each_with_index do |article, i|
66
+ formatted_teaser = format_teaser(article.teaser)
67
+ if i % 2 == 0
68
+ puts Rainbow("#{i + 1}").bright + ")." + Rainbow(" #{article.title}").bright
69
+ puts
70
+ puts Rainbow("#{formatted_teaser}")
71
+ puts
72
+ puts "--------------------"
73
+ puts
74
+ else
75
+ puts Rainbow("#{i + 1}). #{article.title}").bright.fg(:green)
76
+ puts
77
+ puts Rainbow("#{formatted_teaser}").fg(:green)
78
+ puts
79
+ puts "--------------------"
80
+ puts
81
+ end
62
82
  end
63
83
  end
64
84
 
65
- def display_selected_article(article)
66
- puts '-------------------'
67
- puts Rainbow(article.title).bright
68
- article.full_article.each { |text| puts "#{text}\n"}
69
- puts '(END)'
85
+ def handle_article_selection
86
+ sorted_articles = NprCliNewsReader::Article.sort_articles_by_category(@selected_category)
87
+ puts Rainbow("Which article would you like to see the full article for?").bright
88
+ puts "Use the numbers associated with the articles."
89
+ article_input = gets.strip
90
+ while !valid_input?(article_input, sorted_articles)
91
+ puts "Sorry, the input received is not valid to select an article"
92
+ puts "Please enter a number 1-#{sorted_articles.size}:"
93
+ article_input = gets.strip
94
+ end
95
+ article = sorted_articles[article_input.to_i - 1]
96
+ NprCliNewsReader::Scraper.scrape_full_article(article)
97
+ display_full_article(article)
70
98
  end
71
99
 
72
- def restart_or_exit(user_input)
73
- if user_input == 'home'
74
- puts "Redisplaying categories"
75
- sleep(1)
76
- elsif user_input == 'exit'
77
- puts "Thanks for using NPR CLI News Reader."
78
- puts "Goodbye!"
79
- @done = true
100
+ def display_full_article(article)
101
+ puts Rainbow("#{article.title}").bright
102
+ puts "--------------------"
103
+ article.full_article.each do |paragraph_node|
104
+ puts "\t#{paragraph_node.text}"
105
+ puts "--------------------"
106
+ end
107
+ puts Rainbow("Would you like to see the article on your default web browse? (y/n):").bright
108
+ user_input = gets.strip
109
+ if ["yes, y"].include?(user_input.downcase)
110
+ system("open #{article.article_url}")
80
111
  end
81
112
  end
82
113
 
83
-
84
114
  end
@@ -1,35 +1,33 @@
1
1
  class NprCliNewsReader::Scraper
2
2
 
3
- # Return an array of articles, all attributes set except for full_article
4
- def self.get_articles(category_url)
5
- html = open("https://www.npr.org#{category_url}")
6
- doc = Nokogiri::HTML(html)
7
- category = category_url.split("/")[2]
8
- articles = doc.css("article.item")
9
- self.get_and_set_article_attributes(articles, category)
10
- end
3
+ @@base_url = "https://www.npr.org"
11
4
 
12
- #loop over article_nodes grabbing the relevant data for the Article attributes
13
- def self.get_and_set_article_attributes(article_nodes, category)
14
- article_nodes.each do |article|
15
- anchor_tag = article.css('.teaser a')
16
- article_attributes = {
17
- category: category,
18
- title: article.css('.title').text,
19
- short_description: article.css('.teaser').text,
20
- article_url: anchor_tag.attribute('href').to_s
21
- }
22
- NprCliNewsReader::Article.new(article_attributes)
5
+ def self.scrape_articles_for_category(selected_category)
6
+ if NprCliNewsReader::Article.all.detect {|article| article.category == selected_category}
7
+ return
8
+ else
9
+ html = open("#{@@base_url}/sections/#{selected_category}")
10
+ doc = Nokogiri::HTML(html)
11
+ articles = doc.css('article.item')
12
+ articles.each do |article|
13
+ article_attributes = {
14
+ category: selected_category.downcase,
15
+ title: article.css('h2.title a').text.strip,
16
+ teaser: article.css('p.teaser a').text.strip,
17
+ article_url: article.css('h2.title a').attr('href').to_s.strip
18
+ }
19
+ NprCliNewsReader::Article.new(article_attributes)
20
+ end
23
21
  end
22
+
24
23
  end
25
24
 
26
- def self.get_full_article(article_url)
27
- html = open(article_url)
25
+ def self.scrape_full_article(article)
26
+ html = open (article.article_url)
28
27
  doc = Nokogiri::HTML(html)
29
- full_article_container = doc.css('#storytext')
30
- content = full_article_container.css('p')
31
- text_array = content.map {|p_element| p_element.text.strip}
32
- text_array
28
+ # discard the first p tag
29
+ paragraphs = doc.css('#storytext > p')
30
+ article.full_article = paragraphs
33
31
  end
34
32
 
35
33
  end
@@ -1,3 +1,3 @@
1
1
  module NprCliNewsReader
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/to_do_notes.md ADDED
@@ -0,0 +1,21 @@
1
+ TO DO NOTES
2
+
3
+ - Once the article is read, give the user options
4
+ - exit the program
5
+ - go back to article selection
6
+ - go back to category selection
7
+
8
+ - Another feature could be to add a clickable link at the bottom of
9
+ the article that states if clicked it will take them to the article
10
+ on the NPR website.
11
+
12
+ - Complete documentation for github
13
+ - description
14
+ - collaboration guidelines
15
+
16
+ - Complete documentation and setup as Ruby gem on rubygems.org
17
+ - possibly use a tool for documentation
18
+ - description
19
+ - usage
20
+ - installation process
21
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: npr_cli_news_reader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "'Jake Garcia'"
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-24 00:00:00.000000000 Z
11
+ date: 2020-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -104,10 +104,10 @@ files:
104
104
  - lib/npr_cli_news_reader.rb
105
105
  - lib/npr_cli_news_reader/article.rb
106
106
  - lib/npr_cli_news_reader/cli.rb
107
- - lib/npr_cli_news_reader/run.rb
108
107
  - lib/npr_cli_news_reader/scraper.rb
109
108
  - lib/npr_cli_news_reader/version.rb
110
109
  - npr_cli_news_reader.gemspec
110
+ - to_do_notes.md
111
111
  homepage: https://github.com/jgarcia4444/npr_cli_news_reader.git
112
112
  licenses:
113
113
  - MIT
@@ -1,21 +0,0 @@
1
- class NprCliNewsReader::Run
2
- def self.start
3
- cli = NprCliNewsReader::CLI.new
4
- while !cli.done
5
- cli.greeting
6
- cli.display_article_categories
7
- puts "Which category would you like to see news articles for?"
8
- puts "(Enter the # associated with the category)"
9
- category_input = gets.strip
10
- cli.handle_category_input(category_input)
11
- puts "Which article would you like to read?"
12
- puts "(Enter the number associated with the article):"
13
- selected_article = gets.strip
14
- cli.handle_article_input(selected_article)
15
- puts "Would you like to go back to the category selection or exit the program?"
16
- puts "(enter home or exit)"
17
- end_of_program_input = gets.strip.downcase
18
- cli.restart_or_exit(end_of_program_input)
19
- end
20
- end
21
- end