urban_dictionary 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in urban_dictionary.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,19 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ urban_dictionary (0.0.1)
5
+ nokogiri (~> 1.5.2)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ nokogiri (1.5.2)
11
+ rake (0.9.2.2)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.22)
18
+ rake
19
+ urban_dictionary!
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Urban Dictionary #
2
+
3
+ `urban_dictionary` is a Ruby gem to access word definitions and examples from [Urban Dictionary](http://www.urbandictionary.com/). It also provides a command-line tool for getting definitions.
4
+
5
+ ## Installation ##
6
+
7
+ Run `gem install urban_dictionary`.
8
+
9
+ ## Usage ##
10
+
11
+ Get words using `UrbanDictionary.define` or `UrbanDictionary.random_word`. These methods return an instance of UrbanDictionary::Word, which has a list of entries. Each entry has a definition and an example.
12
+
13
+ require 'urban_dictionary'
14
+
15
+ word = UrbanDictionary.define("QED")
16
+ word.entries.size # => 7
17
+ word.entries.each do |entry|
18
+ puts entry.definition
19
+ puts entry.example
20
+ end
21
+
22
+ ## Command Line ##
23
+
24
+ The urban_dictionary gem includes a command-line interface:
25
+
26
+ > urban_dictionary super salad
27
+ super salad
28
+ -----------
29
+
30
+ 1. A mythical dish of the best salad ever compiled
31
+ ...
32
+ ...
33
+
34
+ You can use the `--random` flag to get the definition of random word:
35
+
36
+ > urban_dictionary --random
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby -KU
2
+ require 'urban_dictionary'
3
+ require 'optparse'
4
+
5
+ options = {}
6
+ option_parser = OptionParser.new do |opts|
7
+ opts.banner = "Usage: urban_dictionary <word or phrase>"
8
+ opts.version = UrbanDictionary::VERSION
9
+
10
+ opts.on("-r", "--random", "Define a random word") do |r|
11
+ options[:random] = r
12
+ end
13
+ end
14
+ option_parser.parse(ARGV)
15
+
16
+ if ARGV.empty? && !options[:random]
17
+ puts option_parser.help
18
+ exit(0)
19
+ end
20
+
21
+ word = if options[:random]
22
+ UrbanDictionary.random_word
23
+ else
24
+ UrbanDictionary.define(ARGV.join(" "))
25
+ end
26
+
27
+ output = []
28
+ output << word
29
+ output << '-' * word.size
30
+ output << ''
31
+ word.entries.each_with_index do |entry, i|
32
+ output << "#{i + 1}. #{entry.definition}"
33
+ output << ""
34
+ output << "Example: #{entry.example}"
35
+ output << ""
36
+ output << ""
37
+ end
38
+ puts output.join("\n")
@@ -0,0 +1,14 @@
1
+ module UrbanDictionary
2
+ class Entry
3
+ attr_reader :definition, :example
4
+
5
+ def initialize(definition, example)
6
+ @definition = definition
7
+ @example = example
8
+ end
9
+
10
+ def to_s
11
+ "#{definition}\n#{example}"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module UrbanDictionary
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,35 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+
4
+ module UrbanDictionary
5
+ class Word
6
+ attr_reader :word, :entries
7
+
8
+ # Can raise SocketError if unable to connect to specified URL
9
+ def self.from_url(url)
10
+ html = open(url).read
11
+ doc = Nokogiri::HTML(html)
12
+
13
+ word = doc.css('.word').first.content.strip
14
+ definitions = doc.css('.definition').map{|d| d.content.strip }
15
+ examples = doc.css('.example').map{|e| e.content.strip }
16
+ entries = definitions.zip(examples).map{|d,e| Entry.new(d, e)}
17
+
18
+ Word.new(word, entries)
19
+ end
20
+
21
+ def initialize(word, entries)
22
+ @word = word
23
+ @entries = entries
24
+ end
25
+
26
+ def to_s
27
+ @word
28
+ end
29
+
30
+ def size
31
+ @word.size
32
+ end
33
+ alias :length :size
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+
4
+ require 'urban_dictionary/version'
5
+ require 'urban_dictionary/word'
6
+ require 'urban_dictionary/entry'
7
+
8
+ module UrbanDictionary
9
+ DEFINE_URL = 'http://www.urbandictionary.com/define.php'
10
+ RANDOM_URL = 'http://www.urbandictionary.com/random.php'
11
+
12
+ def self.define(str)
13
+ Word.from_url("#{DEFINE_URL}?term=#{URI.encode(str)}")
14
+ end
15
+
16
+ def self.random_word
17
+ url = URI.parse(RANDOM_URL)
18
+ req = Net::HTTP::Get.new(url.path)
19
+ rsp = Net::HTTP.start(url.host, url.port) {|http|
20
+ http.request(req)
21
+ }
22
+
23
+ Word.from_url(rsp['location'])
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "urban_dictionary/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "urban_dictionary"
7
+ s.version = UrbanDictionary::VERSION
8
+ s.authors = ["Ryan Greenberg"]
9
+ s.email = ["ryangreenberg@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = "Interface to urbandictionary.com"
12
+ s.description = s.summary
13
+
14
+ s.rubyforge_project = "urban_dictionary"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency "nokogiri", "~> 1.5.2"
22
+
23
+ s.add_development_dependency "bundler", "~> 1.0.22"
24
+ s.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: urban_dictionary
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Ryan Greenberg
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-08-05 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 7
28
+ segments:
29
+ - 1
30
+ - 5
31
+ - 2
32
+ version: 1.5.2
33
+ requirement: *id001
34
+ prerelease: false
35
+ name: nokogiri
36
+ type: :runtime
37
+ - !ruby/object:Gem::Dependency
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 59
44
+ segments:
45
+ - 1
46
+ - 0
47
+ - 22
48
+ version: 1.0.22
49
+ requirement: *id002
50
+ prerelease: false
51
+ name: bundler
52
+ type: :development
53
+ - !ruby/object:Gem::Dependency
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirement: *id003
64
+ prerelease: false
65
+ name: rake
66
+ type: :development
67
+ description: Interface to urbandictionary.com
68
+ email:
69
+ - ryangreenberg@gmail.com
70
+ executables:
71
+ - urban_dictionary
72
+ extensions: []
73
+
74
+ extra_rdoc_files: []
75
+
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - README.md
81
+ - Rakefile
82
+ - bin/urban_dictionary
83
+ - lib/urban_dictionary.rb
84
+ - lib/urban_dictionary/entry.rb
85
+ - lib/urban_dictionary/version.rb
86
+ - lib/urban_dictionary/word.rb
87
+ - urban_dictionary.gemspec
88
+ has_rdoc: true
89
+ homepage: ""
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options: []
94
+
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ requirements: []
116
+
117
+ rubyforge_project: urban_dictionary
118
+ rubygems_version: 1.6.2
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Interface to urbandictionary.com
122
+ test_files: []
123
+