acts_as_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2010-06-27
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,16 @@
1
+ README.rdoc
2
+ History.txt
3
+ Manifest.txt
4
+ PostInstall.txt
5
+ Rakefile
6
+ lib/acts_as_api.rb
7
+ lib/acts_as_api/array.rb
8
+ lib/acts_as_api/base.rb
9
+ lib/acts_as_api/rendering.rb
10
+ script/console
11
+ script/destroy
12
+ script/generate
13
+ spec/acts_as_api_spec.rb
14
+ spec/spec.opts
15
+ spec/spec_helper.rb
16
+ tasks/rspec.rake
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on acts_as_api, see http://acts_as_api.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,49 @@
1
+ = acts_as_api
2
+
3
+ * http://github.com/ffwdme/acts_as_api
4
+
5
+
6
+ == DESCRIPTION:
7
+
8
+ acts_as_api makes creating XML/JSON responses in Rails 3 easy and fun.
9
+
10
+ == FEATURES/PROBLEMS:
11
+
12
+ * FIX (list of features or problems)
13
+
14
+ == SYNOPSIS:
15
+
16
+ See example app :)
17
+
18
+ == REQUIREMENTS:
19
+
20
+ * Rails 3 beta 4
21
+
22
+ == INSTALL:
23
+
24
+ sudo gem install acts_as_api
25
+
26
+ == LICENSE:
27
+
28
+ (The MIT License)
29
+
30
+ Copyright (c) 2010 Christian Bäuerlein
31
+
32
+ Permission is hereby granted, free of charge, to any person obtaining
33
+ a copy of this software and associated documentation files (the
34
+ 'Software'), to deal in the Software without restriction, including
35
+ without limitation the rights to use, copy, modify, merge, publish,
36
+ distribute, sublicense, and/or sell copies of the Software, and to
37
+ permit persons to whom the Software is furnished to do so, subject to
38
+ the following conditions:
39
+
40
+ The above copyright notice and this permission notice shall be
41
+ included in all copies or substantial portions of the Software.
42
+
43
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
44
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
46
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
47
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
48
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
49
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/acts_as_api'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'acts_as_api' do
14
+ self.developer 'Christian Bäuerlein', 'christian@ffwdme.com'
15
+ #self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ #self.rubyforge_name = self.name # TODO this is default value
17
+ self.extra_deps = [['activerecord','>= 3.0.0.beta4'], ['actionpack','>= 3.0.0.beta4']]
18
+
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ # In Netbeans the next lines causes every rspec test to run twice.
23
+ # If you're not using Netbeans and having problem with testing - try to disable it.
24
+ #Dir['tasks/**/*.rake'].each { |t| load t }
25
+
26
+ # TODO - want other tests/tasks run by default? Add them to the list
27
+ # remove_task :default
28
+ # task :default => [:spec, :features]
@@ -0,0 +1,35 @@
1
+ require "rubygems"
2
+
3
+ $:.unshift(File.dirname(__FILE__)) unless
4
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
5
+
6
+ require "acts_as_api/base"
7
+ require "acts_as_api/rendering"
8
+ require "acts_as_api/array"
9
+
10
+ # acts_as_api is a gem that aims to make the construction of JSON and XML
11
+ # responses in rails 3 easy and fun.
12
+ #
13
+ # Therefore it attaches a couple of helper methods to active record and
14
+ # the action controller base classes.
15
+ #
16
+ # acts_as_api uses the default serializers of your rails app and doesn't
17
+ # force you into more dependencies.
18
+ module ActsAsApi
19
+ VERSION = '0.0.1'
20
+
21
+ # The accepted response formats
22
+ # Default is +[:xml, :json]+
23
+ ACCEPTED_API_FORMATS = [:xml, :json]
24
+
25
+ end
26
+
27
+ # Attach ourselves to active record
28
+ if defined?(ActiveRecord::Base)
29
+ ActiveRecord::Base.extend ActsAsApi::Base
30
+ end
31
+
32
+ # Attach ourselves to the abstract controller of rails
33
+ if defined?(AbstractController::Rendering)
34
+ AbstractController::Rendering.send :include, ActsAsApi::Rendering
35
+ end
@@ -0,0 +1,25 @@
1
+ # The standard ruby Array class is extended by one method.
2
+ class Array
3
+
4
+ # Neccessary to render an Array of models, e.g. the result of a search.
5
+ #
6
+ # The Array checks all its items if they respond to the +as_api_response+ method.
7
+ # If they do, the result of this method will be collected.
8
+ # If they don't, the item itself will be collected.
9
+ def as_api_response
10
+
11
+ sub_items = []
12
+
13
+ each do |item|
14
+ if item.respond_to?(:as_api_response)
15
+ sub_items << item.as_api_response
16
+ else
17
+ sub_items << item
18
+ end
19
+ end
20
+
21
+ sub_items
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,115 @@
1
+ module ActsAsApi
2
+ # This module enriches the ActiveRecord::Base module of Rails.
3
+ module Base
4
+ # Indicates if the current model acts as api.
5
+ # False by default.
6
+ def acts_as_api?
7
+ false
8
+ end
9
+
10
+ # When invoked, it enriches the current model with the
11
+ # class and instance methods to act as api.
12
+ def acts_as_api
13
+
14
+ class_eval do
15
+ include ActsAsApi::Base::InstanceMethods
16
+ extend ActsAsApi::Base::ClassMethods
17
+ end
18
+
19
+ end
20
+
21
+ module ClassMethods
22
+
23
+ def acts_as_api?#:nodoc:
24
+ self.included_modules.include?(InstanceMethods)
25
+ end
26
+
27
+ # Determines the attributes, methods of the model that are accessible in the api response.
28
+ # *Note*: There is only whitelisting for api accessible attributes.
29
+ # So once the model acts as api, you have to determine all attributes here that should
30
+ # be contained in the api responses.
31
+ def api_accessible(*attributes)
32
+ write_inheritable_attribute(:api_accessible, Set.new(attributes) + (api_accessible_attributes || []))
33
+ end
34
+
35
+ # Returns an array of all the attributes that have been made accessible to the api response.
36
+ def api_accessible_attributes
37
+ read_inheritable_attribute(:api_accessible)
38
+ end
39
+
40
+ end
41
+
42
+
43
+ module InstanceMethods
44
+
45
+ # Creates the api response of the model and returns it as a Hash.
46
+ def as_api_response
47
+ api_attributes = self.class.api_accessible_attributes
48
+
49
+ api_output = {}
50
+
51
+ return api_output if api_attributes.nil?
52
+
53
+ api_attributes.each do |attribute|
54
+
55
+ case attribute
56
+ when Symbol
57
+
58
+ if self.respond_to?(attribute)
59
+ out = send attribute
60
+
61
+ if out.respond_to?(:as_api_response)
62
+ out = out.send(:as_api_response)
63
+ end
64
+
65
+ api_output[attribute] = out
66
+
67
+ end
68
+
69
+ when Hash
70
+
71
+ queue = []
72
+ queue << { :parent => api_output, :item => attribute}
73
+
74
+ until queue.empty? do
75
+
76
+ leaf = queue.pop
77
+
78
+ leaf[:item].each do |k,v|
79
+
80
+ case v
81
+ when Symbol
82
+
83
+ if self.respond_to?(v)
84
+ out = send v
85
+
86
+ if out.respond_to?(:as_api_response)
87
+ out = out.send(:as_api_response)
88
+ end
89
+
90
+ leaf[:parent][k] = out
91
+
92
+ end
93
+
94
+ when Hash
95
+ leaf[:parent][k] ||= {}
96
+ queue << { :parent => leaf[:parent][k], :item => v}
97
+ end
98
+
99
+ end
100
+
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+
107
+ api_output
108
+
109
+ end
110
+
111
+ end
112
+
113
+
114
+ end
115
+ end
@@ -0,0 +1,43 @@
1
+ module ActsAsApi
2
+
3
+ # The methods of this module are included into the AbstractController::Rendering
4
+ # module.
5
+ module Rendering
6
+
7
+ # Provides an alternative to the +render+ method used within the controller
8
+ # to simply generate API outputs.
9
+ #
10
+ # The default Rails serializers are used to serialize the data.
11
+ def render_for_api(render_options)
12
+
13
+ # extract the api format and model
14
+ api_format_options = {}
15
+
16
+ ActsAsApi::ACCEPTED_API_FORMATS.each do |item|
17
+ if render_options.has_key?(item)
18
+ api_format_options[item] = render_options[item]
19
+ render_options.delete item
20
+ end
21
+ end
22
+
23
+
24
+ api_format = api_format_options.keys.first
25
+ api_model = api_format_options.values.first
26
+
27
+ # set the params to render
28
+ output_params = render_options
29
+
30
+ # set the name of the root node - pluralize for arrays
31
+ api_root_name = api_model.is_a?(Array) ? api_model.first.class.name.downcase.pluralize : api_model.class.name.downcase
32
+ output_params[:root] = api_root_name
33
+
34
+ # create the Hash as response
35
+ output_params[api_format] = api_model.as_api_response
36
+
37
+ render output_params
38
+
39
+ end
40
+
41
+ end
42
+
43
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/acts_as_api.rb'}"
9
+ puts "Loading acts_as_api gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,87 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ # Time to add your specs!
4
+ # http://rspec.info/
5
+ describe "setting up acts_as_api" do
6
+
7
+ before(:each) do
8
+ @luke = Customer.new({ :firstname => 'Luke', :lastname => 'Skywalker', :age => 25, :active => true })
9
+ @han = Customer.new({ :firstname => 'Han', :lastname => 'Solo', :age => 35, :active => true })
10
+ @leia = Customer.new({ :firstname => 'Princess', :lastname => 'Leia', :age => 25, :active => false })
11
+
12
+ # always reset the api_accessible values
13
+ Customer.write_inheritable_attribute(:api_accessible, Set.new )
14
+ end
15
+
16
+ it "should be disabled by default" do
17
+ Customer.acts_as_api?.should be_false
18
+ Customer.should_not respond_to :api_accessible
19
+ # now enable it
20
+ Customer.acts_as_api
21
+ # should respond now
22
+ Customer.acts_as_api?.should be_true
23
+ Customer.should respond_to :api_accessible
24
+ end
25
+
26
+ it "check simple attributes list" do
27
+
28
+ Customer.api_accessible :firstname, :lastname
29
+
30
+ response = @luke.as_api_response
31
+
32
+ response.should be_kind_of(Hash)
33
+
34
+ response.should have(2).keys
35
+
36
+ response.keys.should include(:firstname, :lastname)
37
+
38
+ response.values.should include(@luke.firstname, @luke.lastname)
39
+
40
+ end
41
+
42
+ it "check method call in attributes list" do
43
+
44
+ Customer.api_accessible :full_name
45
+
46
+ response = @luke.as_api_response
47
+
48
+ response.should be_kind_of(Hash)
49
+
50
+ response.should have(1).keys
51
+
52
+ response.keys.should include(:full_name)
53
+
54
+ response.values.should include(@luke.full_name)
55
+
56
+ end
57
+
58
+
59
+ it "check renaming the node/key of an attribute" do
60
+
61
+ end
62
+
63
+ it "check renaming the node/key of a method" do
64
+
65
+ end
66
+
67
+ it "check included associations in attributes list (DON'T act_as_api themselves)" do
68
+
69
+ end
70
+
71
+
72
+ it "check included associations in attributes list (DO act_as_api themselves)" do
73
+
74
+ end
75
+
76
+ it "check creating a sub node and putting an attribute in it" do
77
+
78
+ end
79
+
80
+
81
+
82
+ it "check creating multiple sub nodes and putting an attribute in it" do
83
+
84
+ end
85
+
86
+
87
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,18 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'rubygems'
11
+ require 'active_support'
12
+ require 'active_record'
13
+ require 'action_controller'
14
+
15
+ require 'acts_as_api'
16
+
17
+ require 'spec_models'
18
+
data/tasks/rspec.rake ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_api
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - "Christian B\xC3\xA4uerlein"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-30 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 0
31
+ - beta4
32
+ version: 3.0.0.beta4
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: actionpack
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 3
44
+ - 0
45
+ - 0
46
+ - beta4
47
+ version: 3.0.0.beta4
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rubyforge
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 2
59
+ - 0
60
+ - 4
61
+ version: 2.0.4
62
+ type: :development
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: hoe
66
+ prerelease: false
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 2
73
+ - 6
74
+ - 1
75
+ version: 2.6.1
76
+ type: :development
77
+ version_requirements: *id004
78
+ description: acts_as_api makes creating XML/JSON responses in Rails 3 easy and fun.
79
+ email:
80
+ - christian@ffwdme.com
81
+ executables: []
82
+
83
+ extensions: []
84
+
85
+ extra_rdoc_files:
86
+ - History.txt
87
+ - Manifest.txt
88
+ - PostInstall.txt
89
+ files:
90
+ - README.rdoc
91
+ - History.txt
92
+ - Manifest.txt
93
+ - PostInstall.txt
94
+ - Rakefile
95
+ - lib/acts_as_api.rb
96
+ - lib/acts_as_api/array.rb
97
+ - lib/acts_as_api/base.rb
98
+ - lib/acts_as_api/rendering.rb
99
+ - script/console
100
+ - script/destroy
101
+ - script/generate
102
+ - spec/acts_as_api_spec.rb
103
+ - spec/spec.opts
104
+ - spec/spec_helper.rb
105
+ - tasks/rspec.rake
106
+ has_rdoc: true
107
+ homepage: http://github.com/ffwdme/acts_as_api
108
+ licenses: []
109
+
110
+ post_install_message:
111
+ rdoc_options:
112
+ - --main
113
+ - README.rdoc
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ requirements: []
131
+
132
+ rubyforge_project: acts_as_api
133
+ rubygems_version: 1.3.6
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: acts_as_api makes creating XML/JSON responses in Rails 3 easy and fun.
137
+ test_files: []
138
+