lib/rews/item.rb in rews-0.1.0 vs lib/rews/item.rb in rews-0.2.0

- old
+ new

@@ -18,61 +18,89 @@ items = get_item_response_messages.map do |girm| read_items(client, girm[:items]) end.flatten end + # represents an Item from a mailbox on an Exchange server class Item attr_reader :client attr_reader :item_id attr_reader :item_class attr_reader :attributes def initialize(client, item_class, attributes) + @client = client @item_id = ItemId.new(client, attributes[:item_id]) @item_class = item_class @attributes = attributes end + def ==(other) + other.is_a?(Item) && + @client == other.client && + @item_id == other.item_id && + @item_class == other.item_class && + @attributes == other.attributes + end + + # access the Item attributes def [](key) @attributes[key] end - + + # names of the Item attributes def keys @attributes.keys end + + def inspect + "#<#{self.class} @item_class=#{@item_class}, @item_id=#{@folder_id.inspect}, @attributes=#{@attributes.inspect}>" + end end + # identifies an Item class ItemId include Util attr_reader :client + + # the +Id+ of the Item attr_reader :id + + # the version of the Item attr_reader :change_key def initialize(client, item_id) @client=client @id = item_id[:id] @change_key=item_id[:change_key] raise "no id" if !@id end + def ==(other) + @client == other.client && + @id == other.id && + @change_key == other.change_key + end + GET_ITEM_OPTS = { :item_shape=>Shape::ITEM_SHAPE_OPTS, :ignore_change_keys=>nil } + # get the <tt>Item::Item</tt> identified by this <tt>Item::ItemId</tt> def get_item(opts={}) opts = check_opts(GET_ITEM_OPTS, opts) r = with_error_check(client, :get_item_response,:response_messages,:get_item_response_message) do client.request(:wsdl, "GetItem") do soap.namespaces["xmlns:t"]=SCHEMA_TYPES xml = Builder::XmlMarkup.new xml << Shape::ItemShape.new(opts[:item_shape]||{}).to_xml xml.wsdl :ItemIds do - xml << Gyoku.xml(self.to_xml_hash(opts[:ignore_change_keys])) + xml << self.to_xml(opts[:ignore_change_keys]) end soap.body = xml.target! end end @@ -82,46 +110,38 @@ DELETE_ITEM_OPTS = { :delete_type! =>nil, :ignore_change_keys=>false } + # delete the Item from the server def delete_item(opts={}) opts = check_opts(DELETE_ITEM_OPTS, opts) r = with_error_check(client, :delete_item_response, :response_messages, :delete_item_response_message) do client.request(:wsdl, "DeleteItem", :DeleteType=>opts[:delete_type]) do soap.namespaces["xmlns:t"]=SCHEMA_TYPES xml = Builder::XmlMarkup.new xml.wsdl :ItemIds do - xml << Gyoku.xml(self.to_xml_hash(opts[:ignore_change_keys])) + xml << self.to_xml(opts[:ignore_change_keys]) end soap.body = xml.target! end end true end - def to_xml_hash(ignore_change_key=false) - if change_key && !ignore_change_key - { - "t:ItemId"=>"", - :attributes! => { - "t:ItemId" => { - "Id" => id.to_s, - "ChangeKey" => change_key.to_s}}} - else - { - "t:ItemId"=>"", - :attributes! => { - "t:ItemId" => { - "Id" => id.to_s}}} - end + def to_xml(ignore_change_key=false) + xml = Builder::XmlMarkup.new + attrs = {:Id=>id.to_s} + attrs[:ChangeKey] = change_key.to_s if change_key && !ignore_change_key + xml.t :ItemId, attrs + xml.target! end def inspect - "#{self.class}(id: #{id}, change_key: #{change_key})" + "#<#{self.class} @id=#{id}, @change_key=#{change_key}>" end end end end