Module: AFMotion::Rocket

Included in:
AFHTTPRequestOperationManager, AFHTTPSessionManager
Defined in:
lib/afmotion-rocket/version.rb,
lib/afmotion-rocket/client_patch.rb

Defined Under Namespace

Classes: HeaderWrapper

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



3
4
5
6
# File 'lib/afmotion-rocket/client_patch.rb', line 3

def self.included(klass)
  klass.send(:alias_method,"headers_without_wrapper", "headers")
  klass.send(:alias_method,"headers", "headers_with_wrapper")
end

Instance Method Details

#headers_with_wrapperObject



46
47
48
# File 'lib/afmotion-rocket/client_patch.rb', line 46

def headers_with_wrapper
  @headers_wrapper ||= HeaderWrapper.new(self)
end

#rocket_clientObject



8
9
10
11
12
13
14
15
16
# File 'lib/afmotion-rocket/client_patch.rb', line 8

def rocket_client
  @rocket_client ||= begin
    client = AFRocketClient.alloc.init
    self.all_headers.each do |header, value|
      client.requestSerializer.headers[header] = value
    end
    client
  end
end

#subscribe(url, &callback) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/afmotion-rocket/client_patch.rb', line 18

def subscribe(url, &callback)
  url_string = NSURL.URLWithString(url, relativeToURL: self.baseURL).absoluteString
  error_ptr = Pointer.new(:object)
  event_source = rocket_client.SUBSCRIBE(url_string, usingBlock: ->(operations, error) {
    callback.call operations.map(&:to_h)
  }, error: error_ptr)

  # .retain because event_source will otherwise be autoreleased.
  # see also: https://github.com/AFNetworking/AFRocketClient/issues/3 .
  [error_ptr[0], event_source.retain]
end