to_api 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.md +49 -0
  2. data/lib/to_api.rb +3 -3
  3. data/spec/to_api_spec.rb +11 -1
  4. metadata +4 -4
  5. data/README +0 -1
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ Description
2
+ ===========
3
+
4
+ A lightweight, format-agnostic API transformation gem.
5
+
6
+ Synopsis
7
+ ========
8
+
9
+ Models shouldn't know about formats. Controllers shouldn't know what attributes your domain objects have. It should be trivial to serve the same data in multiple formats.
10
+
11
+ ### How do I use it?
12
+
13
+ Implement to_api on your data objects to return a hash with all needed attributes.
14
+
15
+ class Person
16
+ attr_accessor :first_name, :last_name, :secret
17
+
18
+ def to_api
19
+ {:first_name => @first_name, :last_name => @last_name}.to_api
20
+ end
21
+ end
22
+
23
+ class Group
24
+ attr_accessor :name, :description, :people
25
+
26
+ def to_api
27
+ {:name => @name, :description => @description, :people => @people}.to_api
28
+ end
29
+ end
30
+
31
+ Now just call your to\_api method. You'll get back a hash of arrays, hashes, strings, numbers, and nil. All of these are very easily converted to a format of your choice.
32
+
33
+ JSON.generate(group.to_api)
34
+
35
+ ### If I have to implement to\_api, what does the gem do for me?
36
+
37
+ The gem provides to\_api for common ruby classes, allowing simple conversion to json, xml, yaml, etc. Hash and Enumerable transform all their contents, allowing your data objects to simply call to\_api on a hash of relevant attributes.
38
+
39
+ Fixnum, String, DateTime, Symbol, and NilClass are also provided.
40
+
41
+ ### What about Ruby on Rails?
42
+
43
+ If ActiveRecord is present, ActiveRecord::Base#to\_api transforms and returns the attributes hash. It also supports an :includes option that mirrors the ActiveRecord finder usage.
44
+
45
+ person.to_api([{:groups => :members}, :interests])
46
+
47
+ ### What if I need support for other common classes?
48
+
49
+ For most classes, it only takes a couple specs and a few lines of code. Send a pull request; we'd love to take your additions.
data/lib/to_api.rb CHANGED
@@ -8,14 +8,14 @@ if Object.const_defined? :ActiveRecord
8
8
  includes.each do |i|
9
9
  if i.kind_of?(Hash)
10
10
  i.each do |k,v|
11
- include_hash[k] = v
11
+ include_hash[k.to_s] = v
12
12
  end
13
13
  else
14
- include_hash[i] = []
14
+ include_hash[i.to_s] = []
15
15
  end
16
16
  end
17
17
 
18
- include_hash.delete_if{|k,v| !valid_includes.include?(k)}
18
+ include_hash.delete_if{|k,v| !valid_includes.include?(k.to_s)}
19
19
 
20
20
  attributes.each do |k, v|
21
21
  attribute_includes = include_hash[k] || []
data/spec/to_api_spec.rb CHANGED
@@ -160,6 +160,10 @@ describe '#to_api' do
160
160
  @base.to_api("foopy_pantz")["foopy_pantz"].should == "pantz of foop"
161
161
  end
162
162
 
163
+ it "allows symbols" do
164
+ @base.to_api(:foopy_pantz)["foopy_pantz"].should == "pantz of foop"
165
+ end
166
+
163
167
  it "ignores non association includes" do
164
168
  @base.stub!(:yarg => "YYYYARRGG")
165
169
  @base.to_api("yarg")["yarg"].should be_nil
@@ -206,7 +210,13 @@ describe '#to_api' do
206
210
  @base.should_receive(:other_relation).and_return(@other_child)
207
211
  @base.to_api({"fake_child_records" => "foopy_pantz"}, "other_relation").should == {"fake_child_records" => [{}], "other_relation" => {"foo"=>"bar"}}
208
212
  end
209
-
213
+
214
+ it "takes array with subhash as symbols" do
215
+ @child.should_receive(:to_api).with(:foopy_pantz).and_return({})
216
+ @base.should_receive(:other_relation).and_return(@other_child)
217
+ @base.to_api({:fake_child_records => :foopy_pantz}, :other_relation).should == {"fake_child_records" => [{}], "other_relation" => {"foo"=>"bar"}}
218
+ end
219
+
210
220
  it "takes array with singles and subhashes" do
211
221
  @child.should_receive(:to_api).with("foopy_pantz").and_return({})
212
222
  @base.to_api("fake_child_records" => "foopy_pantz").should == {"fake_child_records" => [{}]}
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_api
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 4
10
- version: 1.0.4
9
+ - 5
10
+ version: 1.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Shawn Anderson
@@ -82,7 +82,7 @@ extra_rdoc_files: []
82
82
 
83
83
  files:
84
84
  - lib/to_api.rb
85
- - README
85
+ - README.md
86
86
  - spec/spec_helper.rb
87
87
  - spec/to_api_spec.rb
88
88
  has_rdoc: true
data/README DELETED
@@ -1 +0,0 @@
1
- TODO