Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'set'
- require 'active_model'
- require 'active_support/core_ext/class'
- require 'json'
- require 'acts_as_api'
- class Customer
- attr_accessor :firstname, :lastname, :age, :sex, :phone, :orders
- include ActiveModel::Serializers::Xml
- extend ActsAsApi::Base
- acts_as_api
- api_accessible :default => [ :firstname, :lastname ],
- :v1_public => [ :firstname, :age ],
- :v2_public => [ :firstname, :age, :sex ],
- :v1_private => [ :firstname, :lastname, :age, :sex ],
- :v2_private => [ :firstname, :lastname, :age, :sex, :phone, :orders ]
- def initialize(opts)
- opts.each do |k,v|
- self.send :"#{k}=", v
- end
- end
- def model_name
- 'customer'
- end
- end
- class Order
- attr_accessor :items, :total
- extend ActsAsApi::Base
- acts_as_api
- api_accessible :v2_public => [ :items ],
- :v2_private => [ :items, :total ]
- def initialize(opts)
- opts.each do |k,v|
- self.send :"#{k}=", v
- end
- end
- end
- # jsut a render stub
- def render(text)
- case
- when text[:json] && text[:xml]
- raise "XML or JSON?"
- when text[:json]
- puts text[:json].to_json
- when text[:xml]
- puts text[:xml].to_xml
- end
- end
- def api_template(version = :v2, template = :public)
- "#{version.to_s}_#{template.to_s}".to_sym
- end
- include ActsAsApi::Rendering
- orders = []
- 10.times do
- orders << Order.new(:total => rand(100), :items => %w{a b c d e f g h i j}.select{rand > 0.6})
- end
- @customer = Customer.new :firstname => 'niko', :lastname => 'dittmann', :age => 35, :sex => :male, :phone => '959532', :orders => orders
- render_for_api :default, :xml => @customer
- render_for_api :default, :json => @customer
- render_for_api api_template, :xml => @customer
- render_for_api api_template(:v2, :private), :json => @customer, :root => 'foo'
Add Comment
Please, Sign In to add comment