-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
7 changed files
with
152 additions
and
1 deletion.
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,4 @@ | ||
# Changelog | ||
## Version 1 | ||
### 1.0.0 | ||
* Initial release. |
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,5 @@ | ||
source 'https://rubygems.org' | ||
|
||
ruby '2.3.0' | ||
|
||
gem('httpclient', '>= 2.7.1') |
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,13 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
httpclient (2.7.1) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
httpclient (>= 2.7.1) | ||
|
||
BUNDLED WITH | ||
1.11.2 |
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,2 +1,26 @@ | ||
# urbandict-rb | ||
A ruby interface to Urban Dictionary API | ||
A Ruby interface to Urban Dictionary API | ||
|
||
## Installation | ||
### RubyGems | ||
Execute: | ||
|
||
```shell | ||
$ gem install urbandict | ||
``` | ||
|
||
### Bundler | ||
Add this line to the application's Gemfile: | ||
|
||
```ruby | ||
gem('urbandict') | ||
``` | ||
|
||
Then execute: | ||
|
||
```shell | ||
$ bundle | ||
``` | ||
|
||
## Documentation | ||
Please see the [RubyDocs](http://www.rubydoc.info/gems/urbandict). |
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,61 @@ | ||
class Slang | ||
# @return [Integer] The UrbanDictionary definition ID. | ||
attr_reader :id | ||
|
||
# @return [String] The word being defined. | ||
attr_reader :word | ||
|
||
# @return [String] The author of the definition. | ||
attr_reader :author | ||
|
||
# @return [String] A permalink to the definition. | ||
attr_reader :permalink | ||
|
||
# @return [String] The actual definition. | ||
attr_reader :definition | ||
|
||
# @return [String] An example of the word's usage. | ||
attr_reader :example | ||
|
||
# @return [Integer] The number of thumbs-up the definition has received. | ||
attr_reader :upvotes | ||
|
||
# @return [Integer] The number of thumbs-down the definition has received. | ||
attr_reader :downvotes | ||
|
||
# Creates a new Slang object. | ||
# @param opts [Hash] The options hash. | ||
# @option opts [Integer] :id The definition ID. | ||
# @option opts [String] :word The word being defined. | ||
# @option opts [String] :author The author of the definition. | ||
# @option opts [String] :permalink The permalink for the definition. | ||
# @option opts [String] :definition The definition for the :word | ||
# @option opts [String] :example An example of its usage. | ||
# @option opts [Integer] :thumbs_up The number of upvotes the definition has received. | ||
# @option opts [Integer] :thumbs_down The number of downvotes the definition has received. | ||
def initialize(opts = {}) | ||
@id = opts['defid'] || opts[:defid] | ||
@word = opts['word'] || opts[:word] | ||
@author = opts['author'] || opts[:author] | ||
@permalink = opts['permalink'] || opts[:permalink] | ||
@definition = opts['definition'] || opts[:definition] | ||
@example = opts['example'] || opts[:example] | ||
@upvotes = opts['thumbs_up'] || opts[:thumbs_up] | ||
@downvotes = opts['thumbs_down'] || opts[:thumbs_down] | ||
end | ||
|
||
# Returns a hash representation of this Slang object. | ||
# @return [Hash<Symbol, Integer/String>] See the source for details. | ||
def to_h | ||
{ | ||
id: @id, | ||
word: @word, | ||
author: @author, | ||
permalink: @permalink, | ||
definition: @definition, | ||
example: @example, | ||
upvotes: @upvotes, | ||
downvotes: @downvotes | ||
} | ||
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,25 @@ | ||
require 'httpclient' | ||
require 'json' | ||
require_relative 'slang' | ||
|
||
module UrbanDictionary | ||
module_function | ||
|
||
URL = 'http://api.urbandictionary.com/v0/define' | ||
|
||
# Gets the definitions for the word. | ||
# @param word [String] The word to define. | ||
# @return [Array<Slang>] An array of #{Slang} objects. | ||
def define(word) | ||
params = { | ||
term: word | ||
} | ||
@client = HTTPClient.new if @client.nil? | ||
response = JSON.parse(@client.get(URI.parse(URL), params).body) | ||
ret = [] | ||
response['list'].each do |hash| | ||
ret << Slang.new(hash) | ||
end | ||
ret | ||
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,19 @@ | ||
Gem::Specification.new do |s| | ||
s.authors = ['Eli Foster'] | ||
s.name = 'urbandict' | ||
s.summary = 'An interface to the Urban Dictionary API' | ||
s.version = '1.0.0' | ||
s.license = 'CC-BY-ND-4.0' | ||
s.description = 'An interface to the Urban Dictionary API' | ||
s.email = '[email protected]' | ||
s.homepage = 'https://github.com/elifoster/urbandict-rb' | ||
s.metadata = { | ||
'issue_tracker' => 'https://github.com/elifoster/urbandict-rb/issues' | ||
} | ||
s.files = [ | ||
'CHANGELOG.md', | ||
'lib/slang.rb', | ||
'lib/urbandict.rb' | ||
] | ||
s.add_runtime_dependency('httpclient', '2.7.1') | ||
end |