acts_as_api 0.0.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,17 @@
1
- === 0.0.3 2010-01-01
1
+ === 0.1.4 2010-07-04
2
+
3
+ * Added better documentation.
4
+
5
+ === 0.1.3 2010-07-04
6
+
7
+ * Support for including methods of associated models.
8
+ * Better root name determination for arrays.
9
+
10
+ === 0.0.3 2010-07-01
2
11
 
3
12
  * 1 small bugfix
4
13
 
5
- === 0.0.2 2010-01-01
14
+ === 0.0.2 2010-07-01
6
15
 
7
16
  * 1 major enhancement:
8
17
  * Added a wrapping root node for JSON responses by default.
@@ -1,7 +1,6 @@
1
1
  README.rdoc
2
2
  History.txt
3
3
  Manifest.txt
4
- PostInstall.txt
5
4
  Rakefile
6
5
  lib/acts_as_api.rb
7
6
  lib/acts_as_api/array.rb
@@ -2,18 +2,146 @@
2
2
 
3
3
  * http://github.com/ffwdme/acts_as_api
4
4
 
5
-
6
5
  == DESCRIPTION:
7
6
 
8
7
  acts_as_api makes creating XML/JSON responses in Rails 3 easy and fun.
9
8
 
10
- == FEATURES/PROBLEMS:
9
+ == FEATURES:
10
+
11
+ The built-in XML/JSON support of Rails is great but:
12
+
13
+ You surely don't want to expose your models always with all attributes.
14
+
15
+ acts_as_api enriches the models and controllers of your app in a rails-like way so
16
+ you can easily determine how your API responses should look like.
11
17
 
12
- * FIX (list of features or problems)
18
+ acts_as_api uses the *default Rails serializers* for XML and JSON, so you don't have to
19
+ mess around with even more dependencies. Once you change the serializers for your Rails app,
20
+ they will be changed for acts_as_api too.
13
21
 
14
22
  == SYNOPSIS:
15
23
 
16
- See example app :)
24
+ === Set up your model
25
+
26
+ Say you have a model +Customer+ and every customer has many +Orders+.
27
+
28
+ If you only want to expose the +firstname+ and +lastname+ attribute of a customer
29
+ via the api, you would do something like this:
30
+
31
+
32
+ class Customer < ActiveRecord::Base
33
+
34
+ has_many :orders
35
+
36
+ # let this model act as api!
37
+ acts_as_api
38
+
39
+ # define the accessible attributes/methods for the api response
40
+ api_accessible :firstname, :lastname
41
+
42
+ end
43
+
44
+ === Set up controller
45
+
46
+ Now you just have to exchange the +render+ method in your controller for the +render_for_api+ method.
47
+
48
+ class CustomersController < ApplicationController
49
+
50
+ def show
51
+ @customer = Customer.find(params[:id])
52
+
53
+ respond_to do |format|
54
+ format.html # show.html.erb
55
+ format.xml { render_for_api :xml => @customer }
56
+ format.json { render_for_api :json => @customer }
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ === That's it!
63
+
64
+ Try it. The response should now look like this:
65
+
66
+ <?xml version="1.0" encoding="UTF-8"?>
67
+ <customer>
68
+ <firstname>John</firstname>
69
+ <lastname>Doe</lastname>
70
+ </customer>
71
+
72
+ Other attributes of the model like +created_at+ or +updated_at+ won't be included because they were
73
+ not listed by +api_accessible+ in the model.
74
+
75
+ === What can I include in my responses?
76
+
77
+ You can do basically anything:
78
+
79
+ * Include attributes and all other kinds of methods of your model
80
+ * Include child associations (if they also act_as_api this will be considered)
81
+ * Call methods of a parent association
82
+ * Rename attributes, methods, associations
83
+ * Create your own hierarchies
84
+
85
+ Here are two models from the example app that show how it works.
86
+
87
+ The model +Customer+ shows all kinds of ways to add data to your API response.
88
+
89
+ class Customer < ActiveRecord::Base
90
+
91
+ has_many :orders
92
+
93
+ # let this model act as api!
94
+ acts_as_api
95
+
96
+ # define the accessible attributes/methods for the api response
97
+ # some attributes of the model
98
+ api_accessible :firstname, :lastname, :age,
99
+ # you can include methods
100
+ :full_name,
101
+ # include associated model in response
102
+ :orders,
103
+ # rename the node for orders
104
+ :renamed_orders => :orders,
105
+ # put orders in another subnode
106
+ :subnode_orders => { :sub_oders => :orders },
107
+ # rename nodes/tag names
108
+ :other_node => :say_something,
109
+ # create a deeper node hierarchy
110
+ :maybe => { :useful => { :for => :say_something } }
111
+
112
+ # some example methods
113
+ def full_name
114
+ '' << firstname.to_s << ' ' << lastname.to_s
115
+ end
116
+
117
+ def say_something
118
+ "something"
119
+ end
120
+
121
+ end
122
+
123
+ The model +Order+ also acts as api and is even able to access a method of their parent customer.
124
+
125
+ class Order < ActiveRecord::Base
126
+
127
+ belongs_to :customer
128
+
129
+ # let this model act as api!
130
+ acts_as_api
131
+
132
+ # define the accessible attributes/methods for the api response
133
+ # some attributes of the model
134
+ api_accessible :city, :amount,
135
+ #access a method of the parent association
136
+ :parent_name => "customer.full_name"
137
+
138
+ end
139
+
140
+
141
+ === Is there an example I can play around with?
142
+
143
+ If you want a working example right out of the box, grab the example app: http://github.com/ffwdme/acts_as_api_example
144
+
17
145
 
18
146
  == REQUIREMENTS:
19
147
 
@@ -23,6 +151,18 @@ acts_as_api makes creating XML/JSON responses in Rails 3 easy and fun.
23
151
 
24
152
  sudo gem install acts_as_api
25
153
 
154
+ Then add acts_as_api to the Gemfile of your Rails project
155
+
156
+ gem 'acts_as_api'
157
+
158
+ == Links
159
+
160
+ * Docs: http://rdoc.info/projects/ffwdme/acts_as_api
161
+
162
+ * Found a bug? http://github.com/ffwdme/acts_as_api/issues
163
+
164
+ * Example App: http://github.com/ffwdme/acts_as_api_example
165
+
26
166
  == LICENSE:
27
167
 
28
168
  (The MIT License)
@@ -16,7 +16,7 @@ require "acts_as_api/array"
16
16
  # acts_as_api uses the default serializers of your rails app and doesn't
17
17
  # force you into more dependencies.
18
18
  module ActsAsApi
19
- VERSION = '0.0.3'
19
+ VERSION = '0.1.4'
20
20
 
21
21
  # The accepted response formats
22
22
  # Default is +[:xml, :json]+
@@ -66,6 +66,14 @@ module ActsAsApi
66
66
 
67
67
  end
68
68
 
69
+ when String
70
+ # go up the call chain
71
+ out = self
72
+ attribute.split(".").each do |method|
73
+ out = out.send(method.to_sym)
74
+ end
75
+ api_output[attribute] = out
76
+
69
77
  when Hash
70
78
 
71
79
  queue = []
@@ -91,6 +99,14 @@ module ActsAsApi
91
99
 
92
100
  end
93
101
 
102
+ when String
103
+ # go up the call chain
104
+ out = self
105
+ v.split(".").each do |method|
106
+ out = out.send(method.to_sym)
107
+ end
108
+ leaf[:parent][k] = out
109
+
94
110
  when Hash
95
111
  leaf[:parent][k] ||= {}
96
112
  queue << { :parent => leaf[:parent][k], :item => v}
@@ -28,9 +28,14 @@ module ActsAsApi
28
28
  output_params = render_options
29
29
 
30
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
31
+ if api_model.is_a?(Array)
32
+ api_root_name = api_model.respond_to?(:name) ? api_model.name.downcase.pluralize : api_model.first.class.name.downcase.pluralize
33
+ else
34
+ api_root_name = api_model.class.name.downcase
35
+ end
36
+
32
37
 
33
- output_params[:root] = api_root_name
38
+ output_params[:root] ||= api_root_name
34
39
 
35
40
  api_response = api_model.as_api_response
36
41
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 0
8
- - 3
9
- version: 0.0.3
7
+ - 1
8
+ - 4
9
+ version: 0.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Christian B\xC3\xA4uerlein"
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-01 00:00:00 +02:00
17
+ date: 2010-07-04 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -85,12 +85,10 @@ extensions: []
85
85
  extra_rdoc_files:
86
86
  - History.txt
87
87
  - Manifest.txt
88
- - PostInstall.txt
89
88
  files:
90
89
  - README.rdoc
91
90
  - History.txt
92
91
  - Manifest.txt
93
- - PostInstall.txt
94
92
  - Rakefile
95
93
  - lib/acts_as_api.rb
96
94
  - lib/acts_as_api/array.rb
@@ -1,7 +0,0 @@
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
-