Skip to content

Commit

Permalink
🎉 Initial code commit
Browse files Browse the repository at this point in the history
  • Loading branch information
elifoster committed Feb 22, 2016
1 parent f5fbc8c commit e8170f4
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Changelog
## Version 1
### 1.0.0
* Initial release.
5 changes: 5 additions & 0 deletions Gemfile
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')
13 changes: 13 additions & 0 deletions Gemfile.lock
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
26 changes: 25 additions & 1 deletion README.md
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).
61 changes: 61 additions & 0 deletions lib/slang.rb
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
25 changes: 25 additions & 0 deletions lib/urbandict.rb
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
19 changes: 19 additions & 0 deletions urbandict.gemspec
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

0 comments on commit e8170f4

Please sign in to comment.