Browse Source

simple api

pull/1/head
Nicolae Claudius 13 years ago
parent
commit
eb6e355695
  1. 4
      app/controllers/as_controller.rb
  2. 10
      app/controllers/records_controller.rb
  3. 1
      app/models/domain.rb
  4. 2
      config/environments/production.rb
  5. 2
      config/environments/test.rb
  6. 4
      config/routes.rb
  7. 29
      spec/controllers/records_controller_spec.rb

4
app/controllers/as_controller.rb

@ -1,6 +1,4 @@
class AsController < ApplicationController
respond_to :html, :xml, :json
active_scaffold :a do |conf|
conf.columns = [:name, :type, :content, :ttl, :prio, :change_date, :authentication_token]
conf.create.columns = [:name, :content, :ttl,]
@ -11,7 +9,7 @@ class AsController < ApplicationController
conf.columns[:ttl].options = {:i18n_number => {:delimiter => ''}}
conf.actions.exclude :show
end
before_filter :ensure_nested_under_domain, :except => 'modify'
before_filter :ensure_nested_under_domain
protected

10
app/controllers/records_controller.rb

@ -16,6 +16,8 @@ class RecordsController < ApplicationController
end
end
respond_to :html, :xml, :json
active_scaffold :record do |conf|
conf.sti_children = [:SOA, :NS, :MX, :A, :CNAME, :TXT]
conf.columns = [:name, :type, :content, :ttl, :prio, :change_date, :authentication_token]
@ -24,7 +26,7 @@ class RecordsController < ApplicationController
# conf.create.link.label = "Add Record"
conf.actions.exclude :show
end
before_filter :ensure_nested_under_domain
before_filter :ensure_nested_under_domain, :except => 'modify'
skip_before_filter :authenticate_user!, :only => 'modify'
protect_from_forgery :except => 'modify'
skip_authorize_resource :only => :modify
@ -34,6 +36,10 @@ class RecordsController < ApplicationController
@record = Record.where(:authentication_token => params[:authentication_token]).first!
@record.content = params[:ip] || client_remote_ip
@record.save!
respond_with @arecord
respond_with(@record) do |format|
format.html {
render :text => 'OK'
}
end
end
end

1
app/models/domain.rb

@ -33,6 +33,7 @@ class Domain < ActiveRecord::Base
validates :ns_records, :on => :create, :presence => true, :length => {
:minimum => 2, :maximum => 10, :message => "must have be at least 2, at most 10"}
validates_associated :records
validates :user_id, :presence => true
def slave?; self.type == 'SLAVE' end

2
config/environments/production.rb

@ -58,7 +58,7 @@ Entrydns::Application.configure do
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
config.action_mailer.default_url_options = { :host => 'entrydns.net' }
config.action_mailer.default_url_options = {:host => 'entrydns.net'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "127.0.0.1",

2
config/environments/test.rb

@ -39,4 +39,6 @@ Entrydns::Application.configure do
# Allow pass debug_assets=true as a query parameter to load pages with unpackaged assets
config.assets.allow_debugging = true
config.action_mailer.default_url_options = {:host => 'entrydns.net'}
end

4
config/routes.rb

@ -6,11 +6,9 @@ Entrydns::Application.routes.draw do
as_routes
end
put '/records/modify/:authentication_token', :to => 'records#modify', :as => :modify_record
resources :records do
as_routes
collection do
put 'modify/:authentication_token', :action => 'modify'
end
end
resources :soas do

29
spec/controllers/records_controller_spec.rb

@ -26,7 +26,34 @@ describe RecordsController do
def valid_attributes
{}
end
describe "PUT modify", :focus => true do
include_context "data"
before do
sign_in user
controller.stub(:current_user).and_return(user)
end
it "modifies @record when IP given" do
ip = '127.0.0.2'
put :modify, :authentication_token => a_record.authentication_token, :ip => ip
response.should be_success
assigns(:record).should == a_record
assigns(:record).content.should == ip
end
it "modifies @record with remote IP" do
ip = '127.0.0.3'
request.env["HTTP_X_FORWARDED_FOR"] = ip
put :modify, :authentication_token => a_record.authentication_token
response.should be_success
assigns(:record).should == a_record
assigns(:record).content.should == ip
end
end
describe "GET index" do
it "assigns all records as @records" do
record = Record.create! valid_attributes

Loading…
Cancel
Save