brainyquote 0.2.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 478413eec2c2eb10ee414c075bb906a41076f715
4
+ data.tar.gz: 3c42fe5c9c7a48c899a6f1b092eab72f2513dfd1
5
+ SHA512:
6
+ metadata.gz: aae02218bd9c2ece40bfebcce56daa1b5dc24cecf41bb2a206a86caaf3fe8bc18ed5ebfdfed326aa9420e2b890fbcf92d280eb64112f40b345523c19cb49d3b7
7
+ data.tar.gz: 9cd0fa6a3d6ab99be46b673efd03893855f22d9f0d88bba503b85e4cde1aca36b7d04871e62e7fd596ae14527d6b600f5daaafd6ebf78da457a84029f28e940e
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at dukeoflaser@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in BrainyQuote.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Nathaniel Miller
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # BrainyQuote
2
+ A simple Command Line Interface that allows the user to receive a randomly
3
+ selected quote based on topic of the user's choice.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'brainyquote'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install brainyquote
21
+
22
+ ## Usage
23
+
24
+ After installation, simply type `brainyquote` to access the BrainyQuote interface.
25
+ Hitting 'enter' will display a list of topics.
26
+ Choose a topic via the associated number to receive a quote.
27
+ The user can type `y` to get another quote from the same topic as many times
28
+ as he or she wishes.
29
+ Type `n` to view the list of topics again.
30
+ Leave the interface at any time by typing `exit`.
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/BrainyQuote. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
41
+
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require './lib/brainyquote'
4
+ BrainyQuote::CLI.new.call
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "brainyquote"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'brainyquote/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "brainyquote"
7
+ spec.version = BrainyQuote::VERSION
8
+ spec.authors = ["Nathaniel Miller"]
9
+ spec.email = ["dukeoflaser@gmail.com"]
10
+ spec.summary = "Retrieve random quotes according to topic."
11
+ spec.homepage = "https://github.com/dukeoflaser/brainyquote-gem"
12
+ spec.license = "MIT"
13
+
14
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
15
+ # delete this section to allow pushing this gem to any host.
16
+ if spec.respond_to?(:metadata)
17
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
18
+ else
19
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
20
+ end
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ spec.bindir = "bin"
24
+ spec.executables << 'brainyquote'
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.11"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_runtime_dependency "nokogiri"
30
+ end
@@ -0,0 +1,3 @@
1
+ module BrainyQuote
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require_relative './brainyquote/version'
2
+ require_relative './brainyquote/cli'
3
+ require_relative './brainyquote/quote'
4
+
5
+ require 'nokogiri'
6
+ require 'open-uri'
@@ -0,0 +1,139 @@
1
+ class BrainyQuote::CLI
2
+ def call
3
+ starter
4
+ end
5
+
6
+
7
+
8
+
9
+ private
10
+ def starter
11
+ @filter = 'main'
12
+ puts ""
13
+ display_instructions_for('intro', 'topics', 'exit')
14
+ skip_line
15
+ get_input
16
+ continue_or_exit
17
+ end
18
+
19
+ def display_instructions_for(*options)
20
+ puts "Welcome to BrainyQuote." if options.include?('intro')
21
+ puts "That is not a valid option." if options.include?('invalid')
22
+ puts "Hit 'Enter' to see available topics." if options.include?('topics')
23
+ if options.include?('retry')
24
+ puts "Would you like another quote from this topic? (y/n)"
25
+ puts "Hitting 'n' will display the topic list."
26
+ end
27
+ puts "Enter a number from the list to select a topic." if options.include?('choose')
28
+ puts "Type 'exit' to leave." if options.include?('exit')
29
+ end
30
+
31
+ def get_input
32
+ @input = gets.strip.downcase
33
+ end
34
+
35
+ def continue_or_exit
36
+ until @input == 'exit'
37
+ main_controller
38
+ end
39
+ skip_line
40
+ puts "Enough quotes for now. Goodbye!"
41
+ exit
42
+ end
43
+
44
+ def main_controller
45
+ if @filter == 'main'
46
+ if @input == ""
47
+ topic_controller
48
+ else
49
+ display_instructions_for('invalid', 'topics', 'exit')
50
+ get_input
51
+ end
52
+ elsif @filter == 'topic'
53
+ if (1..122) === @input.to_i
54
+ translate_input_to_topic_name
55
+ retrieve_quote
56
+ format_quote
57
+ display_instructions_for('retry', 'exit')
58
+ decide_to_retry
59
+ else
60
+ display_instructions_for('invalid', 'choose', 'exit')
61
+ get_input
62
+ continue_or_exit
63
+ end
64
+ end
65
+ end
66
+
67
+ def topic_controller
68
+ @filter = 'topic'
69
+ get_topics
70
+ format_topics
71
+ display_instructions_for('choose', 'exit')
72
+ get_input
73
+ continue_or_exit
74
+ end
75
+
76
+ def format_topics
77
+ topics_string = ""
78
+ @topics.each_with_index do |topic, i|
79
+ cell = "#{i + 1}) #{topic}"
80
+ x = (25 - cell.length)
81
+ x.times{ cell << " " }
82
+ topics_string << cell
83
+ end
84
+
85
+ rows = topics_string.scan(/.{1,75}/)
86
+ rows.each {|row| puts row}
87
+ skip_line
88
+ end
89
+
90
+ def format_quote
91
+ skip_line
92
+ puts "~#{@quote.topic_name.split.map(&:capitalize).join(' ')}~"
93
+ skip_line(2)
94
+ puts word_wrap(@quote.text, line_width: 75)
95
+ skip_line
96
+ puts " - #{@quote.author}"
97
+ skip_line(4)
98
+ end
99
+
100
+ #ActionView's WordWrap
101
+ def word_wrap(text, options = {})
102
+ line_width = options.fetch(:line_width, 80)
103
+
104
+ text.split("\n").collect! do |line|
105
+ line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
106
+ end * "\n"
107
+ end
108
+
109
+ def skip_line(n = 1)
110
+ n.times{ puts "" }
111
+ end
112
+
113
+ def get_topics
114
+ @topics = BrainyQuote::Quote.topics
115
+ end
116
+
117
+
118
+ def translate_input_to_topic_name
119
+ @topic_name = @topics[@input.to_i - 1].downcase
120
+ end
121
+
122
+ def retrieve_quote
123
+ @quote = BrainyQuote::Quote.quote_about(@topic_name)
124
+ end
125
+
126
+ def decide_to_retry
127
+ get_input
128
+
129
+ if @input == 'y'
130
+ retrieve_quote
131
+ format_quote
132
+ display_instructions_for('retry', 'exit')
133
+ decide_to_retry
134
+ elsif @input == 'n'
135
+ topic_controller
136
+ end
137
+ end
138
+
139
+ end
@@ -0,0 +1,66 @@
1
+ class BrainyQuote::Quote
2
+ attr_accessor :text, :author
3
+ attr_reader :topic_name
4
+
5
+ def initialize(topic_name)
6
+ @topic_name = topic_name
7
+ end
8
+
9
+ def data_from_topic
10
+ text_and_author = []
11
+ doc = Nokogiri::HTML(open(random_pagination_url))
12
+
13
+ #Retrieve text/author pairs.
14
+ doc.css('.bqQt').each do |data|
15
+ text_and_author << data.text.split("\n").reject!(&:empty?).take(2)
16
+ end
17
+
18
+ #Randomly selectand return a scraped quote/author.
19
+ index = (rand(text_and_author.length) - 1)
20
+ text_and_author[index]
21
+ end
22
+
23
+
24
+ def random_pagination_url
25
+ #Prepare basic topic_url
26
+ page_name = @topic_name.split(/\W/).join.downcase
27
+ url = "http://www.brainyquote.com/quotes/topics/topic_#{page_name}.html"
28
+
29
+ doc = Nokogiri::HTML(open(url))
30
+
31
+ #Find number of available pages for topics and pick one at random
32
+ max = doc.css('ul.pagination').first.css('li:nth-last-child(2)').text.to_i
33
+ page = rand(max)
34
+
35
+ #Render url for selected page
36
+ "http://www.brainyquote.com/quotes/topics/topic_#{page_name}#{page}.html"
37
+ end
38
+
39
+
40
+
41
+
42
+ class << self
43
+ def quote_about(topic_name)
44
+ quote_object = self.new(topic_name)
45
+ quote_data = quote_object.data_from_topic
46
+
47
+ quote_object.text = quote_data.first
48
+ quote_object.author = quote_data.last
49
+ quote_object
50
+ end
51
+
52
+ def topics
53
+ topics = []
54
+ url = 'http://www.brainyquote.com/quotes/topics.html'
55
+ doc = Nokogiri::HTML(open(url))
56
+
57
+ doc.css('table .bqLn a').each do |link|
58
+ topics << link.text
59
+ end
60
+
61
+ topics
62
+ end
63
+ end
64
+
65
+ private :random_pagination_url
66
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brainyquote
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathaniel Miller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-06 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - dukeoflaser@gmail.com
58
+ executables:
59
+ - brainyquote
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/brainyquote
70
+ - bin/console
71
+ - bin/setup
72
+ - brainyquote.gemspec
73
+ - lib/BrainyQuote/version.rb
74
+ - lib/brainyquote.rb
75
+ - lib/brainyquote/cli.rb
76
+ - lib/brainyquote/quote.rb
77
+ homepage: https://github.com/dukeoflaser/brainyquote-gem
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ allowed_push_host: https://rubygems.org
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.6
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Retrieve random quotes according to topic.
102
+ test_files: []