whatimean 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +73 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/whatimean.rb +47 -0
- data/lib/whatimean/find.rb +38 -0
- data/lib/whatimean/google.rb +36 -0
- data/lib/whatimean/version.rb +4 -0
- data/lib/whatimean/word.rb +28 -0
- data/whatimean.gemspec +27 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: deec3dc2c866e548b09ae1b2ef7cc94fe2016052
|
4
|
+
data.tar.gz: 178da8c3f4d4bca07e18a2227876e78bf198eba2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d34f6578a9cd9d0a199433442cc458fccb98a419ff599b888b0e99736c9b3239ae5c146e8140282e5a7bc29820f49af2e0ece312b25fab42c161f3bcc0388d77
|
7
|
+
data.tar.gz: af9c6eb55fb00fe10d5748055cc887332f41574b1855596c420fb58eb8a1378ac50e1f11c0a550c2bbef6dbbca27e2298db47e2a6a2c0dfa0cfc6cd1dd13c804
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Joao Fraga
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Whatimean
|
2
|
+
|
3
|
+
[](https://codeclimate.com/github/joaofraga/whatimean) [](https://codeclimate.com/github/joaofraga/whatimean/coverage) [](https://travis-ci.org/joaofraga/whatimean)
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/whatimean`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
TODO: Delete this and the text above, and describe your gem
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'whatimean'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install whatimean
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
First add your Google Console API Key:
|
28
|
+
|
29
|
+
``` ruby
|
30
|
+
WhatIMean.configure do |config|
|
31
|
+
config.api_key = "MY_API"
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
You can now discover which word is the correct using:
|
36
|
+
|
37
|
+
``` ruby
|
38
|
+
WhatIMean.is? 'word1', 'word2'
|
39
|
+
```
|
40
|
+
|
41
|
+
To discover the winner, just:
|
42
|
+
|
43
|
+
``` ruby
|
44
|
+
word_fight = WhatIMean.is? 'google', 'altavista'
|
45
|
+
word_fight.winner
|
46
|
+
|
47
|
+
#=> #<WhatIMean::Word:0x007ffcba4960d8 @name="google", @hits=824000000, @rate=0.9987031403396075>
|
48
|
+
```
|
49
|
+
|
50
|
+
To list the searched words:
|
51
|
+
|
52
|
+
``` ruby
|
53
|
+
word_fight = WhatIMean.is? 'google', 'altavista'
|
54
|
+
word_fight.words
|
55
|
+
|
56
|
+
#=> [#<WhatIMean::Word:0x007ffcba4960d8 @name="google", @hits=824000000, @rate=0.9987031403396075>, #<WhatIMean::Word:0x007ffcba4ae750 @name="altavista", @hits=1070000, @rate=0.0012968596603924515>]
|
57
|
+
```
|
58
|
+
|
59
|
+
## Development
|
60
|
+
|
61
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
62
|
+
|
63
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/joaofraga/whatimean. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
68
|
+
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
73
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "whatimean"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/whatimean.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'whatimean/find'
|
2
|
+
require 'whatimean/google'
|
3
|
+
require 'whatimean/word'
|
4
|
+
require 'whatimean/version'
|
5
|
+
|
6
|
+
module WhatIMean
|
7
|
+
class << self
|
8
|
+
attr_accessor :configuration
|
9
|
+
end
|
10
|
+
|
11
|
+
# Call this method to modify defaults in your initializers.
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# WhatIMean.configure do |config|
|
15
|
+
# config.api_key = 'MY_API_KEY'
|
16
|
+
# end
|
17
|
+
def self.configure
|
18
|
+
self.configuration ||= Configuration.new
|
19
|
+
yield(configuration)
|
20
|
+
end
|
21
|
+
|
22
|
+
# = Configuration
|
23
|
+
#
|
24
|
+
# Where you configure WhatIMean.
|
25
|
+
class Configuration
|
26
|
+
# The Google Console API key for your project.
|
27
|
+
attr_accessor :api_key
|
28
|
+
|
29
|
+
def initialize
|
30
|
+
@api_key = 'MY_API_KEY'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Compare strings and return a list ordered by occurence
|
35
|
+
# @param [Array<String>] words The words to be compared
|
36
|
+
# @return [Array<Word>] the words with hits counter and comparison rates
|
37
|
+
def self.is(*words)
|
38
|
+
Find.new(client, *words)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
# The WhatIMean::Google client
|
44
|
+
def self.client
|
45
|
+
@client ||= Google.new(WhatIMean.configuration.api_key)
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module WhatIMean
|
2
|
+
# = Find
|
3
|
+
# The Core of WhatIMean, this class interacts with Google and Word and
|
4
|
+
# fetch the search returning each word with hits, name and rate comparison
|
5
|
+
class Find
|
6
|
+
attr_reader :words
|
7
|
+
|
8
|
+
# Compare strings and return a list ordered by occurence
|
9
|
+
# @param [Google] client The WhatIMean::Google client
|
10
|
+
# @param [Array<String>] words The words to be compared
|
11
|
+
# @return [Find] the words with hits counter and comparison rates
|
12
|
+
def initialize(client, *words)
|
13
|
+
@client = client
|
14
|
+
@words = fetch_words(words).sort { |x,y| y.hits <=> x.hits }
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns the word in comparison with most results
|
18
|
+
# @return [Word] The winner with most occurences
|
19
|
+
def winner
|
20
|
+
@words.first
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def fetch_words(words)
|
26
|
+
words = words.map do |word|
|
27
|
+
@client.search(word)
|
28
|
+
end
|
29
|
+
|
30
|
+
calculate_rate(words)
|
31
|
+
end
|
32
|
+
|
33
|
+
def calculate_rate(words)
|
34
|
+
total = words.map(&:hits).inject(&:+).to_f
|
35
|
+
words.map { |word| word.calculate_rate(total) }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'cgi'
|
4
|
+
|
5
|
+
module WhatIMean
|
6
|
+
class Google
|
7
|
+
# Url to Google Custom Search API
|
8
|
+
BASE_URL = 'https://www.googleapis.com/customsearch/v1?cref&key={key}&q={word}'
|
9
|
+
|
10
|
+
# Instantiate a new Google client object
|
11
|
+
# @param [String] key Google Console Custom Search API KEY
|
12
|
+
# @return [Google]
|
13
|
+
def initialize(key)
|
14
|
+
@key = key
|
15
|
+
end
|
16
|
+
|
17
|
+
# Searchs a string with Google Custom Search
|
18
|
+
# @param [String] word
|
19
|
+
# @return [Word]
|
20
|
+
def search(word)
|
21
|
+
WhatIMean::Word.new(name: word, hits: fetch_word(word))
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def url_to(word)
|
27
|
+
BASE_URL.gsub(/{key}/, @key).gsub(/{word}/, CGI.escape(word))
|
28
|
+
end
|
29
|
+
|
30
|
+
def fetch_word(word)
|
31
|
+
open(url_to(word), "Referer" => "http://whatimean.com", "User-Agent" => "WhatIMean::Robot", ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE) do |f|
|
32
|
+
JSON.parse(f.read)['searchInformation']['totalResults'].to_i
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module WhatIMean
|
2
|
+
|
3
|
+
class Word
|
4
|
+
attr_accessor :name, :hits, :rate
|
5
|
+
|
6
|
+
# Instatiate a new Word with following attributes:
|
7
|
+
# @param [Hash] attributes Data that will be set in Word.
|
8
|
+
#
|
9
|
+
# @option attributes [String] :name Word name
|
10
|
+
# @option attributes [Fixnum] :hits Estimated hit count (occurrences)
|
11
|
+
# @option attributes [Float] :rate Rate comparison based on total value
|
12
|
+
def initialize(attributes)
|
13
|
+
attributes.each do |key, value|
|
14
|
+
send("#{key}=", value)
|
15
|
+
end
|
16
|
+
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
# Calculate
|
21
|
+
# @param [Float] total The total ammount to be compared
|
22
|
+
# @return [Word] self object
|
23
|
+
def calculate_rate(total)
|
24
|
+
self.rate = hits / total.to_f
|
25
|
+
self
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/whatimean.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'whatimean/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "whatimean"
|
8
|
+
spec.version = WhatIMean::VERSION
|
9
|
+
spec.authors = ["Joao Fraga"]
|
10
|
+
spec.email = ["jgfraga@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Discover the correct typo of a word using Google search.}
|
13
|
+
spec.description = %q{Discover the correct typo of a word using Google search.}
|
14
|
+
spec.homepage = "https://github.com/joaofraga/whatimean"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "vcr"
|
26
|
+
spec.add_development_dependency "codeclimate-test-reporter"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: whatimean
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joao Fraga
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: vcr
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: codeclimate-test-reporter
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Discover the correct typo of a word using Google search.
|
84
|
+
email:
|
85
|
+
- jgfraga@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/whatimean.rb
|
100
|
+
- lib/whatimean/find.rb
|
101
|
+
- lib/whatimean/google.rb
|
102
|
+
- lib/whatimean/version.rb
|
103
|
+
- lib/whatimean/word.rb
|
104
|
+
- whatimean.gemspec
|
105
|
+
homepage: https://github.com/joaofraga/whatimean
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.4.8
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Discover the correct typo of a word using Google search.
|
129
|
+
test_files: []
|