Class: Pushr::Daemon::FcmSupport::ConnectionFcm

Inherits:
Object
  • Object
show all
Defined in:
lib/pushr/daemon/fcm_support/connection_fcm.rb

Constant Summary collapse

IDLE_PERIOD =
5 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, i) ⇒ ConnectionFcm

Returns a new instance of ConnectionFcm.



10
11
12
13
14
15
# File 'lib/pushr/daemon/fcm_support/connection_fcm.rb', line 10

def initialize(configuration, i)
  @configuration = configuration
  @name = "#{@configuration.app}: ConnectionFcm #{i}"
  @authenticator = Pushr::Daemon::FcmSupport::Authenticator.new(configuration, i)
  @url = "https://fcm.googleapis.com/v1/projects/#{configuration.project_id}/messages:send"
end

Instance Attribute Details

#authenticatorObject (readonly)

Returns the value of attribute authenticator.



7
8
9
# File 'lib/pushr/daemon/fcm_support/connection_fcm.rb', line 7

def authenticator
  @authenticator
end

#configurationObject (readonly)

Returns the value of attribute configuration.



7
8
9
# File 'lib/pushr/daemon/fcm_support/connection_fcm.rb', line 7

def configuration
  @configuration
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/pushr/daemon/fcm_support/connection_fcm.rb', line 7

def name
  @name
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/pushr/daemon/fcm_support/connection_fcm.rb', line 7

def response
  @response
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/pushr/daemon/fcm_support/connection_fcm.rb', line 7

def url
  @url
end

Instance Method Details

#connectObject



17
18
19
20
21
22
23
# File 'lib/pushr/daemon/fcm_support/connection_fcm.rb', line 17

def connect
  @last_use = Time.now
  uri = URI.parse(@url)
  @connection = open_http(uri.host, uri.port)
  @connection.start
  Pushr::Daemon.logger.info("[#{@name}] Connected to #{@url}")
end

#write(data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pushr/daemon/fcm_support/connection_fcm.rb', line 25

def write(data)
  retry_count = 0
  begin
    response = notification_request(data.to_message)
    handler = Pushr::Daemon::FcmSupport::ResponseHandler.new(response, data, retry_count)
    handler.handle
  rescue => e
    retry_count += 1
    if retry_count < 10
      retry
    else
      raise e
    end
  end
end