dad_jokes 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -2
- data/lib/dad_jokes.rb +31 -8
- data/lib/dad_jokes/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6458e7c7dea8b3f11a0781a7c001bcca45c25784
|
4
|
+
data.tar.gz: fbb508013a6343ab5f5ae3b4eb12b1bdec296b29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ed3ca953555fab8d47375cc1e60e425d1814ceb20037d1e9fa228dd53565b1c9627cd0b402876c96653211b024fd65788b6dc3ed5559afdf4161c256cfd7048
|
7
|
+
data.tar.gz: 5425d892c8398142888c50d991419ba910b9b9698efabe1229db83b3c72c4f6c10e881cda338d7c9eb833aec89d6d74ab33c30499d7b9f984df4232813efd2de
|
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# dad_jokes
|
2
|
-
[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](http://opensource.org/licenses/MIT)
|
2
|
+
[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](http://opensource.org/licenses/MIT)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/dad_jokes.svg)](https://badge.fury.io/rb/dad_jokes)
|
4
|
+
[![Gem](https://img.shields.io/gem/dt/dad_jokes.svg?style=flat-square)](https://rubygems.org/gems/dad_jokes)
|
5
|
+
<br/>
|
3
6
|
|
4
7
|
Welcome to <b>Dad Jokes</b>! <br/>
|
5
8
|
Generate random dad jokes using this gem.
|
@@ -22,7 +25,29 @@ Or install it yourself as:
|
|
22
25
|
|
23
26
|
## Usage
|
24
27
|
|
25
|
-
|
28
|
+
#### #1 Generate a random Dad joke
|
29
|
+
|
30
|
+
``` ruby
|
31
|
+
DadJokes.random
|
32
|
+
```
|
33
|
+
|
34
|
+
Sample Output #1 : ``"Dad, I'm hungry." "Hello, Hungry. I'm Dad."``
|
35
|
+
|
36
|
+
Sample Output #2 : ``Why didn’t the skeleton cross the road? Because he had no guts.``
|
37
|
+
|
38
|
+
#### #2 Search for Dad jokes
|
39
|
+
|
40
|
+
``` ruby
|
41
|
+
DadJokes.search(term:, limit:)
|
42
|
+
```
|
43
|
+
|
44
|
+
* _term:_
|
45
|
+
- (**required*)
|
46
|
+
- Search term goes here
|
47
|
+
* _limit:_
|
48
|
+
- (**optional*)
|
49
|
+
- Set limit for the number of jokes (because, obviously you're tired of laughing!).
|
50
|
+
- Default : <b>5 jokes</b>
|
26
51
|
|
27
52
|
## Contributing
|
28
53
|
|
data/lib/dad_jokes.rb
CHANGED
@@ -6,26 +6,49 @@ require 'pry'
|
|
6
6
|
|
7
7
|
module DadJokes
|
8
8
|
|
9
|
-
API_URL = "https://icanhazdadjoke.com/"
|
9
|
+
API_URL = "https://icanhazdadjoke.com/".freeze
|
10
10
|
API_HEADERS = {"Accept": "application/json"}
|
11
|
+
DEFAULT_LIMIT = 5
|
11
12
|
|
12
13
|
def self.random
|
13
14
|
uri = "/"
|
14
|
-
|
15
|
+
joke_object = parse_response(uri)
|
16
|
+
get_joke(joke_object)
|
15
17
|
end
|
16
18
|
|
17
19
|
def self.fetch(joke_id:)
|
18
20
|
uri = "/j/#{joke_id}"
|
19
|
-
|
21
|
+
joke_object = parse_response(uri)
|
22
|
+
get_joke(joke_object)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.search(term: '',limit: DEFAULT_LIMIT)
|
26
|
+
uri = "/search?term=#{term}&limit=#{limit}"
|
27
|
+
response = connection.get(uri)
|
28
|
+
jokes_hash = JSON.parse(response.body)
|
29
|
+
jokes = filter_jokes(jokes_hash)
|
30
|
+
jokes
|
20
31
|
end
|
21
32
|
|
22
33
|
private
|
23
34
|
|
24
|
-
def self.
|
35
|
+
def self.parse_response(uri)
|
25
36
|
response = connection.get(uri)
|
26
|
-
|
27
|
-
|
28
|
-
|
37
|
+
JSON.parse(response.body)
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.get_joke(joke_object)
|
41
|
+
joke_object['joke']
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.filter_jokes(jokes_hash)
|
45
|
+
# binding.pry
|
46
|
+
jokes_arr = jokes_hash['results']
|
47
|
+
jokes = []
|
48
|
+
jokes_arr.each do |joke|
|
49
|
+
jokes << joke['joke']
|
50
|
+
end
|
51
|
+
jokes
|
29
52
|
end
|
30
53
|
|
31
54
|
def self.connection
|
@@ -33,7 +56,7 @@ module DadJokes
|
|
33
56
|
end
|
34
57
|
|
35
58
|
def self.parse_response(body)
|
36
|
-
|
59
|
+
|
37
60
|
end
|
38
61
|
|
39
62
|
end
|
data/lib/dad_jokes/version.rb
CHANGED