Class WAZ::Tables::Service
In: lib/waz/tables/service.rb
Parent: Object

This is internally used by the waz-tables part of the gem and it exposes the Windows Azure Blob API REST methods implementation. You can use this class to perform an specific operation that isn‘t provided by the current API.

Methods

Included Modules

WAZ::Storage::SharedKeyCoreService

Constants

DATASERVICES_NAMESPACE = "http://schemas.microsoft.com/ado/2007/08/dataservices"
DATASERVICES_METADATA_NAMESPACE = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"

Public Instance methods

Creates a table on the current Windows Azure Storage account.

[Source]

    # File lib/waz/tables/service.rb, line 12
12:       def create_table(table_name)
13:         raise WAZ::Tables::InvalidTableName, table_name unless WAZ::Storage::ValidationRules.valid_table_name?(table_name)
14: 
15:         payload = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" \
16:                   "<entry xmlns:d=\"#{DATASERVICES_NAMESPACE}\" xmlns:m=\"#{DATASERVICES_METADATA_NAMESPACE}\" xmlns=\"http://www.w3.org/2005/Atom\">" \
17:                   "<title /><updated>#{Time.now.utc.iso8601}</updated><author><name/></author><id/>" \
18:                   "<content type=\"application/xml\"><m:properties><d:TableName>#{table_name}</d:TableName></m:properties></content></entry>"
19: 
20:         begin
21:           execute :post, 'Tables', {}, default_headers, payload
22:           return {:name => table_name, :url => "#{self.base_url}/Tables('#{table_name}"}
23:         rescue RestClient::RequestFailed
24:           raise WAZ::Tables::TableAlreadyExists, table_name if $!.http_code == 409
25:         end
26:       end

Delete an existing entity in a table.

[Source]

     # File lib/waz/tables/service.rb, line 102
102:       def delete_entity(table_name, partition_key, row_key)
103:         raise WAZ::Tables::InvalidTableName, table_name unless WAZ::Storage::ValidationRules.valid_table_name?(table_name)
104:         
105:         begin
106:           execute :delete, "#{table_name}(PartitionKey='#{partition_key}',RowKey='#{row_key}')", {}, default_headers.merge({'If-Match' => '*'})
107:         rescue RestClient::ResourceNotFound
108:           raise WAZ::Tables::TableDoesNotExist, table_name if $!.http_code == 404 and $!.response.body.include?('TableNotFound')
109:           raise WAZ::Tables::EntityDoesNotExist, "(PartitionKey='#{partition_key}',RowKey='#{row_key}')" if $!.http_code == 404
110:         end
111:       end

Delete a table on the current Windows Azure Storage account.

[Source]

    # File lib/waz/tables/service.rb, line 29
29:       def delete_table(table_name)
30:         raise WAZ::Tables::InvalidTableName, table_name unless WAZ::Storage::ValidationRules.valid_table_name?(table_name)
31:         begin
32:           execute :delete, "Tables('#{table_name}')", {}, default_headers
33:         rescue RestClient::ResourceNotFound
34:           raise WAZ::Tables::TableDoesNotExist, table_name if $!.http_code == 404
35:         end
36:       end

Retrieves an existing entity on the current storage account. TODO: handle specific errors

[Source]

     # File lib/waz/tables/service.rb, line 115
115:       def get_entity(table_name, partition_key, row_key)
116:         raise WAZ::Tables::InvalidTableName, table_name unless WAZ::Storage::ValidationRules.valid_table_name?(table_name)
117:         response = execute(:get, "#{table_name}(PartitionKey='#{partition_key}',RowKey='#{row_key}')", {}, default_headers)
118:         return parse_response(response)
119:       end

Retrieves an existing table on the current storage account. remove Content-Type if it‘s not working

[Source]

    # File lib/waz/tables/service.rb, line 55
55:       def get_table(table_name)
56:         raise WAZ::Tables::InvalidTableName, table_name unless WAZ::Storage::ValidationRules.valid_table_name?(table_name)        
57:         
58:         begin
59:           content = execute :get, "Tables('#{table_name}')", {}, default_headers
60:           doc = REXML::Document.new(content)
61:           item = REXML::XPath.first(doc, "entry")
62:           return {  :name => REXML::XPath.first(item.elements['content'], "m:properties/d:TableName", {"m" => DATASERVICES_METADATA_NAMESPACE, "d" => DATASERVICES_NAMESPACE}).text,
63:                     :url => REXML::XPath.first(item, "id").text }
64:         rescue RestClient::ResourceNotFound
65:           raise WAZ::Tables::TableDoesNotExist, table_name if $!.http_code == 404
66:         end        
67:       end

Insert a new entity on the provided table for the current storage account TODO: catch all api errors as described on Table Service Error Codes on MSDN (msdn.microsoft.com/en-us/library/dd179438.aspx)

[Source]

    # File lib/waz/tables/service.rb, line 71
71:       def insert_entity(table_name, entity)
72:         raise WAZ::Tables::InvalidTableName, table_name unless WAZ::Storage::ValidationRules.valid_table_name?(table_name)
73:         raise WAZ::Tables::TooManyProperties, entity.length if entity.length > 252 
74:         
75:         begin
76:           response = execute(:post, table_name, {}, default_headers, generate_payload(table_name, entity))
77:           return parse_response(response)          
78:         rescue RestClient::RequestFailed          
79:           raise WAZ::Tables::EntityAlreadyExists, entity[:row_key] if $!.http_code == 409 and $!.response.body.include?('EntityAlreadyExists')          
80:         end     
81:       end

Lists all existing tables on the current storage account. remove Content-Type if it‘s not working

[Source]

    # File lib/waz/tables/service.rb, line 40
40:       def list_tables(next_table_name = nil)
41:         query = { 'NextTableName' => next_table_name } unless next_table_name.nil?
42:         content = execute :get, "Tables", query ||= {}, default_headers
43: 
44:         doc = REXML::Document.new(content)
45:         tables = REXML::XPath.each(doc, '/feed/entry').map do |item|
46:             { :name => REXML::XPath.first(item.elements['content'], "m:properties/d:TableName", {"m" => DATASERVICES_METADATA_NAMESPACE, "d" => DATASERVICES_NAMESPACE}).text,
47:               :url => REXML::XPath.first(item, "id").text }
48:         end
49:         
50:         return tables, content.headers[:x_ms_continuation_nexttablename]
51:       end

Merge an existing entity on the current storage account. The Merge Entity operation updates an existing entity by updating the entity‘s properties. This operation does not replace the existing entity, as the Update Entity operation does TODO: handle specific errors

[Source]

    # File lib/waz/tables/service.rb, line 95
95:       def merge_entity(table_name, entity)
96:         raise WAZ::Tables::InvalidTableName, table_name unless WAZ::Storage::ValidationRules.valid_table_name?(table_name)
97:         response = execute(:merge, "#{table_name}(PartitionKey='#{entity[:partition_key]}',RowKey='#{entity[:row_key]}')", {}, default_headers.merge({'If-Match' => '*'}), generate_payload(table_name, entity))
98:         return parse_response(response)
99:       end

Retrieves a set of entities on the current storage account for a given query. When the :top => n is passed it returns only the first n rows that match with the query Optional parameters:

[Source]

     # File lib/waz/tables/service.rb, line 128
128:       def query(table_name, options = {})
129:         raise WAZ::Tables::InvalidTableName, table_name unless WAZ::Storage::ValidationRules.valid_table_name?(table_name)
130:         query = {'$filter' => (options[:expression] or '') }
131:         query.merge!({ '$top' => options[:top] }) unless options[:top].nil?
132:         query.merge!(options[:continuation_token]) unless options[:continuation_token].nil?
133:         response = execute :get, "#{table_name}()", query, default_headers
134:         continuation_token = {'NextPartitionKey' => response.headers[:x_ms_continuation_nextpartitionkey], 'NextRowKey' => response.headers[:x_ms_continuation_nextrowkey]}
135:         parse_response(response, continuation_token)
136:       end

Update an existing entity on the current storage account. TODO: handle specific errors

[Source]

    # File lib/waz/tables/service.rb, line 85
85:       def update_entity(table_name, entity)
86:         raise WAZ::Tables::InvalidTableName, table_name unless WAZ::Storage::ValidationRules.valid_table_name?(table_name)
87:         response = execute(:put, "#{table_name}(PartitionKey='#{entity[:partition_key]}',RowKey='#{entity[:row_key]}')", {}, default_headers.merge({'If-Match' => '*'}) , generate_payload(table_name, entity))
88:         return parse_response(response)
89:       end

[Validate]