Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96b1d83
commit 4215cef
Showing
5 changed files
with
79 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,39 @@ | ||
require "gdefine/version" | ||
require_relative 'gdefine/version' | ||
require 'nokogiri' | ||
require 'open-uri' | ||
|
||
|
||
module Gdefine | ||
# Your code goes here... | ||
|
||
# Using iPhone 5 User agent for smaller page and so Google definition is shown | ||
# (as the open-uri default does has Google not showing the the defition) | ||
USER_AGENT = 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us)' | ||
|
||
def self.define word | ||
html = open "https://www.google.com/search?q=define+#{word}", 'User-Agent' => USER_AGENT | ||
format parse(html) | ||
end | ||
|
||
def self.parse html | ||
doc = Nokogiri::HTML html | ||
|
||
doc.css('.ts tr').inject([]) do |entries, entry| | ||
if entry.css('td').length > 1 # ignore the spacer row | ||
cells = entry.css('td') | ||
|
||
entry = {} | ||
entry[:type] = cells.shift.text | ||
entry[:definition] = cells.text | ||
entries << entry | ||
end | ||
entries | ||
end | ||
end | ||
|
||
def self.format entries | ||
output = entries.inject('') do |total, entry| | ||
total += "\n #{entry[:type]} #{entry[:definition]} \n" | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require_relative '../lib/gdefine' | ||
|
||
HTML = <<DEFINITION | ||
<table class="ts"><tbody><tr> | ||
<td valign="top" width="80px">Noun:</td><td valign="top"> | ||
<table class="ts"><tbody><tr><td>An intense feeling of deep affection: "their <b>love for</b> their country".</td></tr></tbody></table></td></tr><tr height="1px" bgcolor="#ddd"><td height="1px" colspan="2"></td></tr><tr><td valign="top" width="80px">Verb:</td><td valign="top"><table class="ts"><tbody><tr><td>Feel a deep romantic or sexual attachment to (someone): "do you love me?".</td></tr></tbody></table></td></tr><tr height="1px" bgcolor="#ddd"><td height="1px" colspan="2"></td></tr><tr><td valign="top" width="80px">Synonyms:</td><td valign="top"><div><span><i>noun</i>. </span>affection - fondness - darling - passion</div><div><span><i>verb</i>. </span>like - be fond of - fancy - adore</div></td></tr></tbody></table> | ||
DEFINITION | ||
|
||
describe Gdefine do | ||
|
||
describe 'parse' do | ||
it 'should correctly return key value definitions' do | ||
parsed = Gdefine.parse(HTML) | ||
parsed.should have(3).items | ||
parsed.first.should be_a(Hash) | ||
end | ||
end | ||
|
||
describe 'format' do | ||
it 'should return string of definitions' do | ||
entries = [ | ||
{ type: 'Noun:', definition: 'an intense feeling of deep affection' }, | ||
{ type: 'Verb:', definition: 'feel a deep romantic or sexual attachment to' } | ||
] | ||
Gdefine.format(entries).should be_a(String) | ||
end | ||
end | ||
|
||
end |