Skip to content

Commit

Permalink
implementing Retrieve
Browse files Browse the repository at this point in the history
implementing Filter, SimpleFilterPart, and ComplexFilterPart
implementing new Result superclass that makes all properties available
adding unit tests
  • Loading branch information
David Dawson committed Feb 16, 2012
1 parent 7010234 commit 5ea5ef7
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 45 deletions.
6 changes: 3 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ outlined in the guide linked above are used. This is done in an attempt to be
as transparent as possible, so that the API may be used by referring only to
the guide linked above.

Note this SDK is currently very incomplete, allowing you only to create and
update subscribers and trigger sends. The framework is in place, however, to
very easily implement new objects by simply declaring their properties.
Note this SDK is currently quite incomplete, supporting only a subset of the
possible objects and methods. The framework is in place, however, to very
easily implement new objects by simply declaring their properties.

== Synopsis:

Expand Down
6 changes: 6 additions & 0 deletions lib/exact_target_sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ module ExactTargetSDK
autoload :APIObject, 'exact_target_sdk/api_object'
autoload :Attribute, 'exact_target_sdk/attribute'
autoload :Client, 'exact_target_sdk/client'
autoload :ComplexFilterPart, 'exact_target_sdk/complex_filter_part'
autoload :CreateResponse, 'exact_target_sdk/create_response'
autoload :CreateResult, 'exact_target_sdk/create_result'
autoload :FilterPart, 'exact_target_sdk/filter_part'
autoload :Result, 'exact_target_sdk/result'
autoload :RetrieveResponse, 'exact_target_sdk/retrieve_response'
autoload :RetrieveResult, 'exact_target_sdk/retrieve_result'
autoload :SimpleFilterPart, 'exact_target_sdk/simple_filter_part'
autoload :Subscriber, 'exact_target_sdk/subscriber'
autoload :TriggeredSend, 'exact_target_sdk/triggered_send'
autoload :TriggeredSendDefinition, 'exact_target_sdk/triggered_send_definition'
Expand Down
8 changes: 6 additions & 2 deletions lib/exact_target_sdk/api_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def properties
@properties || []
end

def type_name
name.split('::').last
end

private

# Stores the given property name to be used at render time.
Expand All @@ -93,7 +97,7 @@ def initialize(properties = {})
#
# May be overridden.
def type_name
self.class.name.split('::').last
self.class.type_name
end

# By default, runs validation and executes #render_properties!.
Expand All @@ -120,7 +124,7 @@ def render_properties!(xml)

def render_property!(property_name, property_value, xml)
if property_value.is_a?(APIObject)
xml.__send__(property_name) do
xml.__send__(property_name, { "xsi:type" => property_value.type_name } ) do
property_value.render!(xml)
end
elsif property_value.is_a?(Array)
Expand Down
39 changes: 39 additions & 0 deletions lib/exact_target_sdk/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,45 @@ def Create(*args)
CreateResponse.new(response)
end

# Invokes the Retrieve method.
#
# Note that this implementation abstracts away the useless RetrieveRequest
# sub-wrapper, and introduces a RequestResponse wrapper to contain the
# output parameters.
#
# Does not currently support requests that have too many results for one
# batch.
#
# Possible exceptions are:
# HTTPError if an HTTP error (such as a timeout) occurs
# SOAPFault if a SOAP fault occurs
# Timeout if there is a timeout waiting for the response
# InvalidAPIObject if any of the provided objects don't pass validation
#
# Returns a RetrieveResponse object.
def Retrieve(object_type_name, filter, *properties)
object_type_name = object_type_name.type_name if object_type_name.respond_to?(:type_name)
response = execute_request 'Retrieve' do |xml|
xml.RetrieveRequestMsg do
xml.RetrieveRequest do
xml.Options

xml.ObjectType object_type_name

properties.each do |property|
xml.Properties(property)
end

xml.Filter "xsi:type" => filter.type_name do
filter.render!(xml)
end
end
end
end

RetrieveResponse.new(response)
end

# Invokes the Update method.
#
# The provided arguments should each be sub-classes of APIObject, and each
Expand Down
9 changes: 9 additions & 0 deletions lib/exact_target_sdk/complex_filter_part.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module ExactTargetSDK
class ComplexFilterPart < FilterPart

property 'LeftOperand', true
property 'LogicalOperator', true
property 'RightOperand', true

end
end
21 changes: 1 addition & 20 deletions lib/exact_target_sdk/create_result.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
require 'active_support/inflector'

module ExactTargetSDK
class CreateResult

PROPERTIES = [
'StatusCode',
'StatusMessage',
'ErrorCode'
]

PROPERTIES.each do |property|
attr_reader property
end

def initialize(hash)
PROPERTIES.each do |property|
instance_variable_set("@#{property}", hash[property.underscore.to_sym])
end
end

class CreateResult < Result
end
end
6 changes: 6 additions & 0 deletions lib/exact_target_sdk/filter_part.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module ExactTargetSDK

class FilterPart < APIObject
end

end
15 changes: 15 additions & 0 deletions lib/exact_target_sdk/result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'active_support/inflector'

module ExactTargetSDK
class Result

def initialize(hash)
@result = hash
end

def method_missing(symbol, *args)
@result[symbol.to_s.underscore.to_sym]
end

end
end
25 changes: 25 additions & 0 deletions lib/exact_target_sdk/retrieve_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module ExactTargetSDK
class RetrieveResponse

attr_reader :OverallStatus, :RequestID, :Results

def initialize(response)
response = response.to_hash[:retrieve_response_msg]
@OverallStatus = response[:overall_status]
@RequestID = response[:request_id]
@Results = []

results = if response[:results].is_a? Array
response[:results]
elsif response[:results].is_a? Hash
[ response[:results] ]
else
[]
end
results.each do |result|
@Results << RetrieveResult.new(result)
end
end

end
end
4 changes: 4 additions & 0 deletions lib/exact_target_sdk/retrieve_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ExactTargetSDK
class RetrieveResult < Result
end
end
9 changes: 9 additions & 0 deletions lib/exact_target_sdk/simple_filter_part.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module ExactTargetSDK
class SimpleFilterPart < FilterPart

property 'Property', true
property 'SimpleOperator', true
array_property 'Value'

end
end
21 changes: 1 addition & 20 deletions lib/exact_target_sdk/update_result.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
require 'active_support/inflector'

module ExactTargetSDK
class UpdateResult

PROPERTIES = [
'StatusCode',
'StatusMessage',
'ErrorCode'
]

PROPERTIES.each do |property|
attr_reader property
end

def initialize(hash)
PROPERTIES.each do |property|
instance_variable_set("@#{property}", hash[property.underscore.to_sym])
end
end

class UpdateResult < Result
end
end
25 changes: 25 additions & 0 deletions spec/result_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

include ExactTargetSDK

describe Result do

context 'a result initialized with OverallStatus and RequestID' do

subject { Result.new(:overall_status => "OK", :request_id => "abc") }

it 'should respond to OverallStatus with "OK"' do
subject.OverallStatus.should == "OK"
end

it 'should respond to RequestID with "abc"' do
subject.RequestID.should == "abc"
end

it 'should respond to Junk with nil' do
subject.Junk.should_not be
end

end

end

0 comments on commit 5ea5ef7

Please sign in to comment.