urban_cli 1.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.
@@ -0,0 +1,5 @@
1
+
2
+ IMPROMPTU
3
+
4
+ Something that is made up on the spot and given little time to gather and present. Usually referring to speeches that are given only a few minutes to prepare for.
5
+
@@ -0,0 +1,7 @@
1
+
2
+ IMPROMPTU
3
+
4
+ Something that is made up on the spot and given little time to gather and present. Usually referring to speeches that are given only a few minutes to prepare for.
5
+
6
+ URL: http://www.urbandictionary.com/define.php?term=impromptu
7
+
@@ -0,0 +1,9 @@
1
+
2
+ IMPROMPTU
3
+
4
+ Something that is made up on the spot and given little time to gather and present. Usually referring to speeches that are given only a few minutes to prepare for.
5
+
6
+ On the spot
7
+
8
+ Something that is made up on the spot. Can also mean a speech that was made with little or no preparation.
9
+
@@ -0,0 +1,19 @@
1
+ Usage: urban [OPTION]... [PHRASE]
2
+ Search http://urbandictionary.com for definitions of phrases
3
+
4
+ Options:
5
+ -a, --all List all definitions
6
+ -r, --random Return a random phrase and definition
7
+ -u, --url Print the definition's url after the definition
8
+ -h, --help Show this message
9
+ -v, --version Show version information
10
+
11
+ Examples:
12
+ urban cookie monster Search for "cookie monster" and print its
13
+ first definition
14
+ urban -a cookie monster Search for "cookie monster" and print all of
15
+ its available definitions
16
+ urban -r Print a random phrase and its first definition
17
+ urban -ra Print a random phrase and all of its available
18
+ definitions
19
+
@@ -0,0 +1,2 @@
1
+ urban: invalid option: -b
2
+ Try `urban --help' for more information.
@@ -0,0 +1 @@
1
+ urban: no definitions found for SHFJKASDF.
@@ -0,0 +1 @@
1
+ urban: no internet connection available.
@@ -0,0 +1,35 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
2
+
3
+ require "rubygems"
4
+ require "minitest/autorun"
5
+ require "minitest/pride"
6
+ require "minitest/hell"
7
+ require "urban"
8
+ require "ostruct"
9
+
10
+ class Urban::Test < Minitest::Test
11
+
12
+ def load_fixture(filename)
13
+ IO.read(File.expand_path("../fixtures/#{filename}", __FILE__))
14
+ end
15
+
16
+ def empty_entry
17
+ @empty_entry ||= Urban::Dictionary::Entry.new('shfjkasdf', nil, nil)
18
+ end
19
+
20
+ def test_entry
21
+ @test_entry ||= Urban::Dictionary::Entry.new(
22
+ "impromptu",
23
+ [
24
+ "Something that is made up on the spot and given little time to " +
25
+ "gather and present. Usually referring to speeches that are given " +
26
+ "only a few minutes to prepare for.",
27
+ "On the spot",
28
+ "Something that is made up on the spot. " +
29
+ "Can also mean a speech that was made with little or no preparation."
30
+ ],
31
+ "http://www.urbandictionary.com/define.php?term=impromptu"
32
+ )
33
+ end
34
+
35
+ end
@@ -0,0 +1,154 @@
1
+ require "test_helper"
2
+ require "shellwords"
3
+ require "urban/cli"
4
+
5
+ class CLITest < Urban::Test
6
+
7
+ attr_accessor :program, :dictionary
8
+
9
+ def nothing; "" end
10
+
11
+ def assert_program_output(stdout, stderr, args)
12
+ assert_output stdout, stderr do
13
+ program.run(Shellwords.shellwords(args))
14
+ end
15
+ end
16
+
17
+ def setup
18
+ self.program = Urban::CLI.new
19
+ self.dictionary = Urban::Dictionary.new
20
+
21
+ program.dictionary = dictionary
22
+ end
23
+
24
+ class CLIArgumentParsingTest < CLITest
25
+
26
+ def assert_flag_is_set(name)
27
+ ["-#{name.chars.first}", "--#{name}"].each do |args|
28
+ options = program.send(:parse, [args])
29
+ assert options.send(name)
30
+ end
31
+ end
32
+
33
+ def test_defaults
34
+ options = program.send(:parse, [])
35
+ refute options.help
36
+ refute options.version
37
+ refute options.random
38
+ refute options.all
39
+ assert_empty options.phrase
40
+ end
41
+
42
+ def test_phrase
43
+ options = program.send :parse, ["foo bar"]
44
+ assert_equal "foo bar", options.phrase
45
+ end
46
+
47
+ def test_version_flag
48
+ assert_flag_is_set "version"
49
+ end
50
+
51
+ def test_random_flag
52
+ assert_flag_is_set "random"
53
+ end
54
+
55
+ def test_all_flag
56
+ assert_flag_is_set "all"
57
+ end
58
+
59
+ def test_url_flag
60
+ assert_flag_is_set "url"
61
+ end
62
+ end
63
+
64
+ class CLIRunnerStandardOutputTest < CLITest
65
+
66
+ def setup
67
+ super
68
+ end
69
+
70
+ def test_help_flag_prints_help
71
+ help_screen = load_fixture "screens/help.txt"
72
+ assert_program_output help_screen, nothing, nothing
73
+ end
74
+
75
+ def test_version_flag_prints_version
76
+ version_screen = "Urban #{Urban::VERSION} (c) Thomas Miller\n"
77
+ assert_program_output version_screen, nothing, "--version"
78
+ end
79
+
80
+ def test_random_flag_prints_single_definition
81
+ single_definition = load_fixture "screens/definition.txt"
82
+
83
+ dictionary.stub(:random, test_entry) do
84
+ assert_program_output single_definition, nothing, "--random"
85
+ end
86
+ end
87
+
88
+ def test_phrase_prints_single_definition
89
+ single_definition = load_fixture "screens/definition.txt"
90
+ dictionary.stub(:search, test_entry) do
91
+ assert_program_output single_definition, nothing, "impromptu"
92
+ end
93
+ end
94
+
95
+ def test_random_and_all_flag_prints_multiple_definitions
96
+ multiple_definitions = load_fixture "screens/definitions.txt"
97
+ dictionary.stub(:random, test_entry) do
98
+ assert_program_output multiple_definitions, nothing, "--all --random"
99
+ end
100
+ end
101
+
102
+ def test_phrase_and_all_flag_prints_multiple_definitions
103
+ multiple_definitions = load_fixture "screens/definitions.txt"
104
+ dictionary.stub(:search, test_entry) do
105
+ assert_program_output multiple_definitions, nothing, "--all impromptu"
106
+ end
107
+ end
108
+
109
+ def test_random_and_url_flag_prints_definition_with_url
110
+ definition_with_url = load_fixture "screens/definition_with_url.txt"
111
+ dictionary.stub(:random, test_entry) do
112
+ assert_program_output definition_with_url, nothing, "--url --random"
113
+ end
114
+ end
115
+
116
+ def test_phrase_and_url_flag_prints_definition_with_url
117
+ definition_with_url = load_fixture "screens/definition_with_url.txt"
118
+ dictionary.stub(:search, test_entry) do
119
+ assert_program_output definition_with_url, nothing, "--url impromptu"
120
+ end
121
+ end
122
+
123
+ end
124
+
125
+ class CLIRunnerErrorOutputTest < CLITest
126
+
127
+ def setup
128
+ super
129
+ end
130
+
131
+ def test_search_missing_phrase_prints_error
132
+ missing_phrase_error = load_fixture "screens/missing_phrase_error.txt"
133
+
134
+ dictionary.stub :search, empty_entry do
135
+ assert_program_output nothing, missing_phrase_error, "shfjkasdf"
136
+ end
137
+ end
138
+
139
+
140
+ def test_search_with_no_internet_prints_error
141
+ no_internet_error = load_fixture "screens/no_internet_error.txt"
142
+ raise_socket_error = Proc.new { raise SocketError }
143
+
144
+ dictionary.stub :search, raise_socket_error do
145
+ assert_program_output nothing, no_internet_error, "shfjkasdf"
146
+ end
147
+ end
148
+
149
+ def test_invalid_option_prints_help
150
+ invalid_option_error = load_fixture "screens/invalid_option_error.txt"
151
+ assert_program_output nothing, invalid_option_error, "-b"
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,47 @@
1
+ require "test_helper"
2
+ require "urban/dictionary"
3
+
4
+ class DictionaryTest < Urban::Test
5
+
6
+ attr_accessor :web_service, :dictionary, :response
7
+
8
+ def setup
9
+ self.web_service = MiniTest::Mock.new
10
+ self.dictionary = Urban::Dictionary.new
11
+
12
+ dictionary.web_service = web_service
13
+
14
+ self.response = OpenStruct.new(
15
+ :url => "http://www.urbandictionary.com/define.php?term=impromptu",
16
+ :stream => load_fixture("impromptu.html")
17
+ )
18
+ end
19
+
20
+ def test_process_extracts_elements_from_html
21
+ entry = dictionary.send(:process, response )
22
+ assert_equal(test_entry, entry)
23
+ end
24
+
25
+ def test_dictionary_calls_random
26
+ web_service.expect(:random, response)
27
+
28
+ assert_equal(test_entry, dictionary.random)
29
+ web_service.verify
30
+ end
31
+
32
+ def test_dictionary_calls_search
33
+ web_service.expect(:search, response, ['impromptu'])
34
+
35
+ assert_equal(test_entry, dictionary.search('impromptu'))
36
+ web_service.verify
37
+ end
38
+
39
+ def test_dictionary_returns_empty_for_missing_phrases
40
+ response.stream = load_fixture 'missing.html'
41
+ web_service.expect(:search, response, ['shfjkasdf'])
42
+
43
+ assert_equal(empty_entry, dictionary.search('shfjkasdf'))
44
+ web_service.verify
45
+ end
46
+
47
+ end
@@ -0,0 +1,38 @@
1
+ require "test_helper"
2
+ require "urban/web"
3
+
4
+ class WebTest < Urban::Test
5
+
6
+ attr_accessor :web_client
7
+
8
+ def setup
9
+ self.web_client = Urban::Web.new
10
+ end
11
+
12
+ def test_build_query_with_multiple_query_params
13
+ expected = "?term=bar&name=foo"
14
+ actual = web_client.send :build_query, { :term => :bar, :name => :foo }
15
+ assert_equal expected, actual
16
+ end
17
+
18
+ def test_build_uri_with_no_query_params
19
+ expected = "http://www.urbandictionary.com/test.php"
20
+ actual = web_client.send :build_uri, :test
21
+ assert_equal expected, actual
22
+ end
23
+
24
+ def test_build_uri_with_a_query_param
25
+ expected = "http://www.urbandictionary.com/test.php?term=foo"
26
+ actual = web_client.send :build_uri, :test, { :term => :foo }
27
+ assert_equal expected, actual
28
+ end
29
+
30
+ def test_build_response
31
+ response = OpenStruct.new :base_uri =>
32
+ "http://www.urbandictionary.com/define.php?term=impromptu"
33
+ expected = Urban::Web::Response.new response.base_uri, response
34
+ actual = web_client.send :build_response, response
35
+ assert_equal expected, actual
36
+ end
37
+
38
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: urban_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Erik Nilsen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-16 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.5
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.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
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.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: hoe
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.13'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.13'
83
+ description: |-
84
+ Urban is a command line utility with an API to query definitions from Urban
85
+ Dictionary.
86
+ email:
87
+ - enilsen16@live.com
88
+ executables:
89
+ - urban
90
+ extensions: []
91
+ extra_rdoc_files:
92
+ - HISTORY.rdoc
93
+ - Manifest.txt
94
+ - README.rdoc
95
+ files:
96
+ - ".gemtest"
97
+ - HISTORY.rdoc
98
+ - LICENSE
99
+ - Manifest.txt
100
+ - README.rdoc
101
+ - Rakefile
102
+ - bin/urban
103
+ - lib/urban.rb
104
+ - lib/urban/cli.rb
105
+ - lib/urban/dictionary.rb
106
+ - lib/urban/web.rb
107
+ - test/fixtures/impromptu.html
108
+ - test/fixtures/missing.html
109
+ - test/fixtures/screens/definition.txt
110
+ - test/fixtures/screens/definition_with_url.txt
111
+ - test/fixtures/screens/definitions.txt
112
+ - test/fixtures/screens/help.txt
113
+ - test/fixtures/screens/invalid_option_error.txt
114
+ - test/fixtures/screens/missing_phrase_error.txt
115
+ - test/fixtures/screens/no_internet_error.txt
116
+ - test/test_helper.rb
117
+ - test/urban/cli_test.rb
118
+ - test/urban/dictionary_test.rb
119
+ - test/urban/web_test.rb
120
+ homepage:
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options:
126
+ - "--main"
127
+ - README.rdoc
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.4.5
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Urban is a command line utility with an API to query definitions from Urban
146
+ Dictionary.
147
+ test_files:
148
+ - test/test_helper.rb
149
+ - test/urban/cli_test.rb
150
+ - test/urban/dictionary_test.rb
151
+ - test/urban/web_test.rb