Class: SimplerDB::RESTServlet
- Inherits:
-
WEBrick::HTTPServlet::AbstractServlet
- Object
- WEBrick::HTTPServlet::AbstractServlet
- SimplerDB::RESTServlet
- Defined in:
- lib/simplerdb/servlet.rb
Overview
A WEBrick servlet to handle API requests over REST
Instance Method Summary collapse
-
#build_result(action) ⇒ Object
Build a result for the given action.
-
#do_createdomain(req) ⇒ Object
Handle CreateDomain requests.
-
#do_deleteattributes(req) ⇒ Object
DeleteAttributes requests.
-
#do_deletedomain(req) ⇒ Object
DeleteDomains requests.
- #do_GET(req, res) ⇒ Object (also: #do_POST, #do_DELETE)
-
#do_getattributes(req) ⇒ Object
GetAttributes request.
-
#do_listdomains(req) ⇒ Object
Handle ListDomains requests.
-
#do_putattributes(req) ⇒ Object
PutAttributes requests.
-
#do_query(req) ⇒ Object
Query request.
-
#error_xml(code, msg) ⇒ Object
Return the error response for the given error code and message.
- #parse_attribute_args(req) ⇒ Object
Instance Method Details
#build_result(action) ⇒ Object
Build a result for the given action. The given block will add action-specific return fields.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/simplerdb/servlet.rb', line 151 def build_result(action) xml = '' doc = Builder::XmlMarkup.new(:target => xml) doc.tag!("#{action}Response", :xmlns => "http://sdb.amazonaws.com/doc/2007-11-07") do if block_given? yield doc end doc.ResponseMetadata do doc.RequestId "1234" doc.BoxUsage "0" end end xml end |
#do_createdomain(req) ⇒ Object
Handle CreateDomain requests
49 50 51 52 53 |
# File 'lib/simplerdb/servlet.rb', line 49 def do_createdomain(req) name = req.query["DomainName"] DB.instance.create_domain(name) build_result("CreateDomain") end |
#do_deleteattributes(req) ⇒ Object
DeleteAttributes requests
96 97 98 99 100 101 102 |
# File 'lib/simplerdb/servlet.rb', line 96 def do_deleteattributes(req) domain_name = req.query["DomainName"] item_name = req.query["ItemName"] attrs = parse_attribute_args(req) DB.instance.delete_attributes(domain_name, item_name, attrs) build_result("DeleteAttributes") end |
#do_deletedomain(req) ⇒ Object
DeleteDomains requests
80 81 82 83 84 |
# File 'lib/simplerdb/servlet.rb', line 80 def do_deletedomain(req) name = req.query["DomainName"] DB.instance.delete_domain(name) build_result("DeleteDomain") end |
#do_GET(req, res) ⇒ Object Also known as: do_POST, do_DELETE
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/simplerdb/servlet.rb', line 24 def do_GET(req, res) action = req.query["Action"] begin if action.nil? # Raise an error raise ClientException.new(:MissingAction, "No action was supplied with this request") else # Process the action appropriately xml = eval("do_#{action.downcase}(req)") res.body = xml res.status = 200 end rescue ClientException => e res.body = error_xml(e.code, e.msg) res.status = 400 # What is the right status? end res['content-type'] = 'text/xml' end |
#do_getattributes(req) ⇒ Object
GetAttributes request
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/simplerdb/servlet.rb', line 105 def do_getattributes(req) domain_name = req.query["DomainName"] item_name = req.query["ItemName"] attr_name = req.query["AttributeName"] attrs = DB.instance.get_attributes(domain_name, item_name) build_result("GetAttributes") do |doc| doc.GetAttributesResult do if attrs attrs.each do |attr| doc.Attribute do doc.Name attr.name doc.Value attr.value end end end end end end |
#do_listdomains(req) ⇒ Object
Handle ListDomains requests
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/simplerdb/servlet.rb', line 56 def do_listdomains(req) max = req.query["MaxNumberOfDomains"] next_token = req.query["NextToken"] max = max.to_i if max domains,token = DB.instance.list_domains(max, next_token) build_result("ListDomains") do |doc| doc.ListDomainsResult do if domains domains.each do |domain| doc.DomainName domain end end if token doc.NextToken token end end end end |
#do_putattributes(req) ⇒ Object
PutAttributes requests
87 88 89 90 91 92 93 |
# File 'lib/simplerdb/servlet.rb', line 87 def do_putattributes(req) domain_name = req.query["DomainName"] item_name = req.query["ItemName"] attrs = parse_attribute_args(req) DB.instance.put_attributes(domain_name, item_name, attrs) build_result("PutAttributes") end |
#do_query(req) ⇒ Object
Query request
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/simplerdb/servlet.rb', line 126 def do_query(req) domain_name = req.query["DomainName"] max_items = req.query["MaxItems"].to_i max_items = 100 if (max_items < 1 || max_items > 250) next_token = req.query["NextToken"] query = req.query["QueryExpression"] results,token = DB.instance.query(domain_name, query, max_items, next_token) build_result("Query") do |doc| doc.QueryResult do if results results.each do |res| doc.ItemName res.name end end if token doc.NextToken token end end end end |
#error_xml(code, msg) ⇒ Object
Return the error response for the given error code and message
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/simplerdb/servlet.rb', line 169 def error_xml(code, msg) xml = '' doc = Builder::XmlMarkup.new(:target => xml) doc.Response do doc.Errors do doc.Error do doc.Code code.to_s doc.Message msg doc.BoxUsage "0" end end doc.RequestID "1234" end xml end |
#parse_attribute_args(req) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/simplerdb/servlet.rb', line 188 def parse_attribute_args(req) args = [] for i in (0...100) name = req.query["Attribute.#{i}.Name"] value = req.query["Attribute.#{i}.Value"] replace = (req.query["Attribute.#{i}.Replace"] == "true") if name && value args << AttributeParam.new(name, value, replace) end end args end |