api-resource 0.3.1 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a3e80d387ce02c31b964baf0af8c7b05727d567
4
- data.tar.gz: 04640fa12119b4360510ce960e1f086169d193a4
3
+ metadata.gz: d12c1f0d2bfa63f06021846af93ce43caa9b513c
4
+ data.tar.gz: 988a25712ce86f55beff42464da754d2d368648f
5
5
  SHA512:
6
- metadata.gz: ea767642dc77ae6826f2db89c8e3dfef7207064b7725fe230e2887f84cfd035f0d848e687d57761851d76c17ac4ae1c6871c8a050d4f16a2569ec9851e76ee54
7
- data.tar.gz: 526cf4bc2c812d20e138d0e01271937b6382ea417fb43ef23dc5c5bd81b7630ba0650682631441d5c0c0721911a4739812472bf651133bc3cefcf8a74443cfa3
6
+ metadata.gz: 6e3d9f2c3dc5cb3a22e15f434dbfb600b6c019a7d8ca3e0b3eaf24ea6245826b989908d2dee3923d1c7b81530f2ae55ecf9101caf856089c8ee31291e750e813
7
+ data.tar.gz: 9a80b9bf3a460a9a4a8370d428abf75534aac82bfddac9312abd487a95132a0f96851badb696b4e566e220c34c9266829f6fcc7418e735f01a0c6b6fee0ccc25
@@ -34,7 +34,7 @@ module ApiResource
34
34
  def self.find(id)
35
35
  result = client(:get, {}, resource_name, id)
36
36
  json = JSON.parse(result)
37
- self.new(json['data'])
37
+ self.new(json['data'] || json)
38
38
  end
39
39
 
40
40
  def self.all
@@ -60,15 +60,23 @@ module ApiResource
60
60
  send("#{key}=", value)
61
61
  else
62
62
  # check for embedded resource
63
- next unless value.is_a?(Hash) && value['data']
64
- child_class = self.class.load_class((value['meta'] && value['meta']['type']) || key)
63
+ if value.is_a?(Hash)
64
+ meta = value['meta']
65
+ data = value['data'] || value
66
+ elsif value.is_a?(Array)
67
+ meta = nil
68
+ data = value
69
+ else
70
+ next
71
+ end
72
+ puts meta
73
+ child_class = self.class.load_class((meta && meta['type']) || key)
65
74
  next unless child_class
66
- data = value['data']
67
75
  if data.is_a?(Hash)
68
76
  child_value = child_class.new(data)
69
77
  define_singleton_method(key) { child_value }
70
78
  elsif data.is_a?(Array)
71
- child_values = ResourceCollection.new(data, value['meta'], child_class)
79
+ child_values = ResourceCollection.new(data, meta, child_class)
72
80
  define_singleton_method(key) { child_values }
73
81
  end
74
82
  end
@@ -1,3 +1,3 @@
1
1
  module ApiResource
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.5'
3
3
  end
@@ -19,12 +19,18 @@ RSpec.describe ApiResource::Resource do
19
19
  def check_returned_array(type, expected, actual)
20
20
  expect(actual).to be_a Enumerable
21
21
  actual.each { |e| expect(e).to be_a type }
22
- expect(actual.map { |e| e.attributes }).to match (expected[:data])
22
+ expect(actual.map { |e| e.attributes }).to match ((expected.is_a?(Hash) && expected[:data]) || expected)
23
23
  end
24
24
 
25
25
  def check_returned_object(type, expected, actual, *except)
26
26
  expect(actual).to be_a(type)
27
- expect(actual.attributes).to match(expected[:data].except(*except))
27
+ expect(actual.attributes).to match((expected[:data] || expected).except(*except))
28
+ end
29
+
30
+ def req(verb, path, resource)
31
+ stub_request(verb, "#{BlogResource.base_url}#{path}").
32
+ with(headers: { 'Accept' => 'application/json' }).
33
+ to_return(status: 200, body: resource.to_json)
28
34
  end
29
35
 
30
36
  before do
@@ -33,25 +39,26 @@ RSpec.describe ApiResource::Resource do
33
39
  { id: 38, title: 'Bonjour!' }] }
34
40
  end
35
41
  context '#find' do
36
-
37
- it 'creates the resource when found' do
38
- expected_value = { data: { id: 1257, title: 'Hello' } }
42
+ def check_found_resource(expected_value)
43
+ expected_value = { id: 1257, title: 'Hello' }
39
44
  resource_id = 3
40
- stub_request(:get, "#{Blog.base_url}/blogs/#{resource_id}").
41
- with(headers: { 'Accept' => 'application/json' }).
42
- to_return(status: 200, body: expected_value.to_json)
45
+ req(:get, "/blogs/#{resource_id}", expected_value)
43
46
  result = Blog.find(resource_id)
44
47
  check_returned_object(Blog, expected_value, result)
45
48
  end
49
+
50
+ it 'creates resource with data rooted json' do
51
+ check_found_resource(data: { id: 1257, title: 'Hello' })
52
+ end
53
+ it 'creates resource with un-rooted json' do
54
+ check_found_resource(id: 1257, title: 'Hello')
55
+ end
46
56
  end
47
57
 
48
58
  context '#all' do
49
59
 
50
60
  it 'fetches all resources' do
51
- stub_request(:get, "#{Blog.base_url}/blogs").
52
- with(headers: { 'Accept' => 'application/json' }).
53
- to_return(status: 200, body: @expected_resources.to_json)
54
-
61
+ req(:get, '/blogs', @expected_resources)
55
62
  result = Blog.all
56
63
  check_returned_array(Blog, @expected_resources, result)
57
64
  end
@@ -60,10 +67,7 @@ RSpec.describe ApiResource::Resource do
60
67
  context '#where' do
61
68
 
62
69
  it 'fetches resourced filtered by query string with GET' do
63
- stub_request(:get, "#{Blog.base_url}/blogs?title=hello+world&tags%5B%5D=hello&tags%5B%5D=first&tags%5B%5D=greeting").
64
- with(headers: { 'Accept' => 'application/json' }).
65
- to_return(status: 200, body: @expected_resources.to_json)
66
-
70
+ req(:get, '/blogs?title=hello+world&tags%5B%5D=hello&tags%5B%5D=first&tags%5B%5D=greeting', @expected_resources)
67
71
  result = Blog.where(title: 'hello world', tags: ['hello', 'first', 'greeting'])
68
72
  check_returned_array(Blog, @expected_resources, result)
69
73
  end
@@ -83,97 +87,67 @@ RSpec.describe ApiResource::Resource do
83
87
 
84
88
  it 'fetches resource nested with parent resource' do
85
89
  resource_id = 3
86
-
87
- stub_request(:get, "#{Blog.base_url}/tags/#{resource_id}/blogs").
88
- with(headers: { 'Accept' => 'application/json' }).
89
- to_return(status: 200, body: @expected_resources.to_json)
90
-
90
+ req(:get, "/tags/#{resource_id}/blogs", @expected_resources)
91
91
  result = Blog.by_tag(resource_id)
92
92
  check_returned_array(Blog, @expected_resources, result)
93
93
  end
94
94
  end
95
95
 
96
96
  context '#child_ressources' do
97
+ before do
98
+ @blog = { data: { id: 1257, title: 'Tarte a la creme' } }
99
+ @tags_data = [{ id: 33, name: 'food' }, { id: 39, name: 'cuisine' }]
100
+ @category_data = { id: 33, name: 'Hobbies' }
101
+ end
97
102
  context 'has_many' do
98
- it 'defines a method and parses resources from inlined json array' do
99
- resource_values = { data: {
100
- id: 1257, title: 'Tarte a la creme',
101
- tags: { data: [{ id: 33, name: 'food' }, { id: 39, name: 'cuisine' }] }
102
- } }
103
+ def check_has_many(relation_name, relation_values)
104
+ resource_values = @blog.merge(data: { relation_name => relation_values })
103
105
  resource_id = 3
104
-
105
- stub_request(:get, "#{Blog.base_url}/blogs/#{resource_id}").
106
- with(headers: { 'Accept' => 'application/json' }).
107
- to_return(status: 200, body: resource_values.to_json)
108
-
106
+ req(:get, "/blogs/#{resource_id}", resource_values)
109
107
  blog = Blog.find(resource_id)
110
- check_returned_object(Blog, resource_values, blog, :tags)
111
- check_returned_array(Tag, resource_values[:data][:tags], blog.tags)
108
+ check_returned_object(Blog, resource_values, blog, relation_name)
109
+ expect(blog).to respond_to(relation_name)
110
+ check_returned_array(Tag, relation_values, blog.send(relation_name))
111
+ yield blog if block_given?
112
112
  end
113
113
 
114
- it 'override type and retrieve meta info' do
115
- resource_values = {
116
- data: {
117
- id: 1257,
118
- title: 'Tarte a la creme',
119
- my_sweet_tags: {
120
- data: [{ id: 33, name: 'food' }, { id: 39, name: 'cuisine' }],
121
- meta: { type: :tags, page: 3, per_page: 50, total: 102 }
122
- }
123
- }
124
- }
125
- resource_id = 3
126
-
127
- stub_request(:get, "#{Blog.base_url}/blogs/#{resource_id}").
128
- with(headers: { 'Accept' => 'application/json' }).
129
- to_return(status: 200, body: resource_values.to_json)
130
-
131
- blog = Blog.find(resource_id)
132
- check_returned_object(Blog, resource_values, blog, :my_sweet_tags)
133
- check_returned_array(Tag, resource_values[:data][:my_sweet_tags], blog.my_sweet_tags)
134
-
135
- expect(blog.my_sweet_tags.page).to eq(3)
136
- expect(blog.my_sweet_tags.per_page).to eq(50)
137
- expect(blog.my_sweet_tags.total).to eq(102)
114
+ it 'gets data rooted inlined array' do
115
+ check_has_many(:tags, data: @tags_data)
116
+ end
117
+ it 'gets un-rooted inlined array' do
118
+ check_has_many(:tags, @tags_data)
138
119
  end
139
- end
140
-
141
- it 'has_one association with inlined json' do
142
- resource_values = { data: {
143
- id: 1257, title: 'Tarte a la creme',
144
- category: { data: { id: 33, name: 'Hobbies' } }
145
- } }
146
- resource_id = 3
147
-
148
- stub_request(:get, "#{Blog.base_url}/blogs/#{resource_id}").
149
- with(headers: { 'Accept' => 'application/json' }).
150
- to_return(status: 200, body: resource_values.to_json)
151
120
 
152
- blog = Blog.find(resource_id)
153
- check_returned_object(Blog, resource_values, blog, :category)
154
- check_returned_object(Category, resource_values[:data][:category], blog.category)
121
+ it 'override type with meta.type and retrieve other meta.* info' do
122
+ check_has_many(:my_sweet_tags,
123
+ data: [{ id: 33, name: 'food' }, { id: 39, name: 'cuisine' }],
124
+ meta: { type: :tags, page: 3, per_page: 50, total: 102 }) do |blog|
125
+ expect(blog.my_sweet_tags.page).to eq(3)
126
+ expect(blog.my_sweet_tags.per_page).to eq(50)
127
+ expect(blog.my_sweet_tags.total).to eq(102)
128
+ end
129
+ end
155
130
  end
131
+ context 'has_one' do
132
+ def check_has_one(relation_name, relation_values)
133
+ resource_values = @blog.merge(data: { relation_name => relation_values })
134
+ resource_id = 3
135
+ req(:get, "/blogs/#{resource_id}", resource_values)
136
+ blog = Blog.find(resource_id)
137
+ check_returned_object(Blog, resource_values, blog, relation_name)
138
+ expect(blog).to respond_to(relation_name)
139
+ check_returned_object(Category, relation_values, blog.send(relation_name))
140
+ end
156
141
 
157
- it 'override type from meta' do
158
- resource_values = {
159
- data: {
160
- id: 1257,
161
- title: 'Tarte a la creme',
162
- main_category: {
163
- data: { id: 33, name: 'Hobbies' },
164
- meta: { type: :category }
165
- }
166
- }
167
- }
168
- resource_id = 3
169
-
170
- stub_request(:get, "#{Blog.base_url}/blogs/#{resource_id}").
171
- with(headers: { 'Accept' => 'application/json' }).
172
- to_return(status: 200, body: resource_values.to_json)
173
-
174
- blog = Blog.find(resource_id)
175
- check_returned_object(Blog, resource_values, blog, :main_category)
176
- check_returned_object(Category, resource_values[:data][:main_category], blog.main_category)
142
+ it 'gets data rooted inlined json' do
143
+ check_has_one(:category, data: @category_data)
144
+ end
145
+ it 'gets un-rooted inlined json' do
146
+ check_has_one(:category, @category_data)
147
+ end
148
+ it 'override type from meta' do
149
+ check_has_one(:main_category, data: { id: 33, name: 'Hobbies' }, meta: { type: :category })
150
+ end
177
151
  end
178
152
  end
179
153
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api-resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chaker Nakhli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-12 00:00:00.000000000 Z
11
+ date: 2015-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simple-hmac