api-client 1.7.0 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +1 -0
- data/examples/controllers/user_controller.rb +26 -1
- data/examples/models/admin.rb +11 -0
- data/examples/models/group.rb +3 -0
- data/examples/models/user.rb +3 -1
- data/lib/api-client/base.rb +3 -2
- data/lib/api-client/version.rb +1 -1
- data/spec/api-client/base_spec.rb +5 -5
- data/spec/spec_helper.rb +4 -0
- metadata +5 -3
data/README.md
CHANGED
@@ -62,6 +62,7 @@ It can handle associations. It will automatically instantiate an association for
|
|
62
62
|
```ruby
|
63
63
|
class Person < ApiClient::Base
|
64
64
|
self.associations = { :houses => "House", :cars => "Car" }
|
65
|
+
end
|
65
66
|
```
|
66
67
|
|
67
68
|
This code will create a setter and a getter for houses and cars and initialize the respective class inside the setter.
|
@@ -5,4 +5,29 @@ class UserController < ApplicationController
|
|
5
5
|
@users = User.get("api.example.com/users")
|
6
6
|
respond_with(@users)
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
|
+
def new
|
10
|
+
@user = User.new
|
11
|
+
respond_with(@user)
|
12
|
+
end
|
13
|
+
|
14
|
+
def create
|
15
|
+
@user = User.post("api.example.com/users", :user => params[:user])
|
16
|
+
respond_with(@user)
|
17
|
+
end
|
18
|
+
|
19
|
+
def edit
|
20
|
+
@user = User.get("api.example.com/users/#{params[:id]}")
|
21
|
+
respond_with(@user)
|
22
|
+
end
|
23
|
+
|
24
|
+
def update
|
25
|
+
@user = User.patch("api.example.com/users", { :id => params[:id], :user => params[:user]})
|
26
|
+
respond_with(@user)
|
27
|
+
end
|
28
|
+
|
29
|
+
def show
|
30
|
+
@user = User.get("api.example.com/users/#{params[:id]}")
|
31
|
+
respond_with(@user)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Admin < ApiClient::Base
|
2
|
+
self.remote_object = "user"
|
3
|
+
self.association = { :groups => "Group" }
|
4
|
+
|
5
|
+
# Any of this fields can be called to manage rails form.
|
6
|
+
attr_accessor :id, :email, :password, :password_confirmation
|
7
|
+
|
8
|
+
# Validations will work as well
|
9
|
+
validates :email, :presence => true, :uniqueness => true
|
10
|
+
validates :password, :confirmation => true
|
11
|
+
end
|
data/examples/models/user.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
class User < ApiClient::Base
|
2
|
+
self.association = { :groups => "Group" }
|
3
|
+
|
2
4
|
# Any of this fields can be called to manage rails form.
|
3
5
|
attr_accessor :id, :email, :password, :password_confirmation
|
4
6
|
|
5
7
|
# Validations will work as well
|
6
8
|
validates :email, :presence => true, :uniqueness => true
|
7
9
|
validates :password, :confirmation => true
|
8
|
-
end
|
10
|
+
end
|
data/lib/api-client/base.rb
CHANGED
@@ -53,10 +53,11 @@ module ApiClient
|
|
53
53
|
associations.each do |association, class_name|
|
54
54
|
class_eval <<-EVAL
|
55
55
|
def #{association.to_s}=(attributes = {})
|
56
|
-
|
56
|
+
return @#{class_name.constantize} = attributes.map { |attr| #{class_name.constantize}.new(attr) } if attributes.instance_of?(Array)
|
57
|
+
@#{class_name.constantize} = #{class_name.constantize}.new(attributes)
|
57
58
|
end
|
58
59
|
def #{association.to_s}
|
59
|
-
|
60
|
+
@#{class_name.constantize}
|
60
61
|
end
|
61
62
|
EVAL
|
62
63
|
end
|
data/lib/api-client/version.rb
CHANGED
@@ -69,15 +69,15 @@ describe ApiClient::Base do
|
|
69
69
|
|
70
70
|
describe "#associations" do
|
71
71
|
before :each do
|
72
|
-
@
|
72
|
+
@group = Group.new(:members => [{:a => "a"}], :owner => {:b => "b"})
|
73
73
|
end
|
74
74
|
|
75
|
-
it "should instantiate
|
76
|
-
@
|
75
|
+
it "should instantiate all members" do
|
76
|
+
@group.members.first.should be_an_instance_of(User)
|
77
77
|
end
|
78
78
|
|
79
|
-
it "should
|
80
|
-
@
|
79
|
+
it "should instantiate the owner" do
|
80
|
+
@group.owner.should be_an_instance_of(Admin)
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.7.
|
4
|
+
version: 1.7.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -125,6 +125,8 @@ files:
|
|
125
125
|
- api-client.gemspec
|
126
126
|
- examples/controllers/application_controller.rb
|
127
127
|
- examples/controllers/user_controller.rb
|
128
|
+
- examples/models/admin.rb
|
129
|
+
- examples/models/group.rb
|
128
130
|
- examples/models/user.rb
|
129
131
|
- gemfiles/Gemfile.net_http
|
130
132
|
- gemfiles/Gemfile.typhoeus
|
@@ -165,7 +167,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
165
167
|
version: '0'
|
166
168
|
segments:
|
167
169
|
- 0
|
168
|
-
hash:
|
170
|
+
hash: 4497437343432747897
|
169
171
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
172
|
none: false
|
171
173
|
requirements:
|
@@ -174,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
174
176
|
version: '0'
|
175
177
|
segments:
|
176
178
|
- 0
|
177
|
-
hash:
|
179
|
+
hash: 4497437343432747897
|
178
180
|
requirements: []
|
179
181
|
rubyforge_project:
|
180
182
|
rubygems_version: 1.8.24
|