urban 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 279344d172dfd0791a5c6d1bfa1bb08e46689562
4
+ data.tar.gz: 434a62d05989c3b064daa500a8af7e700d7016be
5
+ SHA512:
6
+ metadata.gz: 6f11ea8f3569c5500380a8f478137b07a7c8c2955dfd87ed4859a439b6a50e7ab71e9828e3a0bdef8a8eefac767701f6d38d45d7480b236b6328e35f1c0b1a45
7
+ data.tar.gz: 3dd0ff3b40e0873901e50a38b9f4f0997028dbea37a4ea7e129db8a490ce5890d6cf85e8710fbf1374b1300b4285a9dbb0d61f263066948edb6f3287dc60a407
data/.travis.yml CHANGED
@@ -1,3 +1,11 @@
1
+ language: ruby
1
2
  rvm:
2
- - 1.8.7
3
- - 1.9.2
3
+ - ree
4
+ - jruby-18mode
5
+ - jruby-19mode
6
+ - rbx-18mode
7
+ - rbx-19mode
8
+ - "1.8.7"
9
+ - "1.9.2"
10
+ - "1.9.3"
11
+ - "2.0.0"
data/HISTORY.rdoc CHANGED
@@ -1,25 +1,40 @@
1
+ === 2.0.0
2
+
3
+ * Major enhancements:
4
+
5
+ * Remove deperecated `--list` and `-l` flags.
6
+ * Remove official support for Ruby 1.8.6.
7
+
8
+ * Bug fixes
9
+
10
+ * Urban Dictionary redesigned the website. Xpath selectors were out of date
11
+ and would not return results properly.
12
+ * Undefined words now show a cleaner error. This was apparently not fixed
13
+ in the prior release.
14
+
1
15
  === 1.0.0
2
16
 
3
17
  * Major enhancements:
4
18
 
5
- * Change `list` flag to the `all` flag for more clarity
6
- * Remove query from Urban::Web replace with random and search
7
- * Add `url` flag to print the url of the definition
19
+ * Change `--list` flag to the `--all` flag for more clarity.
20
+ * Deprecate `--list` flag.
21
+ * Remove query from Urban::Web replace with random and search.
22
+ * Add `url` flag to print the url of the definition.
8
23
 
9
24
  * Minor enhancements:
10
25
 
11
- * Add this history file
12
- * Add Urban::Web#fetch for fetching pages from urban dictionary
13
- * Add examples to help
14
- * Remove require 'rubygems' from program and lib
15
- * Test now only stubs singleton instead of class
16
- * Use ~> instead of >= on dependencies
17
- * Replace OpenStruct in Urban::Dictionary with plain Struct
18
- * Move Nokogiri call to Dictionary#process
19
-
20
- * Bug fix:
21
-
22
- * Passing -v or --version no longer prints help
23
- * Undefined words now show a clean error
24
- * No internet connection now shows a clean error
25
- * Invalid options now show a clean error
26
+ * Add this history file.
27
+ * Add Urban::Web#fetch for fetching pages from urban dictionary.
28
+ * Add examples to help.
29
+ * Remove require 'rubygems' from program and lib.
30
+ * Test now only stubs singleton instead of class.
31
+ * Use ~> instead of >= on dependencies.
32
+ * Replace OpenStruct in Urban::Dictionary with plain Struct.
33
+ * Move Nokogiri call to Dictionary#process.
34
+
35
+ * Bug fixs:
36
+
37
+ * Passing -v or --version no longer prints help.
38
+ * Undefined words now show a clean error.
39
+ * No internet connection now shows a clean error.
40
+ * Invalid options now show a clean error.
data/Manifest.txt ADDED
@@ -0,0 +1,25 @@
1
+ .travis.yml
2
+ HISTORY.rdoc
3
+ LICENSE
4
+ Manifest.txt
5
+ README.rdoc
6
+ Rakefile
7
+ bin/urban
8
+ lib/urban.rb
9
+ lib/urban/cli.rb
10
+ lib/urban/dictionary.rb
11
+ lib/urban/web.rb
12
+ script/test
13
+ test/fixtures/impromptu.html
14
+ test/fixtures/missing.html
15
+ test/fixtures/screens/definition.txt
16
+ test/fixtures/screens/definition_with_url.txt
17
+ test/fixtures/screens/definitions.txt
18
+ test/fixtures/screens/help.txt
19
+ test/fixtures/screens/invalid_option_error.txt
20
+ test/fixtures/screens/missing_phrase_error.txt
21
+ test/fixtures/screens/no_internet_error.txt
22
+ test/test_helper.rb
23
+ test/urban/cli_test.rb
24
+ test/urban/dictionary_test.rb
25
+ test/urban/web_test.rb
data/README.rdoc CHANGED
@@ -1,17 +1,22 @@
1
1
  = Urban
2
- {<img src="https://secure.travis-ci.org/tmiller/urban.png"/>}[http://travis-ci.org/tmiller/urban]
3
2
 
4
- Urban is a command line tool that allows you to look up definitions or pull a
5
- random definition from {Urban Dictionary}[http://www.urbandictionary.com].
3
+ * Home :: http://github.com/tmiller/urban
4
+ * Bug Tracker :: http://github.com/tmiller/urban/issues
6
5
 
7
- * {Git}[http://github.com/tmiller/urban]
8
- * {Bug Tracker}[http://github.com/tmiller/urban/issues]
6
+ == Description:
9
7
 
10
- == Requirements
8
+ Urban is a command line utility with an API to query definitions from Urban
9
+ Dictionary.
11
10
 
12
- * Ruby >= 1.8.6
11
+ == BADGES
13
12
 
14
- == Installation
13
+ {<img src="https://secure.travis-ci.org/tmiller/urban.png"/>}[http://travis-ci.org/tmiller/urban] {<img src="https://codeclimate.com/github/tmiller/urban.png" />}[https://codeclimate.com/github/tmiller/urban]
14
+
15
+ == REQUIREMENTS
16
+
17
+ * Ruby >= 1.8.7
18
+
19
+ == INSTALLATION
15
20
 
16
21
  With Rubygems:
17
22
 
@@ -23,7 +28,7 @@ With git and local working copy
23
28
  $ cd urban
24
29
  $ sudo rake install
25
30
 
26
- == CLI Usage
31
+ == CLI USAGE
27
32
 
28
33
  === 1. Look up a definition
29
34
 
@@ -49,9 +54,9 @@ With git and local working copy
49
54
  $ urban --help
50
55
  $ urban --version
51
56
 
52
- == API Ussage
57
+ == API USAGE
53
58
 
54
- requre 'urban'
59
+ require 'urban'
55
60
 
56
61
  # Search for a word
57
62
  entry = Urban::Dictionary.search('impromtpu')
@@ -68,10 +73,6 @@ With git and local working copy
68
73
  puts definition
69
74
  end
70
75
 
71
- == To Do
72
-
73
- * Add YARD documentation for API
74
-
75
76
  ---
76
77
 
77
78
  Copyright (c) 2011 Thomas Miller. See LICENSE for details.
data/Rakefile CHANGED
@@ -1,38 +1,21 @@
1
- require 'rubygems'
2
- require 'bundler/gem_helper'
3
- Bundler::GemHelper.install_tasks
1
+ # -*- ruby -*-
2
+ require "rubygems"
3
+ require "hoe"
4
4
 
5
+ Hoe.plugin :minitest
6
+ Hoe.plugin :git
5
7
 
6
- task :default => :test
8
+ Hoe.spec "urban" do
9
+ self.readme_file = "README.rdoc"
10
+ self.history_file = "History.rdoc"
7
11
 
8
- require 'rake/testtask'
9
- Rake::TestTask.new do |t|
10
- t.libs << "test"
11
- t.test_files = FileList['test/**/*_test.rb']
12
- t.verbose = true
13
- t.warning = true
14
- end
12
+ license "MIT"
13
+ developer("Tom Miller", "jackerran@gmail.com")
15
14
 
16
- Rake::TestTask.new do |t|
17
- t.name = 'test:cli'
18
- t.libs << "test"
19
- t.test_files = FileList['test/**/cli_test.rb']
20
- t.verbose = true
21
- t.warning = true
22
- end
15
+ dependency 'nokogiri', '~> 1.5.0'
23
16
 
24
- Rake::TestTask.new do |t|
25
- t.name = 'test:dictionary'
26
- t.libs << "test"
27
- t.test_files = FileList['test/**/dictionary_test.rb']
28
- t.verbose = true
29
- t.warning = true
17
+ dependency 'rake', '~> 10.0', :development
18
+ dependency 'minitest', '~> 5.0', :development
30
19
  end
31
20
 
32
- Rake::TestTask.new do |t|
33
- t.name = 'test:web'
34
- t.libs << "test"
35
- t.test_files = FileList['test/**/web_test.rb']
36
- t.verbose = true
37
- t.warning = true
38
- end
21
+ # vim: syntax=ruby
data/bin/urban CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'urban/version'
3
+ require 'urban'
4
4
  require 'urban/cli'
5
5
 
6
6
  Urban::CLI.new.run(ARGV)
data/lib/urban.rb CHANGED
@@ -1,3 +1,6 @@
1
1
  require 'urban/dictionary'
2
- require 'urban/version'
3
2
  require 'urban/web'
3
+
4
+ module Urban
5
+ VERSION = '2.0.0'
6
+ end
data/lib/urban/cli.rb CHANGED
@@ -1,7 +1,6 @@
1
- require 'optparse'
2
- require 'ostruct'
3
- require 'socket'
4
- require 'urban/dictionary'
1
+ require "optparse"
2
+ require "ostruct"
3
+ require "socket"
5
4
 
6
5
  module Urban
7
6
  class CLI
@@ -9,94 +8,89 @@ module Urban
9
8
  attr_accessor :dictionary
10
9
 
11
10
  def initialize
12
- @dictionary = Urban::Dictionary
11
+ @dictionary = Urban::Dictionary.new
13
12
  end
14
13
 
15
14
  def run(args = ARGV)
16
- begin
17
- options = parse(args)
18
- output = case
19
- when options.help ; options.help_screen
20
- when options.version ; "Urban #{Urban::VERSION} (c) Thomas Miller"
21
- when options.random ; dictionary.random
22
- when !options.phrase.empty? ; dictionary.search(options.phrase)
23
- else ; options.help_screen
24
- end
25
-
26
- if output.respond_to?(:phrase)
27
- if output.definitions
28
- print_entry(output, options)
29
- else
30
- $stderr.puts "urban: no definitions found for #{entry.phrase.upcase}."
31
- end
32
- else
33
- puts output
34
- end
35
-
36
- rescue SocketError
37
- $stderr.puts 'urban: no internet connection available.'
38
- rescue OptionParser::InvalidOption => e
39
- $stderr.puts "urban: #{e.message}\nTry `urban --help' for more information."
40
- rescue Exception => e
41
- $stderr.puts e.message
15
+
16
+ options = parse(args)
17
+ results = lookup(options)
18
+
19
+ case
20
+ when results.definitions
21
+ generate_output results, options
22
+ when results.phrase
23
+ error "no definitions found for #{results.phrase.upcase}."
24
+ else
25
+ $stdout.puts options.version ? version : usage
42
26
  end
27
+
28
+ rescue SocketError
29
+ error "no internet connection available."
30
+ rescue OptionParser::InvalidOption => e
31
+ error "#{e.message}\nTry `urban --help' for more information."
32
+ rescue Object => e
33
+ error e.message
43
34
  end
44
35
 
45
36
  private
46
37
 
47
- def print_error(entry)
38
+ def error(message)
39
+ $stderr.puts "urban: #{message}"
48
40
  end
49
41
 
50
- def print_entry(entry, options)
51
- puts "WARNING: --list and -l are deprecated please use --all or -a instead" if options.list
52
- puts "\n#{entry.phrase.upcase}\n\n"
42
+ def generate_output(entry, options)
43
+ output = "\n#{entry.phrase.upcase}\n\n"
53
44
  if options.all
54
- entry.definitions.each { |definition| puts "#{definition}\n\n" }
45
+ output << "#{entry.definitions.join("\n\n")}\n\n"
55
46
  else
56
- puts "#{entry.definitions.first}\n\n"
47
+ output << "#{entry.definitions.first}\n\n"
57
48
  end
58
- puts "URL: #{entry.url}\n\n" if options.url
49
+ output << "URL: #{entry.url}\n\n" if options.url
50
+
51
+ $stdout.puts output
59
52
  end
60
53
 
61
54
  def parse(args)
62
55
  options = OpenStruct.new
63
- options.random = options.all = options.version = options.help = false
64
-
65
- opts = OptionParser.new do |o|
66
- o.banner = <<-EOB
67
- Usage: urban [OPTION]... [PHRASE]
68
- Search http://urbandictionary.com for definitions of phrases
69
-
70
- EOB
71
-
72
- o.separator "Options:"
73
- o.on('-a', '--all', 'List all definitions') do
74
- options.all = true
75
- end
76
56
 
77
- o.on('-r', '--random', 'Return a random phrase and definition') do
78
- options.random = true
79
- end
57
+ options_parser = OptionParser.new do |o|
58
+ o.on("-a", "--all") { options.all = true }
59
+ o.on("-r", "--random") { options.random = true }
60
+ o.on("-u", "--url") { options.url = true }
61
+ o.on("-h", "--help")
62
+ o.on("-v", "--version") { options.version = true }
63
+ end
80
64
 
81
- o.on('-u', '--url', "Print the definition's url after the definition") do
82
- options.url = true
83
- end
65
+ options_parser.parse!(args)
66
+ options.phrase = args.join(" ")
67
+ options.search = !options.phrase.empty?
68
+ options
69
+ end
84
70
 
85
- o.on('-h', '--help', 'Show this message') do
86
- options.help = true
87
- end
71
+ def lookup(options)
72
+ case
73
+ when options.random then dictionary.random
74
+ when options.search then dictionary.search(options.phrase)
75
+ else OpenStruct.new
76
+ end
77
+ end
88
78
 
89
- o.on('-v', '--version', 'Show version information') do
90
- options.version = true
91
- end
79
+ def version
80
+ "Urban #{Urban::VERSION} (c) Thomas Miller"
81
+ end
92
82
 
93
- o.on('-l', '--list', 'DEPRECATED please use --all or -a instead') do
94
- options.list = true
95
- options.all = true
96
- end
97
- end
83
+ def usage
84
+ <<-EOS
85
+ Usage: urban [OPTION]... [PHRASE]
86
+ Search http://urbandictionary.com for definitions of phrases
98
87
 
99
- examples = <<-EOE
88
+ Options:
89
+ -a, --all List all definitions
90
+ -r, --random Return a random phrase and definition
91
+ -u, --url Print the definition's url after the definition
92
+ -h, --help Show this message
93
+ -v, --version Show version information
100
94
 
101
95
  Examples:
102
96
  urban cookie monster Search for "cookie monster" and print its
@@ -107,11 +101,7 @@ Examples:
107
101
  urban -ra Print a random phrase and all of its available
108
102
  definitions
109
103
 
110
- EOE
111
- opts.parse!(args)
112
- options.phrase = args.join(' ')
113
- options.help_screen = opts.help + examples
114
- options
104
+ EOS
115
105
  end
116
106
  end
117
107
  end
@@ -1,12 +1,10 @@
1
- require 'nokogiri'
2
- require 'urban/version'
3
- require 'urban/web'
1
+ require "nokogiri"
4
2
 
5
3
  module Urban
6
- module Dictionary
7
- extend self
4
+ class Dictionary
8
5
 
9
6
  Entry = Struct.new(:phrase, :definitions, :url)
7
+
10
8
  attr_writer :web_service
11
9
 
12
10
  def random
@@ -18,7 +16,7 @@ module Urban
18
16
  end
19
17
 
20
18
  def web_service
21
- @web_service ||= Urban::Web
19
+ @web_service ||= Urban::Web.new
22
20
  end
23
21
 
24
22
  private
@@ -28,15 +26,17 @@ module Urban
28
26
  if not_defined = document.at_xpath('//div[@id="not_defined_yet"]/i')
29
27
  Entry.new(not_defined.content.strip, nil, nil)
30
28
  else
31
- Entry.new( document.at_xpath('//td[@class="word"][1]').content.strip ,
32
- parse_definitions(document),
33
- response.url)
29
+ Entry.new(
30
+ document.at_xpath('//a[@class="word"][1]').content.strip,
31
+ parse_definitions(document),
32
+ response.url
33
+ )
34
34
  end
35
35
  end
36
36
 
37
37
  def parse_definitions(document)
38
- definitions = document.xpath('//td/div[@class="definition"]').map do |node|
39
- node.xpath('//br').each { |br| br.replace(Nokogiri::XML::Text.new("\n", node.document)) };
38
+ document.xpath('//div[@class="def-panel"]/div[@class="meaning"]').map do |node|
39
+ node.xpath("//br").each { |br| br.replace(Nokogiri::XML::Text.new("\n", node.document)) };
40
40
  node.content.strip
41
41
  end
42
42
  end