ably-pubsub-core 0.0.1.pre → 2.0.0

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.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ably/auth.rb +5 -5
  3. data/lib/ably/exceptions.rb +5 -0
  4. data/lib/ably/internal.rb +115 -0
  5. data/lib/ably/logger.rb +3 -3
  6. data/lib/ably/models/channel_details.rb +1 -1
  7. data/lib/ably/models/channel_metrics.rb +1 -1
  8. data/lib/ably/models/channel_occupancy.rb +1 -1
  9. data/lib/ably/models/channel_state_change.rb +7 -7
  10. data/lib/ably/models/channel_status.rb +1 -1
  11. data/lib/ably/models/cipher_params.rb +1 -1
  12. data/lib/ably/models/connection_details.rb +2 -2
  13. data/lib/ably/models/connection_state_change.rb +6 -6
  14. data/lib/ably/models/http_paginated_response.rb +3 -3
  15. data/lib/ably/models/message.rb +69 -2
  16. data/lib/ably/models/message_operation.rb +53 -0
  17. data/lib/ably/models/paginated_result.rb +2 -2
  18. data/lib/ably/models/presence_message.rb +1 -1
  19. data/lib/ably/models/protocol_message.rb +14 -1
  20. data/lib/ably/models/publish_result.rb +32 -0
  21. data/lib/ably/models/stats.rb +41 -84
  22. data/lib/ably/models/update_delete_result.rb +32 -0
  23. data/lib/ably/modules/ably.rb +13 -12
  24. data/lib/ably/modules/channels_collection.rb +1 -1
  25. data/lib/ably/modules/http_helpers.rb +11 -11
  26. data/lib/ably/{rest → pubsub/http}/channel/push_channel.rb +3 -3
  27. data/lib/ably/pubsub/http/channel.rb +284 -0
  28. data/lib/ably/pubsub/http/channels.rb +49 -0
  29. data/lib/ably/pubsub/http/client.rb +738 -0
  30. data/lib/ably/pubsub/http/middleware/encoder.rb +51 -0
  31. data/lib/ably/pubsub/http/middleware/exceptions.rb +55 -0
  32. data/lib/ably/pubsub/http/middleware/external_exceptions.rb +26 -0
  33. data/lib/ably/pubsub/http/middleware/fail_if_unsupported_mime_type.rb +24 -0
  34. data/lib/ably/pubsub/http/middleware/logger.rb +60 -0
  35. data/lib/ably/pubsub/http/middleware/parse_json.rb +29 -0
  36. data/lib/ably/pubsub/http/middleware/parse_message_pack.rb +45 -0
  37. data/lib/ably/pubsub/http/presence.rb +117 -0
  38. data/lib/ably/{rest → pubsub/http}/push/admin.rb +5 -5
  39. data/lib/ably/{rest → pubsub/http}/push/channel_subscriptions.rb +1 -1
  40. data/lib/ably/{rest → pubsub/http}/push/device_registrations.rb +1 -1
  41. data/lib/ably/pubsub/http/push.rb +27 -0
  42. data/lib/ably/pubsub/http.rb +38 -0
  43. data/lib/ably/pubsub/realtime/auth.rb +285 -0
  44. data/lib/ably/{realtime → pubsub/realtime}/channel/channel_manager.rb +2 -2
  45. data/lib/ably/{realtime → pubsub/realtime}/channel/channel_properties.rb +5 -5
  46. data/lib/ably/{realtime → pubsub/realtime}/channel/channel_state_machine.rb +2 -2
  47. data/lib/ably/{realtime → pubsub/realtime}/channel/publisher.rb +9 -4
  48. data/lib/ably/{realtime → pubsub/realtime}/channel/push_channel.rb +2 -2
  49. data/lib/ably/pubsub/realtime/channel.rb +545 -0
  50. data/lib/ably/pubsub/realtime/channels.rb +92 -0
  51. data/lib/ably/{realtime → pubsub/realtime}/client/incoming_message_dispatcher.rb +43 -8
  52. data/lib/ably/{realtime → pubsub/realtime}/client/outgoing_message_dispatcher.rb +2 -2
  53. data/lib/ably/pubsub/realtime/client.rb +372 -0
  54. data/lib/ably/{realtime → pubsub/realtime}/connection/connection_manager.rb +6 -6
  55. data/lib/ably/{realtime → pubsub/realtime}/connection/connection_state_machine.rb +2 -2
  56. data/lib/ably/{realtime → pubsub/realtime}/connection/websocket_transport.rb +1 -1
  57. data/lib/ably/pubsub/realtime/connection.rb +724 -0
  58. data/lib/ably/{realtime → pubsub/realtime}/models/nil_channel.rb +3 -3
  59. data/lib/ably/{realtime → pubsub/realtime}/presence/members_map.rb +2 -2
  60. data/lib/ably/{realtime → pubsub/realtime}/presence/presence_manager.rb +3 -3
  61. data/lib/ably/{realtime → pubsub/realtime}/presence/presence_state_machine.rb +2 -2
  62. data/lib/ably/{realtime → pubsub/realtime}/presence.rb +23 -23
  63. data/lib/ably/{realtime → pubsub/realtime}/push/admin.rb +8 -8
  64. data/lib/ably/{realtime → pubsub/realtime}/push/channel_subscriptions.rb +6 -6
  65. data/lib/ably/{realtime → pubsub/realtime}/push/device_registrations.rb +6 -6
  66. data/lib/ably/pubsub/realtime/push.rb +27 -0
  67. data/lib/ably/pubsub/realtime/recovery_key_context.rb +38 -0
  68. data/lib/ably/pubsub/realtime.rb +58 -0
  69. data/lib/ably/pubsub.rb +11 -0
  70. data/lib/ably/version.rb +2 -2
  71. data/lib/ably.rb +6 -2
  72. metadata +50 -46
  73. data/lib/ably/models/stats_types.rb +0 -107
  74. data/lib/ably/realtime/auth.rb +0 -283
  75. data/lib/ably/realtime/channel.rb +0 -440
  76. data/lib/ably/realtime/channels.rb +0 -71
  77. data/lib/ably/realtime/client.rb +0 -363
  78. data/lib/ably/realtime/connection.rb +0 -738
  79. data/lib/ably/realtime/push.rb +0 -25
  80. data/lib/ably/realtime/recovery_key_context.rb +0 -36
  81. data/lib/ably/realtime.rb +0 -60
  82. data/lib/ably/rest/channel.rb +0 -194
  83. data/lib/ably/rest/channels.rb +0 -47
  84. data/lib/ably/rest/client.rb +0 -727
  85. data/lib/ably/rest/middleware/encoder.rb +0 -49
  86. data/lib/ably/rest/middleware/exceptions.rb +0 -53
  87. data/lib/ably/rest/middleware/external_exceptions.rb +0 -24
  88. data/lib/ably/rest/middleware/fail_if_unsupported_mime_type.rb +0 -22
  89. data/lib/ably/rest/middleware/logger.rb +0 -58
  90. data/lib/ably/rest/middleware/parse_json.rb +0 -27
  91. data/lib/ably/rest/middleware/parse_message_pack.rb +0 -43
  92. data/lib/ably/rest/presence.rb +0 -113
  93. data/lib/ably/rest/push.rb +0 -25
  94. data/lib/ably/rest.rb +0 -40
@@ -1,49 +0,0 @@
1
- require 'faraday'
2
- require 'json'
3
-
4
- module Ably
5
- module Rest
6
- module Middleware
7
- # Encode the body of the message according to the mime type
8
- class Encoder < Faraday::Middleware
9
- CONTENT_TYPE = 'Content-Type'.freeze unless defined? CONTENT_TYPE
10
-
11
- def call(env)
12
- encode env if env.body
13
- @app.call env
14
- end
15
-
16
- private
17
- def encode(env)
18
- env.body = case request_type(env)
19
- when 'application/x-msgpack'
20
- to_msgpack(env.body)
21
- when 'application/json', '', nil
22
- env.request_headers[CONTENT_TYPE] = 'application/json'
23
- to_json(env.body)
24
- else
25
- env.body
26
- end
27
- end
28
-
29
- def to_msgpack(body)
30
- body.to_msgpack
31
- end
32
-
33
- def to_json(body)
34
- if body.kind_of?(String)
35
- body
36
- else
37
- body.to_json
38
- end
39
- end
40
-
41
- def request_type(env)
42
- type = env.request_headers[CONTENT_TYPE].to_s
43
- type = type.split(';', 2).first if type.index(';')
44
- type
45
- end
46
- end
47
- end
48
- end
49
- end
@@ -1,53 +0,0 @@
1
- require 'faraday'
2
- require 'json'
3
-
4
- module Ably
5
- module Rest
6
- module Middleware
7
- # HTTP exceptions raised by Ably due to an error status code
8
- # Ably returns JSON/Msgpack error codes and messages so include this if possible in the exception messages
9
- class Exceptions < Faraday::Middleware
10
- def on_complete(env)
11
- if env.status >= 400
12
- error_status_code = env.status
13
- error_code = nil
14
-
15
- if env.body.kind_of?(Hash)
16
- error = env.body.fetch('error', {})
17
- error_status_code = error['statusCode'].to_i if error['statusCode']
18
- error_code = error['code'].to_i if error['code']
19
-
20
- if error
21
- message = "#{error['message']} (status: #{error_status_code}, code: #{error_code})"
22
- else
23
- message = env.body
24
- end
25
- else
26
- message = env.body
27
- end
28
-
29
- message = 'Unknown server error' if message.to_s.strip == ''
30
- request_id = env.request.context[:request_id] if env.request.context
31
- exception_args = [message, error_status_code, error_code, nil, { request_id: request_id }]
32
-
33
- if env.status >= 500
34
- raise Ably::Exceptions::ServerError.new(*exception_args)
35
- elsif env.status == 401
36
- if Ably::Exceptions::TOKEN_EXPIRED_CODE.include?(error_code)
37
- raise Ably::Exceptions::TokenExpired.new(*exception_args)
38
- else
39
- raise Ably::Exceptions::UnauthorizedRequest.new(*exception_args)
40
- end
41
- elsif env.status == 403
42
- raise Ably::Exceptions::ForbiddenRequest.new(*exception_args)
43
- elsif env.status == 404
44
- raise Ably::Exceptions::ResourceMissing.new(*exception_args)
45
- else
46
- raise Ably::Exceptions::InvalidRequest.new(*exception_args)
47
- end
48
- end
49
- end
50
- end
51
- end
52
- end
53
- end
@@ -1,24 +0,0 @@
1
- require 'faraday'
2
-
3
- module Ably
4
- module Rest
5
- module Middleware
6
- # HTTP exceptions raised due to a status code error on a 3rd party site
7
- # Used by auth calls
8
- class ExternalExceptions < Faraday::Middleware
9
- def on_complete(env)
10
- if env.status >= 400
11
- error_status_code = env.status
12
- message = "Error #{error_status_code}: #{(env.body || '')[0...200]}"
13
-
14
- if error_status_code >= 500
15
- raise Ably::Exceptions::ServerError, message
16
- else
17
- raise Ably::Exceptions::InvalidRequest, message
18
- end
19
- end
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,22 +0,0 @@
1
- require 'faraday'
2
- require 'json'
3
-
4
- module Ably
5
- module Rest
6
- module Middleware
7
- class FailIfUnsupportedMimeType < Faraday::Middleware
8
- def on_complete(env)
9
- unless env.response_headers['Ably-Middleware-Parsed'] == true
10
- # Ignore empty body with success status code for no body response
11
- return if env.body.to_s.empty? && env.status == 204
12
-
13
- unless (500..599).include?(env.status)
14
- raise Ably::Exceptions::InvalidResponseBody,
15
- "Content Type #{env.response_headers['Content-Type']} is not supported by this client library"
16
- end
17
- end
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,58 +0,0 @@
1
- require 'faraday'
2
-
3
- module Ably
4
- module Rest
5
- module Middleware
6
- class Logger < Faraday::Middleware
7
- extend Forwardable
8
-
9
- def initialize(app, logger = nil)
10
- super(app)
11
- @logger = logger || begin
12
- require 'logger'
13
- ::Logger.new(STDOUT)
14
- end
15
- end
16
-
17
- def_delegators :@logger, :debug, :info, :warn, :error, :fatal
18
-
19
- def call(env)
20
- debug { "=> URL: #{env.method} #{env.url}, Headers: #{dump_headers env.request_headers}" }
21
- debug { "=> Body: #{body_for(env)}" }
22
- super
23
- end
24
-
25
- def on_complete(env)
26
- debug "<= Status: #{env.status}, Headers: #{dump_headers env.response_headers}"
27
- debug "<= Body: #{body_for(env)}"
28
- end
29
-
30
- private
31
- def dump_headers(headers)
32
- headers.map { |k, v| "#{k}: #{v.inspect}" }.join(", ")
33
- end
34
-
35
- def body_for(env)
36
- return '' if !env.body || env.body.empty?
37
-
38
- if env.request_headers['Content-Type'] == 'application/x-msgpack'
39
- MessagePack.unpack(env.body)
40
- else
41
- env.body
42
- end
43
-
44
- rescue StandardError
45
- readable_body(env.body)
46
- end
47
-
48
- def readable_body(body)
49
- if body.respond_to?(:encoding) && body.encoding == Encoding::ASCII_8BIT
50
- body.unpack('H*')
51
- else
52
- body
53
- end
54
- end
55
- end
56
- end
57
- end
58
- end
@@ -1,27 +0,0 @@
1
- require 'faraday'
2
- require 'json'
3
-
4
- module Ably
5
- module Rest
6
- module Middleware
7
- class ParseJson < Faraday::Middleware
8
- def on_complete(env)
9
- if env.response_headers['Content-Type'] == 'application/json'
10
- env.body = parse(env.body) unless env.response_headers['Ably-Middleware-Parsed'] == true
11
- env.response_headers['Ably-Middleware-Parsed'] = true
12
- end
13
- end
14
-
15
- def parse(body)
16
- if body.length > 0
17
- JSON.parse(body)
18
- else
19
- body
20
- end
21
- rescue JSON::ParserError => e
22
- raise Ably::Exceptions::InvalidResponseBody, "Expected JSON response: #{e.message}"
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,43 +0,0 @@
1
- require 'faraday'
2
- require 'msgpack'
3
-
4
- module Ably
5
- module Rest
6
- module Middleware
7
- class ParseMessagePack < Faraday::Middleware
8
- def on_complete(env)
9
- if env.response_headers['Content-Type'] == 'application/x-msgpack'
10
- env.body = parse(env.body) unless env.response_headers['Ably-Middleware-Parsed'] == true
11
- env.response_headers['Ably-Middleware-Parsed'] = true
12
- end
13
- rescue Ably::Exceptions::InvalidResponseBody => e
14
- debug_info = {
15
- method: env.method,
16
- url: env.url,
17
- base64_body: base64_body(env.body),
18
- response_headers: env.response_headers
19
- }
20
- raise Ably::Exceptions::InvalidResponseBody, "#{e.message}\nRequest env: #{debug_info}"
21
- end
22
-
23
- def parse(body)
24
- if body.length > 0
25
- MessagePack.unpack(body)
26
- else
27
- body
28
- end
29
- rescue MessagePack::UnknownExtTypeError => e
30
- raise Ably::Exceptions::InvalidResponseBody, "MessagePack::UnknownExtTypeError body could not be decoded: #{e.message}. Got Base64:\n#{base64_body(body)}"
31
- rescue MessagePack::MalformedFormatError => e
32
- raise Ably::Exceptions::InvalidResponseBody, "MessagePack::MalformedFormatError body could not be decoded: #{e.message}. Got Base64:\n#{base64_body(body)}"
33
- end
34
-
35
- def base64_body(body)
36
- Base64.encode64(body)
37
- rescue => err
38
- "[#{err.message}! Could not base64 encode body: '#{body}']"
39
- end
40
- end
41
- end
42
- end
43
- end
@@ -1,113 +0,0 @@
1
- module Ably
2
- module Rest
3
- # Enables the retrieval of the current and historic presence set for a channel.
4
- #
5
- class Presence
6
- include Ably::Modules::Conversions
7
-
8
- # {Ably::Rest::Client} for this Presence object
9
- #
10
- # @return {Ably::Rest::Client}
11
- #
12
- # @private
13
- attr_reader :client
14
-
15
- # {Ably::Rest::Channel} this Presence object is associated with
16
- #
17
- # @return [Ably::Rest::Channel]
18
- #
19
- attr_reader :channel
20
-
21
- # Initialize a new Presence object
22
- #
23
- # @param client [Ably::Rest::Client]
24
- # @param channel [Channel] The channel object
25
- #
26
- def initialize(client, channel)
27
- @client = client
28
- @channel = channel
29
- end
30
-
31
- # Retrieves the current members present on the channel and the metadata for each member, such as their
32
- # {Ably::Models::PresenceMessage::ACTION} and ID. Returns a {Ably::Models::PaginatedResult} object,
33
- # containing an array of {Ably::Models::PresenceMessage} objects.
34
- #
35
- # @spec RSPa, RSP3a, RSP3a2, RSP3a3
36
- #
37
- # @param [Hash] options the options for the set of members present
38
- # @option options [Integer] :limit An upper limit on the number of messages returned. The default is 100, and the maximum is 1000. (RSP3a)
39
- # @option options [String] :client_id Filters the list of returned presence members by a specific client using its ID. (RSP3a2)
40
- # @option options [String] :connection_id Filters the list of returned presence members by a specific connection using its ID. (RSP3a3)
41
- #
42
- # @return [Ably::Models::PaginatedResult<Ably::Models::PresenceMessage>] A {Ably::Models::PaginatedResult} object containing an array of {Ably::Models::PresenceMessage} objects.
43
- #
44
- def get(options = {})
45
- options = options = {
46
- :limit => 100
47
- }.merge(options)
48
-
49
- paginated_options = {
50
- coerce_into: 'Ably::Models::PresenceMessage',
51
- async_blocking_operations: options.delete(:async_blocking_operations),
52
- }
53
-
54
- response = client.get(base_path, options)
55
-
56
- Ably::Models::PaginatedResult.new(response, base_path, client, paginated_options) do |presence_message|
57
- presence_message.tap do |message|
58
- decode_message message
59
- end
60
- end
61
- end
62
-
63
- # Retrieves a {Ably::Models::PaginatedResult} object, containing an array of historical {Ably::Models::PresenceMessage}
64
- # objects for the channel. If the channel is configured to persist messages, then presence messages can be retrieved
65
- # from history for up to 72 hours in the past. If not, presence messages can only be retrieved from history for up to two minutes in the past.
66
- #
67
- # @spec RSP4a
68
- #
69
- # @param [Hash] options the options for the message history request
70
- # @option options [Integer,Time] :start The time from which messages are retrieved, specified as milliseconds since the Unix epoch. (RSP4b1)
71
- # @option options [Integer,Time] :end The time until messages are retrieved, specified as milliseconds since the Unix epoch. (RSP4b1)
72
- # @option options [Symbol] :direction The order for which messages are returned in. Valid values are backwards which orders messages from most recent to oldest, or forwards which orders messages from oldest to most recent. The default is backwards. (RSP4b2)
73
- # @option options [Integer] :limit An upper limit on the number of messages returned. The default is 100, and the maximum is 1000. (RSP4b3)
74
- #
75
- # @return [Ably::Models::PaginatedResult<Ably::Models::PresenceMessage>] A {Ably::Models::PaginatedResult} object containing an array of {Ably::Models::PresenceMessage} objects.
76
- #
77
- def history(options = {})
78
- url = "#{base_path}/history"
79
- options = options = {
80
- :direction => :backwards,
81
- :limit => 100
82
- }.merge(options)
83
-
84
- [:start, :end].each { |option| options[option] = as_since_epoch(options[option]) if options.has_key?(option) }
85
- raise ArgumentError, ":end must be equal to or after :start" if options[:start] && options[:end] && (options[:start] > options[:end])
86
-
87
- paginated_options = {
88
- coerce_into: 'Ably::Models::PresenceMessage',
89
- async_blocking_operations: options.delete(:async_blocking_operations),
90
- }
91
-
92
- response = client.get(url, options)
93
-
94
- Ably::Models::PaginatedResult.new(response, url, client, paginated_options) do |presence_message|
95
- presence_message.tap do |message|
96
- decode_message message
97
- end
98
- end
99
- end
100
-
101
- private
102
- def base_path
103
- "/channels/#{URI.encode_www_form_component(channel.name)}/presence"
104
- end
105
-
106
- def decode_message(presence_message)
107
- presence_message.decode client.encoders, channel.options
108
- rescue Ably::Exceptions::CipherError, Ably::Exceptions::EncoderError => e
109
- client.logger.error { "Decoding Error on presence channel '#{channel.name}', presence message client_id '#{presence_message.client_id}'. #{e.class.name}: #{e.message}" }
110
- end
111
- end
112
- end
113
- end
@@ -1,25 +0,0 @@
1
- require 'ably/rest/push/admin'
2
-
3
- module Ably
4
- module Rest
5
- # Class providing push notification functionality
6
- class Push
7
- include Ably::Modules::Conversions
8
-
9
- # @private
10
- attr_reader :client
11
-
12
- def initialize(client)
13
- @client = client
14
- end
15
-
16
- # Admin features for push notifications like managing devices and channel subscriptions
17
- #
18
- # @return [Ably::Rest::Push::Admin]
19
- #
20
- def admin
21
- @admin ||= Admin.new(self)
22
- end
23
- end
24
- end
25
- end
data/lib/ably/rest.rb DELETED
@@ -1,40 +0,0 @@
1
- require 'ably/rest/channel'
2
- require 'ably/rest/channels'
3
- require 'ably/rest/client'
4
- require 'ably/rest/push'
5
- require 'ably/rest/presence'
6
-
7
- require 'ably/models/message_encoders/base'
8
-
9
- Dir.glob(File.expand_path("models/*.rb", File.dirname(__FILE__))).each do |file|
10
- require file
11
- end
12
-
13
- module Ably
14
- # Rest provides the top-level class to be instanced for the Ably Rest library
15
- #
16
- # @example
17
- # client = Ably::Rest.new("xxxxx")
18
- # channel = client.channel("test")
19
- # channel.publish "greeting", "data"
20
- #
21
- module Rest
22
- # Convenience method providing an alias to {Ably::Rest::Client} constructor.
23
- #
24
- # @param (see Ably::Rest::Client#initialize)
25
- # @option options (see Ably::Rest::Client#initialize)
26
- #
27
- # @return [Ably::Rest::Client]
28
- #
29
- # @example
30
- # # create a new client authenticating with basic auth
31
- # client = Ably::Rest.new('key.id:secret')
32
- #
33
- # # create a new client authenticating with basic auth and a client_id
34
- # client = Ably::Rest.new(key: 'key.id:secret', client_id: 'john')
35
- #
36
- def self.new(options)
37
- Ably::Rest::Client.new(options)
38
- end
39
- end
40
- end