| 1234567891011121314151617181920212223242526272829303132333435 |
- # frozen_string_literal: true
- require './lib/api_error/base_error'
- class CreatePokemonFromBase64Service < BaseService
- def execute(base64_encoded_pokemon)
- response = client.parse_base64(base64_encoded_pokemon)
- pokemon = ::Pokemon.transaction do
- ::Pokemon.create!(response.pokemon.map(&:to_h))
- end
- success(pokemon)
- rescue PKParse::Error => e
- error(APIError::BaseError.new(e.message, internal_error: e))
- rescue ActiveRecord::ActiveRecordError => e
- msg = "Failed to commit parsed results: #{e}\n#{e.backtrace.join("\n")}"
- PKParse.logger.error(msg)
- error(APIError::BaseError.new(
- 'Failed to commit the uploaded pokemon.',
- internal_error: e,
- ))
- end
- private
- def success(pokemon)
- super().merge(pokemon: pokemon)
- end
- def client
- @client ||= PKParse::Client.new
- end
- end
|