howdy 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -45,5 +45,6 @@ Rake::RDocTask.new do |rdoc|
45
45
  rdoc.rdoc_dir = 'rdoc'
46
46
  rdoc.title = "howdy #{version}"
47
47
  rdoc.rdoc_files.include('README*')
48
- rdoc.rdoc_files.include('lib/**/*.rb')
48
+ rdoc.rdoc_files.include('lib/*/*.rb', 'lib/core/**/*.rb')
49
+ rdoc.options << "-c UTF-8"
49
50
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.1.0
data/bin/how CHANGED
@@ -19,6 +19,13 @@ opts = OptionParser.new do |opts|
19
19
  Howdy::Dictionary.set(dict)
20
20
  end
21
21
 
22
+ opts.on("-l", "--list", "List available dictionaries") do |l|
23
+ Howdy::Dictionary.available.each do |dict|
24
+ puts " #{dict.dict_label} - #{dict.dict_description}"
25
+ end
26
+ exit
27
+ end
28
+
22
29
  opts.on_tail("-h", "--help", "Show this message") do |h|
23
30
  puts opts
24
31
  exit
data/howdy.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{howdy}
8
- s.version = "0.0.3"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["mlomnicki"]
@@ -28,12 +28,12 @@ Gem::Specification.new do |s|
28
28
  "bin/how",
29
29
  "howdy.gemspec",
30
30
  "lib/howdy.rb",
31
- "lib/howdy/dictionaries/base.rb",
31
+ "lib/howdy/base.rb",
32
+ "lib/howdy/core_ext/ruby/string/underscore.rb",
32
33
  "lib/howdy/dictionaries/dict_pl.rb",
33
34
  "lib/howdy/dictionaries/dictionary_com.rb",
34
35
  "lib/howdy/dictionaries/ling_pl.rb",
35
36
  "lib/howdy/dictionaries/urban_dictionary_com.rb",
36
- "lib/howdy/pair.rb",
37
37
  "spec/howdy_spec.rb",
38
38
  "spec/spec_helper.rb"
39
39
  ]
data/lib/howdy/base.rb ADDED
@@ -0,0 +1,199 @@
1
+ require 'open-uri'
2
+ require 'uri'
3
+ require 'nokogiri'
4
+
5
+ module Howdy # :nodoc
6
+
7
+ # Standard Howdy exception
8
+ class HowdyError < Exception; end
9
+
10
+ # Howdy::Dictionary exposes accessors of dictionaries collection.
11
+ module Dictionary
12
+
13
+ # Returns current dictionary class
14
+ def self.current
15
+ @@dictionary ||= default
16
+ end
17
+
18
+ # Set current dictionary. Pass a class as an argument
19
+ # Howdy::Dictionary.set(DictionaryCom)
20
+ def self.set(dictionary)
21
+ @@dictionary = dictionary
22
+ end
23
+
24
+ # Returns default dictionary
25
+ def self.default
26
+ DictionaryCom
27
+ end
28
+
29
+ # Returns array of available dictionaries
30
+ def self.available
31
+ @@available_dictionaries ||= []
32
+ end
33
+
34
+ # Base class for all dictionaries. Inherit from it if you want to implement
35
+ # your own dictionary.
36
+ # Example:
37
+ #
38
+ # class AwesomeDictionary < Howdy::Dictionary::Base
39
+ #
40
+ # url 'http://awesome.example.com?word=#{user_query}'
41
+ # label 'example.com'
42
+ # description 'Most awesome dictionary you've ever seen'
43
+ #
44
+ # def parse
45
+ # document.css('table.results tr td.word') do |cell|
46
+ # result << cell.content
47
+ # end
48
+ # end
49
+ # end
50
+ class Base
51
+
52
+ attr_reader :user_query
53
+
54
+ def self.inherited(base) # :nodoc
55
+ Howdy::Dictionary.available << base
56
+ end
57
+
58
+ def initialize(user_query) # :nodoc
59
+ @user_query = user_query
60
+ end
61
+
62
+ # Sets URL to query the web dictionary.
63
+ # You must pass protocol as well:
64
+ # http://dictionary.com is correct
65
+ # dictionary.com is incorrect
66
+ #
67
+ # URL is interpolated:
68
+ # 'http://dictionary.com/browse/#{user_query}'
69
+ #
70
+ # <tt>user_query</tt> is a string given by the user
71
+ #
72
+ # You may provide own interpolations easily:
73
+ # Example:
74
+ # class Dictionary < Howdy::Dictionary::Base
75
+ # url 'http://awesome.example.com?word=#{user_query}&language=#{lang}'
76
+ # label 'example.com'
77
+ # description 'Interpolations example'
78
+ #
79
+ # def lang
80
+ # File.read('/etc/howdy/language.conf')
81
+ # end
82
+ #
83
+ # def parse
84
+ # # implement parsing
85
+ # end
86
+ #
87
+ # end
88
+ #
89
+ # In that example #{lang} in url will be replaced with content of
90
+ # /etc/howdy/language.conf
91
+ #
92
+ # Indicate the url must be given using <b>SINGLE QUOTES</b>.
93
+ def self.url(dictionary_url)
94
+ @url = dictionary_url
95
+ end
96
+
97
+ # Returns URL of the dictionary
98
+ def self.dict_url
99
+ @url
100
+ end
101
+
102
+ # Sets a label of the dictionary. Label is used when listing available
103
+ # dictionaries
104
+ def self.label(human_name)
105
+ @name = human_name.to_s
106
+ end
107
+
108
+ # Sets a description of the dictionary. Description is used when listing
109
+ # available dictionaries.
110
+ def self.description(dictionary_description)
111
+ @description = dictionary_description
112
+ end
113
+
114
+ # Returns label of the dictionary.
115
+ def self.dict_label
116
+ @name ||= self.label.underscore
117
+ end
118
+
119
+ # Returns description of the dictionary.
120
+ def self.dict_description
121
+ @description ||= ""
122
+ end
123
+
124
+ # Sets an encoding of content returned by HTTP response.
125
+ def self.encoding(enc)
126
+ @encoding = enc.to_s
127
+ end
128
+
129
+ # Returns an encoding of HTTP response. UTF-8 by default.
130
+ def self.doc_encoding
131
+ @encoding ||= 'UTF-8'
132
+ end
133
+
134
+ # Method which transformates raw content received in HTTP response to
135
+ # human-friendly form. Usually you'll use +document+ which exposes
136
+ # the raw content wrapped by Nokogiri parser. Read to nokogiri
137
+ # documentation for details.
138
+ #
139
+ # Each human readable word goes to +result+ array.
140
+ #
141
+ # Example:
142
+ # def parse
143
+ # document.css('table.content tr td.word').each do |row|
144
+ # result << row.content
145
+ # end
146
+ # end
147
+ #
148
+ def parse
149
+ raise Howdy::HowdyError, "Implement parse in your Dictionary::Base subclass"
150
+ end
151
+
152
+ # Returns content of HTTP response wrapped in Nokogiri::HTML backend.
153
+ def document
154
+ @document ||= Nokogiri::HTML(open(URI.escape(interpolated_url)), nil, self.class.doc_encoding)
155
+ end
156
+
157
+ # Array which stores the answers for the user query. Used in +parse+ method.
158
+ # Example:
159
+ # result << 'Polyglot - a person who speaks, writes, or reads a number of languages'
160
+ def result
161
+ @result ||= []
162
+ end
163
+
164
+ # Prints the contents of +result+ array. You may ovveride it in subclass.
165
+ def print
166
+ if result.empty?
167
+ puts "Nothing found"
168
+ else
169
+ result.each do |item|
170
+ puts item.to_s
171
+ end
172
+ end
173
+ end
174
+
175
+ protected
176
+ # Returns url with interpolated literals. Literals are interpolated using
177
+ # corresponding instance methods.
178
+ # Example:
179
+ # url 'http://example.com/word/#{foo}'
180
+ #
181
+ # def foo
182
+ # 'bar'
183
+ # end
184
+ #
185
+ # <tt>interpolated_url</tt> will return:
186
+ # http://example/com/word/bar
187
+ def interpolated_url
188
+ @interpolated_url ||= self.class.dict_url.gsub(/#\{[^\}]+\}/) do |interpolation|
189
+ method = interpolation[2..-2].to_sym # drop leading '#{' and trailing '}'
190
+ raise Howdy::HowdyError.new("Dictionary has to respond to #{method} in order to use it in query") unless respond_to?(method)
191
+ send(method)
192
+ end
193
+ end
194
+
195
+ end
196
+
197
+ end
198
+
199
+ end
@@ -0,0 +1,9 @@
1
+ # Stolen from Steam project by Sven Fuchs http://github.com/svenfuchs/steam
2
+
3
+ class String
4
+
5
+ # Returns underscored string
6
+ def underscore
7
+ self[0, 1].downcase + self[1..-1].gsub(/[A-Z]/) { |c| "_#{c.downcase}" }
8
+ end
9
+ end unless String.method_defined?(:underscore)
@@ -5,6 +5,8 @@ module Howdy
5
5
  class DictPl < Base
6
6
 
7
7
  url 'http://www.dict.pl/dict?word=#{user_query}'
8
+ label 'dict.pl'
9
+ description 'Polish-English dictionary'
8
10
 
9
11
  def parse
10
12
  document.css('table.resTable tr.resRow').each do |row|
@@ -5,6 +5,8 @@ module Howdy
5
5
  class DictionaryCom < Base
6
6
 
7
7
  url 'http://dictionary.reference.com/browse/#{user_query}'
8
+ label 'dictionary.com'
9
+ description 'English dictionary'
8
10
 
9
11
  def parse
10
12
  document.css('div.KonaBody div.results_content table.luna-Ent tr').each do |row|
@@ -5,6 +5,8 @@ module Howdy
5
5
  class LingPl < Base
6
6
 
7
7
  url 'http://www2.ling.pl/lingfeed-ps.php?word=#{user_query}&sType=0&chooseLang=1'
8
+ label 'ling.pl'
9
+ description 'Polish-English dictionary'
8
10
 
9
11
  def parse
10
12
  document.css('div.dictdef')[0..2].each do |definiton|
@@ -1,7 +1,10 @@
1
1
  module Howdy
2
2
  module Dictionary
3
3
  class UrbanDictionaryCom < Base
4
+
4
5
  url 'http://www.urbandictionary.com/define.php?term=#{user_query}'
6
+ label 'urbandictionary.com'
7
+ description 'Slang dictionary'
5
8
 
6
9
  def parse
7
10
  document.css('table#entries tr td.text').each do |cell|
data/lib/howdy.rb CHANGED
@@ -1,15 +1,6 @@
1
- $: << File.join(File.dirname(__FILE__),'howdy')
1
+ $: << File.join(File.dirname(__FILE__), 'howdy')
2
2
 
3
- require 'pair'
4
- require 'dictionaries/base'
5
- require 'dictionaries/dict_pl'
6
- require 'dictionaries/ling_pl'
7
- require 'dictionaries/dictionary_com'
8
- require 'dictionaries/urban_dictionary_com'
9
-
10
- module Howdy
11
-
12
- class HowdyError < Exception; end
13
-
14
- end
3
+ require 'core_ext/ruby/string/underscore'
4
+ require 'base'
5
+ Dir[File.join(File.dirname(__FILE__), 'howdy', 'dictionaries', '*.rb')].each { |dict| require dict }
15
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: howdy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mlomnicki
@@ -51,12 +51,12 @@ files:
51
51
  - bin/how
52
52
  - howdy.gemspec
53
53
  - lib/howdy.rb
54
- - lib/howdy/dictionaries/base.rb
54
+ - lib/howdy/base.rb
55
+ - lib/howdy/core_ext/ruby/string/underscore.rb
55
56
  - lib/howdy/dictionaries/dict_pl.rb
56
57
  - lib/howdy/dictionaries/dictionary_com.rb
57
58
  - lib/howdy/dictionaries/ling_pl.rb
58
59
  - lib/howdy/dictionaries/urban_dictionary_com.rb
59
- - lib/howdy/pair.rb
60
60
  - spec/howdy_spec.rb
61
61
  - spec/spec_helper.rb
62
62
  has_rdoc: true
@@ -1,80 +0,0 @@
1
- require 'open-uri'
2
- require 'uri'
3
- require 'nokogiri'
4
-
5
- module Howdy
6
-
7
- module Dictionary
8
-
9
- def self.current
10
- @@dictionary ||= default_dictionary
11
- end
12
-
13
- def self.set(dictionary)
14
- @@dictionary = dictionary
15
- end
16
-
17
- def self.default_dictionary
18
- DictionaryCom
19
- end
20
-
21
- class Base
22
-
23
- attr_reader :user_query
24
-
25
- def initialize(user_query)
26
- @user_query = user_query
27
- end
28
-
29
- def self.url(dictionary_url)
30
- @url = dictionary_url
31
- end
32
-
33
- def self.dict_url
34
- @url
35
- end
36
-
37
- def self.encoding(enc)
38
- @encoding = enc.to_s
39
- end
40
-
41
- def self.doc_encoding
42
- @encoding ||= 'UTF-8'
43
- end
44
-
45
- def parse
46
- raise Howdy::HowdyError, "Implement parse in your Dictionary::Base subclass"
47
- end
48
-
49
- def document
50
- @document ||= Nokogiri::HTML(open(URI.escape(interpolated_url)), nil, self.class.doc_encoding)
51
- end
52
-
53
- def result
54
- @result ||= []
55
- end
56
-
57
- def print
58
- if result.empty?
59
- puts "Nothing found"
60
- else
61
- result.each do |item|
62
- puts item.to_s
63
- end
64
- end
65
- end
66
-
67
- protected
68
- def interpolated_url
69
- @interpolated_url ||= self.class.dict_url.gsub(/#\{[^\}]+\}/) do |interpolation|
70
- method = interpolation[2..-2].to_sym # drop leading '#{' and trailing '}'
71
- raise Howdy::HowdyError.new("Dictionary has to respond to #{method} in order to use it in query") unless respond_to?(method)
72
- send(method)
73
- end
74
- end
75
-
76
- end
77
-
78
- end
79
-
80
- end
data/lib/howdy/pair.rb DELETED
@@ -1,22 +0,0 @@
1
- module Howdy
2
-
3
- class Pair
4
-
5
- attr_accessor :left, :right
6
-
7
- CENTER = 85
8
-
9
- def initialize(left = nil, right = nil)
10
- @left = left
11
- @right = right
12
- end
13
-
14
- def to_s
15
- "#{@left.center(CENTER)} => #{@right.center(CENTER)}"
16
- end
17
-
18
- alias :inspect :to_s
19
-
20
- end
21
-
22
- end