Skip to content
Browse files

segment management api

  • Loading branch information...
1 parent b3327a4 commit 2a1cf104fc4ccd52ffe013d6a41a1cd165e24102 @tpitale tpitale committed Dec 9, 2010
Showing with 76 additions and 2 deletions.
  1. +1 −0 lib/garb.rb
  2. +1 −2 lib/garb/management/account.rb
  3. +27 −0 lib/garb/management/segment.rb
  4. +47 −0 test/unit/garb/management/segment_test.rb
View
1 lib/garb.rb
@@ -33,6 +33,7 @@
# management
require 'garb/management/feed'
+require 'garb/management/segment'
require 'garb/management/account'
require 'garb/management/web_property'
require 'garb/management/profile'
View
3 lib/garb/management/account.rb
@@ -5,8 +5,7 @@ class Account
attr_accessor :id, :title, :name
def self.all(session = Session)
- feed = Feed.new(session, '/accounts') # builds request and parses response
-
+ feed = Feed.new(session, '/accounts')
feed.entries.map {|entry| new_from_entry(entry, session)}
end
View
27 lib/garb/management/segment.rb
@@ -0,0 +1,27 @@
+module Garb
+ module Management
+ class Segment
+ attr_accessor :session, :path
+ attr_accessor :id, :name, :definition
+
+ def self.all(session = Session)
+ feed = Feed.new(session, '/segments')
+ feed.entries.map {|entry| new_from_entry(entry, session)}
+ end
+
+ def self.new_from_entry(entry, session)
+ segment = new
+ segment.session = session
+ segment.path = Garb.parse_link(entry, "self").gsub(Feed::BASE_URL, '')
+ segment.properties = entry['dxp:segment']
+ segment
+ end
+
+ def properties=(properties)
+ self.id = properties['id']
+ self.name = properties['name']
+ self.definition = properties['dxp:definition']
+ end
+ end
+ end
+end
View
47 test/unit/garb/management/segment_test.rb
@@ -0,0 +1,47 @@
+require 'test_helper'
+
+module Garb
+ module Management
+ class SegmentTest < MiniTest::Unit::TestCase
+ context "The Segment class" do
+ should "turn entries for path into array of accounts" do
+ feed = stub(:entries => ["entry1"])
+ Feed.stubs(:new).returns(feed)
+
+ Segment.stubs(:new_from_entry)
+ Segment.all
+
+ assert_received(Feed, :new) {|e| e.with(Session, '/segments')}
+ assert_received(feed, :entries)
+ assert_received(Segment, :new_from_entry) {|e| e.with("entry1", Session)}
+ end
+ end
+
+ context "A Segment" do
+ setup do
+ entry = {
+ "link" => [{"rel" => "self", "href" => Feed::BASE_URL+"/segments/12"}],
+ "dxp:segment" => {
+ "id" => "gaid::-3",
+ "name" => "Returning Visitor",
+ "dxp:definition" => "ga:visitorType==Returning Visitor"
+ }
+ }
+ @segment = Segment.new_from_entry(entry, Session)
+ end
+
+ should "have an id" do
+ assert_equal "gaid::-3", @segment.id
+ end
+
+ should "have a name" do
+ assert_equal "Returning Visitor", @segment.name
+ end
+
+ should "have a definition" do
+ assert_equal "ga:visitorType==Returning Visitor", @segment.definition
+ end
+ end
+ end
+ end
+end

0 comments on commit 2a1cf10

Please sign in to comment.
Something went wrong with that request. Please try again.