dictionary-rb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "yard", "~> 0.7"
11
+ gem "rdoc", "~> 3.12"
12
+ gem "bundler", "~> 1.0"
13
+ gem "jeweler", "~> 1.8.7"
14
+ gem "nokogiri"
15
+ gem "thor"
16
+ gem "highline"
17
+ gem "redcarpet"
18
+ end
@@ -0,0 +1,69 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.5)
5
+ builder (3.2.2)
6
+ diff-lcs (1.1.3)
7
+ faraday (0.8.9)
8
+ multipart-post (~> 1.2.0)
9
+ git (1.2.6)
10
+ github_api (0.10.1)
11
+ addressable
12
+ faraday (~> 0.8.1)
13
+ hashie (>= 1.2)
14
+ multi_json (~> 1.4)
15
+ nokogiri (~> 1.5.2)
16
+ oauth2
17
+ hashie (2.0.5)
18
+ highline (1.6.20)
19
+ jeweler (1.8.8)
20
+ builder
21
+ bundler (~> 1.0)
22
+ git (>= 1.2.5)
23
+ github_api (= 0.10.1)
24
+ highline (>= 1.6.15)
25
+ nokogiri (= 1.5.10)
26
+ rake
27
+ rdoc
28
+ json (1.8.1)
29
+ jwt (0.1.11)
30
+ multi_json (>= 1.5)
31
+ multi_json (1.8.4)
32
+ multi_xml (0.5.5)
33
+ multipart-post (1.2.0)
34
+ nokogiri (1.5.10)
35
+ oauth2 (0.9.3)
36
+ faraday (>= 0.8, < 0.10)
37
+ jwt (~> 0.1.8)
38
+ multi_json (~> 1.3)
39
+ multi_xml (~> 0.5)
40
+ rack (~> 1.2)
41
+ rack (1.5.2)
42
+ rake (10.1.1)
43
+ rdoc (3.12.2)
44
+ json (~> 1.4)
45
+ redcarpet (3.0.0)
46
+ rspec (2.8.0)
47
+ rspec-core (~> 2.8.0)
48
+ rspec-expectations (~> 2.8.0)
49
+ rspec-mocks (~> 2.8.0)
50
+ rspec-core (2.8.0)
51
+ rspec-expectations (2.8.0)
52
+ diff-lcs (~> 1.1.2)
53
+ rspec-mocks (2.8.0)
54
+ thor (0.16.0)
55
+ yard (0.8.7.3)
56
+
57
+ PLATFORMS
58
+ ruby
59
+
60
+ DEPENDENCIES
61
+ bundler (~> 1.0)
62
+ highline
63
+ jeweler (~> 1.8.7)
64
+ nokogiri
65
+ rdoc (~> 3.12)
66
+ redcarpet
67
+ rspec (~> 2.8.0)
68
+ thor
69
+ yard (~> 0.7)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Ankur Goel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,124 @@
1
+ #dictionary-rb
2
+
3
+ Provides meanings, similar words, usage examples from [Urban Dictionary](http://urbandictionary.com) and [Dictionary Reference](http://dictionary.reference.com/) with **CLI** support.
4
+
5
+ ##Installation
6
+
7
+ `gem install dictionary-rb`
8
+
9
+ ##Usage
10
+ --------
11
+
12
+ ```ruby
13
+ require 'dictionary-rb'
14
+ include DictionaryRB
15
+ word = Word.new('pudding')
16
+ word.meaning
17
+ #=> "a thick, soft dessert, typically containing flour or some other thickener, milk, eggs, a flavoring, and sweetener"
18
+
19
+ #I shall haz more meanings
20
+ word.meanings
21
+ #=> ["a thick, soft dessert, typically containing flour or some other thickener, milk, eggs, a flavoring, and sweetener", "a similar dish unsweetened and served with or as a main dish", "British",.. ]
22
+
23
+
24
+ #Find urban dictionary meaning instead:
25
+ word.urban_meaning
26
+ #=> "a desert given to kids who eat their meat"
27
+
28
+
29
+ #Do more with them
30
+ word.urban.examples
31
+ #=> ["eat your meat! how can you have any pudding if you dont eat your meat?!", " Oh, no! Get the pudding,here comes a gang of Leprechauns and children.", "Adrian says, \"I like pudding.\"", "My cousin is a whore...I think I'll name her Pudding."..]
32
+
33
+ word.dictionary.examples
34
+ #=> ["Turn into an earthen pudding-dish, cover, and cook slowly three and one-half hours.", "Turn in a buttered pudding-dish, and bake thirty-five minutes in a slow oven.", "The proof is in the pudding and if people want to make themselves feel better  for bottle feeding they can go ahead.",..]
35
+
36
+ word.urban.synonyms
37
+ #=>[ "bill cosby", "pie", "fat", "pudd"]
38
+ ```
39
+
40
+ ##CLI
41
+ ------
42
+ * To list all tasks
43
+ `dictionary help`
44
+
45
+ * To list help for a task
46
+ `dictionary help task_name`
47
+ **example** : `dictionary help urban`
48
+
49
+ ### Using `meaning`
50
+
51
+ ```bash
52
+ > dictionary meaning boast
53
+ boast - to speak with exaggeration and excessive pride, especially about oneself
54
+ ```
55
+
56
+ ####Available options and flags:
57
+
58
+ ```bash
59
+ > dictionary help meaning
60
+ Usage:
61
+ dictionary meaning WORD
62
+
63
+ Options:
64
+ [--words=one two three] # Specify multiple WORDS in succession
65
+ [--file] # Pass file(s) as argument(s) instead.
66
+ [--example] # Generate an example too.
67
+ [--similar] # Generate a synonym too.
68
+ [--urban] # Give meaning from Urban Dictionary instead.
69
+ [--write=FILE] # Also write results to specified FILE
70
+
71
+ gives dictionary meaning of the WORD
72
+ ```
73
+
74
+
75
+ ```bash
76
+ > dictionary meaning --words boast sly --urban --example
77
+ boast - To gloat or brag about something - He was boasting about his fly girlfriend.
78
+ sly - To do something cunningly, preferably without any one seeing, apart from your best mate to prove you did it - "Hey Jo, see that girl there? Well we danced and she i" Slllly!"ying my child!!!"
79
+ ```
80
+
81
+ ### Using `similar`
82
+
83
+ ```bash
84
+ > dictionary similar answer
85
+ answer is similar to - check
86
+ ```
87
+
88
+ ####Available options and flags
89
+
90
+ ```bash
91
+ > dictionary help similar
92
+ Usage:
93
+ dictionary similar WORD
94
+
95
+ Options:
96
+ [--words=one two three] # Specify multiple WORDS in succession
97
+ [--file] # Pass file(s) as arugment(s) instead.
98
+ [--urban] # Give similar word from Urban Dictionary
99
+ [--count=N] # Gives N similar words
100
+ [--write=FILE] # Write results to specified FILE
101
+
102
+ gives syonyms of word(s)
103
+ ```
104
+
105
+ ```bash
106
+ > dictionary similar --words boast --urban --count 2
107
+ boast is similar to - annoying, humble
108
+ ```
109
+
110
+ **Similar tasks are present for "urban" and "examples", use `help` to discover**
111
+
112
+
113
+ ##Contribution
114
+
115
+ * Fork the [repository](https://github.com/AnkurGel/dictionary-rb)
116
+ * Have a look at the [API Documentation](). It is pretty well documented with explanations of all methods and modules.
117
+ * Do your changes in your remote branch
118
+ * Create a pull request.
119
+ * Earn a beer! :)
120
+
121
+
122
+ ##Copyright
123
+
124
+ Copyright (c) 2014 Ankur Goel.
@@ -0,0 +1,19 @@
1
+ = dictionary-rb
2
+
3
+ Provides meanings, similar words, usage examples from {Urban Dictionary}[http://urbandictionary.com] and {Dictionary Reference}[http://dictionary.reference.com/] with CLI support.
4
+
5
+ == Contributing to dictionary-rb
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2014 Ankur Goel. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "dictionary-rb"
18
+ gem.homepage = "http://github.com/AnkurGel/dictionary-rb"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Provides urban-dictionary and dictionary-reference meanings.}
21
+ gem.description = %Q{Provides urban-dictionary and dictionary-reference meanings. Equipped with CLI interface and general-purpose library.}
22
+ gem.email = "ankurgel@gmail.com"
23
+ gem.authors = ["Ankur Goel"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ task :default => :spec
35
+
36
+ require 'yard'
37
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,125 @@
1
+ #!/usr/bin/env ruby
2
+ require "thor"
3
+ require "dictionary-rb"
4
+
5
+ class DictionaryRB_CLI < Thor
6
+
7
+ desc "meaning WORD", "gives dictionary meaning of the WORD"
8
+ option :words, :desc => "Specify multiple WORDS in succession", :type => :array
9
+ option :file, :type => :boolean, :desc => "Pass file(s) as argument(s) instead."
10
+ option :example, :type => :boolean, :desc => "Generate an example too."
11
+ option :similar, :type => :boolean, :desc => "Generate a synonym too."
12
+ option :urban, :type => :boolean, :desc => "Give meaning from Urban Dictionary instead."
13
+ option :write, :desc => "Also write results to specified FILE", :banner => "FILE"
14
+ def meaning(word=nil)
15
+ if options[:urban]
16
+ invoke :urban, [word], options
17
+ else
18
+ urban_or_dictionary_meaning(word, options, :dictionary)
19
+ end
20
+ end
21
+
22
+
23
+ desc "urban WORD", "gives UrbanDictionary meaning of the WORD"
24
+ option :words, :desc => "Specify multiple WORDS in succession", :type => :array
25
+ option :file, :type => :boolean, :desc => "Pass file(s) as argument(s) instead."
26
+ option :example, :type => :boolean, :desc => "Generate an example too."
27
+ option :similar, :type => :boolean, :desc => "Generate a synonym too."
28
+ option :write, :desc => "Write results to specified FILE", :banner => "FILE"
29
+ def urban(word=nil)
30
+ urban_or_dictionary_meaning(word, options, :urban)
31
+ end
32
+
33
+
34
+ desc "similar WORD", "gives syonyms of word(s)"
35
+ option :words, :desc => "Specify multiple WORDS in succession", :type => :array
36
+ option :file, :type => :boolean, :desc => "Pass file(s) as arugment(s) instead."
37
+ option :urban, :type => :boolean, :desc => "Give similar word from Urban Dictionary"
38
+ option :count, :type => :numeric, :desc => "Gives N similar words"
39
+ option :write, :desc => "Write results to specified FILE", :banner => "FILE"
40
+ def similar(word=nil)
41
+ write_file = options[:write] ? File.open(File.expand_path(options[:write]), 'wb') : nil
42
+ words = options_simplifier(word, options)
43
+ method = options[:urban] ? :urban : :dictionary
44
+ words.each do |word|
45
+ obj = DictionaryRB::Word.new(word).method(method).call
46
+ results = obj.similar_words.shuffle.first(
47
+ options[:count] ? options[:count] : 1)
48
+ say "#{word} is similar to - #{results.join(', ')}"
49
+ write_file.puts("#{word} is similar to - #{results.join(', ')}") if write_file
50
+ end
51
+ write_file.close if write_file
52
+ end
53
+
54
+
55
+ desc "examples WORD", "gives examples of word(s)"
56
+ option :words, :desc => "Specify multiple WODS in succession", :type => :array
57
+ option :file, :type => :boolean, :desc => "Pass file(s) as arugment(s) instead."
58
+ option :urban, :type => :boolean, :desc => "Give example from Urban Dictionary"
59
+ option :count, :type => :numeric, :desc => "Given N examples"
60
+ option :write, :desc => "Write results to specified FILE", :banner => "FILE"
61
+ def examples(word=nil)
62
+ write_file = options[:write] ? File.open(File.expand_path(options[:write]), 'wb') : nil
63
+ words = options_simplifier(word, options)
64
+ method = options[:urban] ? :urban : :dictionary
65
+ words.each do |word|
66
+ obj = DictionaryRB::Word.new(word).method(method).call
67
+ results = obj.examples.shuffle.first(
68
+ options[:count] ? options[:count] : 1
69
+ )
70
+ say "#{word}'s examples: #{results.join(', ')}"
71
+ write_file.puts("#{word}'s examples: #{results.join(', ')}") if write_file
72
+ end
73
+ write_file.close if write_file
74
+ end
75
+
76
+
77
+ private
78
+ def options_simplifier(word, options)
79
+ if options[:file]
80
+ files_path = options[:words] ? options[:words] : [word]
81
+ words = files_to_words files_path
82
+ elsif options[:words] #no 'file' flag is found, but 'words' options are
83
+ words = sanitize_options_words(options[:words])
84
+ else
85
+ words = [word]
86
+ end
87
+ words
88
+ end
89
+
90
+ def urban_or_dictionary_meaning(word, options, method)
91
+ write_file = options[:write] ? File.open(File.expand_path(options[:write]), 'wb') : nil
92
+ words = options_simplifier(word, options)
93
+ words.each do |word|
94
+ obj = DictionaryRB::Word.new(word).method(method).call
95
+ meaning = obj.meaning
96
+ example = options[:example] ? " - " + obj.examples.first : ""
97
+ similar = options[:similar] ? " - " + obj.similar_words.first : ""
98
+ say "#{word} - #{meaning}#{example}#{similar}"
99
+ write_file.puts("#{word} - #{meaning}#{example}#{similar}") if write_file
100
+ end
101
+ write_file.close if write_file
102
+ end
103
+
104
+ def files_to_words(files)
105
+ files.map do |file|
106
+ file_path = File.expand_path(file)
107
+ raise ArgumentError, say("Error: File #{file_path} doesn't exists!", :red) unless File.exists?(file_path)
108
+ File.open(file_path, 'rb').read.split("\n").map do |word|
109
+ raise ArgumentError, "Error: Not a valid word - #{word}, :red" unless word.strip.match(/^[\w\s]+$/)
110
+ word
111
+ end
112
+ end.flatten.uniq
113
+ end
114
+
115
+ def sanitize_options_words(words)
116
+ words.map do |word|
117
+ raise ArgumentError, "Error: Not a valid word - #{word}, :red" unless word.strip.match(/^[\w\s]+$/)
118
+ word
119
+ end
120
+ end
121
+
122
+
123
+ end
124
+
125
+ DictionaryRB_CLI.start
@@ -0,0 +1,83 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "dictionary-rb"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ankur Goel"]
12
+ s.date = "2014-02-01"
13
+ s.description = "Provides urban-dictionary and dictionary-reference meanings. Equipped with CLI interface and general-purpose library."
14
+ s.email = "ankurgel@gmail.com"
15
+ s.executables = ["dictionary"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.md",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".rspec",
24
+ "Gemfile",
25
+ "Gemfile.lock",
26
+ "LICENSE.txt",
27
+ "README.md",
28
+ "README.rdoc",
29
+ "Rakefile",
30
+ "VERSION",
31
+ "bin/dictionary",
32
+ "dictionary-rb.gemspec",
33
+ "lib/dictionary-rb.rb",
34
+ "lib/dictionary-rb/dictionary.rb",
35
+ "lib/dictionary-rb/string_patch.rb",
36
+ "lib/dictionary-rb/urban.rb",
37
+ "lib/dictionary-rb/word.rb",
38
+ "spec/dictionary-rb_spec.rb",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+ s.homepage = "http://github.com/AnkurGel/dictionary-rb"
42
+ s.licenses = ["MIT"]
43
+ s.require_paths = ["lib"]
44
+ s.rubygems_version = "1.8.23"
45
+ s.summary = "Provides urban-dictionary and dictionary-reference meanings."
46
+
47
+ if s.respond_to? :specification_version then
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
52
+ s.add_development_dependency(%q<yard>, ["~> 0.7"])
53
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
54
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
55
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.7"])
56
+ s.add_development_dependency(%q<nokogiri>, [">= 0"])
57
+ s.add_development_dependency(%q<thor>, [">= 0"])
58
+ s.add_development_dependency(%q<highline>, [">= 0"])
59
+ s.add_development_dependency(%q<redcarpet>, [">= 0"])
60
+ else
61
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
62
+ s.add_dependency(%q<yard>, ["~> 0.7"])
63
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
64
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
65
+ s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
66
+ s.add_dependency(%q<nokogiri>, [">= 0"])
67
+ s.add_dependency(%q<thor>, [">= 0"])
68
+ s.add_dependency(%q<highline>, [">= 0"])
69
+ s.add_dependency(%q<redcarpet>, [">= 0"])
70
+ end
71
+ else
72
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
73
+ s.add_dependency(%q<yard>, ["~> 0.7"])
74
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
75
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
76
+ s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
77
+ s.add_dependency(%q<nokogiri>, [">= 0"])
78
+ s.add_dependency(%q<thor>, [">= 0"])
79
+ s.add_dependency(%q<highline>, [">= 0"])
80
+ s.add_dependency(%q<redcarpet>, [">= 0"])
81
+ end
82
+ end
83
+
@@ -0,0 +1,12 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+ require 'debugger'
4
+ require 'cgi'
5
+ require 'dictionary-rb/word'
6
+ require 'dictionary-rb/dictionary'
7
+ require 'dictionary-rb/urban'
8
+ require 'dictionary-rb/string_patch'
9
+
10
+ module DictionaryRB
11
+
12
+ end
@@ -0,0 +1,86 @@
1
+ module DictionaryRB
2
+
3
+ # Parses the page for a word at {http://dictionary.reference.com Reference Dictionary}
4
+ # and extracts the {#meanings}, {#examples} and {#similar_words} for it.
5
+ # Lot of take care has been taken to prevent it from hitting the {PREFIX ENDPOINT}
6
+ # so as to make it quickly generate the other results, once a URL is parsed.
7
+ class Dictionary
8
+
9
+ # The associated word
10
+ attr_reader :word
11
+ # Endpoint for Reference Dictionary
12
+ PREFIX = "http://dictionary.reference.com/browse/"
13
+
14
+ # @param word [String] The word for Reference Dictionary
15
+ # @example
16
+ # word = DictionaryRB::Dictionary.new('question')
17
+ def initialize(word)
18
+ @word = word if word.is_a? String
19
+ @word = word.word if word.is_a? Word
20
+ @examples = Array.new
21
+ end
22
+
23
+ # Fetches and gives meanings for word from Reference Dictionary
24
+ # @note This method will hit the {PREFIX ENDPOINT} and will consume some time to generate result
25
+ # @example
26
+ # word.meanings
27
+ # #=> ["a sentence in an interrogative form, addressed to someone in order to get information in reply",
28
+ # "a problem for discussion or under discussion",
29
+ # "a matter for investigation",... ]
30
+ # @see #meaning
31
+ # @return [Array] containing the meanings for the word
32
+ def meanings
33
+ url = PREFIX + CGI::escape(@word)
34
+ @doc = Nokogiri::HTML(open(url))
35
+
36
+ nodes = [@doc.css('.luna-Ent .dndata')]
37
+ nodes = [@doc.css('.pbk .luna-Ent')] if nodes.flatten.empty?
38
+
39
+ (nodes ||= []).push(@doc.css(".td3n2")).flatten!
40
+ results = nodes.map(&:text).map do |result|
41
+ result.split ':'
42
+ end.map { |x| x[0].split(/[.;]/) }.flatten.map(&:strip).reject(&:empty?)
43
+ @meaning = results.first
44
+ results
45
+ end
46
+
47
+ # Fetches and gives first meaning for the word
48
+ # @example
49
+ # word.meaning
50
+ # #=> "a sentence in an interrogative form, addressed to someone in order to get information in reply"
51
+ # @see #meanings
52
+ # @return [String] containing the meaning for the word
53
+ def meaning
54
+ meanings.first
55
+ end
56
+
57
+ # Fetches and gives the examples for the word
58
+ # @example
59
+ # word.examples
60
+ # #=> ["There is an easy answer to this question, and it involves some good news and  some bad news.",
61
+ # "Then there was an awkward silence as though they were waiting for me to answer  another question.",..]
62
+
63
+ # @return [Array] containing the examples.
64
+ def examples
65
+ @doc ||= Nokogiri::HTML(open(PREFIX + CGI::escape(@word)))
66
+ @example_results ||= @doc.css('.exsentences').map{ |x| x.text.strip }.reject(&:empty?).flatten
67
+ @example_results #to prevent above computations on repeated call on object
68
+ end
69
+
70
+ # Fetches and gives synonyms for the word
71
+ # @example
72
+ # word.similar_words
73
+ # #=> => ["answer", "inquire", "question mark", "sentence",.. ]
74
+ # @return [Array] containing similar words
75
+ def similar_words
76
+ @doc ||= Nokogiri::HTML(open(PREFIX + CGI::escape(@word)))
77
+ @similar_words = @doc.css("#relatedwords .fla a").map(&:text).reject(&:empty?)
78
+ @similar_words
79
+ end
80
+ alias_method :synonyms, :similar_words
81
+
82
+ def to_s
83
+ sprintf("Free Dictionary (word: %s, meaning: %s", @word, @meaning)
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,21 @@
1
+ class String
2
+ self.class_eval do
3
+ %w(meaning urban_meaning to_urban to_dictionary).each do |method|
4
+ define_method method do
5
+ if obj=method.match(/^to_(.*)/)
6
+ DictionaryRB::Word.new(self).method(obj[1]).call
7
+ else
8
+ DictionaryRB::Word.new(self).method(method).call
9
+ end
10
+ end
11
+ end
12
+
13
+ def to_word
14
+ DictionaryRB::Word.new(self)
15
+ end
16
+ end
17
+
18
+ def respond_to?(method)
19
+ %w(meaning urban_meaning to_urban to_dictionary to_word).include?(method) || super
20
+ end
21
+ end
@@ -0,0 +1,78 @@
1
+ module DictionaryRB
2
+
3
+ # Parses the page for a word at {http://www.urbandictionary.com/ Urban Dictionary}
4
+ # and extracts the {#meanings}, {#examples} and {#similar_words} for it.
5
+ # Lot of take care has been taken to prevent it from hitting the {PREFIX ENDPOINT}
6
+ # so as to make it quickly generate the other results, once a URL is parsed.
7
+ class Urban
8
+
9
+ #The associated word
10
+ attr_reader :word
11
+ # Endpoint for Urban Dictionary
12
+ PREFIX = "http://www.urbandictionary.com/define.php?term="
13
+
14
+ # @param word [String] The word for Urban Dictionary
15
+ # @example
16
+ # word = DictionaryRB::Urban.new('Krunal')
17
+ def initialize(word)
18
+ @word = word if word.is_a? String
19
+ @word = word.word if word.is_a? Word
20
+ end
21
+
22
+ # Fetches and gives the first meaning of the word.
23
+ # @example
24
+ # word.meaning
25
+ # #=> "A fuck, nothing more, just a fuck"
26
+ # @see #meanings
27
+ # @return [String] containing meaning for the word
28
+ def meaning
29
+ meanings.first
30
+ end
31
+
32
+ # Fetches and gives meanings for the word from Urban Dictionary
33
+ # @example
34
+ # word.meanings
35
+ # #=> ["A fuck, nothing more, just a fuck",
36
+ # "Describes someone as being the sexiest beast alive. Anyone who is blessed with the name Krunal should get a medal.",..]
37
+ # @see #meaning
38
+ # @return [Array] containing the meanings for the word.
39
+ def meanings
40
+ url = PREFIX + CGI::escape(@word)
41
+ @doc ||= Nokogiri::HTML(open(url))
42
+
43
+ nodes = @doc.css('div#outer.container div.row.three_columns div.span6 div#content div.box div.inner div.meaning')
44
+ results = nodes.text.split("\n").reject(&:empty?)
45
+ @meaning = results.first
46
+ results
47
+ end
48
+
49
+ # Fetches and gives the examples for the word.
50
+ # @example
51
+ # word.examples
52
+ # #=> ["I hate that guy, he is a krunal", "Hot chick - God i want ur Krunalness\rKrunal - I know...",]
53
+ # @return [Array] containing the examples
54
+ def examples
55
+ @doc ||= Nokogiri::HTML(open(PREFIX + CGI::escape(@word)))
56
+ nodes = @doc.css('div#outer.container div.row.three_columns div.span6 div#content div.box div.inner div.example')
57
+ nodes.text.split("\n").reject(&:empty?)
58
+ end
59
+
60
+ # Fetches and gives synonyms for the word.
61
+ # @example
62
+ # word.synonyms
63
+ # #=> ["agam", "indian", "kerpal",.. ]
64
+ # @see #synonyms
65
+ # @return [Array] containing synonyms for the word
66
+ def similar_words
67
+ @doc ||= Nokogiri::HTML(open(PREFIX + CGI::escape(@word)))
68
+ nodes = @doc.css('div#outer.container div.row.three_columns div.span6 div#content div.tags a')
69
+ nodes.map(&:text).reject(&:empty?)
70
+ end
71
+
72
+ alias_method :synonyms, :similar_words
73
+
74
+ def to_s
75
+ sprintf("Urban Dictionary (word: %s, meaning: %s)", @word, @meaning)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,114 @@
1
+ module DictionaryRB
2
+
3
+ # Contains the word and offers methods for {Urban} and Reference {Dictionary}
4
+ # Also provides methods like {#meaning}, {#urban_meaning} etc for direct access.
5
+ # Lot of precautions has been taken to prevent repeated call on endpoints
6
+ class Word
7
+
8
+ # The associated word
9
+ attr_reader :word
10
+ # Urban Dictionary instance
11
+ attr_reader :urban
12
+ # Reference Dictionary instance
13
+ attr_reader :dictionary
14
+
15
+
16
+ # @param word [String] The word for Dictionary
17
+ # @example
18
+ # word = DictionaryRB::Word.new('boast')
19
+ def initialize(word)
20
+ @word = word
21
+ end
22
+
23
+ # Gives Reference Dictionary instance object for word
24
+ # @example
25
+ # word.dictionary.synonyms
26
+ # @see Dictionary
27
+ # @return [DictionaryRB::Dictionary] the associated object in `Dictionary`
28
+ def dictionary
29
+ @dictionary ||= Dictionary.new(@word)
30
+ end
31
+
32
+ # Gives dictionary meaning for the word
33
+ # @example
34
+ # word.dictionary_meaning
35
+ # #or
36
+ # word.meaning
37
+ # @note This method will hit the {Dictionary::PREFIX ENDPOINT} and will consume some time to generate result
38
+ # @return [String] containing meaning from Reference {Dictionary} for the word
39
+ def dictionary_meaning
40
+ if @dictionary_meaning.nil?
41
+ @dictionary = Dictionary.new(@word)
42
+ meanings = @dictionary.meanings
43
+ if meanings.is_a? Array and not meanings.empty?
44
+ @dictionary_meanings = meanings
45
+ @dictionary_meaning = @dictionary.meaning
46
+ end
47
+ end
48
+ @dictionary_meaning
49
+ end
50
+
51
+ # Fetches all meanings from Reference Dictionary for the word.
52
+ # It is greedily evaluated to save computation when {#dictionary_meaning} is called.
53
+ # @example
54
+ # word.dictionary_meanings
55
+ # #or
56
+ # word.meanings
57
+ # @see #dictionary_meaning
58
+ # @return [Array] array containing the meanings from Reference {Dictionary} for the word
59
+ def dictionary_meanings
60
+ if @dictionary_meanings.nil?
61
+ dictionary_meaning
62
+ end
63
+ @dictionary_meanings
64
+ end
65
+
66
+ # Gives Urban Dictionary instance object for word
67
+ # @example
68
+ # word.urban.synonyms
69
+ # @see Urban
70
+ # @return [DictionaryRB::Urban] the associated object in `Urban`
71
+ def urban
72
+ @urban ||= Urban.new(@word)
73
+ end
74
+
75
+ # Fetches the first meaning from Urban Dictionary for the word.
76
+ # @note This method will hit the {Urban::PREFIX ENDPOINT} and will consume some time to generate result
77
+ # @example
78
+ # word.urban_meaning
79
+ # @see #urban_meanings
80
+ # @return [String] containing meaning from {Urban} Dictionary for the word
81
+ def urban_meaning
82
+ if @urban_meaning.nil?
83
+ @urban = Urban.new(@word)
84
+ meanings = @urban.meanings
85
+ if meanings.is_a?(Array) and not meanings.empty?
86
+ @urban_meanings = meanings
87
+ @urban_meaning = @urban.meaning
88
+ end
89
+ end
90
+ @urban_meaning
91
+ end
92
+
93
+ # Fetches all meanings from the Urban Dictionary for the word.
94
+ # It is greedily evaluated when {#urban_meaning} is called
95
+ # @example
96
+ # word.urban_meanings
97
+ # #=> ['brag', 'lie', 'boaster']
98
+ # @see #urban_meaning
99
+ # @return [Array] array containing meanings from {Urban} Dictionary for the word
100
+ def urban_meanings
101
+ if @urban_meanings.nil?
102
+ urban_meaning
103
+ end
104
+ @urban_meanings
105
+ end
106
+
107
+ alias meaning dictionary_meaning
108
+ alias meanings dictionary_meanings
109
+
110
+ def to_s
111
+ sprintf("%s", word)
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "DictionaryRb" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'dictionary-rb'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,215 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dictionary-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ankur Goel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.8.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.8.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: yard
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.7'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.7'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rdoc
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.12'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '1.0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.8.7
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.8.7
94
+ - !ruby/object:Gem::Dependency
95
+ name: nokogiri
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: thor
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: highline
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: redcarpet
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: Provides urban-dictionary and dictionary-reference meanings. Equipped
159
+ with CLI interface and general-purpose library.
160
+ email: ankurgel@gmail.com
161
+ executables:
162
+ - dictionary
163
+ extensions: []
164
+ extra_rdoc_files:
165
+ - LICENSE.txt
166
+ - README.md
167
+ - README.rdoc
168
+ files:
169
+ - .document
170
+ - .rspec
171
+ - Gemfile
172
+ - Gemfile.lock
173
+ - LICENSE.txt
174
+ - README.md
175
+ - README.rdoc
176
+ - Rakefile
177
+ - VERSION
178
+ - bin/dictionary
179
+ - dictionary-rb.gemspec
180
+ - lib/dictionary-rb.rb
181
+ - lib/dictionary-rb/dictionary.rb
182
+ - lib/dictionary-rb/string_patch.rb
183
+ - lib/dictionary-rb/urban.rb
184
+ - lib/dictionary-rb/word.rb
185
+ - spec/dictionary-rb_spec.rb
186
+ - spec/spec_helper.rb
187
+ homepage: http://github.com/AnkurGel/dictionary-rb
188
+ licenses:
189
+ - MIT
190
+ post_install_message:
191
+ rdoc_options: []
192
+ require_paths:
193
+ - lib
194
+ required_ruby_version: !ruby/object:Gem::Requirement
195
+ none: false
196
+ requirements:
197
+ - - ! '>='
198
+ - !ruby/object:Gem::Version
199
+ version: '0'
200
+ segments:
201
+ - 0
202
+ hash: -378285379
203
+ required_rubygems_version: !ruby/object:Gem::Requirement
204
+ none: false
205
+ requirements:
206
+ - - ! '>='
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ requirements: []
210
+ rubyforge_project:
211
+ rubygems_version: 1.8.23
212
+ signing_key:
213
+ specification_version: 3
214
+ summary: Provides urban-dictionary and dictionary-reference meanings.
215
+ test_files: []