nytimes_top_stories 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ class NytimesTopStories::CLI
2
+ def call
3
+ puts NytimesTopStories::Scraper.get_date
4
+ puts "Headlines live from NYT:"
5
+ self.display_stories
6
+ self.menu
7
+ self.goodbye
8
+ end
9
+
10
+ def display_stories
11
+ NytimesTopStories::Story.new_from_array
12
+ NytimesTopStories::Story.all.each.with_index(1) {|story, i| puts "#{i}: #{story.headline}"}
13
+ end
14
+
15
+ def menu
16
+ choice = nil
17
+ until choice == "exit"
18
+ puts "Enter story number for more information, or type 'list'/'exit'"
19
+ choice = gets.strip.downcase
20
+ if choice.to_i > 0 && !NytimesTopStories::Story.all[choice.to_i-1].nil?
21
+ story = NytimesTopStories::Story.all[choice.to_i-1]
22
+ story.puts_story
23
+ elsif choice == "list"
24
+ self.call
25
+ elsif choice == "exit"
26
+ break
27
+ else
28
+ puts "Input invalid. Type 'list' or 'exit'."
29
+ end
30
+ end
31
+ end
32
+
33
+ def goodbye
34
+ puts "The truth is more important now than ever."
35
+ exit(0)
36
+ end
37
+ end
@@ -0,0 +1,35 @@
1
+ require 'open-uri'
2
+
3
+ class NytimesTopStories::Scraper
4
+
5
+ def self.open_site(site = "http://nytimes.com")
6
+ html = open(site)
7
+ doc = Nokogiri::HTML(html)
8
+ end
9
+
10
+ def self.get_top_stories(site = "http://nytimes.com")
11
+ doc = self.open_site(site)
12
+ top_stories_array = []
13
+ stories = doc.css("#top-news .collection .theme-summary")
14
+ stories.each do |story|
15
+ story_hash = {}
16
+ story_hash[:headline] = story.css(".story-heading a").text
17
+ story_hash[:byline] = story.css(".byline").text.strip
18
+ if !story.css("ul").empty?
19
+ story_hash[:summary] = story.css("ul").text.strip
20
+ else
21
+ story_hash[:summary] = story.css("p.summary").text.strip
22
+ end
23
+ if !story.at_css(".story-heading a").nil?
24
+ story_hash[:url] = story.css(".story-heading a").attr("href").value
25
+ end
26
+ top_stories_array << story_hash
27
+ end
28
+ top_stories_array
29
+ end
30
+
31
+ def self.get_date(site = "http://nytimes.com")
32
+ doc = self.open_site(site)
33
+ doc.css(".masthead-menu .date").text
34
+ end
35
+ end
@@ -0,0 +1,41 @@
1
+ class NytimesTopStories::Story
2
+ attr_accessor :headline, :byline, :summary, :url
3
+ @@all = []
4
+ def initialize(story_hash)
5
+ @headline = story_hash[:headline].gsub(/â/,"'")
6
+ @byline = story_hash[:byline]
7
+ @summary = story_hash[:summary].gsub(/â/,"'")
8
+ @url = story_hash[:url]
9
+ @@all << self
10
+ end
11
+
12
+ def self.new_from_array(array = NytimesTopStories::Scraper.get_top_stories)
13
+ self.clear_all
14
+ array.each do |scraped_story|
15
+ story = NytimesTopStories::Story.new(scraped_story) unless scraped_story[:headline].empty? || scraped_story[:byline].empty? || scraped_story[:summary].empty? || scraped_story[:url].empty?
16
+ end
17
+ end
18
+
19
+ def puts_story
20
+ puts @headline
21
+ puts @byline
22
+ puts @summary
23
+ puts "Press enter to open, enter any other input to escape."
24
+ choice = gets.strip
25
+ if choice == ""
26
+ self.open_story
27
+ end
28
+ end
29
+
30
+ def open_story
31
+ system("open #{@url}")
32
+ end
33
+
34
+ def self.all
35
+ @@all
36
+ end
37
+
38
+ def self.clear_all
39
+ @@all.clear
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module NytimesTopStories
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1 @@
1
+ require_relative "../config/environment.rb"
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "./lib/nytimes_top_stories/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nytimes_top_stories"
8
+ spec.version = NytimesTopStories::VERSION
9
+ spec.authors = ["Luisa Scavo"]
10
+ spec.email = ["luisascavo@gmail.com"]
11
+
12
+ spec.summary = %q{Pulls headlines from the NYT website and offers more information on request}
13
+ spec.homepage = "https://github.com/weezwo/nytimes-top-stories"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against " \
22
+ # "public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.15"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+
36
+ spec.add_dependency 'nokogiri', '~> 1.6', '>= 1.6.8'
37
+ spec.add_dependency'require_all', '~> 1.4'
38
+ end
data/spec.md ADDED
@@ -0,0 +1,15 @@
1
+ # Specifications for the CLI Assessment
2
+
3
+ Specs:
4
+ - [x] Have a CLI for interfacing with the application
5
+ Upon execution, the program presents the user with a list of articles scraped
6
+ from the NY Times homepage. The user can interact with the program through the
7
+ command line.
8
+ - [x] Pull data from an external source
9
+ I use Nokogiri to scrape the NY Times website for content with id #top-news,
10
+ and classes .collection and .theme-summary. This content is then combed for
11
+ attributes matching headline, byline, and a short summary.
12
+ - [x] Implement both list and detail views
13
+ Upon execution, the program presents the user with a list of articles.
14
+ Each is numbered, and if that number is entered, the details for that article
15
+ are displayed.
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nytimes_top_stories
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Luisa Scavo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-12 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.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: '1.6'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 1.6.8
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '1.6'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 1.6.8
75
+ - !ruby/object:Gem::Dependency
76
+ name: require_all
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.4'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.4'
89
+ description:
90
+ email:
91
+ - luisascavo@gmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".rspec"
98
+ - ".travis.yml"
99
+ - Gemfile
100
+ - LICENSE.txt
101
+ - README.md
102
+ - Rakefile
103
+ - bin/console
104
+ - bin/nytimes-top-stories
105
+ - bin/setup
106
+ - config/environment.rb
107
+ - fixtures/nytindex.htm
108
+ - lib/nytimes_top_stories.rb
109
+ - lib/nytimes_top_stories/cli.rb
110
+ - lib/nytimes_top_stories/scraper.rb
111
+ - lib/nytimes_top_stories/story.rb
112
+ - lib/nytimes_top_stories/version.rb
113
+ - nytimes_top_stories.gemspec
114
+ - spec.md
115
+ homepage: https://github.com/weezwo/nytimes-top-stories
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.6.12
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Pulls headlines from the NYT website and offers more information on request
139
+ test_files: []