jeckle 0.4.0.beta2 → 0.4.0.beta3

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: 5c79777c7365212885ef91b227f9d19cb13b8dba
4
- data.tar.gz: 912d10f04efd0a2cbeb3a5d6c9dd7264599de47a
3
+ metadata.gz: daea79214eac076d0f45217ccad97e299f0b8ed4
4
+ data.tar.gz: 815eb79a5058729ef025476a438cff1a5fc358c4
5
5
  SHA512:
6
- metadata.gz: bdc09ebc6b956cb4d26e408b06b30080da9f70e71f93c1b780d98bd9996f7af42792f1dfee690a9d8b1fb2c96e80009331d47a5e60597d6ef00a800055e7700c
7
- data.tar.gz: cca19437364450efa47b3d8a242fc6344a12e80559618d399e194d80c8da16143351f96883b7a688c623237f9f25e18d2a9d3f7694a93b80aa23a713eb990f5a
6
+ metadata.gz: d7299c4462181501c834c7b33cfde8d06beca7ad21611c87a2b173070e2e9f864b3f01977045ee914cb54efe71c8c913e6fdce6021669c37735303144688ef17
7
+ data.tar.gz: 83388f41b76f585a801c13d7f4742237448f6982c44a5210abc5c0e3f37ba1a6172d74aa65dc1eefb82f0a7e3002337536c89961c309f0a0b78134efd31aff44
@@ -0,0 +1,25 @@
1
+ require 'delegate'
2
+
3
+ module Jeckle
4
+ class CollectionResponse < SimpleDelegator
5
+ attr_reader :response, :collection
6
+
7
+ def initialize(collection, context: context, response: response)
8
+ @collection = Array(collection).map do |attributes|
9
+ context.new(attributes)
10
+ end
11
+
12
+ @response = response
13
+
14
+ super(@collection)
15
+ end
16
+
17
+ def body
18
+ @response.body
19
+ end
20
+
21
+ def headers
22
+ @response.headers
23
+ end
24
+ end
25
+ end
@@ -45,8 +45,8 @@ module Jeckle
45
45
  # Post.find(1) # => posts/1
46
46
  #
47
47
  def find(id)
48
- endpoint = "#{resource_name}/#{id}"
49
- response = run_request(endpoint).response.body
48
+ endpoint = "#{resource_name}/#{id}"
49
+ response = run_request(endpoint).response.body
50
50
  attributes = parse_response(response, member_root_name)
51
51
 
52
52
  new(attributes)
@@ -62,10 +62,11 @@ module Jeckle
62
62
  # Post.where({ status: 'published' }) # => posts/?status=published
63
63
  #
64
64
  def search(params = {})
65
- response = run_request(resource_name, params).response.body || []
66
- collection = parse_response(response, collection_root_name)
65
+ request = run_request(resource_name, params)
66
+ response = request.response
67
+ collection = parse_response(response.body, collection_root_name)
67
68
 
68
- Array(collection).map { |attributes| new(attributes) }
69
+ CollectionResponse.new(collection, context: self, response: response)
69
70
  end
70
71
  alias :where :search
71
72
 
@@ -102,4 +103,4 @@ module Jeckle
102
103
  end
103
104
  end
104
105
  end
105
- end
106
+ end
@@ -1,3 +1,3 @@
1
1
  module Jeckle
2
- VERSION = '0.4.0.beta2'
2
+ VERSION = '0.4.0.beta3'
3
3
  end
data/lib/jeckle.rb CHANGED
@@ -5,7 +5,10 @@ require 'virtus'
5
5
 
6
6
  require 'jeckle/version'
7
7
 
8
- %w(setup api model request http rest_actions resource errors).each do |file_name|
8
+ %w(
9
+ setup api model request http rest_actions
10
+ resource errors collection_response
11
+ ).each do |file_name|
9
12
  require "jeckle/#{file_name}"
10
13
  end
11
14
 
@@ -4,4 +4,8 @@ class FakeResource
4
4
  api :my_super_api
5
5
 
6
6
  attribute :id, Integer
7
+
8
+ def ==(other)
9
+ id == other.id
10
+ end
7
11
  end
@@ -2,7 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  RSpec.describe Jeckle::RESTActions do
4
4
  let(:api) { FakeResource.api_mapping[:default_api] }
5
- let(:fake_request) { OpenStruct.new response: OpenStruct.new(body: body) }
5
+ let(:response) { OpenStruct.new(body: body) }
6
+ let(:fake_request) { OpenStruct.new response: response }
6
7
 
7
8
  describe '.root' do
8
9
  context 'when collection is true' do
@@ -78,7 +79,7 @@ RSpec.describe Jeckle::RESTActions do
78
79
  Post.find(1001)
79
80
  end
80
81
 
81
- it 'returns an instance of resource' do
82
+ it 'returns an instance of resource keeping the response' do
82
83
  allow(Jeckle::Request).to receive(:run).and_return(fake_request)
83
84
 
84
85
  expect(post).to be_an_instance_of(Post)
@@ -102,11 +103,14 @@ RSpec.describe Jeckle::RESTActions do
102
103
 
103
104
  it 'returns an Array of resources' do
104
105
  allow(Jeckle::Request).to receive(:run).and_return(fake_request)
106
+ fake_resources = FakeResource.search(query)
105
107
 
106
- expect(FakeResource.search query).to match [
107
- an_instance_of(FakeResource),
108
- an_instance_of(FakeResource)
108
+ expect(fake_resources).to match [
109
+ FakeResource.new(id: 1001),
110
+ FakeResource.new(id: 1002)
109
111
  ]
112
+
113
+ expect(fake_resources.response).to be(response)
110
114
  end
111
115
  end
112
116
 
@@ -123,6 +127,7 @@ RSpec.describe Jeckle::RESTActions do
123
127
  Post.new(id: 1001),
124
128
  Post.new(id: 1002)
125
129
  ]
130
+ expect(results.response).to be(response)
126
131
  end
127
132
  end
128
133
 
@@ -131,8 +136,10 @@ RSpec.describe Jeckle::RESTActions do
131
136
 
132
137
  it 'returns an empty Array' do
133
138
  allow(Jeckle::Request).to receive(:run).and_return(fake_request)
139
+ results = FakeResource.search(query)
134
140
 
135
- expect(FakeResource.search query).to match []
141
+ expect(results).to match []
142
+ expect(results.response).to be(response)
136
143
  end
137
144
  end
138
145
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jeckle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.beta2
4
+ version: 0.4.0.beta3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas D'Stefano
@@ -143,6 +143,7 @@ files:
143
143
  - jeckle.gemspec
144
144
  - lib/jeckle.rb
145
145
  - lib/jeckle/api.rb
146
+ - lib/jeckle/collection_response.rb
146
147
  - lib/jeckle/errors.rb
147
148
  - lib/jeckle/http.rb
148
149
  - lib/jeckle/model.rb