gdefine 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gdefine.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 James Riley
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
1
+ # gdefine
2
+
3
+ Simply get a word definition from Google without leaving the command line.
4
+ 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.
5
+
6
+ ## Installation
7
+
8
+ $ gem install gdefine
9
+
10
+ ## Usage
11
+
12
+ $ gdefine love
13
+
14
+ Noun: An intense feeling of deep affection: "their love for their country".An intense feeling of deep affection: "their love for their country".
15
+
16
+ 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?".
17
+
18
+ Synonyms: noun.  affection - fondness - darling - passionverb.  like - be fond of - fancy - adore
19
+
20
+ ## Contributing
21
+
22
+ 1. Fork it
23
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
24
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
25
+ 4. Push to the branch (`git push origin my-new-feature`)
26
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'gdefine')
4
+
5
+ begin
6
+ puts Gdefine.define(ARGV.first)
7
+ rescue Errno::EACCES
8
+ puts "Sorry there was a problem"
9
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/gdefine/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["James Riley"]
6
+ gem.email = ["james@codansa.com"]
7
+ gem.description = %q{Get a Google word definition from the command line}
8
+ gem.summary = %q{For those who live in the terminal, get a word definition without leaving the comfort of your home}
9
+ gem.homepage = "http://www.github.com/mrjamesriley/gdefine"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "gdefine"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Gdefine::VERSION
17
+
18
+ gem.add_dependency 'nokogiri'
19
+ gem.add_dependency 'rspec'
20
+ end
@@ -0,0 +1,39 @@
1
+ require_relative 'gdefine/version'
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+
5
+
6
+ module Gdefine
7
+
8
+ # Using iPhone 5 User agent for smaller page and so Google definition is shown
9
+ # (as the open-uri default does has Google not showing the the defition)
10
+ USER_AGENT = 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us)'
11
+
12
+ def self.define word
13
+ html = open "https://www.google.com/search?q=define+#{word}", 'User-Agent' => USER_AGENT
14
+ format parse(html)
15
+ end
16
+
17
+ def self.parse html
18
+ doc = Nokogiri::HTML html
19
+
20
+ doc.css('.ts tr').inject([]) do |entries, entry|
21
+ if entry.css('td').length > 1 # ignore the spacer row
22
+ cells = entry.css('td')
23
+
24
+ entry = {}
25
+ entry[:type] = cells.shift.text
26
+ entry[:definition] = cells.text
27
+ entries << entry
28
+ end
29
+ entries
30
+ end
31
+ end
32
+
33
+ def self.format entries
34
+ output = entries.inject('') do |total, entry|
35
+ total += "\n #{entry[:type]} #{entry[:definition]} \n"
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,3 @@
1
+ module Gdefine
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ require_relative '../lib/gdefine'
2
+
3
+ HTML = <<DEFINITION
4
+ <table class="ts"><tbody><tr>
5
+ <td valign="top" width="80px">Noun:</td><td valign="top">
6
+ <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>
7
+ DEFINITION
8
+
9
+ describe Gdefine do
10
+
11
+ describe 'parse' do
12
+ it 'should correctly return key value definitions' do
13
+ parsed = Gdefine.parse(HTML)
14
+ parsed.should have(3).items
15
+ parsed.first.should be_a(Hash)
16
+ end
17
+ end
18
+
19
+ describe 'format' do
20
+ it 'should return string of definitions' do
21
+ entries = [
22
+ { type: 'Noun:', definition: 'an intense feeling of deep affection' },
23
+ { type: 'Verb:', definition: 'feel a deep romantic or sexual attachment to' }
24
+ ]
25
+ Gdefine.format(entries).should be_a(String)
26
+ end
27
+ end
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gdefine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - James Riley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Get a Google word definition from the command line
47
+ email:
48
+ - james@codansa.com
49
+ executables:
50
+ - gdefine
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - Gemfile
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - bin/gdefine
61
+ - gdefine.gemspec
62
+ - lib/gdefine.rb
63
+ - lib/gdefine/version.rb
64
+ - spec/gdefine_spec.rb
65
+ homepage: http://www.github.com/mrjamesriley/gdefine
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.24
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: For those who live in the terminal, get a word definition without leaving
89
+ the comfort of your home
90
+ test_files:
91
+ - spec/gdefine_spec.rb