mmonews 0.1.2 → 0.1.3

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: 5e6edb4e05746c7b265427a9abebdf5e1c965e78dd0580b424b2ca67ac61868a
4
- data.tar.gz: 97d2a69ff4094151d34bf1d116965e4597bbe45d3f634c7355ec628914e8f27a
3
+ metadata.gz: f1262a49fb0aefef8f24dfda82dff05c185da5db3118ec76702b47ffc9aa6262
4
+ data.tar.gz: b30496c7cf076bc22ede93bf59b59f88178d00018db529052cf50a9b225ddf64
5
5
  SHA512:
6
- metadata.gz: aa6da74809926a84c36fbf8cca5b1581dc089a6c4e069347b213afb5cb2e93bd5e132e05421bb58b4adfe9568ecc9090725bcdbbec92b00af31c46d999316943
7
- data.tar.gz: 481588b2ba7d0864a78b560fc2bde57014719cc6e6b3a4a7453eb267de986df77b931e2428df277f58ba6375c6d5944356ebb7bb7bad35e3cad690f163460760
6
+ metadata.gz: 980898303abf8162d1823a11ebe0e18cd598b4c09cf5cfb453d4fbbd2d30cb8049c490bc6c26b632e599bd2976f5c756bcb5ec4a80ed19fdb167d9622e5449f8
7
+ data.tar.gz: 24b220fa238cf94d8f350234cd168264de2d1b64cc7ccaff1a0afdc44000942a3af679d83708d4de93eeee37417d5a454b17098458ac4d282f8be7887aca569c
@@ -0,0 +1,45 @@
1
+ module Mmonews
2
+ class Article
3
+ attr_accessor :title, :summary, :content, :source,
4
+ :author, :url, :date_published
5
+
6
+ @@all = []
7
+
8
+ def initialize(article_attributes)
9
+ article_attributes.each{ |key, value| send("#{key}=", value) }
10
+ end
11
+
12
+ def self.all
13
+ @@all
14
+ end
15
+
16
+ def save
17
+ self.class.all << self
18
+ end
19
+
20
+ def self.create(article_hash)
21
+ self.new(article_hash).tap{ |s| s.save }
22
+ end
23
+
24
+ def fetch_article
25
+ data = Scraper.new(url).document
26
+ self.content = data.search('div.commonContent').text.strip.gsub(/[\t\r\n]/, '')
27
+ self
28
+ end
29
+
30
+ def self.print_articles
31
+ self.all.each_with_index do |article, i|
32
+ puts "#{i+1}. #{article.title}"
33
+ puts "\t#{article.author} - #{article.date_published} - #{article.source}"
34
+ puts "#{article.summary}\n"
35
+ end
36
+ end
37
+
38
+ def print_full
39
+ puts "#{self.title}"
40
+ puts "\t#{self.author} - #{self.date_published} - #{self.source}"
41
+ puts self.content
42
+ end
43
+
44
+ end
45
+ end
@@ -1,8 +1,31 @@
1
1
  module Mmonews
2
2
  class CLI
3
3
  def call
4
- puts "it Works!"
4
+ puts "----- Welcome to MMONEWSCLI -----"
5
+ generate_articles
6
+ display_articles
7
+
8
+ input = 0
9
+ until input && input.between?(1, Mmonews::Article.all.length)
10
+ puts "\n\t Which article index # would you like to look at? (numbers only)"
11
+ input = gets.strip.to_i
12
+ end
13
+ display_one_article(input)
5
14
  end
6
-
15
+
16
+ def generate_articles
17
+ scrapers = [Mmonews::MmorpgScraper]
18
+ scrapers.each{ |s| s.new.create_articles }
19
+ end
20
+
21
+ def display_articles
22
+ Mmonews::Article.print_articles
23
+ end
24
+
25
+ def display_one_article(index)
26
+ article = Mmonews::Article.all[index - 1].fetch_article
27
+ article.print_full
28
+ end
29
+
7
30
  end
8
31
  end
@@ -0,0 +1,35 @@
1
+ module Mmonews
2
+ class MmorpgScraper
3
+ attr_accessor :url, :page_scraper
4
+
5
+ BASE_URL = 'https://mmorpg.com'
6
+ SOURCE = 'mmorpg.com'
7
+
8
+ def initialize
9
+ self.url = BASE_URL + '/articles'
10
+ self.page_scraper = Mmonews::Scraper.new(url)
11
+ end
12
+
13
+ def page
14
+ @page ||= page_scraper.document.search('.entry')
15
+ end
16
+
17
+ def create_articles
18
+ page.collect do |p|
19
+ article_url = BASE_URL + p.search('h1 a.title').attribute('href').value.strip
20
+ author = p.search('p.body span.desc a').text
21
+
22
+ article = {
23
+ title: p.search('h1 a.title').text,
24
+ date_published: p.search('p.body span.desc').text.gsub(" by #{author}", ""),
25
+ author: author,
26
+ source: SOURCE,
27
+ url: article_url,
28
+ summary: p.search('p.body').text
29
+ }
30
+ Mmonews::Article.create(article)
31
+ end
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ module Mmonews
2
+ class Scraper
3
+ attr_accessor :url, :elapsed_time
4
+
5
+ @@all = []
6
+
7
+ def initialize(url = "")
8
+ self.url = url if self.class.valid_url?(url)
9
+ self.save
10
+ end
11
+
12
+ def save
13
+ self.class.all << self
14
+ end
15
+
16
+ def self.all
17
+ @@all
18
+ end
19
+
20
+ def document
21
+ start_time = Time.now
22
+ begin
23
+ @document ||= Nokogiri::HTML(open(self.url))
24
+ rescue StandardError => error
25
+ puts "Nokogiri invalid url:\n #{self.class} :: #{self}"
26
+ puts "url: #{self.url}"
27
+ end
28
+ end_time = Time.now
29
+ self.elapsed_time = (end_time - start_time)
30
+ @document
31
+ end
32
+
33
+ def self.valid_url?(url)
34
+ url =~ /\A#{URI::regexp}\z/ ? true : false
35
+ end
36
+
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Mmonews
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mmonews
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Sleeper