Skip to content

Commit

Permalink
Notification API responding from the DB
Browse files Browse the repository at this point in the history
  • Loading branch information
theotherzach committed Jul 28, 2013
1 parent cb1d774 commit dc67dad
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 12 deletions.
14 changes: 7 additions & 7 deletions frontend/config/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 22,9 @@ module.exports = {
res.json([
{
id : "51dcb40d4a31e7a3cb000010",
type: "argument",
unread: true,
link: "http://localhost:8000/submissions/51dcb40d4a31e7a3cb000011",
kind: "argument",
read: true,
link: "submissions/51dcb40d4a31e7a3cb000011",
from: "Bob",
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
// Of our options, the Unix date string seems the most sane.
Expand All @@ -33,17 33,17 @@ module.exports = {
},
{
id : "51dcb40d4a31e7a3cb000031",
type: "nitpick",
unread: true,
kind: "nitpick",
read: true,
link: "http://localhost:8000/submissions/51dcb40d4a31e7a3cb00000f",
from: "Alice",
date: 1374453865190,
humanReadableDate: "about 12 hours ago",
},
{
id : "51dcb40d4a31e7a3cb000012",
type: "argument",
unread: false,
kind: "argument",
read: false,
link: "http://localhost:8000/submissions/51dcb40d4a31e7a3cb00000f",
from: "Bob",
date: 1374453866182,
Expand Down
1 change: 1 addition & 0 deletions lib/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 10,7 @@
require 'app/exercises'
require 'app/trails'
require 'app/about'
require 'app/presenters/notifications_presenter'
require 'app/nitpick'
require 'app/helpers/fuzzy_time_helper'
require 'app/helpers/gravatar_helper'
Expand Down
5 changes: 3 additions & 2 deletions lib/app/api/notifications_api.rb
Original file line number Diff line number Diff line change
@@ -1,14 1,15 @@
class ExercismApp < Sinatra::Base
get '/api/v1/notifications' do
notification_user
notifications = Dispatch.notifications_for_user(notification_user)
NotificationsPresenter.new(notifications).to_json
end

private
def notification_user
if params[:key]
User.find_by(:key, params[:key])
elsif session[:github_id]
@current_user
current_user
else
no_user_error
end
Expand Down
28 changes: 28 additions & 0 deletions lib/app/presenters/notifications_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 1,28 @@
#requiring here doesn't feel right
require_relative '../helpers/fuzzy_time_helper'

class NotificationsPresenter
include Sinatra::FuzzyTimeHelper

def initialize(notifications)
@notifications = transform(notifications)
end

def to_json
@notifications.to_json
end

def transform(notifications)
notifications.map { |notification|
{
id: notification.id,
kind: notification.kind,
read: notification.read,
link: notification.link,
from: notification.from,
date: notification.at.to_time.to_i,
humanReadableDate: ago(notification.at),
}
}
end
end
11 changes: 11 additions & 0 deletions lib/exercism/use_cases/notification.rb
Original file line number Diff line number Diff line change
@@ -1,5 1,16 @@
class Notification
include Mongoid::Document

field :re, as: :regarding, type: String
field :r, as: :read, type: Boolean, default: false
field :l, as: :link, type: String
field :f, as: :from, type: String
field :at, type: Time, default: ->{ Time.now.utc }

belongs_to :user

def self.recent_for_user(user)
where(user: user).limit(100).descending(:at)
end
end

1 change: 1 addition & 0 deletions lib/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 2,4 @@
require 'services/message'
require 'services/nitpick_message'
require 'services/approval_message'
require 'services/dispatch'
5 changes: 5 additions & 0 deletions lib/services/dispatch.rb
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
class Dispatch
def self.notifications_for_user(user)
Notification.recent_for_user(user)
end
end
23 changes: 20 additions & 3 deletions test/app/notifications_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 9,19 @@ def app

attr_reader :alice
def setup
@alice = User.create(username: 'alice', github_id: 1, current: {'ruby' => 'word-count', 'javascript' => 'anagram'})
Notification.create(user: @alice ,unread: true, from: "Bob", type: "argument")
@alice = User.create({
username: 'alice',
github_id: 1,
current: {'ruby' => 'word-count', 'javascript' => 'anagram'}
})
Notification.create({
user: @alice,
unread: true,
from: "Bob",
type: "argument",
kind: "Nitpick",
link: "a/link/to/a/thing",
})
end

def teardown
Expand All @@ -26,9 37,9 @@ def not_logged_in
end

def test_get_notifications_when_logged_in
skip
get '/api/v1/notifications', {}, 'rack.session' => logged_in
assert last_response.body.include?("Bob")
assert last_response.body.include?("just now")
end

def test_get_notifications_when_not_logged_in
Expand All @@ -37,15 48,21 @@ def test_get_notifications_when_not_logged_in
end

def test_get_notifications_from_cli
skip
get '/api/v1/notifications', @alice.key
assert last_response.body.include?("just now")
end

def test_mark_notification_read_when_logged_in
skip
end

def test_mark_notification_read_when_not_logged_in
skip
end

def test_mark_notification_from_cli
skip
end
end

0 comments on commit dc67dad

Please sign in to comment.