unagi-notifications 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,88 @@
1
+ var initialize = function() {
2
+ var opts = $('#js-notifications-container');
3
+
4
+ if (opts.length == 0) {
5
+ return;
6
+ }
7
+
8
+ if (firebase.apps.length === 0) {
9
+ var config = {
10
+ apiKey: `${opts.data('firebase-api-key')}`,
11
+ authDomain: `${opts.data('firebase-project-id')}.firebaseapp.com/`,
12
+ databaseURL: `https://${opts.data('firebase-project-id')}.firebaseio.com/`,
13
+ };
14
+
15
+ firebase.initializeApp(config);
16
+ }
17
+
18
+ var destination = opts.data('destination');
19
+
20
+ var ref = firebase
21
+ .database()
22
+ .ref(`notifications/${destination}`);
23
+
24
+ ref.off();
25
+
26
+ ref
27
+ .orderByChild('read_at')
28
+ .equalTo(false)
29
+ .on('child_added', onNotificationAdded.bind(this));
30
+ };
31
+
32
+ var onNotificationAdded = function(notification) {
33
+ var value = notification.val();
34
+ var key = notification.key;
35
+
36
+ appendNotification(value, key);
37
+ setNotificationsCount();
38
+ }
39
+
40
+ var appendNotification = function(notification, key) {
41
+ var container = $('#js-notifications-container');
42
+
43
+ var notification_container = container.data('notification-container');
44
+ var notification_container_class = container.data('notification-container-class');
45
+ var notification_class = container.data('notification-class');
46
+
47
+ var id = 'js-notification-' + Math.random().toString(36).substr(2, 9)
48
+
49
+ var html = "" +
50
+ "<" + notification_container + " class='" + notification_container_class +"' id='" + id +"'>" +
51
+ "<a href='" + notification.link + "'>" +
52
+ "<span class='js-notification " + notification_class + "'>" +
53
+ notification.text +
54
+ "</span>" +
55
+ "</a>" +
56
+ "</" + notification_container + ">"
57
+
58
+ container.prepend(html);
59
+ bindOnClickListener(id, key, notification);
60
+ };
61
+
62
+ var bindOnClickListener = function(html_id, key, notification) {
63
+ var opts = $('#js-notifications-container');
64
+
65
+ $('#' + html_id).on('click', function() {
66
+ firebase
67
+ .database()
68
+ .ref(`notifications/${opts.data('destination')}`)
69
+ .child(key)
70
+ .update({
71
+ read_at: Date.now()
72
+ });
73
+ });
74
+ };
75
+
76
+ var setNotificationsCount = function() {
77
+ var container = $('#js-notifications-count');
78
+
79
+ if (container.length == 0){
80
+ return;
81
+ }
82
+
83
+ container.text($('.js-notification').length);
84
+ };
85
+
86
+ $(document).ready(initialize); // No Turbolinks
87
+ $(document).on('page:load', initialize); // Rails <= 4
88
+ $(document).on('turbolinks:load', initialize); // Rails 5
@@ -0,0 +1,7 @@
1
+ module Notifications
2
+ class Engine < Rails::Engine
3
+ initializer 'notifications' do
4
+ ActionView::Base.send(:include, Notifications::Helper)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,37 @@
1
+ module Notifications
2
+ module Helper
3
+ def notifications_for(destination, opts = {})
4
+ opts = set_default_options(opts)
5
+
6
+ content_tag opts[:container], '',
7
+ id: 'js-notifications-container',
8
+ class: opts[:container_class],
9
+ data: {
10
+ firebase_project_id: Notifications.configuration.firebase_project_id,
11
+ firebase_api_key: Notifications.configuration.firebase_api_key,
12
+ destination: destination,
13
+ notification_container: opts[:notification_container],
14
+ notification_container_class: opts[:notification_container_class],
15
+ notification_class: opts[:notification_class]
16
+ }
17
+ end
18
+
19
+ def notifications_count_for(origin, opts = {})
20
+ content_tag (opts[:container] || :div), '',
21
+ id: 'js-notifications-count',
22
+ class: opts[:container_class]
23
+ end
24
+
25
+ private
26
+
27
+ def set_default_options(opts)
28
+ opts[:container] ||= :div
29
+ opts[:container_class] ||= ''
30
+ opts[:notification_container] ||= :div
31
+ opts[:notification_container_class] ||= ''
32
+ opts[:notificaton_class] ||= ''
33
+
34
+ opts
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,10 @@
1
+ require 'rails/railtie'
2
+ require 'action_view/base'
3
+
4
+ module Notifications
5
+ class Railtie < ::Rails::Railtie #:nodoc:
6
+ config.after_initialize do
7
+ Notifications::ViewHelpers = ActionView::Base.new
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,37 @@
1
+ require 'net/http'
2
+ require 'openssl'
3
+ require 'uri'
4
+ require 'json'
5
+ require 'faraday'
6
+
7
+ module Notifications
8
+ class Uploader
9
+ class << self
10
+ def call(origin: nil, destination:, text:, link:, created_at: Time.now)
11
+ message = JSON.generate(
12
+ origin: origin,
13
+ text: text,
14
+ link: link,
15
+ read_at: false,
16
+ created_at: created_at.to_datetime.to_i
17
+ )
18
+
19
+ connection = Faraday.new(url: firebase_host)
20
+
21
+ connection.post do |req|
22
+ req.url "/notifications/#{destination}.json"
23
+ req.headers['Content-Type'] = 'application/json'
24
+ req.body = message
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def firebase_host
31
+ project_id = Notifications.configuration.firebase_project_id
32
+
33
+ "https://#{project_id}.firebaseio.com"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Notifications
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,27 @@
1
+ require 'rails/railtie'
2
+ require 'action_view/railtie'
3
+ require 'action_controller/railtie'
4
+
5
+ module Notifications
6
+ def self.configure
7
+ @configuration ||= Configuration.new
8
+
9
+ yield @configuration if block_given?
10
+ end
11
+
12
+ def self.configuration
13
+ @configuration
14
+ end
15
+
16
+ class Configuration
17
+ attr_accessor :firebase_project_id, :firebase_api_key
18
+ end
19
+
20
+ configure
21
+ end
22
+
23
+ require 'notifications/version'
24
+ require 'notifications/uploader'
25
+ require 'notifications/engine'
26
+ require 'notifications/railtie'
27
+ require 'notifications/helper'
@@ -0,0 +1,45 @@
1
+
2
+ # -*- encoding: utf-8 -*-
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+
5
+ require 'notifications/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'unagi-notifications'
9
+ spec.version = Notifications::VERSION
10
+ spec.platform = Gem::Platform::RUBY
11
+ spec.authors = ['Lucas Hourquebie']
12
+ spec.email = ['lucashour1993@gmail.com']
13
+
14
+ spec.summary = 'Notifications system integration'
15
+ spec.description = 'Notifications system using Google Firebase for Realtime Database'
16
+ spec.homepage = 'http://unagi.com.ar'
17
+ spec.license = 'MIT'
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata["allowed_push_host"] = 'https://rubygems.org'
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ spec.files = [
29
+ 'lib/notifications',
30
+ 'lib/notifications/uploader',
31
+ 'lib/notifications/engine',
32
+ 'lib/notifications/realtie',
33
+ 'lib/notifications/helper'
34
+ ]
35
+ spec.files = `git ls-files`.split("\n")
36
+ spec.test_files = `git ls-files -- {spec}/*`.split("\n")
37
+ spec.require_paths = ['lib']
38
+
39
+ spec.add_development_dependency "bundler", "~> 1.16"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+
42
+ spec.add_runtime_dependency 'actionpack', '>= 3.2'
43
+ spec.add_runtime_dependency 'railties', '>= 3.2'
44
+ spec.add_dependency 'faraday'
45
+ end
@@ -0,0 +1,11 @@
1
+ require "test_helper"
2
+
3
+ class NotificationsTest < Minitest::Test
4
+ def test_that_it_has_a_version_number
5
+ refute_nil ::Notifications::VERSION
6
+ end
7
+
8
+ def test_it_does_something_useful
9
+ assert false
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require "notifications"
3
+
4
+ require "minitest/autorun"
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unagi-notifications
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Lucas Hourquebie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: actionpack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: railties
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Notifications system using Google Firebase for Realtime Database
84
+ email:
85
+ - lucashour1993@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".DS_Store"
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - lib/assets/javascripts/firebase.js
99
+ - lib/assets/javascripts/unagi-notifications.js
100
+ - lib/notifications.rb
101
+ - lib/notifications/engine.rb
102
+ - lib/notifications/helper.rb
103
+ - lib/notifications/railtie.rb
104
+ - lib/notifications/uploader.rb
105
+ - lib/notifications/version.rb
106
+ - notifications.gemspec
107
+ - test/notifications_test.rb
108
+ - test/test_helper.rb
109
+ homepage: http://unagi.com.ar
110
+ licenses:
111
+ - MIT
112
+ metadata:
113
+ allowed_push_host: https://rubygems.org
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.6.8
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Notifications system integration
134
+ test_files: []