Skip to content

Commit

Permalink
Sites can pull events. Events are incomplete and miss specs though.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnall committed Apr 13, 2011
1 parent d491de2 commit 0e44e51
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
31 changes: 27 additions & 4 deletions lib/pwnalytics_client/event.rb
Expand Up @@ -13,16 +13,39 @@ class Event
def initialize(site, json_data)
@site = site


@name = json_data['name']
@url = self.class.parse_url json_data['page']
@ref = self.class.parse_url json_data['page']
if json_data['browser'] && json_data['browser']['time']
@time = Time.at(json_data['browser']['time'] / 1000.0)
else
@time = nil
end
@data = json_data['data']
end

# PwnalyticsClient::Site that the event occured on.
attr_reader :site
# Pwnalytics UID for the site.
attr_reader :uid
# User-friendly name for the site

# Event name used for filtering in queries.
attr_reader :name
# URL of the page where the event was triggerred.
attr_reader :url
# Referer URL for the page where the event was triggerred.
attr_reader :ref
# When the event was triggerred.
attr_reader :time
# User-defined data associated with the event.
attr_reader :data

# Parses an URL out of a Pwnalytics server response.
def self.parse_url(json_data)
if json_data && json_data['url']
URI.parse json_data['url']
else
nil
end
end
end # class PwnalyticsClient::Event

end # namespace PwnalyticsClient
12 changes: 10 additions & 2 deletions spec/lib/pwnalytics_client/site_spec.rb
Expand Up @@ -20,10 +20,18 @@

let(:result) do
client.should_receive(:request).with('/web_properties/AA/events.json?').
and_return([{'uid' => 'AA123456', 'name' => 'Pwnalytics itself'},
{'uid' => 'CDCDCDCD', 'name' => 'Test Property'}])
and_return([{'name' => 'page', 'referrer' => {'url' => 'null'},'data' => {},'pixels' => {'screen' => {'height' => 1440,'width' => 4240},'document' => {'height' => 863,'width' => 'document_width'},'window' => {'x' => 2215,'y' => 181}},'id' => 1,'visitor' => {'uid' => '12f49459540.11faa9b963f5c4f0'},'page' => {'url' => 'http://localhost:3000/'},'ip' => '127.0.0.1', 'browser' => {'ua' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Ubuntu/10.10 Chromium/10.0.648.204 Chrome/10.0.648.204 Safari/534.16', 'time' => 1302682242437}},
{'name' => 'unload', 'referrer' => {'url' => 'null'},'data' => {},'pixels' => {'screen' => {'height' => 1440,'width' => 4240},'document' => {'height' => 863,'width' => 'document_width'},'window' => {'x' => 2215,'y' => 181}},'id' => 2,'visitor' => {'uid' => '12f49459540.11faa9b963f5c4f0'},'page' => {'url' => 'http://localhost:3000/'},'ip' => '127.0.0.1', 'browser' => {'ua' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Ubuntu/10.10 Chromium/10.0.648.204 Chrome/10.0.648.204 Safari/534.16', 'time' => 1302682245565}}])
site.events
end

it 'should map all results to events' do
result.should have(2).events
end

it 'should map events correctly' do
result.first.name.should == 'page'
result.last.name.should == 'unload'
end
end
end

0 comments on commit 0e44e51

Please sign in to comment.