urban_dictionary 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ require "spec_helper"
2
+
3
+ describe UrbanDictionary::CLI do
4
+ CLI = UrbanDictionary::CLI
5
+
6
+ def suppress_exit(expected_status)
7
+ begin
8
+ yield
9
+ fail "Expected block to exit with status #{expected_status}, but no SystemExit exception was raised"
10
+ rescue SystemExit => e
11
+ expect(e.status).to eql(expected_status)
12
+ end
13
+ end
14
+
15
+ def mk_cli(str, dictionary = nil)
16
+ config = CLI::Config.new(
17
+ :args => str.to_argv,
18
+ :stdout => Test::IO.new,
19
+ :stderr => Test::IO.new,
20
+ )
21
+ config.update(:dictionary, dictionary) unless dictionary.nil?
22
+ cli = CLI.new(config)
23
+ [cli, config]
24
+ end
25
+
26
+ def mk_word(term, definitions, examples)
27
+ UrbanDictionary::Word.new(
28
+ term,
29
+ Array(definitions).zip(Array(examples)).map {|ea| UrbanDictionary::Entry.new(*ea) }
30
+ )
31
+ end
32
+
33
+ describe "#run" do
34
+ it "outputs help when called with no arguments" do
35
+ cli, config = mk_cli("")
36
+ cli.run
37
+ expect(config.stdout).to include("Usage: urban_dictionary")
38
+ end
39
+
40
+ it "outputs error when word has no definition" do
41
+ dictionary = double("dictionary", :define => nil)
42
+ cli, config = mk_cli("undefined word", dictionary)
43
+ suppress_exit(1) { cli.run }
44
+ expect(config.stderr).to include("No definition found for 'undefined word'")
45
+ end
46
+
47
+ it "outputs a word's definition when found" do
48
+ word = mk_word("sample word", "a definition", "an example")
49
+ dictionary = double("dictionary", :define => word)
50
+ cli, config = mk_cli(word.word, dictionary)
51
+ cli.run
52
+ expect(config.stdout).to include(word.word)
53
+ end
54
+
55
+ it "outputs a random word when --random is provided" do
56
+ random_word = mk_word("random word", "random definition", "random example")
57
+ dictionary = double("dictionary", :random_word => random_word)
58
+ cli, config = mk_cli("--random", dictionary)
59
+ cli.run
60
+ expect(config.stdout).to include(random_word.word)
61
+ end
62
+
63
+ it "accepts --format to specify output format" do
64
+ word = mk_word("a word", "a definition", "a example")
65
+ dictionary = double("dictionary", :define => word)
66
+ cli, config = mk_cli("--format=json a word", dictionary)
67
+ cli.run
68
+ obj = MultiJson.load(config.stdout.content)
69
+ expect(obj).to include("word" => word.word)
70
+ end
71
+ end
72
+ end
@@ -3,7 +3,7 @@ require "spec_helper"
3
3
  describe UrbanDictionary::Entry do
4
4
  describe "#to_s" do
5
5
  it "should print out the formatted example and definition" do
6
- UrbanDictionary::Entry.new("pie", "delicious").to_s.should eq("pie\ndelicious")
6
+ expect(UrbanDictionary::Entry.new("pie", "delicious").to_s).to eq("pie\ndelicious")
7
7
  end
8
8
  end
9
9
  end
@@ -1,34 +1,42 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe UrbanDictionary::Word do
4
-
4
+
5
5
  describe "instance method" do
6
- subject{ UrbanDictionary::Word.new("pie", [mock, mock]) }
6
+ subject{ UrbanDictionary::Word.new("pie", [double, double]) }
7
+
8
+ describe '#to_s' do
9
+ subject { super().to_s }
10
+ it { should eq("pie") }
11
+ end
7
12
 
8
- its(:to_s){ should eq("pie") }
9
- its(:size){ should eq(3) }
10
- its(:length){ should eq(3) }
13
+ describe '#size' do
14
+ subject { super().size }
15
+ it { should eq(3) }
16
+ end
17
+
18
+ describe '#length' do
19
+ subject { super().length }
20
+ it { should eq(3) }
21
+ end
11
22
  end
12
23
 
13
24
  describe "class method" do
14
25
  describe "#from_url" do
15
26
  let(:url){ "the_url" }
16
- let(:html_with_results){ File.open(File.expand_path("../../html/test_with_results.html", __FILE__)) }
17
- let(:html_without_results){ File.open(File.expand_path("../../html/test_without_results.html", __FILE__)) }
18
-
19
- it "should parse a valid word" do
20
- UrbanDictionary::Word.should_receive(:open).with(url).and_return(html_with_results)
27
+ let(:html_with_results){ Test.load_fixture("on_fleek_2016-02-15.html") }
28
+ let(:html_without_results){ Test.load_fixture("sisyphus_2016-02-15.html") }
21
29
 
22
- word = UrbanDictionary::Word.from_url(url)
23
- word.should be_a(UrbanDictionary::Word)
24
- word.word.should eq("test1234")
25
- word.entries.length.should eq(1)
26
- word.entries.first.should be_an_instance_of(UrbanDictionary::Entry)
30
+ it "parses a valid word" do
31
+ word = UrbanDictionary::Word.from_html(html_with_results)
32
+ expect(word).to be_a(UrbanDictionary::Word)
33
+ expect(word.word).to eq("on fleek")
34
+ expect(word.entries.length).to eq(7)
35
+ expect(word.entries.first).to be_an_instance_of(UrbanDictionary::Entry)
27
36
  end
28
37
 
29
- it "should gracefully handle an unknown word by returning nil" do
30
- UrbanDictionary::Word.should_receive(:open).with(url).and_return(html_without_results)
31
- UrbanDictionary::Word.from_url(url).should be_nil
38
+ it "returns nil for unknown words" do
39
+ expect(UrbanDictionary::Word.from_html(html_without_results)).to be_nil
32
40
  end
33
41
  end
34
42
  end
@@ -1,31 +1,32 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe UrbanDictionary do
4
- let(:result){ mock :result }
4
+ let(:result){ double :result }
5
5
 
6
6
  describe "class method" do
7
7
  describe "#define" do
8
- it "should delegate to Word#from_url" do
9
- UrbanDictionary::Word.should_receive(:from_url).with("#{UrbanDictionary::DEFINE_URL}?term=pie").and_return(result)
10
- UrbanDictionary.define("pie").should eq(result)
8
+ it "delegates to Word#from_url" do
9
+ expect(UrbanDictionary::Word).to receive(:from_url).with("#{UrbanDictionary::DEFINE_URL}?term=pie").and_return(result)
10
+ expect(UrbanDictionary.define("pie")).to eq(result)
11
11
  end
12
12
 
13
- it "should url encode the request" do
14
- UrbanDictionary::Word.should_receive(:from_url).with("#{UrbanDictionary::DEFINE_URL}?term=asdf%25asdf").and_return(result)
15
- UrbanDictionary.define("asdf%asdf").should eq(result)
13
+ it "URL-encodes the request" do
14
+ expect(UrbanDictionary::Word).to receive(:from_url).with("#{UrbanDictionary::DEFINE_URL}?term=asdf%25asdf").and_return(result)
15
+ expect(UrbanDictionary.define("asdf%asdf")).to eq(result)
16
16
  end
17
17
  end
18
18
 
19
19
  describe "#random_word" do
20
- let(:request){ mock :request }
21
- let(:response){ {'location' => 'location'} }
22
- let(:http){ mock :http }
20
+ let(:request){ double :request }
21
+ let(:url) { 'http://example.com' }
22
+ let(:response){ {'location' => url } }
23
+ let(:http){ double :http }
23
24
 
24
25
  it "should do an HTTP GET to determine the random word" do
25
- Net::HTTP.should_receive(:start).with("www.urbandictionary.com", 80).and_yield(http).and_return(response)
26
- http.should_receive(:request).with(an_instance_of(Net::HTTP::Get))
27
- UrbanDictionary.should_receive(:define).with('location').and_return(result)
28
- UrbanDictionary.random_word.should eq(result)
26
+ expect(Net::HTTP).to receive(:start).with("www.urbandictionary.com", 80).and_yield(http).and_return(response)
27
+ expect(http).to receive(:request).with(an_instance_of(Net::HTTP::Get))
28
+ expect(UrbanDictionary::Word).to receive(:from_url).with(url).and_return(result)
29
+ expect(UrbanDictionary.random_word).to eq(result)
29
30
  end
30
31
  end
31
32
  end
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.version = UrbanDictionary::VERSION
8
8
  s.authors = ["Ryan Greenberg"]
9
9
  s.email = ["ryangreenberg@gmail.com"]
10
- s.homepage = ""
10
+ s.homepage = "https://github.com/ryangreenberg/urban_dictionary"
11
11
  s.summary = "Interface to urbandictionary.com"
12
12
  s.description = s.summary
13
13
 
@@ -18,8 +18,10 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- s.add_runtime_dependency "nokogiri", "~> 1.5.2"
21
+ s.add_runtime_dependency "nokogiri", "~> 1.5"
22
+ s.add_runtime_dependency "multi_json", "~> 1.0"
22
23
 
23
24
  s.add_development_dependency "rake"
24
- s.add_development_dependency "rspec"
25
+ s.add_development_dependency "rspec", "~> 3.4"
26
+ s.add_development_dependency "webmock", "~> 1.22.6"
25
27
  end
metadata CHANGED
@@ -1,36 +1,46 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: urban_dictionary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Ryan Greenberg
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-12-03 00:00:00.000000000 Z
11
+ date: 2016-02-16 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: nokogiri
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
21
- version: 1.5.2
19
+ version: '1.5'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
29
- version: 1.5.2
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
30
41
  - !ruby/object:Gem::Dependency
31
42
  name: rake
32
43
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
44
  requirements:
35
45
  - - ! '>='
36
46
  - !ruby/object:Gem::Version
@@ -38,7 +48,6 @@ dependencies:
38
48
  type: :development
39
49
  prerelease: false
40
50
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
51
  requirements:
43
52
  - - ! '>='
44
53
  - !ruby/object:Gem::Version
@@ -46,19 +55,31 @@ dependencies:
46
55
  - !ruby/object:Gem::Dependency
47
56
  name: rspec
48
57
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
58
  requirements:
51
- - - ! '>='
59
+ - - ~>
52
60
  - !ruby/object:Gem::Version
53
- version: '0'
61
+ version: '3.4'
54
62
  type: :development
55
63
  prerelease: false
56
64
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
65
  requirements:
59
- - - ! '>='
66
+ - - ~>
60
67
  - !ruby/object:Gem::Version
61
- version: '0'
68
+ version: '3.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 1.22.6
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 1.22.6
62
83
  description: Interface to urbandictionary.com
63
84
  email:
64
85
  - ryangreenberg@gmail.com
@@ -69,56 +90,61 @@ extra_rdoc_files: []
69
90
  files:
70
91
  - .gitignore
71
92
  - .rspec
93
+ - .travis.yml
94
+ - CHANGELOG.md
72
95
  - Gemfile
73
- - Gemfile.lock
96
+ - LICENSE.md
74
97
  - README.md
75
98
  - Rakefile
76
99
  - bin/urban_dictionary
77
100
  - lib/urban_dictionary.rb
101
+ - lib/urban_dictionary/cli.rb
78
102
  - lib/urban_dictionary/entry.rb
103
+ - lib/urban_dictionary/formatters.rb
79
104
  - lib/urban_dictionary/version.rb
80
105
  - lib/urban_dictionary/word.rb
106
+ - spec/html/add_html_example.rb
107
+ - spec/html/on_fleek_2016-02-15.html
108
+ - spec/html/sisyphus_2016-02-15.html
81
109
  - spec/html/test_with_results.html
82
110
  - spec/html/test_without_results.html
83
111
  - spec/spec_helper.rb
112
+ - spec/urban_dictionary/cli_spec.rb
84
113
  - spec/urban_dictionary/entry_spec.rb
85
114
  - spec/urban_dictionary/word_spec.rb
86
115
  - spec/urban_dictionary_spec.rb
87
116
  - urban_dictionary.gemspec
88
- homepage: ''
117
+ homepage: https://github.com/ryangreenberg/urban_dictionary
89
118
  licenses: []
119
+ metadata: {}
90
120
  post_install_message:
91
121
  rdoc_options: []
92
122
  require_paths:
93
123
  - lib
94
124
  required_ruby_version: !ruby/object:Gem::Requirement
95
- none: false
96
125
  requirements:
97
126
  - - ! '>='
98
127
  - !ruby/object:Gem::Version
99
128
  version: '0'
100
- segments:
101
- - 0
102
- hash: -774214380254322335
103
129
  required_rubygems_version: !ruby/object:Gem::Requirement
104
- none: false
105
130
  requirements:
106
131
  - - ! '>='
107
132
  - !ruby/object:Gem::Version
108
133
  version: '0'
109
- segments:
110
- - 0
111
- hash: -774214380254322335
112
134
  requirements: []
113
135
  rubyforge_project: urban_dictionary
114
- rubygems_version: 1.8.24
136
+ rubygems_version: 2.5.2
115
137
  signing_key:
116
- specification_version: 3
138
+ specification_version: 4
117
139
  summary: Interface to urbandictionary.com
118
140
  test_files:
141
+ - spec/html/add_html_example.rb
142
+ - spec/html/on_fleek_2016-02-15.html
143
+ - spec/html/sisyphus_2016-02-15.html
119
144
  - spec/html/test_with_results.html
120
145
  - spec/html/test_without_results.html
121
146
  - spec/spec_helper.rb
147
+ - spec/urban_dictionary/cli_spec.rb
122
148
  - spec/urban_dictionary/entry_spec.rb
123
149
  - spec/urban_dictionary/word_spec.rb
124
150
  - spec/urban_dictionary_spec.rb
data/Gemfile.lock DELETED
@@ -1,28 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- urban_dictionary (0.0.2)
5
- nokogiri (~> 1.5.2)
6
-
7
- GEM
8
- remote: http://rubygems.org/
9
- specs:
10
- diff-lcs (1.1.3)
11
- nokogiri (1.5.5)
12
- rake (0.9.2.2)
13
- rspec (2.11.0)
14
- rspec-core (~> 2.11.0)
15
- rspec-expectations (~> 2.11.0)
16
- rspec-mocks (~> 2.11.0)
17
- rspec-core (2.11.1)
18
- rspec-expectations (2.11.3)
19
- diff-lcs (~> 1.1.3)
20
- rspec-mocks (2.11.3)
21
-
22
- PLATFORMS
23
- ruby
24
-
25
- DEPENDENCIES
26
- rake
27
- rspec
28
- urban_dictionary!