Skip to content

Instantly share code, notes, and snippets.

@thePaulista
Last active May 31, 2016 20:29
Show Gist options
  • Save thePaulista/53d8ce877d5faa347e38c5b5bec35617 to your computer and use it in GitHub Desktop.
Save thePaulista/53d8ce877d5faa347e38c5b5bec35617 to your computer and use it in GitHub Desktop.
API V1 routes.rb
rails new rails_engine -d postgresql --skip-test-unit --skip-turbolinks --skip-spring
___________________________________________________________________________
source 'https://rubygems.org'
gem 'rails', '4.1.4'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'faker'
gem 'haml-rails'
gem 'therubyracer'
gem 'less-rails-bootstrap'
gem 'bcrypt'
group :development do
gem 'spring'
end
group :development, :test do
gem 'capybara'
gem 'pry', :require => 'pry'
end
__________________________________________________________________
Change null_session in the applications controller
___________________________________________________________________
Make a Api/V1 folder, and a ApiController folder in the Api folder
class Api::ApiController < ApplicationControler
protect_from_forgery with: :null_session
before_action :authenticate!
def authenticate! #this will do a pop up window, username will be email
authenticate_or_request_with_http_basic("Please provide your username and password") do |email, password|
user = User.find_by(email: email)
# user = User.find_by(token: params[:token]) # if you want to authenticate with a token instead of email. see below
return true if user && user.authenticate(password)
head :unauthorized
end
end
end
#in the User add a token column.
#before_create :generate_token!
#
#private
#
#def generate_token!
# unique = Time.now.to_s + email + ENV["SECRET_KEY_BASE"]
# self.token = Digest.SHA.digest(unique)
# self, b/c otherwise, the token will just be a local variable.
#end
__________________________________________________________________
module Api
module V1
class ItemsController < ApiController
respond_to :json, :xml
def index
respond_with Item.all
end
end
end
end
____ROUTES___________________________________________________________________________________________________________
Rails.application.routes.draw do
root 'items#index'
resources :items
resources :users
resources :orders, only: [:index, :show]
namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :items, except: [:new, :edit] ###, defaults: { format: :json }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment