require 'mimic' require 'socket' # Listen on the following :port # Add the following to your objective-c code, in the place where your # server root is configured # #ifdef FRANKIFIED # server = "http://localhost:11988" # #else # server = # #endif Mimic.mimic(:port => 11988) do |m| # use the 'views' directory adjacent to this file for API templates set :views, File.expand_path('views', File.dirname(__FILE__)) # EXAMPLE ROUTE 1 - XML summary using erb template # Uncomment to get xml defined by 'views/my_objects_xml.erb' containing all MyObject instances # @see views/my_objects_xml.erb # # get '/my_objects.xml' do # @my_objects = MyObject.find_all # erb :my_objects_xml, :content_type => 'application/xml' # end # EXAMPLE ROUTE 2 - JSON summary using erb template # Uncomment to get json defined by 'views/my_objects_json.erb' containing all MyObject instances # @see views/my_objects_json.erb # # get '/my_objects.json' do # @my_objects = MyObject.find_all # erb :my_objects_json, :content_type => 'application/json' # end # EXAMPLE ROUTE 3 - JSON detail using erb template # Uncomment to get json defined by 'views/my_object_json.erb' containing detail about a single MyObject instance # found using the URL parameter "name", i.e. /myobject.json?name=Fred # # get '/my_object.json' do # @my_objects = MyObject.find_first({:name => params[:name]}) # erb :my_object_json, :content_type => 'application/json' # end # EXAMPLE ROUTE 4 - JSON summary using JBuilder # Uncomment to get json defined programatically. # This is recommended if you are building from scratch or testing a system which has concise, well defined JSON, # as this will be more maintainable than erb templates # You will also need to add # gem 'jbuilder' # to your Gemfile # See more on JBuilder here https://github.com/rails/jbuilder # # get '/my_objects.json' do # my_objects = MyObject.find_all # json = Jbuilder.encode do |json| # json.array!(my_objects) do |o| # json.name o.name # end # end # [200, {'Content-Type' => 'application/json'}, json] # end # EXAMPLE ROUTE 5 - Authentication # An example route requiring authentication, and setting a cookie once the user 'admin', 'password' authorized # def protected! # unless authorized? # response['WWW-Authenticate'] = %(Basic realm="Restricted Area") # throw(:halt, [401, "Not authorized\n"]) # end # end # # def authorized? # if request.cookies.key?('my cookie') # true # else # @auth ||= Rack::Auth::Basic::Request.new(request.env) # if(@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ['admin', 'password']) # response.set_cookie('my cookie', :value => {:user => 'admin@admin.com'}, :expires => Time.now + 10.minutes, :path => "/") # true # else # false # end # end # end # # get '/my_secret_objects.xml' do # protected! # @my_objects = MyObject.find_all({:name => 'secret'}) # erb :my_objects_json, :content_type => 'application/json' # end end