Class: PixabayApi::ApiBase
- Inherits:
-
Object
- Object
- PixabayApi::ApiBase
- Defined in:
- lib/pixabay_api/api_base.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
Returns the value of attribute params.
Instance Method Summary collapse
- #find(keyword:, options: {}) ⇒ Object
- #find_and_return_array(keyword:, options: {}) ⇒ Object
-
#initialize ⇒ ApiBase
constructor
A new instance of ApiBase.
Constructor Details
#initialize ⇒ ApiBase
Returns a new instance of ApiBase.
13 14 15 |
# File 'lib/pixabay_api/api_base.rb', line 13 def initialize @api = PixabayApi.configuration.api_key end |
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
11 12 13 |
# File 'lib/pixabay_api/api_base.rb', line 11 def params @params end |
Instance Method Details
#find(keyword:, options: {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pixabay_api/api_base.rb', line 17 def find(keyword:, options: {}) self.params = { q: keyword }.merge() response_raw = PixabayApi::Request.create( api_key: @api, params: params, endpoint: endpoint ) response = PixabayApi::Response.new(response_raw) if response.failed? raise PixabayApi::Error::RequestError.new(response.body) end response end |
#find_and_return_array(keyword:, options: {}) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/pixabay_api/api_base.rb', line 33 def find_and_return_array(keyword:, options: {}) response = find(keyword: keyword, options: ) return [] if response.body['totalHits'].to_i.zero? response.body['hits'] end |