Skip to content

Commit

Permalink
Added API v1 with specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Feb 10, 2013
1 parent b6c3fa5 commit d3f8362
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 83 deletions.
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--format=documentation

34 changes: 7 additions & 27 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
source 'https://rubygems.org'

gem 'rails', '3.2.8'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'
gem 'jquery-rails'
gem 'grape'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby

gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'
group :test do
gem "rspec"
gem "rspec-rails"
gem "capybara"
end
61 changes: 61 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,41 @@ GEM
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
backports (2.6.7)
builder (3.0.4)
capybara (2.0.2)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 1.0.0)
childprocess (0.3.7)
ffi (~> 1.0, >= 1.0.6)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
coffee-script (2.2.0)
coffee-script-source
execjs
coffee-script-source (1.4.0)
descendants_tracker (0.0.1)
diff-lcs (1.1.3)
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
ffi (1.3.1)
grape (0.2.6)
activesupport
builder
hashie (~> 1.2)
multi_json (>= 1.3.2)
multi_xml (>= 0.5.2)
rack
rack-accept
rack-mount
virtus
hashie (1.2.0)
hike (1.2.1)
i18n (0.6.1)
journey (1.0.4)
Expand All @@ -53,10 +77,16 @@ GEM
treetop (~> 1.4.8)
mime-types (1.21)
multi_json (1.5.0)
multi_xml (0.5.3)
nokogiri (1.5.6)
polyglot (0.3.3)
rack (1.4.5)
rack-accept (0.4.5)
rack (>= 0.4)
rack-cache (1.2)
rack (>= 0.4)
rack-mount (0.8.3)
rack (>= 1.0.0)
rack-ssl (1.3.3)
rack
rack-test (0.6.2)
Expand All @@ -79,11 +109,32 @@ GEM
rake (10.0.3)
rdoc (3.12.1)
json (~> 1.4)
rspec (2.12.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
rspec-core (2.12.2)
rspec-expectations (2.12.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.12.2)
rspec-rails (2.12.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
rubyzip (0.9.9)
sass (3.2.5)
sass-rails (3.2.6)
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
selenium-webdriver (2.29.0)
childprocess (>= 0.2.5)
multi_json (~> 1.0)
rubyzip
websocket (~> 1.0.4)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
Expand All @@ -98,14 +149,24 @@ GEM
uglifier (1.3.0)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
virtus (0.5.4)
backports (~> 2.6.1)
descendants_tracker (~> 0.0.1)
websocket (1.0.7)
xpath (1.0.0)
nokogiri (~> 1.3)

PLATFORMS
ruby

DEPENDENCIES
capybara
coffee-rails (~> 3.2.1)
grape
jquery-rails
rails (= 3.2.8)
rspec
rspec-rails
sass-rails (~> 3.2.3)
sqlite3
uglifier (>= 1.0.3)
8 changes: 8 additions & 0 deletions app/api/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class API < Grape::API
prefix 'api'
format :json
mount API_v1
end



13 changes: 13 additions & 0 deletions app/api/api_v1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class API_v1 < Grape::API
version 'v1', :using => :path, :vendor => 'acme', :format => :json
resource :system do
desc "Returns pong."
get :ping do
{ :ping => "pong" }
end
desc "Raises an exception."
get :raise do
raise "Unexpected error."
end
end
end
4 changes: 4 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class Application < Rails::Application
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)

# Auto-load API and its subdirectories
config.paths.add "app/api", :glob => "**/*.rb"
config.autoload_paths += Dir["#{Rails.root}/app/api/*"]

# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
Expand Down
9 changes: 9 additions & 0 deletions config/initializers/reload_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if Rails.env.development?
api_files = Dir["#{Rails.root}/app/api/**/*.rb"]
api_reloader = ActiveSupport::FileUpdateChecker.new(api_files) do
Rails.application.reload_routes!
end
ActionDispatch::Callbacks.to_prepare do
api_reloader.execute_if_updated
end
end
57 changes: 1 addition & 56 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,3 @@
GrapeOnRails::Application.routes.draw do
# The priority is based upon order of creation:
# first created -> highest priority.

# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action

# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)

# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products

# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end

# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end

# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end

# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end

# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => 'welcome#index'

# See how all your routes lay out with "rake routes"

# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id))(.:format)'
mount API => '/'
end
13 changes: 13 additions & 0 deletions spec/api/api_v1_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'

describe API do
context "v1" do
context "system" do
it "ping" do
get "/api/v1/system/ping"
response.body.should == { :ping => "pong" }.to_json
end
end
end
end

21 changes: 21 additions & 0 deletions spec/features/grape-on-rails_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

describe "Grape on Rails", :js => true, :type => :feature do
context "homepage" do
before :each do
visit "/"
end
it "displays index.html page" do
page.find("#header h2").should have_content "riding Ruby on Rails"
end
end
context "exception" do
before :each do
visit "/api/v1/system/raise"
end
it "displays 500 page" do
page.find("title").text.should == "Action Controller: Exception caught"
end
end
end

14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rubygems'

require File.expand_path("../../config/environment", __FILE__)

require 'rspec/rails'
RSpec.configure do |config|
config.mock_with :rspec
config.expect_with :rspec
config.include RSpec::Rails::RequestExampleGroup, :type => :request, :example_group => {
:file_path => /spec\/api/
}
end

require 'capybara/rspec'

0 comments on commit d3f8362

Please sign in to comment.