Sha256: 1701d5e5aceb3305b24ad4413cb96aeadcf98f6091b54b32f875c86b85b4fe1d

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require 'forwardable'

module Pokeedex # :nodoc:
  module Pokemon # :nodoc:
    module Searcher # :nodoc:
      class Base # :nodoc:
        ##
        # The base class for the Pokemon searcher. It holds the methods to search the Pokemon data from the database or the Pokemon website
        extend Forwardable

        ##
        # The query to search the Pokemon data
        attr_reader :query

        ##
        # The records found from the search
        attr_reader :records

        def initialize(query)
          @query = query
          @records = collection
        end

        ##
        # Return the first record found from the search
        # @return [Pokeedex::Pokemon::Model::Base]
        def_delegators :@records, :each, :first, :last, :size, :count, :empty?

        private

        def collection
          search_or_create
        end

        def search_or_create
          result = search_from_model
          return search_and_create_from_scrapper_remote if result.empty?

          result
        end

        def search_from_model
          Pokemon::Model::Base.search(query)
        end

        def search_and_create_from_scrapper_remote
          result = Scrapper::Pokedex.new(number_or_name: query).crawl
          Pokemon::Model::Base.create(result)

          search_from_model
        rescue Sequel::ValidationFailed
          []
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pokeedex-0.1.5 lib/pokeedex/pokemon/searcher/base.rb
pokeedex-0.1.0 lib/pokeedex/pokemon/searcher/base.rb