random_shakespeare 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3ec652ae04ed8c69f0d448854bc3a630fb2b57c7
4
+ data.tar.gz: dcb48a9ed245363c88951e754b54652e86bd3595
5
+ SHA512:
6
+ metadata.gz: c9391349a2a2d8305a640821dd5e778e0f1d3cab5f476075303419f2f3fd496672d0966789e6cdb7b71caef43df69f61d3bd186193ef80efa7296fc198317400
7
+ data.tar.gz: 3ef78225fa0e306c9028bbd5dabc47b9abdd7b7042538a866fc9eb3e3353625d6384eebe00c4787e042c265ea23d23961c2cab8c1ca1e59330ef3a1ab383dc96
data/.byebug_history ADDED
@@ -0,0 +1,47 @@
1
+ q
2
+ y
3
+ c
4
+ parser.lines_by_speaker('Gentlewoman').count
5
+ c
6
+ File.exist? filename
7
+ File.exist ? filename
8
+ File.exist ? file
9
+ filename
10
+ q
11
+ FILENAME
12
+ q
13
+ File.exist? filename
14
+ filename
15
+ q
16
+ filename
17
+ File.exist? filename
18
+ q
19
+ File.exist? filename
20
+ filename
21
+ q
22
+ File.join(File.expand_path('.'), filename)
23
+ File.expand_path('.').join(filename)
24
+ File.expand_path '.'
25
+ File.path __FILE__
26
+ File.dirname __FILE__
27
+ __FILE__
28
+ File.absolute_path
29
+ File.dirname
30
+ File.basename
31
+ File.path
32
+ File.methods
33
+ File.dir
34
+ File.dif
35
+ PWD
36
+ pwd
37
+ File.pwd
38
+ File.path
39
+ File.exist? filename
40
+ filename
41
+ c
42
+ parser.lines_by_speaker('Gentlewoman').count
43
+ parser.lines_by_speaker('Gentlewoman')
44
+ c
45
+ parser.lines_by_speaker('Gentlewoman').count
46
+ _.count
47
+ parser.lines_by_speaker('Gentlewoman')
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /lib/random_shakespeare/data/*
11
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in random_shakespeare.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ random_shakespeare (0.1.0)
5
+ activesupport (= 4.2.6)
6
+ nokogiri (= 1.6.7)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (4.2.6)
12
+ i18n (~> 0.7)
13
+ json (~> 1.7, >= 1.7.7)
14
+ minitest (~> 5.1)
15
+ thread_safe (~> 0.3, >= 0.3.4)
16
+ tzinfo (~> 1.1)
17
+ addressable (2.4.0)
18
+ crack (0.4.3)
19
+ safe_yaml (~> 1.0.0)
20
+ diff-lcs (1.2.5)
21
+ hashdiff (0.3.0)
22
+ i18n (0.7.0)
23
+ json (1.8.3)
24
+ mini_portile2 (2.0.0)
25
+ minitest (5.8.4)
26
+ nokogiri (1.6.7)
27
+ mini_portile2 (~> 2.0.0.rc2)
28
+ rake (10.5.0)
29
+ rspec (3.4.0)
30
+ rspec-core (~> 3.4.0)
31
+ rspec-expectations (~> 3.4.0)
32
+ rspec-mocks (~> 3.4.0)
33
+ rspec-core (3.4.4)
34
+ rspec-support (~> 3.4.0)
35
+ rspec-expectations (3.4.0)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.4.0)
38
+ rspec-mocks (3.4.1)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.4.0)
41
+ rspec-support (3.4.1)
42
+ safe_yaml (1.0.4)
43
+ thread_safe (0.3.5)
44
+ tzinfo (1.2.2)
45
+ thread_safe (~> 0.1)
46
+ webmock (1.24.5)
47
+ addressable (>= 2.3.6)
48
+ crack (>= 0.3.2)
49
+ hashdiff
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ bundler (~> 1.11)
56
+ rake (~> 10.0)
57
+ random_shakespeare!
58
+ rspec (~> 3.0)
59
+ webmock (= 1.24.5)
60
+
61
+ BUNDLED WITH
62
+ 1.11.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Matilda Smeds
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.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # *"They are, my lord, without the palace gate."*
2
+
3
+ ## About
4
+
5
+ RandomShakespeare gives you absolutely random quotes from Macbeth.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'random_shakespeare'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install random_shakespeare
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'random_shakespeare'
27
+ random_shakespeare #=> "Hey, it's a random quote!"
28
+ ```
29
+
30
+ Note that for the first run, you need internet access to download [Macbeth xml file](https://www.ibiblio.org/xml/examples/shakespeare/macbeth.xml).
31
+
32
+ ## About
33
+
34
+ I wrote this for practice, I realize it's rather useless. I am happy with the test suite though. Feedback is welcome, as always.
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ 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).
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/matildasmeds/random_shakespeare.
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
49
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "random_shakespeare"
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
data/bin/setup ADDED
@@ -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,44 @@
1
+ # lib/downloader.rb
2
+ require 'open-uri'
3
+ module RandomShakespeare
4
+ class Downloader
5
+ URL = 'http://www.ibiblio.org/xml/examples/shakespeare/macbeth.xml'
6
+ FILENAME = 'lib/random_shakespeare/data/macbeth.xml'
7
+
8
+ def initialize(filename = nil)
9
+ @filename = filename || FILENAME
10
+ end
11
+
12
+ def file
13
+ download unless File.exist? @filename
14
+ open_file
15
+ end
16
+
17
+ def self.url
18
+ URL
19
+ end
20
+
21
+ def self.filename
22
+ @filename || FILENAME
23
+ end
24
+
25
+ private
26
+
27
+ def download
28
+ begin
29
+ f = open(URL)
30
+ IO.copy_stream(f, @filename)
31
+ rescue Exception => e
32
+ raise "Check your connection: #{e.inspect}"
33
+ end
34
+ end
35
+
36
+ def open_file
37
+ begin
38
+ File.open(@filename, &:read)
39
+ rescue Exception => e
40
+ raise "Unable to open file: #{e.inspect}"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,41 @@
1
+ require_relative 'parser.rb'
2
+ require_relative 'downloader.rb'
3
+ module RandomShakespeare
4
+ class Lines
5
+ def initialize(parser = nil)
6
+ @parser = parser || Parser.new(Downloader.filename)
7
+ load_lines!
8
+ end
9
+
10
+ def next
11
+ load_lines! if lines_empty?
12
+ random_line
13
+ end
14
+
15
+ private
16
+
17
+ def random_line
18
+ rand_speaker = @lines.keys[rand(@lines.keys.count)]
19
+ speaker_lines = @lines[rand_speaker]
20
+ line = speaker_lines.delete_at(rand(speaker_lines.count))
21
+ clean_up(rand_speaker, speaker_lines)
22
+ line
23
+ end
24
+
25
+ def clean_up(speaker, speaker_lines)
26
+ if speaker_lines.count == 0
27
+ @lines.delete(speaker)
28
+ else
29
+ @lines[speaker] = speaker_lines
30
+ end
31
+ end
32
+
33
+ def lines_empty?
34
+ @lines == {}
35
+ end
36
+
37
+ def load_lines!
38
+ @lines = @parser.all_lines
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,58 @@
1
+ require 'nokogiri'
2
+ require_relative 'downloader'
3
+ require 'active_support/core_ext/string/inflections.rb'
4
+ module RandomShakespeare
5
+ class Parser
6
+ def initialize(filename)
7
+ file = get_file(filename)
8
+ @doc = Nokogiri::XML(file)
9
+ end
10
+
11
+ def all_lines
12
+ return @all_lines if doc_parsed?
13
+ @all_lines = parse_doc
14
+ end
15
+
16
+ def total_lines
17
+ all_lines.values.flatten.count
18
+ end
19
+
20
+ def lines_by_speaker(speaker)
21
+ all_lines[speaker.titleize]
22
+ end
23
+
24
+ def speakers
25
+ all_lines.keys
26
+ end
27
+
28
+ private
29
+
30
+ def get_file(filename)
31
+ filename = File.join(File.expand_path('.'), filename)
32
+ if File.exist? (filename)
33
+ File.open(filename, &:read)
34
+ else
35
+ Downloader.new.file
36
+ end
37
+ end
38
+
39
+ def doc_parsed?
40
+ !@all_lines.nil? && !@all_lines.empty?
41
+ end
42
+
43
+ def parse_doc
44
+ ret = {}
45
+ @doc.xpath('//SPEECH').each do |speech_doc|
46
+ parse_speech_doc(ret, speech_doc)
47
+ end
48
+ ret
49
+ end
50
+
51
+ def parse_speech_doc(ret, speech_doc)
52
+ speaker = speech_doc.xpath('SPEAKER').text.titleize
53
+ ret[speaker] ||= []
54
+ lines = speech_doc.xpath('LINE').map(&:text)
55
+ ret[speaker] << lines.join("\n")
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module RandomShakespeare
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ require_relative "random_shakespeare/version"
2
+ require_relative "random_shakespeare/downloader"
3
+ require_relative "random_shakespeare/parser"
4
+ require_relative "random_shakespeare/lines"
5
+
6
+ $RANDOM_SHAKESPEARE_TONlJtbwdGTkbIzC ||= RandomShakespeare::Lines.new
7
+
8
+ def random_shakespeare
9
+ $RANDOM_SHAKESPEARE_TONlJtbwdGTkbIzC.next
10
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ require_relative 'lib/random_shakespeare/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "random_shakespeare"
6
+ spec.version = RandomShakespeare::VERSION
7
+ spec.authors = ["Matilda Smeds"]
8
+ spec.email = ["matilda.smeds@gmail.com"]
9
+
10
+ spec.summary = "RandomShakespeare gives you random quotes from Macbeth."
11
+ spec.description = "Usage: \
12
+ require 'random_shakespeare'
13
+ random_shakespeare"
14
+ spec.homepage = "https://github.com/matildasmeds/random_shakespeare"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency "nokogiri", "1.6.7"
31
+ spec.add_runtime_dependency "activesupport", "4.2.6"
32
+ spec.add_development_dependency "bundler", "~> 1.11"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ spec.add_development_dependency "webmock", "1.24.5"
36
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: random_shakespeare
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matilda Smeds
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 4.2.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 4.2.6
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 1.24.5
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 1.24.5
97
+ description: |-
98
+ Usage: require 'random_shakespeare'
99
+ random_shakespeare
100
+ email:
101
+ - matilda.smeds@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".byebug_history"
107
+ - ".gitignore"
108
+ - ".rspec"
109
+ - ".travis.yml"
110
+ - Gemfile
111
+ - Gemfile.lock
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - bin/console
116
+ - bin/setup
117
+ - lib/random_shakespeare.rb
118
+ - lib/random_shakespeare/downloader.rb
119
+ - lib/random_shakespeare/lines.rb
120
+ - lib/random_shakespeare/parser.rb
121
+ - lib/random_shakespeare/version.rb
122
+ - random_shakespeare.gemspec
123
+ homepage: https://github.com/matildasmeds/random_shakespeare
124
+ licenses:
125
+ - MIT
126
+ metadata:
127
+ allowed_push_host: https://rubygems.org
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.4.5.1
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: RandomShakespeare gives you random quotes from Macbeth.
148
+ test_files: []