Sha256: 0b16655d4868757a8dddf95e153227cea8bae88661173197b01859589afe5883

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 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(movie)
          else
            Omdb::Api::Error.new(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(movie)
            end
          )
        else
          Omdb::Api::Error.new(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

4 entries across 4 versions & 1 rubygems

Version Path
omdb-api-1.4.2 lib/omdb/api/public_api.rb
omdb-api-1.4.1 lib/omdb/api/public_api.rb
omdb-api-1.4.0 lib/omdb/api/public_api.rb
omdb-api-1.3.0 lib/omdb/api/public_api.rb