simple-feed 2.0.2 → 3.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.envrc +3 -0
- data/.github/workflows/rubocop.yml +33 -0
- data/.github/workflows/ruby.yml +34 -0
- data/.gitignore +2 -1
- data/.relaxed-rubocop-2.4.yml +174 -0
- data/.rspec +1 -1
- data/.rubocop.yml +43 -1150
- data/.rubocop_todo.yml +203 -0
- data/.travis.yml +7 -10
- data/CHANGELOG.md +120 -0
- data/Gemfile +4 -0
- data/Guardfile +3 -3
- data/README.adoc +601 -0
- data/Rakefile +5 -6
- data/bin/console +1 -0
- data/codecov.yml +28 -0
- data/examples/shared/provider_example.rb +51 -26
- data/lib/simple-feed.rb +2 -0
- data/lib/simple_feed.rb +2 -0
- data/lib/simplefeed/activity/base.rb +2 -0
- data/lib/simplefeed/activity/multi_user.rb +8 -6
- data/lib/simplefeed/activity/single_user.rb +7 -6
- data/lib/simplefeed/dsl/activities.rb +4 -3
- data/lib/simplefeed/dsl/formatter.rb +79 -40
- data/lib/simplefeed/dsl.rb +3 -1
- data/lib/simplefeed/event.rb +52 -13
- data/lib/simplefeed/feed.rb +36 -28
- data/lib/simplefeed/providers/base/provider.rb +7 -4
- data/lib/simplefeed/providers/hash.rb +2 -0
- data/lib/simplefeed/providers/key.rb +60 -36
- data/lib/simplefeed/providers/proxy.rb +28 -14
- data/lib/simplefeed/providers/redis/driver.rb +25 -27
- data/lib/simplefeed/providers/redis/provider.rb +41 -39
- data/lib/simplefeed/providers/redis/stats.rb +12 -13
- data/lib/simplefeed/providers/redis.rb +2 -0
- data/lib/simplefeed/providers.rb +24 -10
- data/lib/simplefeed/response.rb +7 -7
- data/lib/simplefeed/version.rb +3 -1
- data/lib/simplefeed.rb +27 -21
- data/man/running-example-redis-debug.png +0 -0
- data/man/running-example.png +0 -0
- data/man/sf-color-dump.png +0 -0
- data/simple-feed.gemspec +23 -16
- metadata +115 -44
- data/README.md +0 -415
- data/examples/hash_provider_example.rb +0 -24
- data/lib/simplefeed/key/template.rb +0 -52
- data/lib/simplefeed/key/type.rb +0 -26
- data/lib/simplefeed/providers/hash/paginator.rb +0 -31
- data/lib/simplefeed/providers/hash/provider.rb +0 -198
@@ -1,3 +1,6 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
$LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
2
5
|
|
3
6
|
# Please set @feed in the enclosing context
|
@@ -5,9 +8,15 @@ $LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
|
|
5
8
|
raise ArgumentError, 'No @feed defined in the enclosing example' unless defined?(@feed)
|
6
9
|
|
7
10
|
require 'simplefeed'
|
11
|
+
require 'uuid'
|
12
|
+
|
13
|
+
srand(Time.now.to_i % 100003)
|
14
|
+
|
15
|
+
@number_of_users = ARGV[0] ? ARGV[0].to_i : 2
|
16
|
+
@users = @number_of_users.times.map do |n|
|
17
|
+
n % 2 == 0 ? UUID.generate : rand(100003)
|
18
|
+
end
|
8
19
|
|
9
|
-
@number_of_users = ARGV[0] ? ARGV[0].to_i : 1
|
10
|
-
@users = Array.new(@number_of_users) { rand(200_000_000...800_000_000) }
|
11
20
|
@activity = @feed.activity(@users)
|
12
21
|
@uid = @users.first
|
13
22
|
|
@@ -20,48 +29,64 @@ class Object
|
|
20
29
|
end
|
21
30
|
|
22
31
|
def p(*args)
|
23
|
-
printf "
|
32
|
+
printf "%40s ❯ %s\n", args[0].strip.blue.bold, args[1].bold.red
|
24
33
|
end
|
25
34
|
|
26
35
|
with_activity(@activity) do
|
27
|
-
|
28
|
-
|
36
|
+
header "#{@activity.feed.provider_type} provider example".upcase,
|
37
|
+
"Starting with a blank feed, no items",
|
38
|
+
align: :center
|
29
39
|
|
30
40
|
wipe
|
31
41
|
|
32
|
-
store('value one')
|
33
|
-
store('value two')
|
34
|
-
store('value three')
|
35
|
-
|
36
|
-
hr
|
42
|
+
store('value one') { p 'storing new value', 'value one' }
|
43
|
+
store('value two') { p 'storing new value', 'value two' }
|
44
|
+
store('value three') { p 'storing new value', 'value three' }
|
37
45
|
|
38
|
-
total_count
|
39
|
-
unread_count
|
46
|
+
total_count { |r| p 'total_count is now', "#{r[@uid]._v}" }
|
47
|
+
unread_count { |r| p 'unread_count is now', "#{r[@uid]._v}" }
|
40
48
|
|
41
|
-
header 'paginate(page: 1, per_page: 2)'
|
49
|
+
header 'activity.paginate(page: 1, per_page: 2)'
|
42
50
|
paginate(page: 1, per_page: 2) { |r| puts r[@uid].map(&:to_color_s) }
|
43
|
-
header 'paginate(page: 2, per_page: 2, reset_last_read: true)'
|
44
|
-
paginate(page: 2, per_page: 2, reset_last_read: true) { |r| puts r[@uid].map(&:to_color_s) }
|
45
51
|
|
46
|
-
|
52
|
+
header 'activity.paginate(page: 2, per_page: 2, reset_last_read: true)'
|
53
|
+
paginate(page: 2, per_page: 2, reset_last_read: true) { |r| puts r[@uid].map(&:to_color_s) }
|
47
54
|
|
48
|
-
total_count
|
49
|
-
unread_count
|
55
|
+
total_count { |r| p 'total_count ', "#{r[@uid]._v}" }
|
56
|
+
unread_count { |r| p 'unread_count ', "#{r[@uid]._v}" }
|
50
57
|
|
51
|
-
|
52
|
-
store('value four') { p 'storing', 'value four' }
|
58
|
+
store('value four') { p 'storing', 'value four' }
|
53
59
|
|
54
60
|
color_dump
|
55
61
|
|
56
62
|
header 'deleting'
|
57
63
|
|
58
|
-
delete('value three')
|
59
|
-
|
60
|
-
|
64
|
+
delete('value three') { p 'deleting', 'value three' }
|
65
|
+
|
66
|
+
total_count { |r| p 'total_count ', "#{r[@uid]._v}" }
|
67
|
+
unread_count { |r| p 'unread_count ', "#{r[@uid]._v}" }
|
68
|
+
|
61
69
|
hr
|
62
|
-
delete('value four') { p 'deleting', 'value four' }
|
63
|
-
total_count { |r| p 'total_count ', "#{r[@uid]._v}" }
|
64
|
-
unread_count { |r| p 'unread_count ', "#{r[@uid]._v}" }
|
65
70
|
|
71
|
+
delete('value four') { p 'deleting', 'value four' }
|
72
|
+
total_count { |r| p 'total_count ', "#{r[@uid]._v}" }
|
73
|
+
unread_count { |r| p 'unread_count ', "#{r[@uid]._v}" }
|
74
|
+
|
75
|
+
puts
|
76
|
+
end
|
77
|
+
|
78
|
+
notes = [
|
79
|
+
'Thanks for trying SimpleFeed!', 'For any questions, reach out to',
|
80
|
+
'kigster@gmail.com',
|
81
|
+
]
|
82
|
+
|
83
|
+
unless ENV['REDIS_DEBUG']
|
84
|
+
notes << [
|
85
|
+
'———',
|
86
|
+
'To see REDIS commands, set REDIS_DEBUG environment variable to true,',
|
87
|
+
'and re-run the example.'
|
88
|
+
]
|
66
89
|
end
|
67
90
|
|
91
|
+
header notes.flatten,
|
92
|
+
align: :center
|
data/lib/simple-feed.rb
CHANGED
data/lib/simple_feed.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'forwardable'
|
2
4
|
require_relative 'base'
|
3
5
|
|
4
6
|
module SimpleFeed
|
5
7
|
module Activity
|
6
8
|
class MultiUser < Base
|
7
|
-
|
8
9
|
attr_reader :user_ids
|
9
10
|
|
10
11
|
extend Forwardable
|
@@ -16,7 +17,7 @@ module SimpleFeed
|
|
16
17
|
# API Examples
|
17
18
|
# ============
|
18
19
|
#
|
19
|
-
|
20
|
+
# ```ruby
|
20
21
|
# @multi = SimpleFeed.get(:feed_name).activity(User.active.map(&:id))
|
21
22
|
#
|
22
23
|
# @multi.store(value:, at:)
|
@@ -54,15 +55,15 @@ module SimpleFeed
|
|
54
55
|
# # => [Response] { user_id => [Time] last_read, ... }
|
55
56
|
#
|
56
57
|
# @multi.total_count
|
57
|
-
# # => [Response] { user_id => [Integer] total_count, ... }
|
58
|
+
# # => [Response] { user_id => [Integer, String] total_count, ... }
|
58
59
|
#
|
59
60
|
# @multi.unread_count
|
60
|
-
# # => [Response] { user_id => [Integer] unread_count, ... }
|
61
|
+
# # => [Response] { user_id => [Integer, String] unread_count, ... }
|
61
62
|
#
|
62
63
|
# @multi.last_read
|
63
64
|
# # => [Response] { user_id => [Time] last_read, ... }
|
64
65
|
#
|
65
|
-
|
66
|
+
# ```
|
66
67
|
|
67
68
|
SimpleFeed::Providers.define_provider_methods(self) do |instance, method, *args, **opts, &block|
|
68
69
|
opts.merge!(user_ids: instance.user_ids)
|
@@ -71,8 +72,9 @@ module SimpleFeed
|
|
71
72
|
opts.delete(:event)
|
72
73
|
end
|
73
74
|
response = instance.feed.send(method, *args, **opts, &block)
|
74
|
-
|
75
|
+
block&.call(response)
|
75
76
|
raise StandardError, "Nil response from provider #{instance.feed.provider&.provider&.class}, method #{method}(#{opts})" unless response
|
77
|
+
|
76
78
|
response
|
77
79
|
end
|
78
80
|
|
@@ -1,8 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'base'
|
2
4
|
|
3
5
|
module SimpleFeed
|
4
6
|
module Activity
|
5
|
-
|
6
7
|
# Lazy implementation of SingleUser based on delegating an array of
|
7
8
|
# one user_id to +MultiUser+
|
8
9
|
|
@@ -16,7 +17,7 @@ module SimpleFeed
|
|
16
17
|
yield(user_id)
|
17
18
|
end
|
18
19
|
|
19
|
-
|
20
|
+
# ```ruby
|
20
21
|
# @activity = SimpleFeed.get(:feed_name).activity(current_user.id)
|
21
22
|
#
|
22
23
|
# @activity.store(value:, at:)
|
@@ -61,20 +62,20 @@ module SimpleFeed
|
|
61
62
|
# @activity.last_read
|
62
63
|
# # => [Time] last_read
|
63
64
|
|
64
|
-
|
65
65
|
SimpleFeed::Providers.define_provider_methods(self) do |instance, method, *args, **opts, &block|
|
66
66
|
response = instance.user_activity.send(method, *args, **opts, &block)
|
67
67
|
unless response.has_user?(instance.user_id)
|
68
68
|
raise StandardError, "Nil response from provider #{instance.feed.provider&.provider&.class}, method #{method}(#{opts}) — user_id #{instance.user_id}"
|
69
69
|
end
|
70
|
+
|
70
71
|
response = response[instance.user_id]
|
71
|
-
|
72
|
+
block&.call(response)
|
72
73
|
response
|
73
74
|
end
|
74
75
|
|
75
76
|
def initialize(user_id:, feed:)
|
76
|
-
@feed
|
77
|
-
@user_id
|
77
|
+
@feed = feed
|
78
|
+
@user_id = user_id
|
78
79
|
self.user_activity = MultiUser.new(feed: feed, user_ids: [user_id])
|
79
80
|
end
|
80
81
|
end
|
@@ -1,8 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'formatter'
|
2
4
|
module SimpleFeed
|
3
5
|
module DSL
|
4
6
|
class Activities
|
5
|
-
|
6
7
|
include SimpleFeed::DSL::Formatter
|
7
8
|
|
8
9
|
attr_accessor :activity, :feed
|
@@ -14,7 +15,7 @@ module SimpleFeed
|
|
14
15
|
self.class.instance_eval do
|
15
16
|
attr_accessor key
|
16
17
|
end
|
17
|
-
|
18
|
+
send("#{key}=".to_sym, value)
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
@@ -60,7 +61,7 @@ module SimpleFeed
|
|
60
61
|
|
61
62
|
def print_debug_info(method, **opts)
|
62
63
|
brackets = opts.empty? ? ['', ''] : %w{( )}
|
63
|
-
printf "\n#{
|
64
|
+
printf "\n#{feed.name.to_s.blue}.#{method.to_s.cyan.bold}#{brackets[0]}#{opts.to_s.gsub(/[{}]/, '').blue}#{brackets[1]} \n" if SimpleFeed::DSL.debug?
|
64
65
|
response = yield if block_given?
|
65
66
|
puts response.inspect.yellow if SimpleFeed::DSL.debug?
|
66
67
|
response
|
@@ -1,6 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'tty-box'
|
4
|
+
require 'tty-screen'
|
1
5
|
require 'simplefeed/dsl'
|
2
6
|
require 'simplefeed/activity/single_user'
|
3
7
|
require 'simplefeed/activity/multi_user'
|
8
|
+
require 'awesome_print'
|
9
|
+
|
4
10
|
module SimpleFeed
|
5
11
|
module DSL
|
6
12
|
# This module exports method #color_dump which receives an activity and
|
@@ -13,45 +19,46 @@ module SimpleFeed
|
|
13
19
|
|
14
20
|
def color_dump(this_activity = activity)
|
15
21
|
this_activity = if this_activity.is_a?(SimpleFeed::Activity::SingleUser)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
22
|
+
this_activity.feed.activity([this_activity.user_id])
|
23
|
+
else
|
24
|
+
this_activity
|
25
|
+
end
|
20
26
|
_puts
|
21
27
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
28
|
+
feed_header(feed) do
|
29
|
+
[
|
30
|
+
field('Feed Name', feed.name, "\n"),
|
31
|
+
field('Provider', feed.provider.provider.class, "\n"),
|
32
|
+
field('Max Size', feed.max_size, "\n")
|
33
|
+
]
|
26
34
|
end
|
27
35
|
|
28
36
|
with_activity(this_activity) do
|
29
|
-
this_activity.
|
37
|
+
this_activity.each_with_index do |user_id, index|
|
30
38
|
this_last_event_at = nil
|
31
39
|
this_last_read = (last_read[user_id] || 0.0).to_f
|
32
40
|
|
41
|
+
fields = []
|
33
42
|
[['User ID', user_id, "\n"],
|
34
43
|
['Activities', sprintf('%d total, %d unread', total_count[user_id], unread_count[user_id]), "\n"],
|
35
|
-
['Last Read', this_last_read ? Time.at(this_last_read) : 'N/A'],
|
36
|
-
|
37
|
-
field(field, value, *args)
|
44
|
+
['Last Read', this_last_read ? Time.at(this_last_read) : 'N/A'],].each do |field, value, *args|
|
45
|
+
fields << field(field, value, *args)
|
38
46
|
end
|
39
47
|
|
40
|
-
|
48
|
+
header(title: { top_center: " « User Activity #{index + 1} » " }, style: { fg: :green }) { fields }
|
41
49
|
|
42
50
|
this_events = fetch[user_id]
|
43
51
|
this_events_count = this_events.size
|
44
|
-
this_events.each_with_index do |
|
45
|
-
|
46
|
-
if this_last_event_at.nil? && _event.at < this_last_read
|
52
|
+
this_events.each_with_index do |evt, idx|
|
53
|
+
if this_last_event_at.nil? && evt.at < this_last_read
|
47
54
|
print_last_read_separator(this_last_read)
|
48
|
-
elsif this_last_event_at && this_last_read < this_last_event_at && this_last_read >
|
55
|
+
elsif this_last_event_at && this_last_read < this_last_event_at && this_last_read > evt.at
|
49
56
|
print_last_read_separator(this_last_read)
|
50
57
|
end
|
51
58
|
|
52
|
-
this_last_event_at =
|
53
|
-
|
54
|
-
if
|
59
|
+
this_last_event_at = evt.at # float
|
60
|
+
output "[%2d] %16s %s\n", idx, evt.time.strftime(TIME_FORMAT).blue.bold, evt.value
|
61
|
+
if idx == this_events_count - 1 && this_last_read < evt.at
|
55
62
|
print_last_read_separator(this_last_read)
|
56
63
|
end
|
57
64
|
end
|
@@ -60,17 +67,18 @@ module SimpleFeed
|
|
60
67
|
end
|
61
68
|
|
62
69
|
def print_last_read_separator(lr)
|
63
|
-
|
70
|
+
output "———— %16s [last read] ———————————— \n", Time.at(lr).strftime(TIME_FORMAT).red.bold
|
64
71
|
end
|
65
72
|
end
|
66
73
|
|
74
|
+
# This allows redirecting output in tests.
|
67
75
|
@print_method = :printf
|
68
76
|
|
69
77
|
class << self
|
70
78
|
attr_accessor :print_method
|
71
79
|
end
|
72
80
|
|
73
|
-
def
|
81
|
+
def output(*args, **opts, &block)
|
74
82
|
send(SimpleFeed::DSL.print_method, *args, **opts, &block)
|
75
83
|
end
|
76
84
|
|
@@ -82,35 +90,66 @@ module SimpleFeed
|
|
82
90
|
sprintf ' %20s ', text
|
83
91
|
end
|
84
92
|
|
85
|
-
TIME_FORMAT = '%Y-%m-%d %H:%M:%S.%L'
|
86
|
-
|
87
93
|
def field_value(value)
|
88
94
|
case value
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
+
when Numeric
|
96
|
+
sprintf '%-20d', value
|
97
|
+
when Time
|
98
|
+
sprintf '%-30s', value.strftime(TIME_FORMAT)
|
99
|
+
else
|
100
|
+
sprintf '%-20s', value.to_s
|
95
101
|
end
|
96
102
|
end
|
97
103
|
|
98
|
-
def field(label, value,
|
99
|
-
|
104
|
+
def field(label, value, _sep = '')
|
105
|
+
field_label(label) + ' ❯ ' + field_value(value)
|
106
|
+
end
|
107
|
+
|
108
|
+
def hr
|
109
|
+
output hr_string.magenta
|
110
|
+
end
|
111
|
+
|
112
|
+
def hr_string
|
113
|
+
'―' * width + "\n"
|
114
|
+
end
|
115
|
+
|
116
|
+
def width
|
117
|
+
@width ||= [[TTY::Screen.width - 5, 60].max, 75].min
|
100
118
|
end
|
101
119
|
|
102
|
-
def
|
103
|
-
|
120
|
+
def feed_header(feed, &block)
|
121
|
+
header title: { top_left: " « #{feed.name.capitalize} Feed » " },
|
122
|
+
border: :thick,
|
123
|
+
style: {
|
124
|
+
fg: :black,
|
125
|
+
bg: :green,
|
126
|
+
border: { fg: :bright_black, bg: :green }
|
127
|
+
}, &block
|
104
128
|
end
|
105
129
|
|
106
|
-
def
|
107
|
-
|
130
|
+
def header(*args, **opts)
|
131
|
+
message = args.join("\n")
|
132
|
+
msg = block_given? ? (yield || message) : message + "\n"
|
133
|
+
config = box_config(**opts)
|
134
|
+
lines = Array(msg).flatten
|
135
|
+
box = TTY::Box.frame(*lines, **config)
|
136
|
+
output "\n#{box}"
|
108
137
|
end
|
109
138
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
139
|
+
private
|
140
|
+
|
141
|
+
def box_config(**opts)
|
142
|
+
{
|
143
|
+
width: width,
|
144
|
+
align: :left,
|
145
|
+
padding: [1, 3],
|
146
|
+
style: {
|
147
|
+
fg: :bright_yellow,
|
148
|
+
border: {
|
149
|
+
fg: :bright_magenta,
|
150
|
+
}
|
151
|
+
}
|
152
|
+
}.merge(opts)
|
114
153
|
end
|
115
154
|
end
|
116
155
|
end
|
data/lib/simplefeed/dsl.rb
CHANGED
data/lib/simplefeed/event.rb
CHANGED
@@ -1,12 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'json'
|
2
4
|
|
3
5
|
module SimpleFeed
|
4
6
|
class Event
|
5
7
|
attr_accessor :value, :at
|
8
|
+
|
6
9
|
include Comparable
|
7
10
|
|
11
|
+
class << self
|
12
|
+
attr_accessor :is_time
|
13
|
+
end
|
14
|
+
|
15
|
+
# This proc can be overridden in a configuration if needed.
|
16
|
+
# @example To always assume this is time, set it like so,
|
17
|
+
# before defining your feeds.
|
18
|
+
#
|
19
|
+
# SimpleFeed::Event.is_time = ->(*) { true }
|
20
|
+
#
|
21
|
+
self.is_time = ->(float) {
|
22
|
+
# assume it's time if epoch is > June 1974 and < December 2040.
|
23
|
+
float < 2_237_932_800.0 && float > 139_276_800.0
|
24
|
+
}
|
25
|
+
|
8
26
|
def initialize(*args, value: nil, at: Time.now)
|
9
|
-
if args && args.
|
27
|
+
if args && !args.empty?
|
10
28
|
self.value = args[0]
|
11
29
|
self.at = args[1]
|
12
30
|
end
|
@@ -20,7 +38,11 @@ module SimpleFeed
|
|
20
38
|
end
|
21
39
|
|
22
40
|
def time
|
41
|
+
return nil unless Event.is_time[at]
|
42
|
+
|
23
43
|
Time.at(at)
|
44
|
+
rescue ArgumentError
|
45
|
+
nil
|
24
46
|
end
|
25
47
|
|
26
48
|
def <=>(other)
|
@@ -36,15 +58,11 @@ module SimpleFeed
|
|
36
58
|
self.value == other.value
|
37
59
|
end
|
38
60
|
|
39
|
-
def to_h
|
40
|
-
{ value: value, at: at, time: time }
|
41
|
-
end
|
42
|
-
|
43
61
|
def hash
|
44
62
|
self.value.hash
|
45
63
|
end
|
46
64
|
|
47
|
-
def to_json
|
65
|
+
def to_json(*_args)
|
48
66
|
to_h.to_json
|
49
67
|
end
|
50
68
|
|
@@ -52,16 +70,38 @@ module SimpleFeed
|
|
52
70
|
YAML.dump(to_h)
|
53
71
|
end
|
54
72
|
|
73
|
+
def to_h
|
74
|
+
return @to_h if @to_h
|
75
|
+
|
76
|
+
@to_h ||= { value: value, at: at }
|
77
|
+
@to_h.merge!(time: time) if time
|
78
|
+
@to_h
|
79
|
+
end
|
80
|
+
|
55
81
|
def to_s
|
56
|
-
|
82
|
+
return @to_s if @to_s
|
83
|
+
|
84
|
+
output = StringIO.new
|
85
|
+
output.print "<SimpleFeed::Event: "
|
86
|
+
output.print(time.nil? ? "[#{at}]" : "[#{time&.strftime(::SimpleFeed::TIME_FORMAT)}]")
|
87
|
+
output.print ", [\"#{value}\"]"
|
88
|
+
@to_s = output.string
|
57
89
|
end
|
58
90
|
|
91
|
+
COLOR_MAP = {
|
92
|
+
1 => ->(word) { word.green.bold },
|
93
|
+
3 => ->(word) { word.yellow.bold },
|
94
|
+
}.freeze
|
95
|
+
|
59
96
|
def to_color_s
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
97
|
+
return @to_color_s if @to_color_s
|
98
|
+
|
99
|
+
output = StringIO.new
|
100
|
+
to_s.split(/[\[\]]/).each_with_index do |word, index|
|
101
|
+
output.print(COLOR_MAP[index]&.call(word) || word.cyan)
|
102
|
+
end
|
103
|
+
output.print '>'
|
104
|
+
@to_color_s = output.string
|
65
105
|
end
|
66
106
|
|
67
107
|
def inspect
|
@@ -75,6 +115,5 @@ module SimpleFeed
|
|
75
115
|
raise ArgumentError, "Required arguments missing, value=[#{value}], at=[#{at}]"
|
76
116
|
end
|
77
117
|
end
|
78
|
-
|
79
118
|
end
|
80
119
|
end
|
data/lib/simplefeed/feed.rb
CHANGED
@@ -1,11 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'providers'
|
2
4
|
require_relative 'activity/base'
|
3
|
-
|
5
|
+
require_relative 'providers/key'
|
4
6
|
|
5
7
|
module SimpleFeed
|
6
8
|
class Feed
|
9
|
+
attr_accessor :per_page,
|
10
|
+
:max_size,
|
11
|
+
:batch_size,
|
12
|
+
:namespace,
|
13
|
+
:data_key_transformer,
|
14
|
+
:meta_key_transformer
|
7
15
|
|
8
|
-
attr_accessor :per_page, :max_size, :batch_size, :meta, :namespace
|
9
16
|
attr_reader :name
|
10
17
|
|
11
18
|
SimpleFeed::Providers.define_provider_methods(self) do |feed, method, opts, &block|
|
@@ -13,23 +20,21 @@ module SimpleFeed
|
|
13
20
|
end
|
14
21
|
|
15
22
|
def initialize(name)
|
16
|
-
@name
|
17
|
-
@name
|
23
|
+
@name = name
|
24
|
+
@name = name.underscore.to_sym unless name.is_a?(Symbol)
|
18
25
|
# set the defaults if not passed in
|
19
|
-
@meta
|
20
|
-
@namespace
|
21
|
-
@per_page
|
22
|
-
@max_size
|
23
|
-
@batch_size
|
24
|
-
@
|
25
|
-
|
26
|
-
|
27
|
-
def key_template
|
28
|
-
SimpleFeed::Key::Template.new(namespace)
|
26
|
+
@meta = {}
|
27
|
+
@namespace = nil
|
28
|
+
@per_page ||= 50
|
29
|
+
@max_size ||= 1000
|
30
|
+
@batch_size ||= 10
|
31
|
+
@meta_key_transformer = nil
|
32
|
+
@data_key_transformer = nil
|
33
|
+
@proxy = nil
|
29
34
|
end
|
30
35
|
|
31
36
|
def provider=(definition)
|
32
|
-
@proxy
|
37
|
+
@proxy = Providers::Proxy.from(definition)
|
33
38
|
@proxy.feed = self
|
34
39
|
@proxy
|
35
40
|
end
|
@@ -38,10 +43,6 @@ module SimpleFeed
|
|
38
43
|
@proxy
|
39
44
|
end
|
40
45
|
|
41
|
-
def key(user_id)
|
42
|
-
SimpleFeed::Providers::Key.new(user_id, key_template)
|
43
|
-
end
|
44
|
-
|
45
46
|
def provider_type
|
46
47
|
SimpleFeed::Providers::Base::Provider.class_to_registry(@proxy.provider.class)
|
47
48
|
end
|
@@ -61,28 +62,35 @@ module SimpleFeed
|
|
61
62
|
user_activity(one_or_more_users)
|
62
63
|
end
|
63
64
|
|
64
|
-
# @deprecated Please use {#activity} instead
|
65
|
-
def for(*args)
|
66
|
-
STDERR.puts 'WARNING: method #for is deprecated, please use #activity'
|
67
|
-
activity(*args)
|
68
|
-
end
|
69
|
-
|
70
65
|
def configure(hash = {})
|
71
66
|
SimpleFeed.symbolize!(hash)
|
72
67
|
class_attrs.each do |attr|
|
73
|
-
|
68
|
+
send("#{attr}=", hash[attr]) if hash.key?(attr)
|
74
69
|
end
|
75
70
|
yield self if block_given?
|
76
71
|
end
|
77
72
|
|
73
|
+
def key(user_id)
|
74
|
+
SimpleFeed::Providers::Key.new(user_id,
|
75
|
+
namespace: namespace,
|
76
|
+
data_key_transformer: data_key_transformer,
|
77
|
+
meta_key_transformer: meta_key_transformer)
|
78
|
+
end
|
79
|
+
|
78
80
|
def eql?(other)
|
79
81
|
other.class == self.class &&
|
80
|
-
%i(per_page max_size name).all? { |m|
|
81
|
-
|
82
|
+
%i(per_page max_size name namespace data_key_transformer meta_key_transformer).all? { |m| send(m).equal?(other.send(m)) } &&
|
83
|
+
provider.provider.class == other.provider.provider.class
|
82
84
|
end
|
83
85
|
|
84
86
|
def class_attrs
|
85
87
|
SimpleFeed.class_attributes(self.class)
|
86
88
|
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def key_template
|
93
|
+
SimpleFeed::Key::Template.new(namespace)
|
94
|
+
end
|
87
95
|
end
|
88
96
|
end
|