urban_dict 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDkzZWJmYmE0OTFjZDM2MzkzYWQ2MDY3Njc0NDliOGIxNjhhZTc3Mg==
4
+ NWI4NzJkOWVlOGIzOWEwN2EzOGZmMDE4YWQ4YmJlY2RlNWJkYTgxMg==
5
5
  data.tar.gz: !binary |-
6
- YmQwY2ZlNTk2MjU0NmU5NjY0OWYwNDYwOTQxNDI1ZTYyMTkxZDc5Mw==
6
+ Zjk0Y2JmZWQxNTM3NjQyMmZhMTNjZWEwN2E5YTljNmExMTZmNjg0Yw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- M2E1MjUzNDg3ZTU1MzNjMDJhZTg1ZmQyYTFlMmY1ZTJmMjgwZjY5OTc3YTY0
10
- MThkM2RkOWUxZmM3YzEyNjJkYjEzOWQ3NTZjYmYyOTlhYmU3M2E2MmE4MjJl
11
- MmFlNjU1ODlhYzdmM2Q1NTJiNjNjMWViOGFmNDA0NDE3N2ZkNDQ=
9
+ YzI0YTg4OTA3ZmQzN2Y5OTZmNDZmMTQzOTdiMDllMThhYjIwZDViNTQ4ZjRl
10
+ ZThhNGZjNTk4ODhhMjk1ZWUyZWVhNWUyMmFiNDYyZTU0OGEyODZlY2I4ODgz
11
+ ZGY1NDY0YjFiMzVhOGRkZDZmMGM2Nzc3NGVhYjY1MTVjMGI1YWU=
12
12
  data.tar.gz: !binary |-
13
- MjdiZjhlMTUzMmQ3YTdmZDJjZGQzNGIyYmE0NGMyNTM4YzY3OWQ5NzQ5YTI1
14
- Y2Q1OWEwMDIyYTBmOWY4MWU5Y2Y5MDU3ZmFjYTExMzlmMzBiM2Q4NjUzNWY2
15
- YWQzZmRjNWFjOTUxZDQxNjFhZjcwYWYzMjY2MjJjNzY1Mzk5NWQ=
13
+ NTU2N2ViODA2ODU5YjhkNGZjOWNjNzY1MGM0MmQ1YjQyMDBhMmRkNGIwN2Q2
14
+ YzFkMmU2Yzc0M2NmYjI0OTJhNzA1ZWEyZmE4ZTM5M2VjZjU0NjAyZDUyMTBh
15
+ MTczYzNiYjczZDM0MGViNmNlOWY3MzMzYzU0ZWMyZDFiYTZiNWQ=
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
- # Urbandictionary
1
+ # UrbanDict
2
2
 
3
- TODO: Write a gem description
3
+ A simple ruby gem to look up word definitions on UrbanDictionary.com.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'urbandictionary'
9
+ gem 'urban_dict'
10
10
 
11
11
  And then execute:
12
12
 
@@ -18,7 +18,9 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ ```
22
+ definition = UrbanDict.define("snapback")
23
+ ```
22
24
 
23
25
  ## Contributing
24
26
 
data/lib/urban_dict.rb CHANGED
@@ -5,37 +5,50 @@ require "json"
5
5
 
6
6
  module UrbanDict
7
7
 
8
- ENDPOINT = "http://api.urbandictionary.com/v0/define"
8
+ class UrbanResponse
9
+ attr_accessor :uri
9
10
 
10
- def self.define(word)
11
-
12
- if word.nil?
13
- exit "a word is required to define"
11
+ def initialize(url)
12
+ @uri = URI.parse(url)
14
13
  end
15
14
 
16
- uri = URI.parse( [ENDPOINT, "?term=", word].join )
15
+ def get_response
16
+ response = Net::HTTP.get_response(@uri)
17
17
 
18
- response = Net::HTTP.get_response(uri)
18
+ unless response.code == "200"
19
+ return { error: "There was a problem using the urban dictionary endpoint. Response code = #{ response.code }" }
20
+ end
19
21
 
20
- unless response.code == "200"
21
- return { error: "There was a problem using the urban dictionary endpoint. Response code = #{ response.code }" }
22
- end
22
+ body = JSON.parse(response.body)
23
+
24
+ definitions = body["list"]
23
25
 
24
- body = JSON.parse(response.body)
26
+ unless definitions.count > 0
27
+ return { error: "No definition found for '#{ word }'." }
28
+ end
25
29
 
26
- definitions = body["list"]
30
+ entry = definitions.first
27
31
 
28
- unless definitions.count > 0
29
- return { error: "No definition found for '#{ word }'." }
30
- return
32
+ #puts entry["definition"]
33
+ #puts "Example: #{ entry["example"]}"
31
34
  end
35
+ end
32
36
 
33
- entry = definitions.first
34
37
 
35
- #puts entry["definition"]
36
- #puts "Example: #{ entry["example"]}"
38
+ #
39
+ ENDPOINT = "http://api.urbandictionary.com/v0/"
40
+
41
+ def self.define(word = nil)
42
+ if word.nil?
43
+ random
44
+ else
45
+ UrbanResponse.new( [ENDPOINT, "define?term=", word].join ).get_response
46
+ end
47
+ end
48
+
49
+ def self.random
50
+ UrbanResponse.new( [ENDPOINT, "random"].join ).get_response
51
+ end
37
52
 
38
- return entry
39
- end
40
53
 
41
54
  end
@@ -1,3 +1,3 @@
1
1
  module UrbanDict
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: urban_dict
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Matthias