Class: PixabayApi::ApiBase

Inherits:
Object
  • Object
show all
Defined in:
lib/pixabay_api/api_base.rb

Direct Known Subclasses

ImagesApi, VideoApi

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApiBase

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

#paramsObject

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(options)
  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: options)

  return [] if response.body['totalHits'].to_i.zero?
  response.body['hits']
end