Module: Stargate::Operation::RowOperation

Included in:
Client
Defined in:
lib/stargate/operation/row_operation.rb

Constant Summary collapse

Converter =
{
  '&' => '&',
  '<' => '&lt;',
  '>' => '&gt;',
  "'" => '&apos;',
  '"' => '&quot;'
}

Instance Method Summary collapse

Instance Method Details

#create_row(table_name, name, timestamp = nil, columns = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/stargate/operation/row_operation.rb', line 34

def create_row(table_name, name, timestamp = nil, columns = nil)
  begin
    request = Request::RowRequest.new(table_name, name, timestamp)
    data = []
    if columns
      if columns.instance_of? Array
        data = columns
      elsif columns.instance_of? Hash
        data = [columns]
      else
        raise StandardError, "Only Array or Hash data accepted"
      end
    end

    xml_data = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><CellSet>"
    xml_data << "<Row key='#{[name].pack('m') rescue ''}'>"
    data.each do |d|
      escape_name = d[:name].gsub(/[&<>'"]/) { |match| Converter[match] }
      xml_data << "<Cell "
      xml_data << "timestamp='#{timestamp}' " if timestamp
      xml_data << "column='#{[escape_name].pack('m') rescue ''}'>"
      xml_data << "#{[d[:value]].pack("m") rescue ''}"
      xml_data << "</Cell>"
    end
    xml_data << "</Row></CellSet>"

    Response::RowResponse.new(post_response(request.create(data.map{|col| col[:name]}), xml_data), :create_row).parse
  rescue Net::ProtocolError => e
    if e.to_s.include?("Table")
      raise TableNotFoundError, "Table '#{table_name}' Not Found"
    elsif e.to_s.include?("Row")
      raise RowNotFoundError, "Row '#{name}' Not Found"
    else
      raise StandardError, "Error encountered while trying to create row: #{e.message}"
    end
  end
end

#delete_row(table_name, name, timestamp = nil, columns = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/stargate/operation/row_operation.rb', line 72

def delete_row(table_name, name, timestamp = nil, columns = nil)
  begin
    request = Request::RowRequest.new(table_name, name, timestamp)
    Response::RowResponse.new(delete_response(request.delete(columns)), :delete_row).parse
  rescue Net::ProtocolError => e
    if e.to_s.include?("Table")
      raise TableNotFoundError, "Table '#{table_name}' Not Found"
    elsif e.to_s.include?("Row")
      raise RowNotFoundError, "Row '#{name}' Not Found"
    end
  end
end

#row_timestamps(table_name, name) ⇒ Object

Raises:

  • (NotImplementedError)


12
13
14
# File 'lib/stargate/operation/row_operation.rb', line 12

def row_timestamps(table_name, name)
  raise NotImplementedError, "Currently not supported in Stargate client"
end

#show_row(table_name, name, timestamp = nil, columns = nil, options = { }) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/stargate/operation/row_operation.rb', line 16

def show_row(table_name, name, timestamp = nil, columns = nil, options = { })
  begin
    options[:version] ||= 1

    request = Request::RowRequest.new(table_name, name, timestamp)
    row = Response::RowResponse.new(get(request.show(columns, options)), :show_row).parse.first
    row.table_name = table_name
    row
  rescue Net::ProtocolError => e
    # TODO: Use better handling instead of this.
    if e.to_s.include?("Table")
      raise TableNotFoundError, "Table '#{table_name}' Not Found"
    elsif e.to_s.include?("404")
      raise RowNotFoundError, "Row '#{name}' Not Found"
    end
  end
end