news_reader_cli 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +3 -3
- data/bin/news_reader_cli +3 -10
- data/lib/news_reader_cli.rb +0 -4
- data/lib/news_reader_cli/api_key_validate.rb +12 -27
- data/lib/news_reader_cli/api_service.rb +3 -2
- data/lib/news_reader_cli/article.rb +5 -30
- data/lib/news_reader_cli/cli.rb +14 -66
- data/lib/news_reader_cli/version.rb +1 -1
- metadata +2 -3
- data/lib/news_reader_cli/api_key_input_validate.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: 1ed81ae14dd7eea58d2bf48038ed725445805b665c912220b979a4178c5596c9
|
4
|
+
data.tar.gz: 00c22455934a4258b613511cbbd36032fc2edb2c2e5eb350fd37a31c8db53467
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f1a7d0f461b6910ebebd551e78f302022127462c4e6e6e07332bf5a1e00d9bf7783d46ffaf5bbe571aabe4cf05047c5b652dd6cc6d61b503ee32751def63f68
|
7
|
+
data.tar.gz: 2e42c7d7c11bf0c418ae7da8cc052dc465fde97e96bd464dd8c457e1b621b393a6a7846bcfbf5e8e9de6d4f49b3d2e289d5f05526cc487ead6b990667c6aeaef
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/news_reader_cli`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
4
|
|
5
|
-
This Ruby Gem provides a
|
5
|
+
This Ruby Gem provides a command Line interface to view live breaking headlines and contents from a variety of top US major publishers.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -39,7 +39,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
39
39
|
|
40
40
|
## Contributing
|
41
41
|
|
42
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
42
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hankeliu2015/news_reader_cli. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
43
43
|
|
44
44
|
## License
|
45
45
|
|
@@ -47,4 +47,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
47
47
|
|
48
48
|
## Code of Conduct
|
49
49
|
|
50
|
-
Everyone interacting in the NewsReaderCli project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
50
|
+
Everyone interacting in the NewsReaderCli project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hankeliu2015/news_reader_cli/blob/master/CODE_OF_CONDUCT.md).
|
data/bin/news_reader_cli
CHANGED
@@ -1,11 +1,4 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
1
|
+
#!/ usr/bin/env ruby
|
2
2
|
require_relative "../lib/news_reader_cli"
|
3
|
-
|
4
|
-
|
5
|
-
# binding.pry
|
6
|
-
|
7
|
-
# NewsReaderCli::ApiKeyValidate.new.key_validate
|
8
|
-
|
9
|
-
# binding.pry
|
10
|
-
# Dotenv.load # defaul will looking for ('.env')
|
11
|
-
NewsReaderCli::CLI.new.start
|
3
|
+
cli = NewsReaderCli::CLI.new
|
4
|
+
cli.start
|
data/lib/news_reader_cli.rb
CHANGED
@@ -5,12 +5,8 @@ require_relative "./news_reader_cli/version" #require "news_reader_cli/version"
|
|
5
5
|
require_relative "./news_reader_cli/cli"
|
6
6
|
require_relative "./news_reader_cli/api_service"
|
7
7
|
require_relative "./news_reader_cli/article"
|
8
|
-
require_relative "./news_reader_cli/api_key_input_validate"
|
9
8
|
require_relative "./news_reader_cli/api_key_validate"
|
10
9
|
|
11
|
-
# Bundler.require(*Rails.groups)
|
12
|
-
# Dotenv::Railtie.load
|
13
|
-
# HOSTNAME = ENV['HOSTNAME']
|
14
10
|
|
15
11
|
module NewsReaderCli
|
16
12
|
# Your code goes here...
|
@@ -1,39 +1,24 @@
|
|
1
1
|
class NewsReaderCli::ApiKeyValidate
|
2
2
|
def key_validate
|
3
|
-
|
4
|
-
# File.expand_path if return true, the file exist. if not, File.new create a file.
|
5
|
-
# if not create one. Let user input the key.
|
6
|
-
# if .env exists, load the file.
|
3
|
+
|
7
4
|
if !File.exist?(".env")
|
8
|
-
key_file = File.new(".env", "w+")
|
9
5
|
puts "please type in API Key from API News:"
|
10
|
-
|
11
|
-
# a condition verify key value is AES 128 Hex string 32 chars
|
12
|
-
|
13
6
|
key = gets.chomp.strip
|
14
|
-
#
|
15
|
-
# while !key.match(/([0-9]|\w){32}/) do
|
16
|
-
#
|
17
|
-
# break if key == "exit"
|
18
|
-
# # need to get out of the loop and remove the file .env
|
19
|
-
#
|
20
|
-
# puts <<-HEREDOC
|
21
|
-
#
|
22
|
-
# Your API keys value shows invalid.
|
23
|
-
# Please check verify your API Key and type in again.
|
24
|
-
#
|
25
|
-
# HEREDOC
|
26
|
-
#
|
27
|
-
# key = gets.chomp.strip
|
28
|
-
# end
|
29
7
|
|
8
|
+
url = 'https://newsapi.org/v2/top-headlines?'\
|
9
|
+
'country=us&'\
|
10
|
+
"apiKey=#{key}&"\
|
11
|
+
'pageSize=100'
|
12
|
+
begin
|
13
|
+
req = open(url)
|
14
|
+
rescue
|
15
|
+
puts "You API Key is not valid, please try again."
|
16
|
+
return self.key_validate
|
17
|
+
end
|
30
18
|
|
31
|
-
|
19
|
+
key_file = File.new(".env", "w+")
|
32
20
|
key_file.puts("key = #{key}")
|
33
21
|
key_file.close
|
34
|
-
|
35
|
-
#binding.pry
|
36
22
|
end
|
37
|
-
|
38
23
|
end
|
39
24
|
end
|
@@ -2,20 +2,21 @@ class NewsReaderCli::ApiService
|
|
2
2
|
|
3
3
|
def self.newsapi
|
4
4
|
#url = "https://newsapi.org/v2/top-headlines?country=us&apiKey=(put key here)&pageSize=100"
|
5
|
+
Dotenv.load
|
5
6
|
assigned_api_key = ENV['key'].strip
|
6
|
-
|
7
|
+
|
7
8
|
url = 'https://newsapi.org/v2/top-headlines?'\
|
8
9
|
'country=us&'\
|
9
10
|
"apiKey=#{assigned_api_key}&"\
|
10
11
|
'pageSize=100'
|
11
12
|
req = open(url)
|
13
|
+
|
12
14
|
response_body = req.read
|
13
15
|
json = JSON.parse(response_body)
|
14
16
|
|
15
17
|
json["articles"].each do |article|
|
16
18
|
NewsReaderCli::Article.new(article)
|
17
19
|
end
|
18
|
-
#binding.pry
|
19
20
|
|
20
21
|
end
|
21
22
|
|
@@ -1,16 +1,13 @@
|
|
1
1
|
class NewsReaderCli::Article
|
2
2
|
|
3
3
|
attr_accessor :source, :author, :title, :description, :url, :urltoimage, :publishedAt, :content
|
4
|
-
|
5
4
|
@@all = []
|
6
5
|
|
7
6
|
def initialize(article_hash)
|
8
|
-
|
9
7
|
article_hash.each do |method, article_data|
|
10
8
|
self.send("#{method}=", article_data) if self.respond_to?("#{method}=")
|
11
9
|
end
|
12
10
|
@@all << self
|
13
|
-
|
14
11
|
end
|
15
12
|
|
16
13
|
def self.all
|
@@ -19,44 +16,22 @@ class NewsReaderCli::Article
|
|
19
16
|
|
20
17
|
#list all the articlels titles (in alphabat )
|
21
18
|
def self.list_all_titles
|
22
|
-
self.all.
|
23
|
-
|
19
|
+
self.all.collect.with_index(1) do |article, index|
|
20
|
+
"#{index}. #{article.title}"
|
24
21
|
end
|
25
22
|
end
|
26
23
|
|
27
24
|
#find the article instance by article index number
|
28
25
|
def self.find_article_by_article_index(number)
|
29
|
-
self.all
|
30
|
-
index == number
|
31
|
-
end
|
32
|
-
end
|
26
|
+
self.all[number - 1]
|
33
27
|
|
34
|
-
|
35
|
-
# def self.find_content_by_article_index(number)
|
36
|
-
# self.all.find.with_index(1) do |article, index|
|
37
|
-
# index == number
|
38
|
-
# #binding.pry
|
39
|
-
# end.content
|
40
|
-
# end
|
28
|
+
end
|
41
29
|
|
42
|
-
# find an article's URL
|
30
|
+
# find an article's URL by using iterations
|
43
31
|
def self.find_url_by_article_index(number)
|
44
32
|
self.all.find.with_index(1) do |article, index|
|
45
33
|
index == number
|
46
34
|
end.url
|
47
35
|
end
|
48
36
|
|
49
|
-
# def self.find_source_name_by_article_index(number)
|
50
|
-
# self.all.find.with_index(1) do |article, index|
|
51
|
-
# index == number
|
52
|
-
# end.source["name"]
|
53
|
-
# end
|
54
|
-
|
55
|
-
# def self.find_pub_date_by_article_index(number)
|
56
|
-
# self.all.find.with_index(1) do |article, index|
|
57
|
-
# index == number
|
58
|
-
# end.publishedAt
|
59
|
-
# end
|
60
|
-
|
61
|
-
|
62
37
|
end #end of class
|
data/lib/news_reader_cli/cli.rb
CHANGED
@@ -2,57 +2,22 @@
|
|
2
2
|
class NewsReaderCli::CLI
|
3
3
|
|
4
4
|
def start
|
5
|
-
|
6
|
-
|
7
|
-
# key_instance = NewsReaderCli::ApiKeyInputValidate.new
|
8
|
-
# key_instance.key_input_validate
|
9
|
-
|
10
5
|
NewsReaderCli::ApiKeyValidate.new.key_validate
|
11
|
-
Dotenv.load
|
12
|
-
#
|
13
|
-
# if !File.exist?(".env")
|
14
|
-
# key_file = File.new(".env", "w+")
|
15
|
-
# key = key_instance.validated_key
|
16
|
-
# key_file.puts("key = #{key}")
|
17
|
-
# key_file.close
|
18
|
-
|
19
|
-
# end
|
20
|
-
|
21
|
-
#compare input key with env variable
|
22
|
-
#if they are equal, keep going. if not change the value.
|
23
|
-
|
24
|
-
# if key_instance.validated_key != ENV['key']#.strip
|
25
|
-
# key = key_instance.validated_key
|
26
|
-
# key_file.puts("key = #{key}")
|
27
|
-
# key_file.close
|
28
|
-
#
|
29
|
-
# end
|
30
|
-
|
31
|
-
# assigned_api_key = ENV['key'].strip
|
32
|
-
#
|
33
|
-
# if assigned_api_key == 'exit'
|
34
|
-
# puts "Goodbye"
|
35
|
-
# return
|
36
|
-
# end
|
37
|
-
|
38
6
|
NewsReaderCli::ApiService.newsapi
|
39
7
|
puts "Welcome to the News Reader CLI!!!"
|
40
8
|
menu
|
41
9
|
goodbye
|
42
|
-
|
43
10
|
end
|
44
11
|
|
45
12
|
def menu
|
46
|
-
|
47
13
|
help_menu
|
48
|
-
|
49
14
|
input = ""
|
50
15
|
|
51
16
|
while input != "exit" do
|
52
17
|
input = gets.chomp.strip
|
53
18
|
|
54
19
|
if input == "list"
|
55
|
-
NewsReaderCli::Article.list_all_titles
|
20
|
+
puts NewsReaderCli::Article.list_all_titles
|
56
21
|
|
57
22
|
elsif input.slice(0) == "a" && (1..(NewsReaderCli::Article.all.length)).include?(input.gsub(/[a]/, '').to_i)
|
58
23
|
found_article_instance = NewsReaderCli::Article.find_article_by_article_index(input.gsub(/[a]/, '').to_i)
|
@@ -64,6 +29,14 @@ class NewsReaderCli::CLI
|
|
64
29
|
puts "Published Date: #{found_article_instance.publishedAt}"
|
65
30
|
puts "Publisher: #{found_article_instance.source["name"]}"
|
66
31
|
puts "Article URL: #{found_article_instance.url}"
|
32
|
+
puts "To open article's URL, type 'y'. Any key return to user menu}"
|
33
|
+
|
34
|
+
input_for_url = gets.chomp.strip
|
35
|
+
if input_for_url == "y"
|
36
|
+
found_url = NewsReaderCli::Article.find_url_by_article_index(input.gsub(/[a]/, '').to_i) #[a] instead of [u]
|
37
|
+
puts "Please check your browser, your article is opening now."
|
38
|
+
system("open", "#{found_url}")
|
39
|
+
end
|
67
40
|
|
68
41
|
elsif input.slice(0) == "u" && (1..(NewsReaderCli::Article.all.length)).include?(input.gsub(/[u]/, '').to_i)
|
69
42
|
found_url = NewsReaderCli::Article.find_url_by_article_index(input.gsub(/[u]/, '').to_i)
|
@@ -72,20 +45,12 @@ class NewsReaderCli::CLI
|
|
72
45
|
system("open", "#{found_url}")
|
73
46
|
|
74
47
|
elsif input == "exit"
|
75
|
-
|
48
|
+
|
76
49
|
else
|
77
50
|
puts "Sorry! input is not valid. Please follow instructions."
|
78
51
|
end
|
79
52
|
|
80
53
|
self.help_menu
|
81
|
-
|
82
|
-
# puts <<-HEREDOC
|
83
|
-
#
|
84
|
-
# 1. Please type 'list' for a list of today's news headlines.
|
85
|
-
# 2. To read an article, please type 'a' follow by index number.
|
86
|
-
# 3. To open an article url, please type 'u' follow by index number.
|
87
|
-
# 4. To Exit, type 'exit'.
|
88
|
-
# HEREDOC
|
89
54
|
end
|
90
55
|
end
|
91
56
|
|
@@ -95,32 +60,15 @@ class NewsReaderCli::CLI
|
|
95
60
|
|
96
61
|
def help_menu
|
97
62
|
puts <<-HEREDOC
|
63
|
+
|
64
|
+
|
98
65
|
1. Please type 'list' for a list of today's news headlines.
|
99
66
|
2. To read an article, please type 'a' follow by index number.
|
67
|
+
For Example, to read article 1, type in a1.
|
100
68
|
3. To open an article's url from publisher's website, please type 'u' follow by index number.
|
69
|
+
For example, to open url for article 1, type in u1.
|
101
70
|
4. To Exit, type 'exit'.
|
102
71
|
HEREDOC
|
103
72
|
end
|
104
73
|
|
105
74
|
end #end of class
|
106
|
-
|
107
|
-
# input2 = ""
|
108
|
-
# input2 = gets.chomp.strip
|
109
|
-
#
|
110
|
-
# if input2.slice!(0) == "a" && (1..(NewsReaderCli::Article.all.length)).include?(input2.to_i)
|
111
|
-
# found_content = NewsReaderCli::Article.find_content_by_article_index(input2.to_i)
|
112
|
-
# puts "#{found_content}"
|
113
|
-
# puts "Please type in 'l' to get today's news headlines"
|
114
|
-
# puts "To Exit, pease type 'exit'."
|
115
|
-
#
|
116
|
-
# elsif input2.slice!(0) == "u" && (1..(NewsReaderCli::Article.all.length)).include?(input2.to_i)
|
117
|
-
# found_url = NewsReaderCli::Article.find_url_by_article_index(input2.to_i)
|
118
|
-
# puts "#{found_url}"
|
119
|
-
# puts "Please type in 'l' to get today's news headlines"
|
120
|
-
# puts "To Exit, pease type 'exit'."
|
121
|
-
# elsif
|
122
|
-
# puts "It is not a valid input article number, please follow instructions."
|
123
|
-
# end
|
124
|
-
# else
|
125
|
-
# puts "Please type 'l' today's news headlines."
|
126
|
-
# puts "To Exit, type 'exit'."
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: news_reader_cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henry
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01-
|
11
|
+
date: 2019-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -101,7 +101,6 @@ files:
|
|
101
101
|
- bin/news_reader_cli
|
102
102
|
- bin/setup
|
103
103
|
- lib/news_reader_cli.rb
|
104
|
-
- lib/news_reader_cli/api_key_input_validate.rb
|
105
104
|
- lib/news_reader_cli/api_key_validate.rb
|
106
105
|
- lib/news_reader_cli/api_service.rb
|
107
106
|
- lib/news_reader_cli/article.rb
|
@@ -1,30 +0,0 @@
|
|
1
|
-
class NewsReaderCli::ApiKeyInputValidate
|
2
|
-
|
3
|
-
# def key_input_validate
|
4
|
-
# puts "please type in API Key from API News:"
|
5
|
-
#
|
6
|
-
# @key = gets.chomp.strip
|
7
|
-
#
|
8
|
-
# while !@key.match(/([0-9]|\w){32}/) do
|
9
|
-
#
|
10
|
-
# break if @key == "exit"
|
11
|
-
# # need to get out of the loop and remove the file .env
|
12
|
-
#
|
13
|
-
# puts <<-HEREDOC
|
14
|
-
#
|
15
|
-
# Your API keys value might missing digits or contain invalid Charactors.
|
16
|
-
# Please verify your API Key and try again. Thanks!
|
17
|
-
#
|
18
|
-
# HEREDOC
|
19
|
-
#
|
20
|
-
# @key = gets.chomp.strip
|
21
|
-
# end
|
22
|
-
#
|
23
|
-
# end #end of method
|
24
|
-
#
|
25
|
-
#
|
26
|
-
# def validated_key
|
27
|
-
# @key
|
28
|
-
# end
|
29
|
-
|
30
|
-
end #end of class
|