Class: Octo::Schedulers

Inherits:
Object
  • Object
show all
Defined in:
lib/octonotification/schedulers.rb

Constant Summary collapse

SCORE =

Random Score

0.98

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.getPEMLocationForClient(eid) ⇒ String

Fetch IOS Certificate



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/octonotification/schedulers.rb', line 72

def self.getPEMLocationForClient(eid)

  if Cequel::Record.redis.get(eid).nil?
    updatePEMLocation(eid)
  end

  if !File.exist?(Cequel::Record.redis.get(eid))
    updatePEMLocation(eid)
  end

  Cequel::Record.redis.get(eid)
end

.perform(user) ⇒ Object

Resque Perform method



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/octonotification/schedulers.rb', line 20

def self.perform(user)
  products = trending_products(user)

  product = products.shuffle[0]
  template = user_template(user)

  msg = {}

  msg[:text] = Octo::TextGenerator.generate(product, template)
  msg[:userToken] = Octo::PushToken.where(user: user)

  msg[:pushKey] = Octo::PushKey.where(enterprise: user.enterprise)

  gcm_sender(msg, user.enterprise_id)

end

Instance Method Details

#gcm_sender(msg, eid) ⇒ Object

Sending notification using GCM



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/octonotification/schedulers.rb', line 40

def gcm_sender(msg, eid)
  apns_config = Octo.get_config :apns

  notification = {
    title: 'Check this out',
    body: msg[:text]
  }

  # some random score to be sent
  score = { score: SCORE }

  if msg.has_key?(:userToken)
    msg[:userToken].each do |pushtype, pushtoken|
      if pushtype == 2
        APNS.host = apns_config[:host]
        APNS.pem  = getPEMLocationForClient(eid)
        apnsresponse = APNS.send_notification(pushtoken, :alert => notification, :other => score )
      elsif [0, 1].include?(pushtype)
        gcmClientKey = msg[:pushKey][:key]
        gcm = GCM.new(gcmClientKey)
        registration_ids = [pushtoken]
        options = {data: score, notification: notification, content_available: true, priority: 'high'}
        gcmresponse = gcm.send(registration_ids, options)
      end
    end
  end

end

Fetch Trending Products



108
109
110
111
# File 'lib/octonotification/schedulers.rb', line 108

def trending_products(user)
  recommender = Octo::Recommender.new
  recommender.recommended_products(user)
end

#updatePEMLocation(eid) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/octonotification/schedulers.rb', line 85

def updatePEMLocation(eid)
  aws_config = Octo.get_config :aws
  certificate_config = Octo.get_config :ioscertificate

  Aws.config.update({
    region: aws_config[:region],
    credentials: Aws::Credentials.new( aws_config[:access_key], aws_config[:secret_key])
  })

  s3 = Aws::S3::Client.new
  object_key = eid + '/' + certificate_config[:filename]
  resp = s3.get_object(bucket: aws_config[:bucket_name], key: object_key)
  
  pem_file = Tempfile.new 'pem_file'
  pem_file.write resp.body.read
  pem_file.close

  Cequel::Record.redis.set( eid, pem_file.path)
end

#user_template(user) ⇒ String

Fetch Notification Template



116
117
118
119
120
121
122
123
124
# File 'lib/octonotification/schedulers.rb', line 116

def user_template(user)
  categories = Octo::Category.where(enterprise: user.enterprise)
  @templates = []
  categories.each do |category|
    temp = Octo::Template.where(enterprise: user.enterprise, category_type: category.text).first
    @templates.push(temp.template_text)
  end
  @templates.shuffle[0]
end