Skip to content

Commit

Permalink
add readme and rspec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjamesriley committed May 13, 2012
1 parent 96b1d83 commit 4215cef
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 15 deletions.
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--color
21 changes: 9 additions & 12 deletions README.md
@@ -1,24 +1,21 @@
# Define
# gdefine

TODO: Write a gem description
Simply get a word definition from Google without leaving the command line.
I'm forever looking up definitions (not that I'm slow, just that I'm reading complicated material of course), and this proves useful given that I live within the terminal.

## Installation

Add this line to your application's Gemfile:
$ gem install gdefine

gem 'define'

And then execute:

$ bundle
## Usage

Or install it yourself as:
$ gdefine love

$ gem install define
Noun: An intense feeling of deep affection: "their love for their country".An intense feeling of deep affection: "their love for their country".

## Usage
Verb: Feel a deep romantic or sexual attachment to (someone): "do you love me?".Feel a deep romantic or sexual attachment to (someone): "do you love me?".

TODO: Write usage instructions here
Synonyms: noun.  affection - fondness - darling - passionverb.  like - be fond of - fancy - adore

## Contributing

Expand Down
5 changes: 4 additions & 1 deletion gdefine.gemspec
Expand Up @@ -11,7 +11,10 @@ Gem::Specification.new do |gem|
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "Gdefine"
gem.name = "gdefine"
gem.require_paths = ["lib"]
gem.version = Gdefine::VERSION

gem.add_dependency 'nokogiri'
gem.add_dependency 'rspec'
end
38 changes: 36 additions & 2 deletions lib/gdefine.rb
@@ -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
29 changes: 29 additions & 0 deletions spec/gdefine_spec.rb
@@ -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>.&nbsp;&nbsp;</span>affection - fondness - darling - passion</div><div><span><i>verb</i>.&nbsp;&nbsp;</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

0 comments on commit 4215cef

Please sign in to comment.