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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f90ae37818da975bb60e122ec8240ce03db15f4
4
- data.tar.gz: b5955f52ebd64e20edeed3ea518dd342c1bea30f
3
+ metadata.gz: 6458e7c7dea8b3f11a0781a7c001bcca45c25784
4
+ data.tar.gz: fbb508013a6343ab5f5ae3b4eb12b1bdec296b29
5
5
  SHA512:
6
- metadata.gz: e44b63549a01be403dcd8d4d8ca7401f539051c9bc005377d094130452a022fa576d41a837308fda25bc6f9979d93dc9cbe1b7f39d61192a4aa882c004ff811e
7
- data.tar.gz: 279e5d5a1579be31bd0b9e0bdb4f14221ad885501d8d5bcecba576281aeeca906406e069f784dfe9564e5bb1a95c8df8cadfa189acfa42413c281844bc9fd0f7
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) <br/>
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
- TODO: Write usage instructions here
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
 
@@ -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
- get_joke(uri)
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
- get_joke(uri)
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.get_joke(uri)
35
+ def self.parse_response(uri)
25
36
  response = connection.get(uri)
26
- parsed_response = parse_response(response.body)
27
- joke = parsed_response['joke']
28
- joke
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
- JSON.parse(body)
59
+
37
60
  end
38
61
 
39
62
  end
@@ -1,3 +1,3 @@
1
1
  module DadJokes
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dad_jokes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - aswinsanakan