dephine 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/dephine +1 -27
- data/dephine.gemspec +1 -1
- data/lib/dephine.rb +2 -48
- data/lib/dephine/cli.rb +40 -0
- data/lib/dephine/dictionary.rb +49 -0
- data/lib/dephine/version.rb +1 -1
- metadata +5 -3
data/bin/dephine
CHANGED
@@ -3,30 +3,4 @@
|
|
3
3
|
$:.unshift File.join(File.expand_path(File.dirname(__FILE__)), %w[.. lib])
|
4
4
|
require 'dephine'
|
5
5
|
|
6
|
-
|
7
|
-
Dephine, Google dictionary in terminal
|
8
|
-
Usage: dephine [word]
|
9
|
-
END
|
10
|
-
|
11
|
-
if ARGV.empty?
|
12
|
-
puts banner
|
13
|
-
else
|
14
|
-
word = Dephine::Dictionary.new(ARGV[0])
|
15
|
-
|
16
|
-
if word.meanings.empty?
|
17
|
-
abort("Word not found in the dictionary!")
|
18
|
-
end
|
19
|
-
|
20
|
-
puts ARGV[0]
|
21
|
-
word.meanings.each do |m|
|
22
|
-
puts
|
23
|
-
puts "#{m[:type].downcase} #{m[:phonetic]}"
|
24
|
-
m[:meanings].each do |d|
|
25
|
-
puts " #{d[:text]}"
|
26
|
-
d[:examples].each do |eg|
|
27
|
-
puts " - #{eg}"
|
28
|
-
end
|
29
|
-
puts
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
6
|
+
Dephine::CLI.start(ARGV)
|
data/dephine.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |gem|
|
|
9
9
|
gem.authors = ["Sina Samavati"]
|
10
10
|
gem.email = ["contact@s1n4.com"]
|
11
11
|
gem.summary = %q{Google dictionary in terminal}
|
12
|
-
gem.homepage = "
|
12
|
+
gem.homepage = "http://s1n4.com/dephine"
|
13
13
|
|
14
14
|
gem.files = `git ls-files`.split($/)
|
15
15
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
data/lib/dephine.rb
CHANGED
@@ -1,49 +1,3 @@
|
|
1
1
|
require 'dephine/version'
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
|
5
|
-
module Dephine
|
6
|
-
class Dictionary
|
7
|
-
# Public: Returns the Array meanings of the word.
|
8
|
-
attr_reader :meanings
|
9
|
-
|
10
|
-
# Public: Initialize some definitions of a word.
|
11
|
-
#
|
12
|
-
# word - A String to be defined.
|
13
|
-
def initialize(word)
|
14
|
-
raise ArgumentError, "Word shouldn't be blank" if word.empty?
|
15
|
-
|
16
|
-
@meanings = []
|
17
|
-
url = "https://www.google.com/dictionary/json?callback=define&q=#{word}&" \
|
18
|
-
"sl=en&tl=en&restrict=pr%2Cde&client=te"
|
19
|
-
|
20
|
-
# fetch and parse the json document
|
21
|
-
content = JSON.parse(open(url).read[7..-11])
|
22
|
-
|
23
|
-
content['primaries'].each do |primary|
|
24
|
-
@meanings << {
|
25
|
-
type: primary['terms'][0]['labels'][0]['text'],
|
26
|
-
phonetic: primary['terms'][1]['text'],
|
27
|
-
meanings: []
|
28
|
-
}
|
29
|
-
|
30
|
-
# add meanings
|
31
|
-
primary['entries'][1..-1].each do |entry|
|
32
|
-
@meanings[-1][:meanings] << {
|
33
|
-
text: entry['terms'][0]['text'].gsub('x27', "'") \
|
34
|
-
.gsub(/x3.*?3e/, ''),
|
35
|
-
examples: []
|
36
|
-
}
|
37
|
-
|
38
|
-
# add examples of a meaning
|
39
|
-
if entry.has_key?('entries')
|
40
|
-
entry['entries'].each do |eg|
|
41
|
-
@meanings[-1][:meanings][-1][:examples] << eg['terms'][0]['text'] \
|
42
|
-
.gsub('x27', "'").gsub(/x3.*?3e/, '')
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end if primary.has_key?('entries')
|
46
|
-
end if content.has_key?('primaries')
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
2
|
+
require 'dephine/dictionary'
|
3
|
+
require 'dephine/cli'
|
data/lib/dephine/cli.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module Dephine
|
2
|
+
class CLI
|
3
|
+
class << self
|
4
|
+
def start(args)
|
5
|
+
banner = <<-END
|
6
|
+
Dephine, Google dictionary in terminal
|
7
|
+
Usage: dephine [word]
|
8
|
+
END
|
9
|
+
|
10
|
+
if args.empty?
|
11
|
+
puts banner
|
12
|
+
else
|
13
|
+
define(args[0])
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def define(arg)
|
19
|
+
word = Dephine::Dictionary.new(arg)
|
20
|
+
|
21
|
+
if word.meanings.empty?
|
22
|
+
abort("Word not found in the dictionary!")
|
23
|
+
end
|
24
|
+
|
25
|
+
puts arg
|
26
|
+
word.meanings.each do |m|
|
27
|
+
puts
|
28
|
+
puts "#{m[:type].downcase} #{m[:phonetic]}"
|
29
|
+
m[:meanings].each do |d|
|
30
|
+
puts " #{d[:text]}"
|
31
|
+
d[:examples].each do |eg|
|
32
|
+
puts " - #{eg}"
|
33
|
+
end
|
34
|
+
puts
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Dephine
|
5
|
+
class Dictionary
|
6
|
+
# Public: Returns the Array meanings of the word.
|
7
|
+
attr_reader :meanings
|
8
|
+
|
9
|
+
# Public: Initialize some definitions of a word.
|
10
|
+
#
|
11
|
+
# word - A String to be defined.
|
12
|
+
def initialize(word)
|
13
|
+
raise ArgumentError, "Word shouldn't be blank" if word.empty?
|
14
|
+
|
15
|
+
@meanings = []
|
16
|
+
url = "https://www.google.com/dictionary/json?callback=define&q=#{word}&" \
|
17
|
+
"sl=en&tl=en&restrict=pr%2Cde&client=te"
|
18
|
+
|
19
|
+
# fetch and parse the json document
|
20
|
+
content = JSON.parse(open(url).read[7..-11])
|
21
|
+
|
22
|
+
content['primaries'].each do |primary|
|
23
|
+
@meanings << {
|
24
|
+
type: primary['terms'][0]['labels'][0]['text'],
|
25
|
+
phonetic: primary['terms'][1]['text'],
|
26
|
+
meanings: []
|
27
|
+
}
|
28
|
+
|
29
|
+
# add meanings
|
30
|
+
primary['entries'][1..-1].each do |entry|
|
31
|
+
@meanings[-1][:meanings] << {
|
32
|
+
text: entry['terms'][0]['text'].gsub('x27', "'") \
|
33
|
+
.gsub(/x3.*?3e/, ''),
|
34
|
+
examples: []
|
35
|
+
}
|
36
|
+
|
37
|
+
# add examples of a meaning
|
38
|
+
if entry.has_key?('entries')
|
39
|
+
entry['entries'].each do |eg|
|
40
|
+
@meanings[-1][:meanings][-1][:examples] << eg['terms'][0]['text'] \
|
41
|
+
.gsub('x27', "'").gsub(/x3.*?3e/, '')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end if primary.has_key?('entries')
|
46
|
+
end if content.has_key?('primaries')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/dephine/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dephine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-15 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|
@@ -27,8 +27,10 @@ files:
|
|
27
27
|
- bin/dephine
|
28
28
|
- dephine.gemspec
|
29
29
|
- lib/dephine.rb
|
30
|
+
- lib/dephine/cli.rb
|
31
|
+
- lib/dephine/dictionary.rb
|
30
32
|
- lib/dephine/version.rb
|
31
|
-
homepage:
|
33
|
+
homepage: http://s1n4.com/dephine
|
32
34
|
licenses: []
|
33
35
|
post_install_message:
|
34
36
|
rdoc_options: []
|