Sha256: 01d8f1ee31f024d02658463187b6596035740e480deebec9788bb8f5ea276de7

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module Omdb
  module Api
    module PublicApi
      %i[find_by_id find_by_title].each do |method|
        define_method(method) do |value, **options|
          request = Omdb::Api::Request.new(self, method, value, options)

          data = request.response
          movie = __format_data(data)

          if request.success?
            Omdb::Api::Movie.new(params: movie)
          else
            Omdb::Api::Error.new(params: movie)
          end
        end
      end

      def search(value, options = {})
        request = Omdb::Api::Request.new(self, 'search', value, options)

        if request.success?
          Omdb::Api::Collection.new(
            request.response.fetch('Search').map do |movie|
              movie = __format_data(movie)

              Omdb::Api::Movie.new(params: movie)
            end
          )
        else
          Omdb::Api::Error.new(params: request.response)
        end
      end

      def __format_data(data)
        keys = data.keys.map(&:underscore)
        keys.zip(data.values).to_h
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
omdb-api-1.2.0 lib/omdb/api/public_api.rb