File tree Expand file tree Collapse file tree 4 files changed +40
-20
lines changed
Expand file tree Collapse file tree 4 files changed +40
-20
lines changed Original file line number Diff line number Diff line change 1+ # Text Scraper
2+ class ImageScraper
3+ def initialize
4+ @base_url = 'https://www.hongkiat.com/blog/programming-jokes/'
5+ unparsed_page = HTTParty . get ( @base_url )
6+ @parsed_page = Nokogiri ::HTML ( unparsed_page . body )
7+ end
8+
9+ def scraper
10+ @parsed_page
11+ end
12+ end
Original file line number Diff line number Diff line change 11require 'httparty'
22require 'nokogiri'
33
4+ require_relative './image_scraper'
5+
46# Image based jokes implementation
57class ImageClone
6- def initialize
7- @base_url = 'https://www.hongkiat.com/blog/programming-jokes/'
8- unparsed_page = HTTParty . get ( @base_url )
9- @parsed_page = Nokogiri ::HTML ( unparsed_page . body )
10- end
11-
12- def image_jokes
8+ def image_jokes ( scrap )
139 my_array = [ ]
14- img_tags = @parsed_page . css ( 'figure.entry-image img[src]' ) . select { |image | image [ 'src' ] . start_with? ( 'https' ) }
10+ img_tags = scrap . css ( 'figure.entry-image img[src]' ) . select { |image | image [ 'src' ] . start_with? ( 'https' ) }
1511 if img_tags . is_a? Array
1612 img_tags . map { |t | t [ :src ] }
1713 else
@@ -20,8 +16,9 @@ def image_jokes
2016 end
2117
2218 def image_random_joke
23- if image_jokes . any?
24- image_jokes . sample
19+ image = ImageScraper . new
20+ if image_jokes ( image . scraper ) . any?
21+ image_jokes ( image . scraper ) . sample
2522 else
2623 'Whoops, something is not quite right!, try again'
2724 end
@@ -30,3 +27,5 @@ def image_random_joke
3027 private :image_jokes
3128end
3229
30+ image_clone = ImageClone . new
31+ puts image_clone . image_random_joke
Original file line number Diff line number Diff line change 11require 'httparty'
22require 'nokogiri'
33
4+ require_relative './text_scraper'
5+
46# Text based jokes implementation
57class TextClone
6- def initialize
7- @text_url = 'https://upjoke.com/programmer-jokes'
8- unparsed_page_text = HTTParty . get ( @text_url )
9- @parsed_page_text = Nokogiri ::HTML ( unparsed_page_text . body )
10- end
11-
12- def text_jokes
8+ def text_jokes ( scrap )
139 my_array = [ ]
14- my_text_jokes = @parsed_page_text . css ( '.joke-wrapper' ) . map ( &:text )
10+ my_text_jokes = scrap . css ( '.joke-wrapper' ) . map ( &:text )
1511 if my_text_jokes . is_a? Array
1612 my_text_jokes
1713 else
@@ -20,8 +16,9 @@ def text_jokes
2016 end
2117
2218 def text_random_joke
23- if text_jokes . any?
24- text_jokes . sample
19+ my_jokes = TextScraper . new
20+ if text_jokes ( my_jokes . scraper ) . any?
21+ text_jokes ( my_jokes . scraper ) . sample
2522 else
2623 'Whoops, something is not quite right!, try again'
2724 end
Original file line number Diff line number Diff line change 1+ # Text Scraper
2+ class TextScraper
3+ def initialize
4+ @text_url = 'https://upjoke.com/programmer-jokes'
5+ unparsed_page_text = HTTParty . get ( @text_url )
6+ @parsed_page_text = Nokogiri ::HTML ( unparsed_page_text . body )
7+ end
8+
9+ def scraper
10+ @parsed_page_text
11+ end
12+ end
You can’t perform that action at this time.
0 commit comments