acts_as_api-0.4.3/ 0000755 0000041 0000041 00000000000 12763720301 014015 5 ustar www-data www-data acts_as_api-0.4.3/Rakefile 0000644 0000041 0000041 00000002064 12763720301 015464 0 ustar www-data www-data require 'bundler' require 'rspec/core' require 'rspec/core/rake_task' Bundler::GemHelper.install_tasks RSpec::Core::RakeTask.new namespace :spec do supported_orms = %w(mongoid active_record) supported_orms.each do |orm| desc "Run #{orm} specs only" RSpec::Core::RakeTask.new(orm) do |t| t.rspec_opts = ["--color"] end task "prepare_#{orm}" do ENV['ACTS_AS_API_ORM'] = orm end Rake::Task["spec:#{orm}"].prerequisites << "spec:prepare_#{orm}" end # task :all => supported_orms.map{|orm| "spec:#{orm}"} desc "Runs specs for all ORMs (#{supported_orms.join(', ')})" task :all do supported_orms.each do |orm| puts "Starting to run specs for #{orm}..." system("bundle exec rake spec:#{orm}") raise "#{orm} failed!" unless $?.exitstatus == 0 end end end gemspec = Gem::Specification.load("acts_as_api.gemspec") task :default => "spec:all" desc "Generate the gh_pages site" task :rocco do system "bundle exec rocco examples/introduction/index.rb -t examples/introduction/layout.mustache" end acts_as_api-0.4.3/Gemfile 0000644 0000041 0000041 00000000605 12763720301 015311 0 ustar www-data www-data source "http://rubygems.org" # Specify your gem's dependencies in acts_as_api.gemspec gemspec group :test do gem 'test-unit', '~> 3.0' gem 'sqlite3-ruby' gem 'mongoid', '>= 3.0.1' gem 'rspec', '>= 2.9.0' gem 'rspec-its' gem 'rspec-collection_matchers' gem 'capybara' gem 'rspec-rails', '>= 2.5.0' gem 'webrat' gem 'shared_engine', :path => './spec/shared_engine' end acts_as_api-0.4.3/examples/ 0000755 0000041 0000041 00000000000 12763720301 015633 5 ustar www-data www-data acts_as_api-0.4.3/examples/introduction/ 0000755 0000041 0000041 00000000000 12763720301 020354 5 ustar www-data www-data acts_as_api-0.4.3/examples/introduction/index.rb 0000644 0000041 0000041 00000010757 12763720301 022022 0 ustar www-data www-data # The built-in XML/JSON support of Rails is great but: # You surely don’t want to expose your models always with all attributes. # # acts_as_api enriches the models and controllers of your app in a rails-like way so you can easily determine how your API responses should look like. ### Features # * DRY templates for your api responses # * Ships with support for **ActiveRecord** and **Mongoid** # * Support for Rails 3 Responders # * Plays very well together with client libs like [Backbone.js][b1] or [RestKit][r1] (iOS). # * Easy but very flexible syntax for defining the templates # * XML, JSON and JSON-P support out of the box, easy to extend # * Support for meta data like pagination info, etc... # * Minimal dependecies (you can also use it without Rails) # * Supports multiple api rendering templates for a models. This is especially useful for API versioning or for example for private vs. public access points to a user’s profile. # [b1]: http://documentcloud.github.com/backbone # [r1]: http://restkit.org # *** ### Rails 3.x Quickstart # Add to gemfile gem 'acts_as_api' # Update your bundle bundle install #### Setting up your Model # Given you have a model `User`. # If you only want to expose the `first_name` and `last_name` attribute of a user via your api, you would do something like this: # Within your model: # # First you activate acts_as_api for your model by calling `acts_as_api`. # # Then you define an api template to render the model with `api_accessible`. class User < ActiveRecord::Base acts_as_api api_accessible :name_only do |template| template.add :first_name template.add :last_name end end # An API template with the name `:name_only` was created. # # See below how to use it in the controller: #### Setting up your Controller # Now you just have to exchange the `render` method in your controller for the `render_for_api` method. class UsersController < ApplicationController def index @users = User.all # Note that it's wise to add a `root` param when rendering lists. respond_to do |format| format.xml { render_for_api :name_only, :xml => @users, :root => :users } format.json { render_for_api :name_only, :json => @users, :root => :users } end end def show @user = User.find(params[:id]) respond_to do |format| format.xml { render_for_api :name_only, :xml => @user } format.json { render_for_api :name_only, :json => @user } end end end #### That's it! # Try it. The JSON response of #show should now look like this: # # Other attributes of the model like `created_at` or `updated_at` won’t be included # because they were not listed by `api_accessible` in the model. { "user": { "first_name": "John", "last_name": "Doe" } } # *** ### But wait! ... there's more # # Often the pure rendering of database values is just not enough, so acts_as_api # provides you some tools to customize your API responses. #### What can I include in my responses? # # You can do basically anything: # # * [Include attributes and all other kinds of methods of your model][w1] # * [Include child associations (if they also act_as_api this will be considered)][w2] # * [Include lambdas and Procs][w3] # * [Call methods of a parent association][w4] # * [Call scopes of your model or child associations][w5] # * [Rename attributes, methods, associations][w6] # * [Create your own hierarchies][w7] # # You can find more advanced examples in the [Github Wiki][wi] # # [wi]: https://github.com/fabrik42/acts_as_api/wiki/ # [w1]: https://github.com/fabrik42/acts_as_api/wiki/Calling-a-method-of-the-model # [w2]: https://github.com/fabrik42/acts_as_api/wiki/Including-a-child-association # [w3]: https://github.com/fabrik42/acts_as_api/wiki/Calling-a-lambda-in-the-api-template # [w4]: https://github.com/fabrik42/acts_as_api/wiki/Calling-a-method-of-the-model # [w5]: https://github.com/fabrik42/acts_as_api/wiki/Calling-a-scope-of-a-sub-resource # [w6]: https://github.com/fabrik42/acts_as_api/wiki/Renaming-an-attribute # [w7]: https://github.com/fabrik42/acts_as_api/wiki/Creating-a-completely-different-response-structure # *** ### Links # * Check out the [source code on Github][so] # * For more usage examples visit the [wiki][wi] # * Found a bug or do you have a feature request? [issue tracker][to] # * [Docs][do] # # [so]: https://github.com/fabrik42/acts_as_api/ # [wi]: https://github.com/fabrik42/acts_as_api/wiki/ # [to]: https://github.com/fabrik42/acts_as_api/issues # [do]: http://rdoc.info/github/fabrik42/acts_as_api acts_as_api-0.4.3/examples/introduction/docco.css 0000644 0000041 0000041 00000015675 12763720301 022173 0 ustar www-data www-data /*--------------------- Layout and Typography ----------------------------*/ body { font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif; font-size: 15px; line-height: 22px; color: #252519; margin: 0; padding: 0; } a { color: #261a3b; } a:visited { color: #261a3b; } p { margin: 0 0 15px 0; } h1, h2, h3, h4, h5, h6 { margin: 0px 0 15px 0; } h1 { margin-top: 40px; } #container { position: relative; } #background { position: fixed; top: 0; left: 525px; right: 0; bottom: 0; background: #f5f5ff; border-left: 1px solid #e5e5ee; z-index: -1; } #jump_to, #jump_page { background: white; -webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777; -webkit-border-bottom-left-radius: 5px; -moz-border-radius-bottomleft: 5px; font: 10px Arial; text-transform: uppercase; cursor: pointer; text-align: right; } #jump_to, #jump_wrapper { position: fixed; right: 0; top: 0; padding: 5px 10px; } #jump_wrapper { padding: 0; display: none; } #jump_to:hover #jump_wrapper { display: block; } #jump_page { padding: 5px 0 3px; margin: 0 0 25px 25px; } #jump_page .source { display: block; padding: 5px 10px; text-decoration: none; border-top: 1px solid #eee; } #jump_page .source:hover { background: #f5f5ff; } #jump_page .source:first-child { } table td { border: 0; outline: 0; } td.docs, th.docs { max-width: 450px; min-width: 450px; min-height: 5px; padding: 10px 25px 1px 50px; overflow-x: hidden; vertical-align: top; text-align: left; } .docs pre { margin: 15px 0 15px; padding-left: 15px; } .docs p tt, .docs p code { background: #f8f8ff; border: 1px solid #dedede; font-size: 12px; padding: 0 0.2em; } .pilwrap { position: relative; } .pilcrow { font: 12px Arial; text-decoration: none; color: #454545; position: absolute; top: 3px; left: -20px; padding: 1px 2px; opacity: 0; -webkit-transition: opacity 0.2s linear; } td.docs:hover .pilcrow { opacity: 1; } td.code, th.code { padding: 14px 15px 16px 25px; width: 100%; vertical-align: top; background: #f5f5ff; border-left: 1px solid #e5e5ee; } pre, tt, code { font-size: 12px; line-height: 18px; font-family: Monaco, Consolas, "Lucida Console", monospace; margin: 0; padding: 0; } /*---------------------- Syntax Highlighting -----------------------------*/ td.linenos { background-color: #f0f0f0; padding-right: 10px; } span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; } body .hll { background-color: #ffffcc } body .c { color: #408080; font-style: italic } /* Comment */ body .err { border: 1px solid #FF0000 } /* Error */ body .k { color: #954121 } /* Keyword */ body .o { color: #666666 } /* Operator */ body .cm { color: #408080; font-style: italic } /* Comment.Multiline */ body .cp { color: #BC7A00 } /* Comment.Preproc */ body .c1 { color: #408080; font-style: italic } /* Comment.Single */ body .cs { color: #408080; font-style: italic } /* Comment.Special */ body .gd { color: #A00000 } /* Generic.Deleted */ body .ge { font-style: italic } /* Generic.Emph */ body .gr { color: #FF0000 } /* Generic.Error */ body .gh { color: #000080; font-weight: bold } /* Generic.Heading */ body .gi { color: #00A000 } /* Generic.Inserted */ body .go { color: #808080 } /* Generic.Output */ body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ body .gs { font-weight: bold } /* Generic.Strong */ body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ body .gt { color: #0040D0 } /* Generic.Traceback */ body .kc { color: #954121 } /* Keyword.Constant */ body .kd { color: #954121; font-weight: bold } /* Keyword.Declaration */ body .kn { color: #954121; font-weight: bold } /* Keyword.Namespace */ body .kp { color: #954121 } /* Keyword.Pseudo */ body .kr { color: #954121; font-weight: bold } /* Keyword.Reserved */ body .kt { color: #B00040 } /* Keyword.Type */ body .m { color: #666666 } /* Literal.Number */ body .s { color: #219161 } /* Literal.String */ body .na { color: #7D9029 } /* Name.Attribute */ body .nb { color: #954121 } /* Name.Builtin */ body .nc { color: #0000FF; font-weight: bold } /* Name.Class */ body .no { color: #880000 } /* Name.Constant */ body .nd { color: #AA22FF } /* Name.Decorator */ body .ni { color: #999999; font-weight: bold } /* Name.Entity */ body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ body .nf { color: #0000FF } /* Name.Function */ body .nl { color: #A0A000 } /* Name.Label */ body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ body .nt { color: #954121; font-weight: bold } /* Name.Tag */ body .nv { color: #19469D } /* Name.Variable */ body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ body .w { color: #bbbbbb } /* Text.Whitespace */ body .mf { color: #666666 } /* Literal.Number.Float */ body .mh { color: #666666 } /* Literal.Number.Hex */ body .mi { color: #666666 } /* Literal.Number.Integer */ body .mo { color: #666666 } /* Literal.Number.Oct */ body .sb { color: #219161 } /* Literal.String.Backtick */ body .sc { color: #219161 } /* Literal.String.Char */ body .sd { color: #219161; font-style: italic } /* Literal.String.Doc */ body .s2 { color: #219161 } /* Literal.String.Double */ body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ body .sh { color: #219161 } /* Literal.String.Heredoc */ body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ body .sx { color: #954121 } /* Literal.String.Other */ body .sr { color: #BB6688 } /* Literal.String.Regex */ body .s1 { color: #219161 } /* Literal.String.Single */ body .ss { color: #19469D } /* Literal.String.Symbol */ body .bp { color: #954121 } /* Name.Builtin.Pseudo */ body .vc { color: #19469D } /* Name.Variable.Class */ body .vg { color: #19469D } /* Name.Variable.Global */ body .vi { color: #19469D } /* Name.Variable.Instance */ body .il { color: #666666 } /* Literal.Number.Integer.Long */ acts_as_api-0.4.3/examples/introduction/layout.mustache 0000644 0000041 0000041 00000003755 12763720301 023436 0 ustar www-data www-data
acts_as_apiMakes creating XML/JSON responses in Rails 3 easy and fun. |
|
|---|---|
| {{{ docs }}} |
{{{ code }}} |