Class: EventMachine::ApnManager::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/em_apn_manager/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = [], opts = [], config = {}) ⇒ CLI

3 ways to specify the redis

  1. config yml file

  2. pass redis-url is ‘redis://username:password@host:port/database’

like ‘redis://test:test@localhost:6379/em_apn_manager`



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/em_apn_manager/cli.rb', line 28

def initialize(args = [], opts = [], config = {})
  super(args, opts, config)

  redis_config = options[:redis]
  if redis_config.nil?
    # read the environment var.
    @environment = ENV["RAILS_ENV"] || "development"
    @environment = options[:environment] if %w{test development production}.include? options[:environment]

    # Read config option, or use default config yml
    config_path = options[:config] || File.join(".", "config", "em_apn_manager.yml")
    if config_path && File.exists?(config_path)
      EM::ApnManager.config = Thor::CoreExt::HashWithIndifferentAccess.new(YAML.load_file(config_path))
      redis_config = EM::ApnManager.config[@environment]
    end

    # default redis
    redis_config ||= "redis://localhost:6379/em_apn_manager"
  end

  # create redis connection
  $apn_manager_redis = Redis.new url: redis_config
end

Instance Method Details

#mock_apn_serverObject



82
83
84
85
# File 'lib/em_apn_manager/cli.rb', line 82

def mock_apn_server
  EM::ApnManager.logger.info("Starting Mock APN Server")
  EM.run { EM.start_server("127.0.0.1", 2195, EM::ApnManager::ApnServer) }
end

#push_test_messageObject

if message is DISCONNECT, mock apns will disconnect.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/em_apn_manager/cli.rb', line 66

def push_test_message
  EM::ApnManager.push_notification({
    env: @environment,
    cert: File.read(ENV["APN_CERT"]), # test cert
    # iPhone5
    # "D42A6795D0C6C0E5F3CC762F905C3654D2A07E72D64CDEC1E2F74AC43C4CC440"
    # iPhone4
    # "BACF565FB283E9F7378D5A0AEBCBCB49A4D7834AED8FAC7FE0CB28144D94E456"
    # My iPhone4s
    # "0F93C49EAAF3544B5218D2BAE893608C515F69B445279AB2B17511C37046C52B"
    token: ["0F93C49EAAF3544B5218D2BAE893608C515F69B445279AB2B17511C37046C52B"].sample,
    message: "Hahahaha I am going to spam you. #{rand * 100}"
  })
end

#serverObject



55
56
57
58
59
60
61
# File 'lib/em_apn_manager/cli.rb', line 55

def server
  daemonize if options[:daemon]
  write_pid_file(options[:pid_file]) if options[:pid_file]

  EM::ApnManager.logger.info("Starting APN Manager")
  EM.run { EM::ApnManager::Manager.run options }
end