Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8 viewing data and stats #100

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ gem 'sinatra', require: 'sinatra/base'
gem 'pg'
gem 'activerecord'
gem 'sinatra-activerecord'
gem 'database_cleaner'

group :development, :test do
gem 'shotgun'
Expand All @@ -12,6 +13,7 @@ group :development, :test do
gem 'capybara'
gem 'launchy'
gem 'rack-test'
gem 'pry'
end

group :evaluations do
Expand Down
40 changes: 40 additions & 0 deletions app/controllers/server.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
require 'json'
require 'digest'
require './app/models/json_parser.rb'

module TrafficSpy
class Server < Sinatra::Base
set :show_exceptions, false

get '/' do
erb :index
end

post '/sources' do
source = Source.new(:root_url => params[:rootUrl], :identifier => params[:identifier])
if source.save
{:identifier => source.identifier}.to_json
else
status StatusMessages.status_message(source)

body source.errors.full_messages.join(", ")
end
end

post '/sources/:identifier/data' do |identifier|
source = Source.find_by(:identifier => params["identifier"])
if params["payload"].nil?
status StatusMessages.blank_payload
body "payload can't be blank"
elsif source
data = JsonParser.parse_json(params["payload"], source)
if data.save
body "Created Successfully"
else
status StatusMessages.status_message(data)
body data.errors.full_messages.join(", ")
end
else
status StatusMessages.blank_identifier
body "Identifier does not exist"
end
end

get '/sources/:identifier' do |identifier|
erb :source_page
end

not_found do
erb :error
end
Expand Down
19 changes: 19 additions & 0 deletions app/models/json_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'json'
require 'digest'

class JsonParser
def self.parse_json(payload, source)
json_payload = JSON.parse(payload)
@data = TrafficSpy::Payload.new(:url => json_payload["url"],
:requested_at => json_payload["requestedAt"],
:responded_in => json_payload["respondedIn"],
:event_name => json_payload["eventName"],
:user_agent => json_payload["userAgent"],
:resolution_width => json_payload["resolutionWidth"],
:resolution_height => json_payload["resolutionHeight"],
:ip => json_payload["ip"],
:hex_digest => Digest::SHA2.hexdigest(payload.to_s),
:source_id => source.id)
end
@data
end
4 changes: 4 additions & 0 deletions app/models/payload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class TrafficSpy::Payload < ActiveRecord::Base
belongs_to :source
validates :hex_digest, uniqueness: true
end
5 changes: 5 additions & 0 deletions app/models/source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class TrafficSpy::Source < ActiveRecord::Base
has_many :payloads
validates_presence_of :identifier, :root_url
validates :identifier, uniqueness: true
end
21 changes: 21 additions & 0 deletions app/models/status_messages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class StatusMessages
def self.status_message(source)
if source.errors.full_messages.join(", ") == "Identifier has already been taken"
return 403
elsif source.errors.full_messages.join(", ") == "Root url can't be blank"
return 400
elsif source.errors.full_messages.join(", ") == "Identifier can't be blank"
return 400
elsif source.errors.full_messages.join(", ") == "Hex digest has already been taken"
return 403
end
end

def self.blank_payload
return 400
end

def self.blank_identifier
return 403
end
end
7 changes: 7 additions & 0 deletions app/views/source_page.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>Most Requested URLS to Least Requested URLS</h1>
<p>Web Browser Breakdown</p>
<p>OS Breakdown</p>
<p>Screen Resolution</p>
<p>Longest to Shortest Response Times</p>
<p>Hyperlinks of each URL</p>
<p>Hyperlink to view event data</p>
1 change: 1 addition & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'bundler'

Bundler.require

require File.expand_path('../config/environment', __FILE__)
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20151020192525_create_sources.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateSources < ActiveRecord::Migration
def change
create_table :sources do |t|
t.text :identifier
t.text :root_url
end
end
end
15 changes: 15 additions & 0 deletions db/migrate/20151021153042_create_payloads.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreatePayloads < ActiveRecord::Migration
def change
create_table :payloads do |t|
t.text :url
t.text :requested_at
t.text :responded_in
t.text :event_name
t.text :user_agent
t.text :resolution_width
t.text :resolution_height
t.text :ip
t.text :hex_digest
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20151021234318_add_source_id_to_payloads.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSourceIdToPayloads < ActiveRecord::Migration
def change
add_column :payloads, :source_id, :integer
end
end
37 changes: 37 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20151021234318) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "payloads", force: :cascade do |t|
t.text "url"
t.text "requested_at"
t.text "responded_in"
t.text "event_name"
t.text "user_agent"
t.text "resolution_width"
t.text "resolution_height"
t.text "ip"
t.text "hex_digest"
t.integer "source_id"
end

create_table "sources", force: :cascade do |t|
t.text "identifier"
t.text "root_url"
end

end
124 changes: 124 additions & 0 deletions test/controllers/processing_request_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
require './test/test_helper'
require 'json'

class ProcessingRequestTest < Minitest::Test
include Rack::Test::Methods

def app
TrafficSpy::Server
end

def setup
DatabaseCleaner.start
end

def teardown
DatabaseCleaner.clean
end

def test_payload_data_equal_json_data
post '/sources', { identifier: "jumpstartlab",
rootUrl: "http://jumpstartlab.com" }
post '/sources/jumpstartlab/data', payload_data

assert_equal 1, TrafficSpy::Source.count
assert_equal 1, TrafficSpy::Payload.count
assert_equal 200, last_response.status
assert_equal "Created Successfully", last_response.body
end

def test_users_receives_400_missing_payload_error
post '/sources', { identifier: "jumpstartlab",
rootUrl: "http://jumpstartlab.com" }
post '/sources/jumpstartlab/data', nil

assert_equal 1, TrafficSpy::Source.count
assert_equal 0, TrafficSpy::Payload.count
assert_equal 400, last_response.status
assert_equal "payload can't be blank", last_response.body
end

def test_two_different_payloads_have_different_hexs
post '/sources', { identifier: "jumpstartlab",
rootUrl: "http://jumpstartlab.com" }
post '/sources/jumpstartlab/data', payload_data

assert_equal 1, TrafficSpy::Payload.count
assert_equal 200, last_response.status
assert_equal "Created Successfully", last_response.body

post '/sources/jumpstartlab/data', payload_data_two

assert_equal 2, TrafficSpy::Payload.count
assert_equal 200, last_response.status
assert_equal "Created Successfully", last_response.body
end

def test_user_receives_403_error_for_duplicate_payload
post '/sources', { identifier: "jumpstartlab",
rootUrl: "http://jumpstartlab.com" }
post '/sources/jumpstartlab/data', payload_data

assert_equal 1, TrafficSpy::Payload.count
assert_equal 200, last_response.status
assert_equal "Created Successfully", last_response.body

post '/sources/jumpstartlab/data', payload_data

assert_equal 403, last_response.status
assert_equal "Hex digest has already been taken", last_response.body
end

def test_user_receives_403_error_for_not_registered
post '/sources/jumpstartlab/data', payload_data

assert_equal 403, last_response.status
assert_equal "Identifier does not exist", last_response.body
end

def test_payload_given_proper_foreign_key
post '/sources', { identifier: "jumpstartlab",
rootUrl: "http://jumpstartlab.com" }
post '/sources', { identifier: "id2",
rootUrl: "http://id2.com" }
post '/sources/jumpstartlab/data', payload_data
post '/sources/id2/data', payload_data_two

assert_equal 2, TrafficSpy::Source.count
assert_equal 2, TrafficSpy::Payload.count

assert_equal 1, TrafficSpy::Payload.first.source_id
#assertion about source identifier

assert_equal 2, TrafficSpy::Payload.last.source_id
end

def payload_data
{"payload" => {"url":"http://jumpstartlab.com/blog",
"requestedAt":"2013-02-16 21:38:28 -0700",
"respondedIn":37,
"referredBy":"http://jumpstartlab.com",
"requestType":"GET",
"parameters":[],
"eventName":"socialLogin",
"userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17",
"resolutionWidth":"1920",
"resolutionHeight":"1280",
"ip":"63.29.38.211" }.to_json }
end

def payload_data_two
{"payload" => {"url":"http://jumpstartlab.com/blog",
"requestedAt":"2013-02-16 21:38:28 -0700",
"respondedIn":666,
"referredBy":"http://jumpstartlab.com",
"requestType":"GET",
"parameters":[],
"eventName":"die with your boots on",
"userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17",
"resolutionWidth":"1920",
"resolutionHeight":"1280",
"ip":"63.29.38.211" }.to_json }
end

end
53 changes: 53 additions & 0 deletions test/controllers/user_registration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require './test/test_helper'

class SourceRegistrationTest < Minitest::Test
include Rack::Test::Methods

def app
TrafficSpy::Server
end

def setup
DatabaseCleaner.start
end

def teardown
DatabaseCleaner.clean
end

def test_user_can_register
post '/sources', { identifier: "jumpstartlab",
rootUrl: "http://jumpstartlab.com" }

assert_equal 1, TrafficSpy::Source.count
assert_equal "{\"identifier\":\"jumpstartlab\"}", last_response.body
assert_equal 200, last_response.status
end

def test_user_receives_400_error_when_missing_identifier
post '/sources', { rootUrl: "http://jumpstartlab.com" }

assert_equal 0, TrafficSpy::Source.count
assert_equal 400, last_response.status
assert_equal "Identifier can't be blank", last_response.body
end

def test_user_receives_400_error_when_missing_rootUrl
post '/sources', { identifier: "jumpstartlab" }

assert_equal 0, TrafficSpy::Source.count
assert_equal 400, last_response.status
assert_equal "Root url can't be blank", last_response.body
end

def test_user_receives_403_error_when_identifier_already_exists
post '/sources', { identifier: "jumpstartlab",
rootUrl: "http://jumpstartlab.com" }
post '/sources', { identifier: "jumpstartlab",
rootUrl: "http://google.com" }

assert_equal 1, TrafficSpy::Source.count
assert_equal 403, last_response.status
assert_equal "Identifier has already been taken", last_response.body
end
end
Loading