Skip to content

Instantly share code, notes, and snippets.

@zorbash
Created February 25, 2014 00:19
Show Gist options
  • Select an option

  • Save zorbash/9200116 to your computer and use it in GitHub Desktop.

Select an option

Save zorbash/9200116 to your computer and use it in GitHub Desktop.
Example api with plain RoR
class MainController < ApplicationController
def songs_random
row = ActiveRecord::Base.connection
.execute("SELECT * FROM songs ORDER BY RANDOM() LIMIT 1;")[0]
render json: { song: row.delete_if { |key, _| key.to_s =~ /^[0-9]+$/ } }
end
def words_random
render json: { word: `shuf /usr/share/dict/words | head -1`.chomp }
end
end
Ror::Application.routes.draw do
get 'songs/random' => 'main#songs_random'
get 'words/random' => 'main#words_random'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment