octonotification 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0bf2d07ab6ff6e8669adfba0cc6080bf375cb03c
4
+ data.tar.gz: c61168a9b84670cc46ed8bf2f7db322e4d42d2e3
5
+ SHA512:
6
+ metadata.gz: 4eb5a9e002878311659beaa267ff4edf07f5937eaa12896c6976f5c83bde632d53a141b84682ba5e09a26497addc438f5535470d0ba36101ab05af072eeef7ce
7
+ data.tar.gz: d6c2b4075066874147b370331565f42f41746bb0f6da2916903e6582f660e172034496f9facbb64e436dfac5c39fdccccb4a85238a50d215acc1cde199b8e9c0
data/README.md ADDED
File without changes
@@ -0,0 +1,8 @@
1
+ require 'octonotification/octohooks'
2
+ require 'octonotification/schedulers'
3
+
4
+ module Octo
5
+ module Notifications
6
+
7
+ end
8
+ end
@@ -0,0 +1,32 @@
1
+ require 'octocore/callbacks'
2
+ require 'octorecommender'
3
+ require 'resque'
4
+ require 'resque-scheduler'
5
+
6
+ module Octo
7
+ module OctoHooks
8
+
9
+ # Define the custom tasks that need to be done
10
+ # upon various hooks
11
+ Octo::Callbacks.after_app_init lambda { |opts|
12
+ update_scheduler opts
13
+ }
14
+
15
+ # Extend the methods here
16
+ module ClassMethods
17
+
18
+ # Updates the scheduler
19
+ # @param [Hash] opts The options hash as passed
20
+ def update_scheduler(opts)
21
+ user = opts[:user]
22
+ recommender = Octo::Recommender.new
23
+ arr = recommender.recommended_time(user)
24
+ arr.each do |r|
25
+ Resque.enqueue_at(r, Octo::Schedulers, user)
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,127 @@
1
+ require 'cequel'
2
+ require 'redis'
3
+ require 'resque'
4
+ require 'gcm'
5
+ require 'apns'
6
+ require 'json'
7
+ require 'aws-sdk'
8
+ require 'tempfile'
9
+
10
+ require 'octorecommender'
11
+ require 'octonotification/textgenerator'
12
+
13
+ module Octo
14
+ class Schedulers
15
+ @queue = :notification_schedule
16
+ SCORE = 0.98 # Random Score
17
+
18
+ # Resque Perform method
19
+ # @param [Octo::User] user The details of a user
20
+ def self.perform(user)
21
+ products = trending_products(user)
22
+
23
+ product = products.shuffle[0]
24
+ template = user_template(user)
25
+
26
+ msg = {}
27
+
28
+ msg[:text] = Octo::TextGenerator.generate(product, template)
29
+ msg[:userToken] = Octo::PushToken.where(user: user)
30
+
31
+ msg[:pushKey] = Octo::PushKey.where(enterprise: user.enterprise)
32
+
33
+ gcm_sender(msg, user.enterprise_id)
34
+
35
+ end
36
+
37
+ # Sending notification using GCM
38
+ # @param [Hash] msg The details of notification
39
+ # @param [String] eid Enterprise Id of the client
40
+ def gcm_sender(msg, eid)
41
+ apns_config = Octo.get_config :apns
42
+
43
+ notification = {
44
+ title: 'Check this out',
45
+ body: msg[:text]
46
+ }
47
+
48
+ # some random score to be sent
49
+ score = { score: SCORE }
50
+
51
+ if msg.has_key?(:userToken)
52
+ msg[:userToken].each do |pushtype, pushtoken|
53
+ if pushtype == 2
54
+ APNS.host = apns_config[:host]
55
+ APNS.pem = getPEMLocationForClient(eid)
56
+ apnsresponse = APNS.send_notification(pushtoken, :alert => notification, :other => score )
57
+ elsif [0, 1].include?(pushtype)
58
+ gcmClientKey = msg[:pushKey][:key]
59
+ gcm = GCM.new(gcmClientKey)
60
+ registration_ids = [pushtoken]
61
+ options = {data: score, notification: notification, content_available: true, priority: 'high'}
62
+ gcmresponse = gcm.send(registration_ids, options)
63
+ end
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ # Fetch IOS Certificate
70
+ # @param [String] eid Enterprise Id of the client
71
+ # @return [String] Path of the IOS certificate file
72
+ def self.getPEMLocationForClient(eid)
73
+
74
+ if Cequel::Record.redis.get(eid).nil?
75
+ updatePEMLocation(eid)
76
+ end
77
+
78
+ if !File.exist?(Cequel::Record.redis.get(eid))
79
+ updatePEMLocation(eid)
80
+ end
81
+
82
+ Cequel::Record.redis.get(eid)
83
+ end
84
+
85
+ def updatePEMLocation(eid)
86
+ aws_config = Octo.get_config :aws
87
+ certificate_config = Octo.get_config :ioscertificate
88
+
89
+ Aws.config.update({
90
+ region: aws_config[:region],
91
+ credentials: Aws::Credentials.new( aws_config[:access_key], aws_config[:secret_key])
92
+ })
93
+
94
+ s3 = Aws::S3::Client.new
95
+ object_key = eid + '/' + certificate_config[:filename]
96
+ resp = s3.get_object(bucket: aws_config[:bucket_name], key: object_key)
97
+
98
+ pem_file = Tempfile.new 'pem_file'
99
+ pem_file.write resp.body.read
100
+ pem_file.close
101
+
102
+ Cequel::Record.redis.set( eid, pem_file.path)
103
+ end
104
+
105
+ # Fetch Trending Products
106
+ # @param [Octo::User] user The details of the user
107
+ # @return [Array<Octo::Product>] An array of Octo::Product recommended
108
+ def trending_products(user)
109
+ recommender = Octo::Recommender.new
110
+ recommender.recommended_products(user)
111
+ end
112
+
113
+ # Fetch Notification Template
114
+ # @param [Octo::User] user The details of the user
115
+ # @return [String] Template Text
116
+ def user_template(user)
117
+ categories = Octo::Category.where(enterprise: user.enterprise)
118
+ @templates = []
119
+ categories.each do |category|
120
+ temp = Octo::Template.where(enterprise: user.enterprise, category_type: category.text).first
121
+ @templates.push(temp.template_text)
122
+ end
123
+ @templates.shuffle[0]
124
+ end
125
+
126
+ end
127
+ end
@@ -0,0 +1,27 @@
1
+ module Octo
2
+ class TextGenerator
3
+
4
+ TEMPLATES = [
5
+ "Check out this cool new %{name} for just Rs %{price}.",
6
+ "%{name} is trending in %{category} right now. Check it out",
7
+ "You should totally see this %{name} in %{category}. It's just for %{price}"
8
+ ]
9
+
10
+ # Generate Notification Template
11
+ # @param [Hash] product Details of the product
12
+ # @param [String] template Text Template
13
+ def self.generate(product, template=nil)
14
+
15
+ pHash = {
16
+ name: product['name'],
17
+ category: product[:categories].shuffle[0],
18
+ price: product['price'].round(2)
19
+ }
20
+ if template.nil?
21
+ TEMPLATES.sample % pHash
22
+ else
23
+ template % pHash
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,7 @@
1
+ module Octo
2
+ module Notification
3
+
4
+ # The version of Recommender module
5
+ VERSION = '0.0.1'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: octonotification
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ravin Gupta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: octorecommender
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.1
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.0.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 0.0.1
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: gcm
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 0.1.1
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.1.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 0.1.1
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.1.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: apns
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 1.0.0
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.0.0
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.0
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 1.0.0
73
+ - !ruby/object:Gem::Dependency
74
+ name: aws-sdk
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 2.2.35
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.2.35
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.2.35
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 2.2.35
93
+ description: |2
94
+ Contains notifications specific stuff
95
+ email: ravin.gupta@octo.ai
96
+ executables: []
97
+ extensions: []
98
+ extra_rdoc_files:
99
+ - README.md
100
+ files:
101
+ - README.md
102
+ - lib/octonotification.rb
103
+ - lib/octonotification/octohooks.rb
104
+ - lib/octonotification/schedulers.rb
105
+ - lib/octonotification/textgenerator.rb
106
+ - lib/octonotification/version.rb
107
+ homepage: https://bitbucket.org/auroraborealisinc/gems
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '2.0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.6
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Octo Notification(s) Module
131
+ test_files: []
132
+ has_rdoc: true