ably 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ably.rb +2 -2
  3. data/lib/ably/auth.rb +39 -7
  4. data/lib/ably/modules/conversions.rb +58 -0
  5. data/lib/ably/{support.rb → modules/http_helpers.rb} +3 -3
  6. data/lib/ably/realtime.rb +5 -23
  7. data/lib/ably/realtime/channel.rb +62 -18
  8. data/lib/ably/realtime/client.rb +76 -22
  9. data/lib/ably/realtime/connection.rb +41 -14
  10. data/lib/ably/realtime/models/error_info.rb +38 -0
  11. data/lib/ably/realtime/models/message.rb +85 -0
  12. data/lib/ably/realtime/models/protocol_message.rb +149 -0
  13. data/lib/ably/realtime/models/shared.rb +17 -0
  14. data/lib/ably/rest.rb +16 -3
  15. data/lib/ably/rest/channel.rb +2 -2
  16. data/lib/ably/rest/client.rb +17 -20
  17. data/lib/ably/rest/models/message.rb +62 -0
  18. data/lib/ably/rest/models/paged_resource.rb +117 -0
  19. data/lib/ably/rest/presence.rb +4 -4
  20. data/lib/ably/token.rb +1 -1
  21. data/lib/ably/version.rb +1 -1
  22. data/spec/acceptance/realtime/channel_spec.rb +86 -0
  23. data/spec/acceptance/rest/auth_spec.rb +14 -5
  24. data/spec/acceptance/rest/channel_spec.rb +2 -2
  25. data/spec/spec_helper.rb +1 -0
  26. data/spec/support/event_machine_helper.rb +22 -0
  27. data/spec/support/model_helper.rb +67 -0
  28. data/spec/unit/realtime/error_info_spec.rb +10 -0
  29. data/spec/unit/realtime/message_spec.rb +115 -0
  30. data/spec/unit/realtime/protocol_message_spec.rb +102 -0
  31. data/spec/unit/realtime/realtime_spec.rb +20 -0
  32. data/spec/unit/rest/message_spec.rb +74 -0
  33. data/spec/unit/{rest_spec.rb → rest/rest_spec.rb} +14 -0
  34. metadata +28 -13
  35. data/lib/ably/message.rb +0 -70
  36. data/lib/ably/rest/paged_resource.rb +0 -117
  37. data/spec/acceptance/realtime_client_spec.rb +0 -12
  38. data/spec/unit/message_spec.rb +0 -73
  39. data/spec/unit/realtime_spec.rb +0 -9
@@ -1,117 +0,0 @@
1
- module Ably
2
- module Rest
3
- # Wraps any Ably HTTP response that supports paging and automatically provides methdos to iterated through
4
- # the array of resources using {#first}, {#next}, {#last?} and {#first?}
5
- #
6
- # Paging information is provided by Ably in the LINK HTTP headers
7
- class PagedResource
8
- include Enumerable
9
-
10
- # @param [Faraday::Response] http_response Initial HTTP response from an Ably request to a paged resource
11
- # @param [String] base_url Base URL for request that generated the http_response so that subsequent paged requests can be made
12
- # @param [Ably::Rest::Client] client {Ably::Client} used to make the request to Ably
13
- # @param [Hash] options Options for this paged resource
14
- # @option options [Symbol] :coerce_into symbol representing class that should be used to represent each item in the PagedResource
15
- #
16
- # @return [Ably::Rest::PagedResource]
17
- def initialize(http_response, base_url, client, coerce_into: nil)
18
- @http_response = http_response
19
- @client = client
20
- @base_url = "#{base_url.gsub(%r{/[^/]*$}, '')}/"
21
- @coerce_into = coerce_into
22
-
23
- @body = if coerce_into
24
- http_response.body.map do |item|
25
- Kernel.const_get(coerce_into).new(item)
26
- end
27
- else
28
- http_response.body
29
- end
30
- end
31
-
32
- # Retrieve the first page of results
33
- #
34
- # @return [Ably::Rest::PagedResource]
35
- def first
36
- PagedResource.new(@client.get(pagination_url('first')), @base_url, @client, coerce_into: @coerce_into)
37
- end
38
-
39
- # Retrieve the next page of results
40
- #
41
- # @return [Ably::Rest::PagedResource]
42
- def next
43
- PagedResource.new(@client.get(pagination_url('next')), @base_url, @client, coerce_into: @coerce_into)
44
- end
45
-
46
- # True if this is the last page in the paged resource set
47
- #
48
- # @return [Boolean]
49
- def last?
50
- !supports_pagination? ||
51
- pagination_header('next').nil?
52
- end
53
-
54
- # True if this is the first page in the paged resource set
55
- #
56
- # @return [Boolean]
57
- def first?
58
- !supports_pagination? ||
59
- pagination_header('first') == pagination_header('current')
60
- end
61
-
62
- # True if the HTTP response supports paging with the expected LINK HTTP headers
63
- #
64
- # @return [Boolean]
65
- def supports_pagination?
66
- !pagination_headers.empty?
67
- end
68
-
69
- # Standard Array accessor method
70
- def [](index)
71
- @body[index]
72
- end
73
-
74
- # Returns number of items within this page, not the total number of items in the entire paged resource set
75
- def length
76
- @body.length
77
- end
78
- alias_method :count, :length
79
- alias_method :size, :length
80
-
81
- # Method ensuring this {Ably::Rest::PagedResource} is {http://ruby-doc.org/core-2.1.3/Enumerable.html Enumerable}
82
- def each(&block)
83
- @body.each do |item|
84
- if block_given?
85
- block.call item
86
- else
87
- yield item
88
- end
89
- end
90
- end
91
-
92
- private
93
- def pagination_headers
94
- link_regex = %r{<(?<url>[^>]+)>; rel="(?<rel>[^"]+)"}
95
- @pagination_headers ||= @http_response.headers['link'].scan(link_regex).inject({}) do |hash, val_array|
96
- url, rel = val_array
97
- hash[rel] = url
98
- hash
99
- end
100
- end
101
-
102
- def pagination_header(id)
103
- pagination_headers[id]
104
- end
105
-
106
- def pagination_url(id)
107
- raise Ably::Exceptions::InvalidPageError, "Paging heading link #{id} does not exist" unless pagination_header(id)
108
-
109
- if pagination_header(id).match(%r{^\./})
110
- "#{@base_url}#{pagination_header(id)[2..-1]}"
111
- else
112
- pagination_header[id]
113
- end
114
- end
115
- end
116
- end
117
- end
@@ -1,12 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "Using the Realtime client" do
4
- describe "initializing the client" do
5
- it "should disallow an invalid key" do
6
- expect { Ably::Realtime::Client.new({}) }.to raise_error(ArgumentError, /api_key is missing/)
7
- expect { Ably::Realtime::Client.new(api_key: 'invalid') }.to raise_error(ArgumentError, /api_key is invalid/)
8
- expect { Ably::Realtime::Client.new(api_key: 'invalid:asdad') }.to raise_error(ArgumentError, /api_key is invalid/)
9
- expect { Ably::Realtime::Client.new(api_key: 'appid.keyuid:keysecret') }.to_not raise_error
10
- end
11
- end
12
- end
@@ -1,73 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Ably::Message do
4
- context 'attributes' do
5
- let(:unique_value) { 'unique_value' }
6
-
7
- %w(name data client_id timestamp channel_serial).each do |attribute|
8
- context "##{attribute}" do
9
- subject { Ably::Message.new({ attribute.to_sym => unique_value }) }
10
-
11
- it "retrieves attribute :#{attribute}" do
12
- expect(subject.public_send(attribute)).to eql(unique_value)
13
- end
14
- end
15
- end
16
-
17
- context '#timestamp_at' do
18
- subject { Ably::Message.new(timestamp: Time.now.to_i * 1000) }
19
- it 'retrieves attribute :key' do
20
- expect(subject.timestamp_at.to_i).to be_within(1).of(Time.now.to_i)
21
- end
22
- end
23
-
24
- context '#raw_message' do
25
- let(:attributes) { { timestamp: Time.now.to_i * 1000 } }
26
- subject { Ably::Message.new(attributes) }
27
-
28
- it 'provides access to #raw_message' do
29
- expect(subject.raw_message).to eql(attributes)
30
- end
31
- end
32
-
33
- context '#[]' do
34
- subject { Ably::Message.new(unusual: 'attribute') }
35
-
36
- it 'provides accessor method to #raw_message' do
37
- expect(subject[:unusual]).to eql('attribute')
38
- end
39
- end
40
- end
41
-
42
- context '==' do
43
- let(:attributes) { { client_id: 'unique' } }
44
-
45
- it 'is true when attributes are the same' do
46
- new_message = -> { Ably::Message.new(attributes) }
47
- expect(new_message[]).to eq(new_message[])
48
- end
49
-
50
- it 'is false when attributes are not the same' do
51
- expect(Ably::Message.new(client_id: 1)).to_not eq(Ably::Message.new(client_id: 2))
52
- end
53
-
54
- it 'is false when class type differs' do
55
- expect(Ably::Message.new(client_id: 1)).to_not eq(nil)
56
- end
57
- end
58
-
59
- context 'is immutable' do
60
- let(:options) { { client_id: 'John' } }
61
- subject { Ably::Message.new(options) }
62
-
63
- it 'prevents changes' do
64
- expect { subject.raw_message[:client_id] = 'Joe' }.to raise_error RuntimeError, /can't modify frozen Hash/
65
- end
66
-
67
- it 'dups options' do
68
- expect(subject.raw_message[:client_id]).to eql('John')
69
- options[:client_id] = 'Joe'
70
- expect(subject.raw_message[:client_id]).to eql('John')
71
- end
72
- end
73
- end
@@ -1,9 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Ably::Realtime do
4
- let(:options) { { api_key: 'app.key:secret' } }
5
-
6
- specify 'constructor returns an Ably::Realtime::Client' do
7
- expect(Ably::Realtime.new(options)).to be_instance_of(Ably::Realtime::Client)
8
- end
9
- end