Skip to content

Commit

Permalink
Added segments and segment criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
SFEley committed Aug 11, 2009
1 parent 43a8f8c commit a92bd1a
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 14 deletions.
11 changes: 9 additions & 2 deletions acts_as_icontact.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

Gem::Specification.new do |s|
s.name = %q{acts_as_icontact}
s.version = "0.4.0"
s.version = "0.4.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Stephen Eley"]
s.date = %q{2009-08-10}
s.date = %q{2009-08-11}
s.default_executable = %q{icontact}
s.description = %q{ActsAsIcontact connects Ruby applications with the iContact e-mail marketing service using the iContact API v2.0. Building on the RestClient gem, it offers two significant feature sets:
Expand Down Expand Up @@ -42,6 +42,7 @@ Gem::Specification.new do |s|
"lib/acts_as_icontact/rails/lists.rb",
"lib/acts_as_icontact/rails/macro.rb",
"lib/acts_as_icontact/rails/mappings.rb",
"lib/acts_as_icontact/readonly.rb",
"lib/acts_as_icontact/resource.rb",
"lib/acts_as_icontact/resource_collection.rb",
"lib/acts_as_icontact/resources/account.rb",
Expand All @@ -52,7 +53,12 @@ Gem::Specification.new do |s|
"lib/acts_as_icontact/resources/custom_field.rb",
"lib/acts_as_icontact/resources/list.rb",
"lib/acts_as_icontact/resources/message.rb",
"lib/acts_as_icontact/resources/message_bounces.rb",
"lib/acts_as_icontact/resources/message_clicks.rb",
"lib/acts_as_icontact/resources/message_opens.rb",
"lib/acts_as_icontact/resources/message_statistics.rb",
"lib/acts_as_icontact/resources/subscription.rb",
"lib/acts_as_icontact/subresource.rb",
"rails/init.rb",
"spec/config_spec.rb",
"spec/connection_spec.rb",
Expand Down Expand Up @@ -100,6 +106,7 @@ Gem::Specification.new do |s|
"spec/resources/custom_field_spec.rb",
"spec/resources/list_spec.rb",
"spec/resources/message_spec.rb",
"spec/resources/segment_spec.rb",
"spec/resources/subscription_spec.rb",
"spec/spec_helper.rb",
"spec/support/active_record/connection_adapters/nulldb_adapter.rb",
Expand Down
35 changes: 35 additions & 0 deletions lib/acts_as_icontact/resources/segment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module ActsAsIcontact
class Segment < Resource

# Name and listId are required
def self.required_on_create
super << 'listId' << 'name'
end

# Name and listId are required
def self.required_on_update
super << 'name'
end

# Cannot pass listId when updating
def self.never_on_update
['listId']
end

# Searches on segment name.
def self.find_by_string(value)
first(:name => value)
end


# Returns the list to which this segment is bound.
def list
@list ||= ActsAsIcontact::List.find(listId.to_i) if (listId.to_i) > 0
end

# Returns a collection of SegmentCriteria resources for this segment. The usual iContact search options (limit, offset, search terms, etc.) can be passed.
def criteria(options={})
@criteria ||= ActsAsIcontact::SegmentCriteria.scoped_find(self, options)
end
end
end
36 changes: 36 additions & 0 deletions lib/acts_as_icontact/resources/segment_criteria.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module ActsAsIcontact
# The list of criteria attached to every Segment. Because of this intrinsic association, the usual #find methods don't
# work; this collection _must_ be obtained using the individual segment's #criteria method.
# Unlike other subresources, SegmentCriteria can be created, modified, and saved.
class SegmentCriteria < Subresource

alias_method :segment, :parent

# fieldName, operator, and values are required
def self.required_on_create
super + %w(fieldName operator values)
end

# Looks like 'criteria' is just a bit too strange for ActiveSupport to know singulars and plurals...
def self.resource_name # :nodoc:
"criterion"
end

def self.collection_name # :nodoc:
"criteria"
end

# Uses criterionId as its ID.
def id
properties["criterionId"]
end


# operator must be one: eq, lt, lte, gt, gte, bet, notcontains, contains
def validate_on_save(fields)
operators = %w(eq lt lte gt gte bet notcontains contains)
raise ActsAsIcontact::ValidationError, "operator must be one of: " + operators.join(', ') unless operators.include?(fields["operator"])
end

end
end
5 changes: 0 additions & 5 deletions spec/resources/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
ActsAsIcontact.account.should be_a_kind_of(RestClient::Resource)
end

it "builds upon the 'connection' object" do
ActsAsIcontact.expects(:connection).returns(ActsAsIcontact.instance_variable_get(:@connection))
ActsAsIcontact.account.should_not be_nil
end

it "can be cleared with the reset_account! method" do
ActsAsIcontact.reset_account!
ActsAsIcontact.instance_variable_get(:@account).should be_nil
Expand Down
7 changes: 1 addition & 6 deletions spec/resources/clientfolder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@
it "returns a RestClient resource" do
ActsAsIcontact.clientfolder.should be_a_kind_of(RestClient::Resource)
end

it "builds upon the 'account' object" do
ActsAsIcontact.expects(:account).returns(ActsAsIcontact.instance_variable_get(:@account))
ActsAsIcontact.clientfolder.should_not be_nil
end


it "can be cleared with the reset_account! method" do
ActsAsIcontact.reset_clientfolder!
ActsAsIcontact.instance_variable_get(:@clientfolder).should be_nil
Expand Down
31 changes: 31 additions & 0 deletions spec/resources/segment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe ActsAsIcontact::Segment do


it "requires a listId" do
s = ActsAsIcontact::Segment.new
lambda{s.save}.should raise_error(ActsAsIcontact::ValidationError, /listId/)
end

it "requires a name" do
s = ActsAsIcontact::Segment.new
lambda{s.save}.should raise_error(ActsAsIcontact::ValidationError, /name/)
end

context "associations" do
# We have _one_ really good segment set up here
before(:each) do
@segment = ActsAsIcontact::Segment.find("People Named John")
end

it "knows its list" do
@segment.list.name.should == "First Test"
end

it "knows its criteria" do
@segment.criteria.first.fieldName.should == "firstName"
end
end

end
8 changes: 7 additions & 1 deletion spec/support/spec_fakeweb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,10 @@

# Campaign
FakeWeb.register_uri(:get, "#{ic}/campaigns?limit=1", :body => %q<{"campaigns":[{"campaignId":"777777","name":"Test Campaign","fromName":"Bob Smith","fromEmail":"bob@example.org","forwardToFriend":0,"subscriptionManagement":1,"clickTrackMode":1,"useAccountAddress":0,"archiveByDefault":0,"description":""}],"count":1}>)
FakeWeb.register_uri(:get, "#{ic}/campaigns/777777", :body => %q<{"campaign":{"campaignId":"777777","name":"Test Campaign","fromName":"Bob Smith","fromEmail":"bob@example.org","forwardToFriend":0,"subscriptionManagement":1,"clickTrackMode":1,"useAccountAddress":0,"archiveByDefault":0,"description":""}}>)
FakeWeb.register_uri(:get, "#{ic}/campaigns/777777", :body => %q<{"campaign":{"campaignId":"777777","name":"Test Campaign","fromName":"Bob Smith","fromEmail":"bob@example.org","forwardToFriend":0,"subscriptionManagement":1,"clickTrackMode":1,"useAccountAddress":0,"archiveByDefault":0,"description":""}}>)

# Segment
FakeWeb.register_uri(:get, "#{ic}/segments?limit=1&name=People%20Named%20John", :body => %q<{"segments":[{"segmentId":"888888","listId":"444444","name":"People Named John","description":"All the people named John"}],"total":1,"limit":1,"offset":1}>)

# Segment Criteria
FakeWeb.register_uri(:get, "#{ic}/segments/888888/criteria?limit=500", :body => %q<{"criteria":[{"criterionId":0,"fieldName":"firstName","operator":"eq","values":["John"]}]}>)

0 comments on commit a92bd1a

Please sign in to comment.