Move codes from api.rb to model