Class: Lowdown::Mock::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/lowdown/mock.rb

Overview

A mock object that can be used instead of a real Connection object.

Defined Under Namespace

Classes: Request

Mock API: Instance Attribute Summary collapse

Real API: Instance Attribute Summary collapse

Mock API: Instance Method Summary collapse

Real API: Instance Method Summary collapse

Constructor Details

#initialize(uri: nil, ssl_context: nil) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • uri (URI, String) (defaults to: nil)

    the details to connect to the APN service.

  • ssl_context (OpenSSL::SSL::SSLContext) (defaults to: nil)

    a SSL context, configured with the certificate/key pair, which is used to connect to the APN service.



88
89
90
91
92
# File 'lib/lowdown/mock.rb', line 88

def initialize(uri: nil, ssl_context: nil)
  @uri, @ssl_context = uri, ssl_context
  @responses = []
  @requests = []
end

Instance Attribute Details

#requestsArray<Request> (readonly)

Returns a list of requests that have been made in order.

Returns:

  • (Array<Request>)

    a list of requests that have been made in order.



77
78
79
# File 'lib/lowdown/mock.rb', line 77

def requests
  @requests
end

#responsesArray<Response> (readonly)

Returns a list of stubbed responses to return in order.

Returns:

  • (Array<Response>)

    a list of stubbed responses to return in order.



82
83
84
# File 'lib/lowdown/mock.rb', line 82

def responses
  @responses
end

#ssl_contextOpenSSL::SSL::SSLContext (readonly)

Returns a SSL context, configured with the certificate/key pair, which is used to connect to the APN service.

Returns:

  • (OpenSSL::SSL::SSLContext)

    a SSL context, configured with the certificate/key pair, which is used to connect to the APN service.



122
123
124
# File 'lib/lowdown/mock.rb', line 122

def ssl_context
  @ssl_context
end

#uriURI (readonly)

Returns the details to connect to the APN service.

Returns:

  • (URI)

    the details to connect to the APN service.



118
119
120
# File 'lib/lowdown/mock.rb', line 118

def uri
  @uri
end

Instance Method Details

#closevoid

This method returns an undefined value.

Changes #open? to return false.



153
154
155
156
157
158
# File 'lib/lowdown/mock.rb', line 153

def close
  flush
  @callbacks.kill
  @callbacks = nil
  @open = false
end

#flushvoid

This method returns an undefined value.

no-op



164
165
166
167
168
169
170
171
# File 'lib/lowdown/mock.rb', line 164

def flush
  caller_thread = Thread.current
  @callbacks.enqueue do
    sleep 0.1
    caller_thread.run
  end
  Thread.stop
end

#openvoid

This method returns an undefined value.

Changes #open? to return true.



144
145
146
147
# File 'lib/lowdown/mock.rb', line 144

def open
  @callbacks = Threading::Consumer.new
  @open = true
end

#open?Boolean

Returns:

  • (Boolean)
  • (Boolean)

    whether or not the Connection is open.



175
176
177
# File 'lib/lowdown/mock.rb', line 175

def open?
  !!@open
end

#post(path, headers, body) {|response| ... } ⇒ void

This method returns an undefined value.

Yields stubbed #responses or if none are available defaults to success responses. It does this on a different thread, just like the real API does.

Parameters:

  • path (String)

    the request path, which should be /3/device/<device-token>.

  • headers (Hash)

    the additional headers for the request. By default it sends :method, :path, and content-length.

  • body (String)

    the (JSON) encoded payload data to send to the service.

Yields:

  • (response)

    called when the request is finished and a response is available.

Yield Parameters:

  • response (Response)

    the Response that holds the status data that came back from the service.



134
135
136
137
138
# File 'lib/lowdown/mock.rb', line 134

def post(path, headers, body, &callback)
  response = @responses.shift || Response.new(":status" => "200", "apns-id" => (headers["apns-id"] || generate_id))
  @requests << Request.new(path, headers, body, response)
  @callbacks.enqueue { callback.call(response) }
end

#requests_as_notifications(unformatted_id_length = nil) ⇒ Array<Notification>

Returns the recorded requests as Notification objects.

Parameters:

  • unformatted_id_length (Integer) (defaults to: nil)

    the expected length of an ID, which ensures that required leading zeroes are not removed.

Returns:

  • (Array<Notification>)

    returns the recorded requests as Notification objects.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lowdown/mock.rb', line 99

def requests_as_notifications(unformatted_id_length = nil)
  @requests.map do |request|
    headers = request.headers
    hash = {
      :token => File.basename(request.path),
      :id => request.response.unformatted_id(unformatted_id_length),
      :payload => JSON.parse(request.body),
      :topic => headers["apns-topic"]
    }
    hash[:expiration] = Time.at(headers["apns-expiration"].to_i) if headers["apns-expiration"]
    hash[:priority] = headers["apns-priority"].to_i if headers["apns-priority"]
    Notification.new(hash)
  end
end