ably 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Ably::Rest::Models::Message do
4
+ include Ably::Modules::Conversions
4
5
  context 'attributes' do
5
6
  let(:unique_value) { 'unique_value' }
6
7
 
@@ -15,7 +16,7 @@ describe Ably::Rest::Models::Message do
15
16
  end
16
17
 
17
18
  context '#sender_timestamp' do
18
- subject { Ably::Rest::Models::Message.new(timestamp: Time.now.to_i * 1000) }
19
+ subject { Ably::Rest::Models::Message.new(timestamp: as_since_epoch(Time.now)) }
19
20
  it 'retrieves attribute :timestamp' do
20
21
  expect(subject.sender_timestamp).to be_a(Time)
21
22
  expect(subject.sender_timestamp.to_i).to be_within(1).of(Time.now.to_i)
@@ -23,7 +24,7 @@ describe Ably::Rest::Models::Message do
23
24
  end
24
25
 
25
26
  context '#json' do
26
- let(:attributes) { { timestamp: Time.now.to_i * 1000 } }
27
+ let(:attributes) { { timestamp: as_since_epoch(Time.now) } }
27
28
  subject { Ably::Rest::Models::Message.new(attributes) }
28
29
 
29
30
  it 'provides access to #json' do
@@ -65,7 +66,7 @@ describe Ably::Rest::Models::Message do
65
66
  expect { subject.json[:client_id] = 'Joe' }.to raise_error RuntimeError, /can't modify frozen Hash/
66
67
  end
67
68
 
68
- it 'dups options' do
69
+ it 'clones options' do
69
70
  expect(subject.json[:client_id]).to eql('John')
70
71
  options[:client_id] = 'Joe'
71
72
  expect(subject.json[:client_id]).to eql('John')
@@ -0,0 +1,176 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ describe Ably::Rest::Models::PagedResource do
5
+ let(:paged_resource_class) { Ably::Rest::Models::PagedResource }
6
+ let(:headers) { Hash.new }
7
+ let(:client) do
8
+ double('client').tap do |client|
9
+ allow(client).to receive(:get).and_return(http_response)
10
+ end
11
+ end
12
+ let(:body) do
13
+ [
14
+ { id: 0 },
15
+ { id: 1 }
16
+ ]
17
+ end
18
+ let(:http_response) do
19
+ double('http_response', {
20
+ body: body,
21
+ headers: headers
22
+ })
23
+ end
24
+ let(:base_url) { 'http://rest.ably.io/channels/channel_name' }
25
+ let(:full_url) { "#{base_url}/whatever?param=exists" }
26
+ let(:paged_resource_options) { Hash.new }
27
+ let(:first_paged_request) { paged_resource_class.new(http_response, full_url, client, paged_resource_options) }
28
+ subject { first_paged_request }
29
+
30
+ it 'returns correct length from body' do
31
+ expect(subject.length).to eql(body.length)
32
+ end
33
+
34
+ it 'supports alias methods for length' do
35
+ expect(subject.count).to eql(subject.length)
36
+ expect(subject.size).to eql(subject.length)
37
+ end
38
+
39
+ it 'is Enumerable' do
40
+ expect(subject).to be_kind_of(Enumerable)
41
+ end
42
+
43
+ it 'is iterable' do
44
+ expect(subject.map { |d| d }).to eql(body)
45
+ end
46
+
47
+ it 'provides [] accessor method' do
48
+ expect(subject[0][:id]).to eql(body[0][:id])
49
+ expect(subject[1][:id]).to eql(body[1][:id])
50
+ expect(subject[2]).to be_nil
51
+ end
52
+
53
+ context 'with coercion' do
54
+ let(:paged_resource_options) { { coerce_into: 'OpenStruct' } }
55
+
56
+ it 'returns coerced objects' do
57
+ expect(subject.first).to be_a(OpenStruct)
58
+ expect(subject.first.id).to eql(body.first[:id])
59
+ end
60
+ end
61
+
62
+ context 'with non paged http response' do
63
+ it 'is the first page' do
64
+ expect(subject).to be_first_page
65
+ end
66
+
67
+ it 'is the last page' do
68
+ expect(subject).to be_last_page
69
+ end
70
+
71
+ it 'does not support pagination' do
72
+ expect(subject.supports_pagination?).to_not eql(true)
73
+ end
74
+
75
+ it 'raises an exception when accessing next page' do
76
+ expect { subject.next_page }.to raise_error Ably::Exceptions::InvalidPageError, /Paging header link next/
77
+ end
78
+
79
+ it 'raises an exception when accessing first page' do
80
+ expect { subject.first_page }.to raise_error Ably::Exceptions::InvalidPageError, /Paging header link first/
81
+ end
82
+ end
83
+
84
+ context 'with paged http response' do
85
+ let(:base_url) { 'http://rest.ably.io/channels/channel_name' }
86
+ let(:full_url) { "#{base_url}/messages" }
87
+ let(:headers) do
88
+ {
89
+ 'link' => [
90
+ '<./history?index=0>; rel="first"',
91
+ '<./history?index=0>; rel="current"',
92
+ '<./history?index=1>; rel="next"'
93
+ ].join(', ')
94
+ }
95
+ end
96
+
97
+ it 'is the first page' do
98
+ expect(subject).to be_first_page
99
+ end
100
+
101
+ it 'is not the last page' do
102
+ expect(subject).to_not be_last_page
103
+ end
104
+
105
+ it 'supports pagination' do
106
+ expect(subject.supports_pagination?).to eql(true)
107
+ end
108
+
109
+ context 'accessing next page' do
110
+ let(:next_body) do
111
+ [ { id: 2 } ]
112
+ end
113
+ let(:next_headers) do
114
+ {
115
+ 'link' => [
116
+ '<./history?index=0>; rel="first"',
117
+ '<./history?index=1>; rel="current"'
118
+ ].join(', ')
119
+ }
120
+ end
121
+ let(:next_http_response) do
122
+ double('http_response', {
123
+ body: next_body,
124
+ headers: next_headers
125
+ })
126
+ end
127
+ let(:subject) { first_paged_request.next_page }
128
+
129
+ before do
130
+ expect(client).to receive(:get).with("#{base_url}/history?index=1").and_return(next_http_response).once
131
+ end
132
+
133
+ it 'returns another PagedResource' do
134
+ expect(subject).to be_a(paged_resource_class)
135
+ end
136
+
137
+ it 'retrieves the next page of results' do
138
+ expect(subject.length).to eql(next_body.length)
139
+ expect(subject[0][:id]).to eql(next_body[0][:id])
140
+ end
141
+
142
+ it 'is not the first page' do
143
+ expect(subject).to_not be_first_page
144
+ end
145
+
146
+ it 'is the last page' do
147
+ expect(subject).to be_last_page
148
+ end
149
+
150
+ it 'raises an exception if trying to access the last page when it is the last page' do
151
+ expect(subject).to be_last_page
152
+ expect { subject.next_page }.to raise_error Ably::Exceptions::InvalidPageError, /There are no more pages/
153
+ end
154
+
155
+ context 'and then first page' do
156
+ before do
157
+ expect(client).to receive(:get).with("#{base_url}/history?index=0").and_return(http_response).once
158
+ end
159
+ subject { first_paged_request.next_page.first_page }
160
+
161
+ it 'returns a PagedResource' do
162
+ expect(subject).to be_a(paged_resource_class)
163
+ end
164
+
165
+ it 'retrieves the first page of results' do
166
+ expect(subject.length).to eql(body.length)
167
+ end
168
+
169
+ it 'is the first page' do
170
+ expect(subject).to be_first_page
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end
176
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ably
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lewis Marshall
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-25 00:00:00.000000000 Z
12
+ date: 2014-09-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
@@ -168,6 +168,7 @@ files:
168
168
  - lib/ably.rb
169
169
  - lib/ably/auth.rb
170
170
  - lib/ably/exceptions.rb
171
+ - lib/ably/models/idiomatic_ruby_wrapper.rb
171
172
  - lib/ably/modules/conversions.rb
172
173
  - lib/ably/modules/http_helpers.rb
173
174
  - lib/ably/realtime.rb
@@ -188,6 +189,7 @@ files:
188
189
  - lib/ably/rest/middleware/parse_json.rb
189
190
  - lib/ably/rest/models/message.rb
190
191
  - lib/ably/rest/models/paged_resource.rb
192
+ - lib/ably/rest/models/presence_message.rb
191
193
  - lib/ably/rest/presence.rb
192
194
  - lib/ably/token.rb
193
195
  - lib/ably/version.rb
@@ -205,11 +207,14 @@ files:
205
207
  - spec/support/model_helper.rb
206
208
  - spec/support/test_app.rb
207
209
  - spec/unit/auth.rb
210
+ - spec/unit/conversions.rb
211
+ - spec/unit/models/idiomatic_ruby_wrapper_spec.rb
208
212
  - spec/unit/realtime/error_info_spec.rb
209
213
  - spec/unit/realtime/message_spec.rb
210
214
  - spec/unit/realtime/protocol_message_spec.rb
211
215
  - spec/unit/realtime/realtime_spec.rb
212
216
  - spec/unit/rest/message_spec.rb
217
+ - spec/unit/rest/paged_resource_spec.rb
213
218
  - spec/unit/rest/rest_spec.rb
214
219
  - spec/unit/token_spec.rb
215
220
  homepage: http://github.com/ably/ably-ruby
@@ -251,11 +256,14 @@ test_files:
251
256
  - spec/support/model_helper.rb
252
257
  - spec/support/test_app.rb
253
258
  - spec/unit/auth.rb
259
+ - spec/unit/conversions.rb
260
+ - spec/unit/models/idiomatic_ruby_wrapper_spec.rb
254
261
  - spec/unit/realtime/error_info_spec.rb
255
262
  - spec/unit/realtime/message_spec.rb
256
263
  - spec/unit/realtime/protocol_message_spec.rb
257
264
  - spec/unit/realtime/realtime_spec.rb
258
265
  - spec/unit/rest/message_spec.rb
266
+ - spec/unit/rest/paged_resource_spec.rb
259
267
  - spec/unit/rest/rest_spec.rb
260
268
  - spec/unit/token_spec.rb
261
269
  has_rdoc: