keikokuc 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -46,6 +46,7 @@ class Keikokuc::NotificationList
46
46
  result, error = client.get_notifications
47
47
  if error.nil?
48
48
  @notifications = result.map do |attributes|
49
+ attributes.merge!(client: client, remote_id: attributes.delete(:id))
49
50
  Keikokuc::Notification.new(attributes)
50
51
  end
51
52
  end
@@ -53,6 +54,16 @@ class Keikokuc::NotificationList
53
54
  error.nil?
54
55
  end
55
56
 
57
+ # Public: marks all notifications as read
58
+ #
59
+ # This is a convenience method for marking all underlying notifications
60
+ # as read.
61
+ #
62
+ # Returns a Boolean set to true if all notifications were read successfully.
63
+ def read_all
64
+ self.inject(true) { |result, notification| result && notification.read }
65
+ end
66
+
56
67
  # Public: the number of notifications
57
68
  #
58
69
  # Returns an Integer set to the number of notifications
@@ -64,7 +75,7 @@ class Keikokuc::NotificationList
64
75
  #
65
76
  # Yields every notification in this collection
66
77
  def each
67
- @notifications.each
78
+ @notifications.each { |n| yield n }
68
79
  end
69
80
 
70
81
  private
@@ -1,3 +1,3 @@
1
1
  module Keikokuc
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
@@ -1,9 +1,34 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Keikokuc
4
+ shared_context 'specs with a fake client' do
5
+ let(:fake_client) { double(:fake_client) }
6
+ end
7
+ shared_context 'with user notifications' do
8
+ let(:user_notifications) do
9
+ [
10
+ {
11
+ id: 1,
12
+ target_name: 'flying-monkey-123',
13
+ message: 'Database HEROKU_POSTGRESQL_BROWN is over row limits',
14
+ url: 'https://devcenter.heroku.com/how-to-fix-problem',
15
+ severity: 'info'
16
+ },
17
+ {
18
+ id: 2,
19
+ target_name: 'rising-cloud-42',
20
+ message: 'High OOM rates',
21
+ url: 'https://devcenter.heroku.com/oom',
22
+ severity: 'fatal'
23
+ }
24
+ ]
25
+ end
26
+ end
27
+
4
28
  describe NotificationList, '#fetch' do
29
+ include_context 'specs with a fake client'
30
+ include_context 'with user notifications'
5
31
  it 'finds all notifications for the current user' do
6
- fake_client = double
7
32
  fake_client.should_receive(:get_notifications).
8
33
  and_return([user_notifications, nil])
9
34
  list = build(:notification_list, client: fake_client)
@@ -20,21 +45,48 @@ module Keikokuc
20
45
  end
21
46
  end
22
47
 
23
- def user_notifications
24
- [
25
- {
26
- resource: 'flying-monkey-123',
27
- message: 'Database HEROKU_POSTGRESQL_BROWN is over row limits',
28
- url: 'https://devcenter.heroku.com/how-to-fix-problem',
29
- severity: 'info'
30
- },
31
- {
32
- resource: 'rising-cloud-42',
33
- message: 'High OOM rates',
34
- url: 'https://devcenter.heroku.com/oom',
35
- severity: 'fatal'
36
- }
37
- ]
48
+ end
49
+
50
+ describe NotificationList, '#read_all' do
51
+ include_context 'specs with a fake client'
52
+ include_context 'with user notifications'
53
+
54
+ it 'marks all notifications as read' do
55
+ fake_client.stub(get_notifications: [user_notifications, nil])
56
+
57
+ now = Time.now
58
+ fake_client.should_receive(:read_notification).with(1).
59
+ and_return([{read_at: now}, nil])
60
+ fake_client.should_receive(:read_notification).with(2).
61
+ and_return([{read_at: now}, nil])
62
+
63
+ list = build(:notification_list, client: fake_client)
64
+
65
+ list.fetch or raise "error fetching"
66
+
67
+ expect(list.read_all).to be_true
68
+
69
+ list.each do |notification|
70
+ expect(notification).to be_read
71
+ expect(notification.read_at).to eq(now)
72
+ end
38
73
  end
74
+
75
+ it 'returns false if any notification fails to be marked as read' do
76
+ fake_client.stub(get_notifications: [user_notifications, nil])
77
+
78
+ now = Time.now
79
+ fake_client.should_receive(:read_notification).with(1).
80
+ and_return([{read_at: now}, nil])
81
+ fake_client.should_receive(:read_notification).with(2).
82
+ and_return([[], :an_error])
83
+
84
+ list = build(:notification_list, client: fake_client)
85
+
86
+ list.fetch or raise "error fetching"
87
+
88
+ expect(list.read_all).to be_false
89
+ end
90
+
39
91
  end
40
92
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keikokuc
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-10-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
- requirement: &70298442791900 !ruby/object:Gem::Requirement
16
+ requirement: &70251215460580 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70298442791900
24
+ version_requirements: *70251215460580
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: yajl-ruby
27
- requirement: &70298442791480 !ruby/object:Gem::Requirement
27
+ requirement: &70251215460160 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70298442791480
35
+ version_requirements: *70251215460160
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70298442791040 !ruby/object:Gem::Requirement
38
+ requirement: &70251215459720 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70298442791040
46
+ version_requirements: *70251215459720
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: factory_girl
49
- requirement: &70298442790620 !ruby/object:Gem::Requirement
49
+ requirement: &70251215459300 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70298442790620
57
+ version_requirements: *70251215459300
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: sham_rack
60
- requirement: &70298442790180 !ruby/object:Gem::Requirement
60
+ requirement: &70251215458860 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70298442790180
68
+ version_requirements: *70251215458860
69
69
  description: Keikoku client
70
70
  email:
71
71
  - harold.gimenez@gmail.com