Home > OS >  Ruby on Rails APi call from Controller with too much logic. Where and how can I make it simpler?
Ruby on Rails APi call from Controller with too much logic. Where and how can I make it simpler?

Time:09-24

I was asked to fetch the Pokemon API from Ruby on Rails. But I'm struggling because I have too much logic in the controller, which is not recommended. Where and how can I make the call so I have less logic inside my controller. I have this:

def index
    pokemons = []
    response = HTTParty.get("https://pokeapi.co/api/v2/pokemon?limit=6&offset=1")
    response = JSON.parse(response.body)
    response.each do |k, value|
        if k == "results"
            value.each do |key, val|
            response = HTTParty.get(key["url"])
            response = JSON.parse(response.body)
            pokemons.push(response)
            end
        end
    end

        @poke_json = pokemons.map do |poke|
            Pokemon.new(img:poke['sprites']['other']['dream_world']["front_default"], name: poke['forms'][0]['name'], weight: poke['weight'], poke_type: poke['types'][0]['type']['name'], poke_ability: poke['abilities'][0]['ability']['name'], poke_id: poke['id'])
                                                    
        end
    render json: { pokemons: @poke_json }
  end ``` 

CodePudding user response:

As a first step you could extract the API client depending code into a class on its own:

# in app/models/pokemon_api.rb
class PokemonApi
  def self.to_json
    pokemons = []

    response = HTTParty.get("https://pokeapi.co/api/v2/pokemon?limit=6&offset=1")
    response = JSON.parse(response.body)
    response.each do |k, value|
      if k == "results"
        value.each do |key, val|
          response = HTTParty.get(key["url"])
          response = JSON.parse(response.body)
          pokemons.push(response)
        end
      end
    end

    pokemons.map do |poke|
      Pokemon.new(
        img:poke['sprites']['other']['dream_world']["front_default"],
        name: poke['forms'][0]['name'], weight: poke['weight'], 
        poke_type: poke['types'][0]['type']['name'], 
        poke_ability: poke['abilities'][0]['ability']['name'], 
        poke_id: poke['id']
      )
    end
  end
end

and then just call that method in your controller

def index
  render json: { pokemons: PokemonApi.to_json }
end
  • Related