endangered_species 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 +7 -0
- data/bin/endangered_species +6 -0
- data/config/environment.rb +9 -0
- data/lib/endangered_species.rb +1 -0
- data/lib/endangered_species/articles.rb +16 -0
- data/lib/endangered_species/cli.rb +264 -0
- data/lib/endangered_species/scraper.rb +50 -0
- data/lib/endangered_species/species.rb +16 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4518bc9fdc5fc640bf7c6f69c1e01a6ab7bcca7ccf35a344c06f00f0abf6f640
|
4
|
+
data.tar.gz: 0f0ca2ee82ae17362f64cfcf0777cd45b4299a7fab9fc5c5949d642d6ebf99fb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: de8eeba59be730bbbe5b3a4b0009d76e683b38dc78386d14c281896258d84d2dca3ef7b27239014b4758458c05861002abe8cf018988a4e3c4afdfb61b289c13
|
7
|
+
data.tar.gz: 520ff8cd4860db7402842e40fedb53f809e32c591f0fcdc366555abc6f80e415371df5381334a6364314f22d8b6ba84a5b1a67f76867be87f75d89f3eff88d7c
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'open-uri'
|
4
|
+
|
5
|
+
require_relative '../lib/endangered_species/version'
|
6
|
+
require_relative '../lib/endangered_species/cli'
|
7
|
+
require_relative '../lib/endangered_species/scraper'
|
8
|
+
require_relative '../lib/endangered_species/species'
|
9
|
+
require_relative '../lib/endangered_species/articles'
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative '../config/environment'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Stores data on all Articles
|
2
|
+
|
3
|
+
class EndangeredSpecies::Articles
|
4
|
+
attr_accessor :title, :date, :summary, :url
|
5
|
+
|
6
|
+
@@all = []
|
7
|
+
|
8
|
+
def self.all #class reader to expose @@all class variable
|
9
|
+
@@all
|
10
|
+
end
|
11
|
+
|
12
|
+
def save
|
13
|
+
@@all << self
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,264 @@
|
|
1
|
+
# CLI controller - user interaction
|
2
|
+
|
3
|
+
class EndangeredSpecies::CLI
|
4
|
+
|
5
|
+
# Main / general user input
|
6
|
+
|
7
|
+
def call
|
8
|
+
puts ""
|
9
|
+
puts "Welcome! Learn about World Wildlife Fund's conservation of species and the environment!"
|
10
|
+
sleep 1
|
11
|
+
puts ""
|
12
|
+
puts "..."
|
13
|
+
puts ""
|
14
|
+
puts "...please wait one moment..."
|
15
|
+
puts ""
|
16
|
+
puts "..."
|
17
|
+
make_species
|
18
|
+
make_articles
|
19
|
+
menu
|
20
|
+
end
|
21
|
+
|
22
|
+
def menu
|
23
|
+
input = ""
|
24
|
+
while !exit?(input)
|
25
|
+
puts ""
|
26
|
+
puts "Enter 1 if you would like to read more about endangered species."
|
27
|
+
puts ""
|
28
|
+
sleep 1
|
29
|
+
puts "or"
|
30
|
+
puts ""
|
31
|
+
puts "Enter 2 if you would like to read the latest news articles from WWF."
|
32
|
+
puts ""
|
33
|
+
input = gets.strip
|
34
|
+
if ["1", "one"].include?(input)
|
35
|
+
sleep 1
|
36
|
+
list_species
|
37
|
+
choose_species(input)
|
38
|
+
elsif ["2", "two"].include?(input)
|
39
|
+
sleep 1
|
40
|
+
list_articles
|
41
|
+
choose_articles(input)
|
42
|
+
else
|
43
|
+
try_again
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def goodbye
|
49
|
+
sleep 1
|
50
|
+
puts ""
|
51
|
+
puts "Thanks for visiting!"
|
52
|
+
puts ""
|
53
|
+
exit
|
54
|
+
end
|
55
|
+
|
56
|
+
def try_again
|
57
|
+
sleep 1
|
58
|
+
puts ""
|
59
|
+
puts "Not sure what you're looking for, please try again!"
|
60
|
+
puts ""
|
61
|
+
sleep 1
|
62
|
+
continue
|
63
|
+
end
|
64
|
+
|
65
|
+
def continue
|
66
|
+
input = ""
|
67
|
+
while !exit?(input)
|
68
|
+
puts ""
|
69
|
+
puts "Return to main menu or exit? Enter Y or N."
|
70
|
+
puts ""
|
71
|
+
input = gets.strip
|
72
|
+
if yes_user(input)
|
73
|
+
menu
|
74
|
+
elsif no_user(input)
|
75
|
+
goodbye
|
76
|
+
else
|
77
|
+
try_again
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# CLI - get user input for Info on species (Enter 1 at menu)
|
83
|
+
|
84
|
+
def make_species
|
85
|
+
EndangeredSpecies::Scraper.new.make_species
|
86
|
+
end
|
87
|
+
|
88
|
+
def list_species
|
89
|
+
EndangeredSpecies::Species.all.each_with_index do |species, i|
|
90
|
+
puts ""
|
91
|
+
puts "#{i+1}. #{species.name}"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def choose_species(input)
|
96
|
+
input = ""
|
97
|
+
while !exit?(input)
|
98
|
+
puts ""
|
99
|
+
puts ""
|
100
|
+
puts "Please enter a number for which species you would like to learn more about:"
|
101
|
+
puts ""
|
102
|
+
input = gets.strip
|
103
|
+
sleep 1
|
104
|
+
if invalid?(input)
|
105
|
+
continue_species
|
106
|
+
elsif input.to_i-1 <= EndangeredSpecies::Species.all.size
|
107
|
+
species = EndangeredSpecies::Species.all[input.to_i-1]
|
108
|
+
print_species(species)
|
109
|
+
print_more?(species)
|
110
|
+
else
|
111
|
+
continue_species
|
112
|
+
end
|
113
|
+
continue
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def print_more?(species)
|
118
|
+
input = ""
|
119
|
+
while !exit?(input)
|
120
|
+
puts ""
|
121
|
+
puts "Would you like to read more? Enter Y or N."
|
122
|
+
puts ""
|
123
|
+
input = gets.strip.downcase
|
124
|
+
if yes_user(input)
|
125
|
+
more_info(species)
|
126
|
+
sleep 1
|
127
|
+
continue_species
|
128
|
+
elsif no_user(input)
|
129
|
+
goodbye
|
130
|
+
else
|
131
|
+
try_again
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def continue_species
|
137
|
+
input = ""
|
138
|
+
while !exit?(input)
|
139
|
+
puts ""
|
140
|
+
puts "Do you want to continue reading about species? Enter Y or N."
|
141
|
+
puts ""
|
142
|
+
input = gets.strip
|
143
|
+
if yes_user(input)
|
144
|
+
list_species
|
145
|
+
choose_species(input)
|
146
|
+
elsif no_user(input)
|
147
|
+
continue
|
148
|
+
else
|
149
|
+
try_again
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def print_species(species)
|
155
|
+
puts ""
|
156
|
+
puts "----------- #{species.name} ----------- "
|
157
|
+
puts ""
|
158
|
+
puts "Scientific name: #{species.scientific}"
|
159
|
+
puts ""
|
160
|
+
puts "Status: #{species.status}"
|
161
|
+
puts ""
|
162
|
+
puts "Website: #{species.url}"
|
163
|
+
puts ""
|
164
|
+
end
|
165
|
+
|
166
|
+
def more_info(species)
|
167
|
+
puts ""
|
168
|
+
puts "----------- #{species.name} ----------- "
|
169
|
+
puts ""
|
170
|
+
puts "#{species.summary}"
|
171
|
+
puts ""
|
172
|
+
puts "#{species.habitat}"
|
173
|
+
puts ""
|
174
|
+
end
|
175
|
+
|
176
|
+
# CLI - get user input for Articles on species (Enter 2 at menu)
|
177
|
+
|
178
|
+
def make_articles
|
179
|
+
EndangeredSpecies::Scraper.new.make_articles
|
180
|
+
end
|
181
|
+
|
182
|
+
def list_articles
|
183
|
+
EndangeredSpecies::Articles.all.each_with_index do |articles, i|
|
184
|
+
puts ""
|
185
|
+
puts "#{i+1}. #{articles.title}"
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def choose_articles(input)
|
190
|
+
input = ""
|
191
|
+
while !exit?(input)
|
192
|
+
puts ""
|
193
|
+
puts ""
|
194
|
+
puts "Please enter a number for which articles you would like to read:"
|
195
|
+
puts ""
|
196
|
+
input = gets.strip
|
197
|
+
sleep 1
|
198
|
+
if invalid?(input)
|
199
|
+
try_again
|
200
|
+
elsif input.to_i-1 <= EndangeredSpecies::Articles.all.size
|
201
|
+
articles = EndangeredSpecies::Articles.all[input.to_i-1]
|
202
|
+
print_articles(articles)
|
203
|
+
continue_articles
|
204
|
+
else
|
205
|
+
continue_articles
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
def continue_articles
|
211
|
+
input = ""
|
212
|
+
while !exit?(input)
|
213
|
+
puts ""
|
214
|
+
puts "Do you want to continue reading articles? Enter Y or N."
|
215
|
+
puts ""
|
216
|
+
input = gets.strip
|
217
|
+
if yes_user(input)
|
218
|
+
list_articles
|
219
|
+
choose_articles(input)
|
220
|
+
elsif no_user(input)
|
221
|
+
continue
|
222
|
+
else
|
223
|
+
try_again
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
def print_articles(articles)
|
229
|
+
puts ""
|
230
|
+
puts "----------- #{articles.title} ----------- "
|
231
|
+
puts ""
|
232
|
+
puts "Date: #{articles.date}"
|
233
|
+
puts ""
|
234
|
+
puts "Summary:"
|
235
|
+
puts "#{articles.summary}"
|
236
|
+
puts ""
|
237
|
+
puts "More info:"
|
238
|
+
puts "#{articles.url}"
|
239
|
+
puts ""
|
240
|
+
end
|
241
|
+
|
242
|
+
# Validate user input
|
243
|
+
|
244
|
+
def is_numeric?(input)
|
245
|
+
true if Integer(input) rescue false
|
246
|
+
end
|
247
|
+
|
248
|
+
def invalid?(input)
|
249
|
+
true if !is_numeric?(input) || ["nil", "0"].include?(input) rescue false
|
250
|
+
end
|
251
|
+
|
252
|
+
def exit?(input)
|
253
|
+
true if input == "exit" || input == "exit!" rescue false
|
254
|
+
end
|
255
|
+
|
256
|
+
def no_user(input)
|
257
|
+
true if ["n", "no", "exit", "exit!"].include?(input.downcase) rescue false
|
258
|
+
end
|
259
|
+
|
260
|
+
def yes_user(input)
|
261
|
+
true if ["y", "yes"].include?(input.downcase) rescue false
|
262
|
+
end
|
263
|
+
|
264
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# Get data and zip it up
|
2
|
+
# instantiate Species or Articles based on user input from CLI controller
|
3
|
+
|
4
|
+
class EndangeredSpecies::Scraper
|
5
|
+
|
6
|
+
def get_species_index
|
7
|
+
Nokogiri::HTML(open("https://www.worldwildlife.org/species/directory"))
|
8
|
+
end
|
9
|
+
|
10
|
+
def scrape_species_index
|
11
|
+
self.get_species_index.css("table.lead.gutter-bottom-2.table-to-list tbody tr")
|
12
|
+
end
|
13
|
+
|
14
|
+
def make_species
|
15
|
+
scrape_species_index.each do |content|
|
16
|
+
species = EndangeredSpecies::Species.new
|
17
|
+
|
18
|
+
species.name = content.css("td.keep a").first.text
|
19
|
+
species.scientific = content.css("td em").text
|
20
|
+
species.status = content.css("td").last.text
|
21
|
+
species.url = "https://www.worldwildlife.org#{content.css("a").attr("href").text}"
|
22
|
+
|
23
|
+
@doc = Nokogiri::HTML(open(species.url))
|
24
|
+
@doc.search("div.wrapper.section-pop").each do |more_info|
|
25
|
+
species.summary = more_info.css("p").text
|
26
|
+
species.habitat = more_info.css("ul.list-data.list-spaced > li").text.gsub("\n\n", " ").gsub("\n", "").gsub("Places","Places:").gsub("Habitats", "\nHabitats:")
|
27
|
+
species.save
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_articles_index
|
33
|
+
Nokogiri::HTML(open("https://www.worldwildlife.org/stories/"))
|
34
|
+
end
|
35
|
+
|
36
|
+
def scrape_articles_index
|
37
|
+
self.get_articles_index.css("div.span9.gutter-horiz-in")
|
38
|
+
end
|
39
|
+
|
40
|
+
def make_articles
|
41
|
+
scrape_articles_index.each do |content|
|
42
|
+
articles = EndangeredSpecies::Articles.new
|
43
|
+
articles.title = content.css("h2 a").text
|
44
|
+
articles.date = content.css("em").text.gsub("WWF Magazine:","")
|
45
|
+
articles.summary = content.css("div").text.gsub("\n","")
|
46
|
+
articles.url = "https://www.worldwildlife.org#{content.css("a").attr("href").text}"
|
47
|
+
articles.save
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Stores data on all Species
|
2
|
+
|
3
|
+
class EndangeredSpecies::Species
|
4
|
+
attr_accessor :name, :status, :url, :summary, :scientific, :location, :habitat, :place
|
5
|
+
|
6
|
+
@@all = []
|
7
|
+
|
8
|
+
def self.all #class reader to expose @@all class variable
|
9
|
+
@@all
|
10
|
+
end
|
11
|
+
|
12
|
+
def save
|
13
|
+
@@all << self
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: endangered_species
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jul Jen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: nokogiri
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Provides details on the world's endangered species.
|
84
|
+
email:
|
85
|
+
- juliajen.prog@gmail.com
|
86
|
+
executables:
|
87
|
+
- endangered_species
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- bin/endangered_species
|
92
|
+
- config/environment.rb
|
93
|
+
- lib/endangered_species.rb
|
94
|
+
- lib/endangered_species/articles.rb
|
95
|
+
- lib/endangered_species/cli.rb
|
96
|
+
- lib/endangered_species/scraper.rb
|
97
|
+
- lib/endangered_species/species.rb
|
98
|
+
homepage: http://rubygems.org/gems/endangered_species
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
metadata: {}
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 2.7.6
|
119
|
+
signing_key:
|
120
|
+
specification_version: 4
|
121
|
+
summary: Endangered Species in the world.
|
122
|
+
test_files: []
|