restfolia 1.0.1 → 1.0.2
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/Changelog.md +12 -0
- data/ReadmeDeveloper.md +0 -1
- data/lib/restfolia/http/request.rb +1 -0
- data/lib/restfolia/resource.rb +1 -1
- data/lib/restfolia/resource_creator.rb +32 -6
- data/lib/restfolia/version.rb +1 -1
- data/test/restfolia/resource_creator_test.rb +32 -0
- data/test/restfolia/resource_test.rb +6 -0
- metadata +5 -4
data/Changelog.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# Restfolia Changelog
|
2
|
+
|
3
|
+
## 1.0.2 / edge
|
4
|
+
|
5
|
+
* \+ [Resource Creator supports Json - Array](https://github.com/rogerleite/restfolia/commit/363b00abf379a2849790ed4be86b9085bddbd2af)
|
6
|
+
* \+ [Support to multiple levels of hash on ResourceCreator](https://github.com/rogerleite/restfolia/commit/aa1e96a82f9b34a9415e46066874e35dfb7a1dbd)
|
7
|
+
* ! [Fix bug on not supporting id attribute on resource](https://github.com/rogerleite/restfolia/issues/5)
|
8
|
+
* \+ [Support http basic authentication](https://github.com/rogerleite/restfolia/commit/372aa40b9b2f4df35b7c30ed70534fc99e3b3233)
|
9
|
+
|
10
|
+
## 1.0.1 / 2012-05-28
|
11
|
+
|
12
|
+
* ! [Fix bug on links method](https://github.com/rogerleite/restfolia/issues/4)
|
data/ReadmeDeveloper.md
CHANGED
data/lib/restfolia/resource.rb
CHANGED
@@ -56,7 +56,7 @@ module Restfolia
|
|
56
56
|
#Add json keys as methods of Resource
|
57
57
|
#http://blog.jayfields.com/2008/02/ruby-replace-methodmissing-with-dynamic.html
|
58
58
|
@_json.each do |method, value|
|
59
|
-
next if self.respond_to?(method)
|
59
|
+
next if self.respond_to?(method) && (method != :id)
|
60
60
|
|
61
61
|
(class << self; self; end).class_eval do
|
62
62
|
define_method(method) do |*args|
|
@@ -38,24 +38,45 @@ module Restfolia
|
|
38
38
|
# objects and transforming in Resources. To create Resource,
|
39
39
|
# this method use #resource_class.new(json).
|
40
40
|
#
|
41
|
-
# json - Hash parsed from Response body.
|
41
|
+
# json - Hash or Array parsed from Response body.
|
42
|
+
#
|
43
|
+
# Returns if json is Hash, returns Resource from #resource_class if
|
44
|
+
# json is Array, returns an Array of Resource from #resource_class.
|
45
|
+
# Raises ArgumentError if json is not a Hash or Array.
|
46
|
+
def create(json)
|
47
|
+
if json.is_a?(Array)
|
48
|
+
json.inject([]) do |result, json_hash|
|
49
|
+
result << create_resource(json_hash)
|
50
|
+
end
|
51
|
+
elsif json.is_a?(Hash)
|
52
|
+
create_resource(json)
|
53
|
+
else
|
54
|
+
raise(ArgumentError, "JSON parameter have to be a Hash or Array object", caller)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
protected
|
59
|
+
|
60
|
+
# Internals: creates Resource looking recursively for JSON
|
61
|
+
# objects and transforming in Resources. To create Resource,
|
62
|
+
# this method use #resource_class.new(json).
|
63
|
+
#
|
64
|
+
# json_hash - Hash parsed from Response body.
|
42
65
|
#
|
43
66
|
# Returns Resource from #resource_class.
|
44
67
|
# Raises ArgumentError if json is not a Hash.
|
45
|
-
def
|
46
|
-
unless
|
68
|
+
def create_resource(json_hash)
|
69
|
+
unless json_hash.is_a?(Hash)
|
47
70
|
raise(ArgumentError, "JSON parameter have to be a Hash object", caller)
|
48
71
|
end
|
49
72
|
|
50
73
|
json_parsed = {}
|
51
|
-
|
74
|
+
json_hash.each do |attr, value|
|
52
75
|
json_parsed[attr] = look_for_resource(attr, value)
|
53
76
|
end
|
54
77
|
resource_class.new(json_parsed)
|
55
78
|
end
|
56
79
|
|
57
|
-
protected
|
58
|
-
|
59
80
|
# Internal: By default, returns Restfolia::Resource. You can use
|
60
81
|
# this method to override and returns a custom Resource.
|
61
82
|
#
|
@@ -108,6 +129,11 @@ module Restfolia
|
|
108
129
|
resources << look_for_resource(attr_name, array_obj)
|
109
130
|
end
|
110
131
|
elsif value.is_a?(Hash)
|
132
|
+
|
133
|
+
value.each do |attr, v|
|
134
|
+
value[attr] = look_for_resource(attr, v)
|
135
|
+
end
|
136
|
+
|
111
137
|
value = resource_class.new(value)
|
112
138
|
end
|
113
139
|
value
|
data/lib/restfolia/version.rb
CHANGED
@@ -59,6 +59,38 @@ describe Restfolia::ResourceCreator do
|
|
59
59
|
resource.attr_test[0][0].nested.must_equal("nested2")
|
60
60
|
end
|
61
61
|
|
62
|
+
it "transforms multiple levels of hash" do
|
63
|
+
resource = subject.create(:slug => "4fb15a526f32412f37000012",
|
64
|
+
:hash1 => {
|
65
|
+
:hash2 => {
|
66
|
+
:link => [
|
67
|
+
{:href => "http://exemplo.com",
|
68
|
+
:rel => "interno",
|
69
|
+
:type => "image/jpeg"
|
70
|
+
}
|
71
|
+
],
|
72
|
+
:origem => "sistema",
|
73
|
+
:hash3 => {
|
74
|
+
:test => "test"
|
75
|
+
}
|
76
|
+
}
|
77
|
+
})
|
78
|
+
|
79
|
+
resource.slug.must_equal("4fb15a526f32412f37000012")
|
80
|
+
resource.hash1.must_be_instance_of(OpenStruct)
|
81
|
+
resource.hash1.hash2.must_be_instance_of(OpenStruct)
|
82
|
+
resource.hash1.hash2.origem.must_equal("sistema")
|
83
|
+
resource.hash1.hash2.hash3.must_be_instance_of(OpenStruct)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "supports Array of hashes" do
|
87
|
+
resource = subject.create([{:slug => "slug1"}, {:slug => "slug2"}])
|
88
|
+
|
89
|
+
resource.must_be_instance_of(Array)
|
90
|
+
resource[0].slug.must_equal("slug1")
|
91
|
+
resource[1].slug.must_equal("slug2")
|
92
|
+
end
|
93
|
+
|
62
94
|
end
|
63
95
|
|
64
96
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restfolia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 2
|
10
|
+
version: 1.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Roger Leite
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-06-22 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: multi_json
|
@@ -104,6 +104,7 @@ files:
|
|
104
104
|
- .gitignore
|
105
105
|
- .travis.yml
|
106
106
|
- .yardopts
|
107
|
+
- Changelog.md
|
107
108
|
- Gemfile
|
108
109
|
- MIT-LICENSE
|
109
110
|
- Rakefile
|