Class: PunkAPI::Client
- Inherits:
-
Object
- Object
- PunkAPI::Client
- Defined in:
- lib/punkapi/client.rb
Constant Summary collapse
- DEFAULT_API_URL =
'https://api.punkapi.com/v2'.freeze
Instance Method Summary collapse
- #beer(id) ⇒ Object
- #beers(params = {}) ⇒ Object
- #connection ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #parse(res) ⇒ Object
- #random_beer ⇒ Object
- #request(url, params = {}) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
8 9 |
# File 'lib/punkapi/client.rb', line 8 def initialize end |
Instance Method Details
#beer(id) ⇒ Object
11 12 13 14 |
# File 'lib/punkapi/client.rb', line 11 def beer(id) beer = request('beers', id: id) beer[0] end |
#beers(params = {}) ⇒ Object
16 17 18 |
# File 'lib/punkapi/client.rb', line 16 def beers(params = {}) request('beers', params) end |
#connection ⇒ Object
37 38 39 40 |
# File 'lib/punkapi/client.rb', line 37 def connection @connection ||= Faraday.new(DEFAULT_API_URL, ssl: { verify: false }) @connection end |
#parse(res) ⇒ Object
31 32 33 34 35 |
# File 'lib/punkapi/client.rb', line 31 def parse(res) response = JSON.parse(res.body, object_class: PunkAPI::Entity) response.code = res.status unless res.success? response end |
#random_beer ⇒ Object
20 21 22 23 |
# File 'lib/punkapi/client.rb', line 20 def random_beer beer = request('beers/random') beer[0] end |
#request(url, params = {}) ⇒ Object
25 26 27 28 29 |
# File 'lib/punkapi/client.rb', line 25 def request(url, params = {}) url += "/#{params.delete(:id)}" if params[:id] res = connection.get url, params parse(res) end |