class PixabayApi::ApiBase

Attributes

params[RW]

Public Class Methods

new() click to toggle source
# File lib/pixabay_api/api_base.rb, line 13
def initialize
  @api = PixabayApi.configuration.api_key
end

Public Instance Methods

find(keyword:, options: {}) click to toggle source
# 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: {}) click to toggle source
# 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

Private Instance Methods

endpoint() click to toggle source
# File lib/pixabay_api/api_base.rb, line 42
def endpoint
  'https://pixabay.com/api/'
end