api-client 1.7.1 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v1.8.0
4
+
5
+ * Methods to return attributes of the class added.
6
+
3
7
  ## v1.7.0
4
8
 
5
9
  * Support for Associations added.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # ApiClient [![Build Status](https://secure.travis-ci.org/plribeiro3000/api-client.png)](http://travis-ci.org/plribeiro3000/api-client)
1
+ # ApiClient [![Build Status](https://secure.travis-ci.org/plribeiro3000/api-client.png?branch=master)](http://travis-ci.org/plribeiro3000/api-client)
2
2
 
3
3
  Client to make Api calls easily. It supports ruby 1.8.7, 1.9.2, 1.9.3, jruby and ree out of the box.
4
4
 
@@ -65,7 +65,7 @@ class Person < ApiClient::Base
65
65
  end
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.
68
+ This code will create a setter and a getter for houses and cars and initialize the respective class.
69
69
 
70
70
  ## TODO
71
71
  * Add support for parallel requests
@@ -30,4 +30,9 @@ class UserController < ApplicationController
30
30
  @user = User.get("api.example.com/users/#{params[:id]}")
31
31
  respond_with(@user)
32
32
  end
33
+
34
+ def delete
35
+ @user = User.delete("api.example.com/users/#{params[:id]}")
36
+ respond_with(@user)
37
+ end
33
38
  end
@@ -53,11 +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
- 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)
56
+ return @#{association.to_s} = attributes.map { |attr| #{class_name.constantize}.new(attr) } if attributes.instance_of?(Array)
57
+ @#{association.to_s} = #{class_name.constantize}.new(attributes)
58
58
  end
59
59
  def #{association.to_s}
60
- @#{class_name.constantize}
60
+ @#{association.to_s}
61
61
  end
62
62
  EVAL
63
63
  end
@@ -67,6 +67,33 @@ module ApiClient
67
67
  alias_method :association, :associations
68
68
  end
69
69
 
70
+ # Overwrite #attr_acessor method to save instance_variable names.
71
+ #
72
+ # @param [Array] instance variables.
73
+ def self.attr_accessor(*vars)
74
+ @attributes ||= []
75
+ @attributes.concat vars
76
+ super
77
+ end
78
+
79
+ # Return an array with all instance variables setted through attr_acessor.
80
+ #
81
+ # @return [Array] instance variables.
82
+ def self.attributes
83
+ @attributes
84
+ end
85
+
86
+ # Return an array with all instance variables setted through attr_acessor and its currently values.
87
+ #
88
+ # @return [Hash] instance variables and its values.
89
+ def attributes
90
+ attributes = {}
91
+ self.class.instance_variable_get("@attributes").map { |attribute| attributes[attribute.to_sym] = self.send("#{attribute}") }
92
+ attributes
93
+ end
94
+
95
+ alias_method :to_hash, :attributes
96
+
70
97
  # Return the hash of errors if existent, otherwise instantiate a new ApiClient::Errors object with self.
71
98
  #
72
99
  # @return [ApiClient::Errors] the validation errors.
@@ -1,5 +1,5 @@
1
1
  # High Level Namespace of the library ApiClient.
2
2
  module ApiClient
3
3
  # Version of the library.
4
- VERSION = "1.7.1"
4
+ VERSION = "1.8.0"
5
5
  end
@@ -43,7 +43,31 @@ describe ApiClient::Base do
43
43
  end
44
44
  end
45
45
 
46
- describe
46
+ describe "#associations" do
47
+ before :each do
48
+ @group = Group.new(:members => [{:a => "a"}], :owner => {:b => "b"})
49
+ end
50
+
51
+ it "should instantiate all members" do
52
+ @group.members.first.should be_an_instance_of(User)
53
+ end
54
+
55
+ it "should instantiate the owner" do
56
+ @group.owner.should be_an_instance_of(Admin)
57
+ end
58
+ end
59
+
60
+ describe "#attributes on the class" do
61
+ it "should return an array of attributes" do
62
+ User.attributes.should == [:a, :b]
63
+ end
64
+ end
65
+
66
+ describe "#attributes on the object" do
67
+ it "should return a hash with the attributes and currently values" do
68
+ User.new.attributes.should == {:a => nil, :b => nil}
69
+ end
70
+ end
47
71
 
48
72
  describe "#errors" do
49
73
  context "when @errors is not nil" do
@@ -67,20 +91,6 @@ describe ApiClient::Base do
67
91
  end
68
92
  end
69
93
 
70
- describe "#associations" do
71
- before :each do
72
- @group = Group.new(:members => [{:a => "a"}], :owner => {:b => "b"})
73
- end
74
-
75
- it "should instantiate all members" do
76
- @group.members.first.should be_an_instance_of(User)
77
- end
78
-
79
- it "should instantiate the owner" do
80
- @group.owner.should be_an_instance_of(Admin)
81
- end
82
- end
83
-
84
94
  describe "#errors=" do
85
95
  before :each do
86
96
  @user = User.new(:errors => { "a" => "message", "b" => "message" })
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.1
4
+ version: 1.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-17 00:00:00.000000000 Z
12
+ date: 2012-09-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -167,7 +167,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
167
167
  version: '0'
168
168
  segments:
169
169
  - 0
170
- hash: 4497437343432747897
170
+ hash: -1461231548081400078
171
171
  required_rubygems_version: !ruby/object:Gem::Requirement
172
172
  none: false
173
173
  requirements:
@@ -176,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
176
  version: '0'
177
177
  segments:
178
178
  - 0
179
- hash: 4497437343432747897
179
+ hash: -1461231548081400078
180
180
  requirements: []
181
181
  rubyforge_project:
182
182
  rubygems_version: 1.8.24