Skip to content

Commit

Permalink
starting simple with the builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Burks committed Dec 18, 2009
1 parent fcc87c8 commit 7e2f5ad
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/ews-api.rb
Expand Up @@ -6,6 +6,7 @@
require 'ews/attachment'
require 'ews/message'
require 'ews/folder'
require 'ews/builder'
require 'ews/parser'
require 'ews/service'

Expand Down
10 changes: 10 additions & 0 deletions lib/ews/builder.rb
@@ -0,0 +1,10 @@
module EWS

class Builder
def base_shape!(shape, opts = {})
opts[:base_shape] ||= :Default
shape.add 't:BaseShape', opts[:base_shape]
end
end

end
31 changes: 15 additions & 16 deletions lib/ews/service.rb
Expand Up @@ -27,16 +27,19 @@ def on_after_create_http_request(req)
end

def on_create_document(doc)
# register namespaces for the request
doc.alias 'tns', 'http://schemas.microsoft.com/exchange/services/2006/messages'
doc.alias 't', 'http://schemas.microsoft.com/exchange/services/2006/types'
register_aliases! doc
end

def on_response_document(doc)
apply_namespaces! doc
parser.parse_response_message doc
end


def register_aliases!(doc)
doc.alias 'tns', 'http://schemas.microsoft.com/exchange/services/2006/messages'
doc.alias 't', 'http://schemas.microsoft.com/exchange/services/2006/types'
end

def apply_namespaces!(doc)
doc.add_namespace 'soap', '"http://schemas.xmlsoap.org/soap/envelope'
doc.add_namespace 't', 'http://schemas.microsoft.com/exchange/services/2006/types'
Expand Down Expand Up @@ -84,12 +87,11 @@ def expand_dl!
# FolderShape: +IdOnly, Default, AllProperties+
def find_folder(parent_folder_name = :root, opts = {})
soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/FindFolder'
opts[:base_shape] ||= :Default

response = invoke('tns:FindFolder', soap_action) do |find_folder|
find_folder.set_attr 'Traversal', 'Deep'
find_folder.add('tns:FolderShape') do |shape|
shape.add 't:BaseShape', opts[:base_shape]
builder.base_shape! shape, opts
end
find_folder.add('tns:ParentFolderIds') do |ids|
ids.add('t:DistinguishedFolderId') do |id|
Expand Down Expand Up @@ -120,11 +122,10 @@ def find_folder(parent_folder_name = :root, opts = {})
#
def get_folder(name = :root, opts = {})
soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/GetFolder'
opts[:base_shape] ||= :Default

response = invoke('tns:GetFolder', soap_action) do |get_folder|
get_folder.add('tns:FolderShape') do |shape|
shape.add 't:BaseShape', opts[:base_shape]
builder.base_shape! shape, opts
end
get_folder.add('tns:FolderIds') do |ids|
ids.add('t:DistinguishedFolderId') { |id| id.set_attr 'Id', name }
Expand Down Expand Up @@ -243,12 +244,11 @@ def create_managed_folder!
# BaseShape
def find_item(parent_folder_name = :root, opts = {})
soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/FindItem'
opts[:base_shape] ||= :IdOnly

response = invoke('tns:FindItem', soap_action) do |find_item|
find_item.set_attr 'Traversal', 'Shallow'
find_item.add('tns:ItemShape') do |shape|
shape.add 't:BaseShape', opts[:base_shape]
builder.base_shape! shape, opts
end
find_item.add('tns:ParentFolderIds') do |ids|
ids.add('t:DistinguishedFolderId') do |folder_id|
Expand Down Expand Up @@ -285,11 +285,10 @@ def find_item(parent_folder_name = :root, opts = {})
# ItmeIds
def get_item(item_id, opts = {})
soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/GetItem'
opts[:base_shape] ||= :Default

response = invoke('tns:GetItem', soap_action) do |get_item|
get_item.add('tns:ItemShape') do |shape|
shape.add 't:BaseShape', opts[:base_shape]
builder.base_shape! shape, opts
shape.add 't:IncludeMimeContent', false
end
get_item.add('tns:ItemIds') do |ids|
Expand Down Expand Up @@ -468,9 +467,9 @@ def set_user_oof_settings!
def parser
@parser ||= Parser.new
end
# helpers

# TODO

def builder
@builder ||= Builder.new
end
end
end
31 changes: 31 additions & 0 deletions spec/ews/builder_spec.rb
@@ -0,0 +1,31 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe EWS::Builder do
before(:each) do
@builder = EWS::Builder.new
@doc = Handsoap::XmlMason::Document.new
@doc.xml_header = nil
EWS::Service.register_aliases! @doc
end

context "#build_base_shape!" do
it "should build a BaseShape node with 'Default'" do
@builder.base_shape!(@doc)
@doc.to_s.should == expected_base_shape('Default')
end

it "should build a BaseShape node with 'AllProperties'" do
@builder.base_shape!(@doc, :base_shape => 'AllProperties')
@doc.to_s.should == expected_base_shape('AllProperties')
end

it "should build a BaseShape node with 'IdOnly'" do
@builder.base_shape!(@doc, :base_shape => 'IdOnly')
@doc.to_s.should == expected_base_shape('IdOnly')
end
end

def expected_base_shape(shape)
%Q|<t:BaseShape xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">#{shape}</t:BaseShape>|
end
end

0 comments on commit 7e2f5ad

Please sign in to comment.