Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
fix specs for api change
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro committed Sep 26, 2012
1 parent daa052f commit 42fff91
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 28 deletions.
31 changes: 31 additions & 0 deletions Guardfile
@@ -0,0 +1,31 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard 'brakeman' do
watch(%r{^app/.+\.(erb|haml|rhtml|rb)$})
watch(%r{^config/.+\.rb$})
watch(%r{^lib/.+\.rb$})
watch('Gemfile')
end

guard 'rspec', :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }

# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }

# Capybara request specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }

# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end

39 changes: 21 additions & 18 deletions app.rb
Expand Up @@ -27,18 +27,18 @@
#
# @!macro [new] list
#
# - +/list+ : Short list of available objects, depending on the
# - +/_list+ : Short list of available objects, depending on the
# current request context: hosts, services, etc.
#
# @!macro [new] state
#
# - +/state+ - Instead of full status information send only
# - +/_state+ - Instead of full status information send only
# current state. For hosts up/down, for services OK, Warn,
# Critical, Unknown (0,1,2-1)
#
# @!macro [new] full
#
# - +/full+ - Show full status information
# - +/_full+ - Show full status information
# TODO Not implemented
#

Expand Down Expand Up @@ -99,7 +99,10 @@ class Nagira < Sinatra::Base
end

##
# TODO
# @method clear_instance_data
# @overload before("clear data")
#
# Clear values onf instance variables before start.
#
before do
@data = []
Expand Down Expand Up @@ -148,13 +151,13 @@ class Nagira < Sinatra::Base
#
# = Examples
#
# GET /objects/list # => :list
# GET /status/state # => :state
# GET /status/:hostname # => :full
# GET /status # => :full
# GET /_objects/_list # => :list
# GET /_status/_state # => :state
# GET /_status/:hostname # => :full
# GET /_status # => :full
#
before do
request.path_info.sub!(/\/(list|state)$/, '')
request.path_info.sub!(/\/_(list|state)$/, '')
@output = ($1 || :full).to_sym
end

Expand Down Expand Up @@ -232,7 +235,7 @@ class Nagira < Sinatra::Base
#
# Get Nagios configuration hash form parsing main Nagios
# configuration file nagios.cfg
get "/config" do
get "/_config" do
@data = $nagios[:config].configuration
nil
end
Expand All @@ -251,7 +254,7 @@ class Nagira < Sinatra::Base
# @macro accepted
# @macro list
#
get "/objects" do
get "/_objects" do

@data = begin
@output == :list ? @objects.keys : @objects
Expand All @@ -271,7 +274,7 @@ class Nagira < Sinatra::Base
# @!macro list
#
#
get "/objects/:type" do |type|
get "/_objects/:type" do |type|
begin
@data = @objects[type.to_sym]
@data = @data.keys if @output == :list
Expand All @@ -293,7 +296,7 @@ class Nagira < Sinatra::Base
# @!macro accepted
# * none
#
get "/objects/:type/:name" do |type,name|
get "/_objects/:type/:name" do |type,name|
begin
@data = @objects[type.to_sym][name]
rescue NoMethodError
Expand All @@ -319,7 +322,7 @@ class Nagira < Sinatra::Base
# @!macro accepted
# @!macro state
#
get "/status/:hostname/services/:service_name" do |hostname,service|
get "/_status/:hostname/_services/:service_name" do |hostname,service|

if @output == :state
@data = @status[hostname]['servicestatus'][service].slice("hostname", "service_description", "current_state")
Expand All @@ -339,7 +342,7 @@ class Nagira < Sinatra::Base
# @!macro state
# @!macro list
# @!macro full
get "/status/:hostname/services" do |hostname|
get "/_status/:hostname/_services" do |hostname|

case @output
when :list
Expand All @@ -362,7 +365,7 @@ class Nagira < Sinatra::Base
# @!macro accepted
# @!macro state
#
get "/status/:hostname" do |hostname|
get "/_status/:hostname" do |hostname|
@data = @status[hostname]['hoststatus'].dup

if @output == :state
Expand All @@ -382,7 +385,7 @@ class Nagira < Sinatra::Base
# @!macro list
# @!macro full
#
get "/status" do
get "/_status" do
@data = @status.dup

case @output
Expand All @@ -401,7 +404,7 @@ class Nagira < Sinatra::Base
#
# Provide information about API routes
#
get "/api" do
get "/_api" do
@data = self.api
nil
end
Expand Down
4 changes: 2 additions & 2 deletions spec/01_nagira_response_spec.rb
Expand Up @@ -13,7 +13,7 @@ def app
@app ||= Nagira
end

TOP_PAGES = %w{ config objects status api }
TOP_PAGES = %w{ _config _objects _status _api }
FORMATS = %w{ xml yaml json}
DEFAULT_FORMAT = ::Nagira.settings.format
TYPES = %w{state list}
Expand Down Expand Up @@ -88,7 +88,7 @@ def app
context "/config" do

before do
get "/config.json"
get "/_config.json"
@data = JSON.parse last_response.body
end

Expand Down
14 changes: 7 additions & 7 deletions spec/02_nagira_data_spec.rb
Expand Up @@ -35,7 +35,7 @@ def app
context "/objects" do

before :all do
get "/objects"
get "/_objects"
@data = JSON.parse last_response.body

# make sure these exist
Expand All @@ -53,15 +53,15 @@ def app
end

%w{host service contact timeperiod}.each do |obj|
context "/objects/#{obj}" do
context "/_objects/#{obj}" do

it "should respond to HTTP resuest" do
get "/objects/#{obj}.json"
get "/_objects/#{obj}.json"
last_response.should be_ok
end

it "response to /objects/#{obj} should be Hash" do
get "/objects/#{obj}.json"
get "/_objects/#{obj}.json"
JSON.parse(last_response.body).should be_a_kind_of Hash
end
end
Expand All @@ -76,13 +76,13 @@ def app

context '/status' do
before :all do
get "/status"
get "/_status"
@data = JSON.parse last_response.body

get "/status/list"
get "/_status/_list"
@list = JSON.parse last_response.body

get "/status/state"
get "/_status/_state"
@state = JSON.parse last_response.body
end

Expand Down
2 changes: 1 addition & 1 deletion spec/03_api_spec.rb
Expand Up @@ -17,7 +17,7 @@ def app

context "API data" do
before :all do
get "/api.json"
get "/_api.json"
@data = JSON.parse last_response.body
end

Expand Down

0 comments on commit 42fff91

Please sign in to comment.