flyer 1.0.0

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: a0beb30f7cd48acf09067ba968ee6c437c554794
4
+ data.tar.gz: a66326cd122f1deedfcc9284e76c968bc0b7d9e1
5
+ SHA512:
6
+ metadata.gz: 0d4115bffda8d7e1291e0bf28612d1e7daaa6c278233d0f1eee132254626564487fb18f0d4efe50d642bff70eb77b71ae1179f1a641fa8ff1472be3528fe0e09
7
+ data.tar.gz: 06d7c279e20ac755338c396c1ae0abc007425ee54a1f5ecf7263bf5c8d901c97a89ed4da8bf926f3bfb52dfe98988f38f797fe00b6bf2ab30981229f120cdcb7
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Linus Oleander
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Flyer
2
+
3
+ Display user notifications in Rails programmatically.
4
+
5
+ ## Install
6
+
7
+ `gem install flyer`
8
+
9
+ ## Usage
10
+
11
+ ``` ruby
12
+ # config/initializers/flyer.rb
13
+ Flyer::Notification.init do |config|
14
+ # Unique id. Used to uniquely identify a notification.
15
+ config.id = "new-user"
16
+
17
+ # Message to be passed to view. Is evaluated in the view context.
18
+ config.message { "Your nickname is #{current_user.nickname}" + icon "flash" }
19
+
20
+ # Optional. Path to be pssed to view. Is evaluated in the controller context.
21
+ config.path { challenge_path }
22
+
23
+ # Optional. Only display notification if this blocks evaluates to true
24
+ # The block is evaluated in the controller context.
25
+ config.on { current_user.admin? and not first_visit? }
26
+
27
+ # Optional. Number of times to display the notification
28
+ # for each user. Default is 1.
29
+ config.limit = 1
30
+
31
+ # Optional. When should the notification be visible?
32
+ config.valid = { from: "2015-04-01", to: "2016-04-01" }
33
+
34
+ # Optional. Arbitrary data to be passed to view.
35
+ config.params = { timeout: 10 }
36
+ end
37
+ ```
38
+
39
+ ``` erb
40
+ -- app/views/_notifications.html.erb (any view)
41
+ <% notifications.each do |notification| %>
42
+ <%= notification.path %>
43
+ <%= notification.message %>
44
+ <%= notification.params %>
45
+ <% end %>
46
+ ```
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Flyer'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
data/lib/flyer.rb ADDED
@@ -0,0 +1,10 @@
1
+ module Flyer
2
+ end
3
+
4
+ require "flyer/errors"
5
+ require "flyer/controller_additions"
6
+ require "flyer/notification"
7
+
8
+ ActionController::Base.class_eval do
9
+ include Flyer::ControllerAdditions
10
+ end
@@ -0,0 +1,30 @@
1
+ module Flyer::ControllerAdditions
2
+ #
3
+ # @return Array<Flyer::ViewObject>
4
+ #
5
+ def notifications
6
+ @notifications ||= begin
7
+ found_notifications = []
8
+ Flyer::Notification.notifications.each do |n|
9
+ notification = Flyer::Notification.new(self)
10
+ n.call(notification)
11
+ notification.validate!
12
+ if notification.visible?
13
+ notification.used!
14
+ found_notifications << notification.view
15
+ end
16
+ end
17
+
18
+ ids = found_notifications.map(&:id)
19
+ unless ids.uniq.count == ids.count
20
+ raise Flyer::FoundNonUniqueIds.new
21
+ end
22
+
23
+ found_notifications
24
+ end
25
+ end
26
+
27
+ def self.included(base)
28
+ base.helper_method :notifications
29
+ end
30
+ end
@@ -0,0 +1,13 @@
1
+ module Flyer
2
+ class PathNotGivenError < StandardError
3
+ end
4
+
5
+ class MessageMissingError < StandardError
6
+ end
7
+
8
+ class IdMissingError < StandardError
9
+ end
10
+
11
+ class FoundNonUniqueIds < StandardError
12
+ end
13
+ end
@@ -0,0 +1,121 @@
1
+ require_relative "view_object"
2
+
3
+ class Flyer::Notification
4
+ attr_accessor :valid, :params, :id, :limit
5
+
6
+ @@notifications = []
7
+
8
+ def self.init(&block)
9
+ @@notifications << block
10
+ end
11
+
12
+ def self.reset!
13
+ @@notifications = []
14
+ end
15
+
16
+ #
17
+ # @return Array<Flyer::Notification>
18
+ #
19
+ def self.notifications
20
+ @@notifications
21
+ end
22
+
23
+ #
24
+ # @controller ActionController::Base
25
+ #
26
+ def initialize(controller)
27
+ @on = []
28
+ @limit = 1
29
+ @controller = controller
30
+ end
31
+
32
+ #
33
+ # Raise an error if @id or @message is missing
34
+ #
35
+ def validate!
36
+ raise Flyer::IdMissingError.new unless @id
37
+ raise Flyer::MessageMissingError.new unless @message
38
+ end
39
+
40
+ #
41
+ # Mark current notification has used
42
+ #
43
+ def used!
44
+ cookies.permanent[token] = current_count + 1
45
+ end
46
+
47
+ #
48
+ # @return Boolean Has the notification expired?
49
+ #
50
+ def expired?
51
+ return false unless @valid
52
+
53
+ if @valid[:to]
54
+ if Date.parse(@valid.fetch(:to)) < Date.today
55
+ return true
56
+ end
57
+ end
58
+
59
+ if @valid[:from]
60
+ if Date.parse(@valid.fetch(:from)) > Date.today
61
+ return true
62
+ end
63
+ end
64
+ end
65
+
66
+ #
67
+ # @return Boolean Should the notification be visible?
68
+ #
69
+ def visible?
70
+ return false if hit_limit?
71
+ return false if expired?
72
+ return true if @on.empty?
73
+ @on.any?{ |blk| @controller.instance_eval(&blk) }
74
+ end
75
+
76
+ #
77
+ # @return Flyer::ViewObject Object to be passed to view
78
+ #
79
+ def view
80
+ Flyer::ViewObject.new(@controller, @path, @message, @params, @id)
81
+ end
82
+
83
+ #
84
+ # @block Proc The evaluation if @block should yield a path
85
+ #
86
+ def path(&block)
87
+ @path = block
88
+ end
89
+
90
+ #
91
+ # @block Proc The evaluation if @block should yield a string
92
+ #
93
+ def message(&block)
94
+ @message = block
95
+ end
96
+
97
+ #
98
+ # @block Proc The evaluation if @block should yield a boolean
99
+ #
100
+ def on(&block)
101
+ @on << block
102
+ end
103
+
104
+ private
105
+
106
+ def token
107
+ "flyer.#{id}".parameterize
108
+ end
109
+
110
+ def hit_limit?
111
+ limit and current_count >= limit
112
+ end
113
+
114
+ def current_count
115
+ cookies[token].to_i
116
+ end
117
+
118
+ def cookies
119
+ @controller.send(:cookies)
120
+ end
121
+ end
@@ -0,0 +1,3 @@
1
+ module Flyer
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ class Flyer::ViewObject
2
+ attr_reader :params, :id
3
+
4
+ #
5
+ # @controller ActionController::Base
6
+ # @path Proc
7
+ # @message Proc
8
+ # @params Hash
9
+ # @id Object
10
+ #
11
+ def initialize(controller, path, message, params, id)
12
+ @controller = controller
13
+ @path = path
14
+ @message = message
15
+ @params = params
16
+ @id = id
17
+ end
18
+
19
+ #
20
+ # @return String
21
+ #
22
+ def message
23
+ @controller.view_context.
24
+ instance_eval(&@message).to_s.html_safe
25
+ end
26
+
27
+ #
28
+ # @return Path / String
29
+ #
30
+ def path
31
+ raise Flyer::PathNotGivenError.new unless @path
32
+ @controller.instance_eval(&@path)
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flyer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Linus Oleander
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: capybara
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: timecop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Display user notifications in Rails programmatically
84
+ email:
85
+ - linus@oleander.io
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - MIT-LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - lib/flyer.rb
94
+ - lib/flyer/controller_additions.rb
95
+ - lib/flyer/errors.rb
96
+ - lib/flyer/notification.rb
97
+ - lib/flyer/version.rb
98
+ - lib/flyer/view_object.rb
99
+ homepage: https://github.com/oleander/flyer-rb
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.4.3
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Display user notifications in Rails programmatically
123
+ test_files: []