Class: Lowdown::Connection::Mock

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

Defined Under Namespace

Classes: Request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(responses = []) ⇒ Mock

Returns a new instance of Mock.



10
11
12
13
# File 'lib/lowdown/connection/mock.rb', line 10

def initialize(responses = [])
  @responses = responses
  @requests = []
end

Instance Attribute Details

#requestsObject (readonly)

Returns the value of attribute requests.



8
9
10
# File 'lib/lowdown/connection/mock.rb', line 8

def requests
  @requests
end

#responsesObject (readonly)

Returns the value of attribute responses.



8
9
10
# File 'lib/lowdown/connection/mock.rb', line 8

def responses
  @responses
end

Instance Method Details

#closeObject



40
41
42
# File 'lib/lowdown/connection/mock.rb', line 40

def close
  @open = false
end

#openObject



36
37
38
# File 'lib/lowdown/connection/mock.rb', line 36

def open
  @open = true
end

#open?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/lowdown/connection/mock.rb', line 44

def open?
  !!@open
end

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

Yields:

  • (response)


30
31
32
33
34
# File 'lib/lowdown/connection/mock.rb', line 30

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

#requests_as_notifications(unformatted_id_length = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lowdown/connection/mock.rb', line 15

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