the-bump-cli 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3e76eb62482732e7e62d3985b2db59574104a3c7f8473b6069c5c170722f72fc
4
+ data.tar.gz: 82e022e52230116d9d38e9c1d567e6d24ebfd86e184bc1786c4118f7dee2f66d
5
+ SHA512:
6
+ metadata.gz: 32e57d707a0036132bca39ad5830b0f3369622f1ebd0da1eda284d71c2ffd99ca41b19423a0fc856d3ddb2fe995d6083fe235f4ef503960d727ca8942776f002
7
+ data.tar.gz: 9813a67f5487bc147875c70e18dd1c06443b18858e068b2ae3e26c898e12b026b089c69270f2f8f7fc3764cff78cd4a25fd8da0ee97ef6c609131a0bd9891c71
data/bin/bump-cli ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/bump-cli"
4
+
5
+ Cli.new.call
@@ -0,0 +1,8 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+ require 'colorize'
4
+ require 'pry'
5
+
6
+ require_relative "../lib/cli.rb"
7
+ require_relative "../lib/article.rb"
8
+ require_relative "../lib/scraper.rb"
data/lib/article.rb ADDED
@@ -0,0 +1,25 @@
1
+ class Article
2
+ attr_accessor :author, :title, :subtitle, :content
3
+ @@all = []
4
+
5
+ def initialize
6
+ @@all << self
7
+ end
8
+
9
+ def self.new_from_hash(hash)
10
+ article = Article.new
11
+ hash.each do |attribute, value|
12
+ article.send("#{attribute}=", value)
13
+ end
14
+
15
+ article.content = article.content.reject {|para| para.text==""}
16
+ end
17
+
18
+ def self.all
19
+ @@all
20
+ end
21
+
22
+ def self.reset_all
23
+ @@all = []
24
+ end
25
+ end
data/lib/bump-cli.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative "../config/environment"
data/lib/cli.rb ADDED
@@ -0,0 +1,118 @@
1
+ class Cli
2
+ attr_accessor :scraper
3
+
4
+ def initialize
5
+ @scraper = Scraper.new
6
+ Article.reset_all
7
+ end
8
+
9
+ def call
10
+ puts "Welcome to The Bump CLI!".colorize(:blue)
11
+ family_stage = set_stage - 1
12
+ scraper.get_articles(family_stage)
13
+
14
+ puts "Please select an article:".colorize(:blue)
15
+ show_article_titles
16
+ article_choice = set_article - 1
17
+
18
+ show_article_header(article_choice)
19
+ show_article_content(article_choice, 0)
20
+
21
+ ask_for_another_article
22
+ end
23
+
24
+ def set_stage
25
+ stage = ""
26
+ until stage.to_i.between?(1,5)
27
+ puts "What stage of pregnancy is your family in?".colorize(:blue)
28
+ puts "Please select the number from the following options:".colorize(:blue)
29
+ puts "1 - Getting Pregnant"
30
+ puts "2 - First Trimester"
31
+ puts "3 - Second Trimester"
32
+ puts "4 - Third Trimester"
33
+ puts "5 - Parenting"
34
+ stage = gets.chomp.to_i
35
+ end
36
+ stage
37
+ end
38
+
39
+ def set_article
40
+ article_choice = gets.chomp.to_i
41
+ until article_choice.to_i.between?(1,5)
42
+ puts "Please select an article by its number".colorize(:blue)
43
+ article_choice = gets.chomp.to_i
44
+ end
45
+ article_choice
46
+ end
47
+
48
+ def show_article_titles
49
+ Article.all.each.with_index(1) do |article, i|
50
+ puts "#{i} - #{article.title}"
51
+ end
52
+ end
53
+
54
+ def show_article_header(choice)
55
+ article = Article.all[choice]
56
+
57
+ puts ""
58
+ puts article.title.colorize(:light_yellow).underline
59
+ puts article.subtitle.colorize(:yellow)
60
+ puts "by #{article.author}"
61
+ puts ""
62
+ end
63
+
64
+ def show_article_content(choice, paragraph)
65
+ article = Article.all[choice]
66
+
67
+ #puts the next paragraph of the article. If what is next is a header, it places that AND the next paragraph
68
+ #if the next paragraph is an ul, then it puts the whole list
69
+ #it also cleans up the text as it puts it out
70
+ if ["h1","h2","h3","h4"].include?(article.content[paragraph].name)
71
+ puts article.content[paragraph].text.gsub(/â.*¢/,"-").gsub("â","\'").colorize(:yellow)
72
+ paragraph += 1
73
+ puts article.content[paragraph].text.gsub(/â.*¢/,"-").gsub("â","\'")
74
+ elsif article.content[paragraph].name == "ul"
75
+ article.content[paragraph].children.each do |li|
76
+ if li.name == "li"
77
+ puts "- " + li.text.gsub(/â.*¢/,"-").gsub("â","\'")
78
+ end
79
+ end
80
+ else
81
+ puts article.content[paragraph].text.gsub(/â.*¢/,"-").gsub("â","\'")
82
+ end
83
+
84
+ #for continuing, this checks if we are at the end of the article, and offers the next paragraph if it is not
85
+ if paragraph + 1 < article.content.length
86
+ response = ask_to_continue
87
+
88
+ if response == ''
89
+ show_article_content(choice, paragraph+1)
90
+ end
91
+ else
92
+ puts "(End of article)"
93
+ end
94
+ end
95
+
96
+ def ask_to_continue
97
+ user_choice = nil
98
+ until ['','exit'].include?(user_choice)
99
+ puts '(press enter to continue, or type "exit" to exit)'.colorize(:blue)
100
+ user_choice = gets.chomp
101
+ end
102
+ user_choice
103
+ end
104
+
105
+ def ask_for_another_article
106
+ puts "Would you like to read another article? (y/n)".colorize(:blue)
107
+ continue = gets.chomp.downcase
108
+
109
+ if continue == 'y'
110
+ Article.reset_all
111
+ call
112
+ elsif continue == 'n'
113
+ puts "Goodbye!".colorize(:blue)
114
+ else
115
+ ask_for_another_article
116
+ end
117
+ end
118
+ end
data/lib/scraper.rb ADDED
@@ -0,0 +1,22 @@
1
+ class Scraper
2
+ def get_articles(stage)
3
+ html = Nokogiri::HTML(open("https://www.thebump.com"))
4
+ all_sections = html.css(".homepage-panel---articles")
5
+ all_sections[stage].css(".homepage-panel--item").collect do |article|
6
+ article_url = article.attribute("href").value
7
+ scrape_article(article_url)
8
+ end
9
+ end
10
+
11
+ def scrape_article(article_url)
12
+ html = Nokogiri::HTML(open(article_url))
13
+ article_hash = {
14
+ title: html.css("div#pre-content-container h1").text.strip,
15
+ subtitle: html.css("div#pre-content-container .dek").text.strip,
16
+ author: html.css("div#pre-content-container .contributor-name").text.strip,
17
+ content: html.css("div.body-content p, div.body-content h2, div.body-content ul")
18
+ }
19
+
20
+ Article.new_from_hash(article_hash)
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: the-bump-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - "'Chris Dalla Santa'"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-03-23 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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: pry
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: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: colorize
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - "'chris.dallasanta@gmail.com'"
86
+ executables:
87
+ - bump-cli
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - bin/bump-cli
92
+ - config/environment.rb
93
+ - lib/article.rb
94
+ - lib/bump-cli.rb
95
+ - lib/cli.rb
96
+ - lib/scraper.rb
97
+ homepage: https://github.com/cdallasanta/bump-cli
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.0.3
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: This Ruby Gem provides a CLI to view articles from www.thebump.com
120
+ test_files: []