Class: DataMapper::Adapters::RestfulAdapter
- Inherits:
-
BaseAdapter
- Object
- AbstractAdapter
- BaseAdapter
- DataMapper::Adapters::RestfulAdapter
- Includes:
- Slf4r::Logger
- Defined in:
- lib/datamapper4rails/adapters/restful_adapter.rb
Instance Method Summary collapse
- #attributes_to_xml(name, attributes) ⇒ Object
- #create_resource(resource) ⇒ Object
- #delete(query) ⇒ Object
- #delete_resource(resource) ⇒ Object
- #http_delete(uri) ⇒ Object
- #http_get(uri) ⇒ Object
- #http_post(uri, data = nil) ⇒ Object
- #http_put(uri, data = nil) ⇒ Object
- #key_value_from_query(query) ⇒ Object
- #keys_from_query(query) ⇒ Object
- #parse_resource(xml, model, query = nil) ⇒ Object
- #read_resource(query) ⇒ Object
- #read_resources(query) ⇒ Object
- #resource_name_from_model(model) ⇒ Object
- #resource_name_from_query(query) ⇒ Object
- #send_request(&block) ⇒ Object
- #single_entity_query?(query) ⇒ Boolean
- #update(attributes, query) ⇒ Object
- #update_resource(resource, attributes) ⇒ Object
Methods inherited from BaseAdapter
#create, #initialize, #read_many, #read_one, #transaction_primitive
Constructor Details
This class inherits a constructor from DataMapper::Adapters::BaseAdapter
Instance Method Details
#attributes_to_xml(name, attributes) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 53 def attributes_to_xml(name, attributes) xml = "<#{name}>" attributes.each do |attr, val| xml += "<#{attr.name}>#{val}</#{attr.name}>" end xml += "</#{name}>" end |
#create_resource(resource) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 195 def create_resource(resource) name = resource.model.name uri = "/#{name.pluralize}.xml" logger.debug { "post #{uri}" } response = http_post(uri, resource.to_xml ) resource_new = parse_resource(REXML::Document::new(response.body).root, resource.model, ::DataMapper::Query.new(resource.repository, resource.model )) # copy all attributes/associations from the downloaded resource # to the given resource # TODO better pass the given resource into parse_resource resource_new.attributes.each do |key, value| resource.send(:properties)[key].set!(resource, value) end resource_new.send(:relationships).each do |key, value| resource.send("#{key}=".to_sym, resource_new.send(key)) end resource end |
#delete(query) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 281 def delete(query) if query.limit == 1 or single_entity_query?(query) name = resource_name_from_query(query) key = key_value_from_query(query) uri = "/#{name.pluralize}/#{key}.xml" logger.debug { "delete #{uri}" } response = http_delete(uri) response.kind_of?(Net::HTTPSuccess) else super end end |
#delete_resource(resource) ⇒ Object
295 296 297 298 299 300 301 302 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 295 def delete_resource(resource) name = resource.name key = key_value_from_query(resource.to_query) uri = "/#{name.pluralize}/#{key}.xml" logger.debug { "delete #{uri}" } response = http_delete(uri) response.kind_of?(Net::HTTPSuccess) end |
#http_delete(uri) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 95 def http_delete(uri) send_request do |http| request = Net::HTTP::Delete.new(uri) request.basic_auth(@uri[:login], @uri[:password]) unless @uri[:login].blank? http.request(request) end end |
#http_get(uri) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 61 def http_get(uri) send_request do |http| request = Net::HTTP::Get.new(uri) request.basic_auth(@uri[:login], @uri[:password]) unless @uri[:login].blank? http.request(request) end end |
#http_post(uri, data = nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 70 def http_post(uri, data = nil) send_request do |http| request = Net::HTTP::Post.new(uri, { 'content-type' => 'application/xml', 'content-length' => data.length.to_s }) request.basic_auth(@uri[:login], @uri[:password]) unless @uri[:login].blank? http.request(request, data) end end |
#http_put(uri, data = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 82 def http_put(uri, data = nil) send_request do |http| request = Net::HTTP::Put.new(uri, { 'content-type' => 'application/xml', 'content-length' => data.length.to_s }) request.basic_auth(@uri[:login], @uri[:password]) unless @uri[:login].blank? # request.set_form_data(data) http.request(request, data) end end |
#key_value_from_query(query) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 34 def key_value_from_query(query) keys = keys_from_query(query) logger.debug { "keys=#{keys.inspect}" } if keys.size == 1 key = keys[0] # return the third element of the condition array # which belongs to the key query.conditions.detect do |c| c[1] == key end[2] else raise "compound keys are not supported" end end |
#keys_from_query(query) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 21 def keys_from_query(query) keys = query.model.key # work around strange missing of properties in model # but the query has still the fields :P if keys.size == 0 query.fields.select do |f| f.key? end else keys end end |
#parse_resource(xml, model, query = nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 113 def parse_resource(xml, model, query = nil) elements = {} associations = {} many_to_many = {} xml.elements.collect do |element| if element.text.nil? if element.attributes['type'] == 'array' many_to_many[element.name.gsub('-','_').to_sym] = element else associations[element.name.gsub('-','_').to_sym] = element end else elements[element.name.gsub('-','_').to_sym] = element.text end end #puts #puts "elements" #p elements #p query #p model resource = model.load(model.properties.collect do |f| elements[f.name] end, query) resource.send("#{keys_from_query(query)[0].name}=".to_sym, elements[keys_from_query(query)[0].name] ) # p resource #p associations associations.each do |name, association| # puts "asso" # p model # p name # p association #p model.relationships is_one_to_one = false asso_model = if rel = model.relationships[name] #puts "rel" #p rel if rel.child_model == model rel.parent_model else rel.child_model end # else #::Extlib::Inflection.constantize(::Extlib::Inflection.classify(name)) # model.find_const(::Extlib::Inflection.classify(name)) end # p asso_model if resource.respond_to? "#{name}=".to_sym # puts # puts "association" # puts name # p model # p asso_model resource.send("#{name}=".to_sym, parse_resource(association, asso_model, ::DataMapper::Query.new(query.repository, asso_model ))) unless asso_model.nil? else resource.send(("#{name.to_s.pluralize}<" + "<").to_sym, parse_resource(association, asso_model, ::DataMapper::Query.new(query.repository, asso_model ))) unless asso_model.nil? end end #puts "many 2 many" #p many_to_many many_to_many.each do |name, many| if model.relationships[name] # TODO else # p ::Extlib::Inflection.classify(name.to_s.singularize) many_model = Object.const_get(::Extlib::Inflection.classify(name.to_s.singularize)) resource.send(name).send(("<" + "<").to_sym, parse_resource(many, many_model, ::DataMapper::Query.new(query.repository, many_model ))) unless many_model.nil? end end resource.instance_variable_set(:@new_record, false) #p resource resource end |
#read_resource(query) ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 218 def read_resource(query) key = key_value_from_query(query) uri = "/#{resource_name_from_query(query).pluralize}/#{key}.xml" logger.debug { "get #{uri}" } response = http_get(uri) if response.kind_of?(Net::HTTPSuccess) logger.debug { response.body.to_s } parse_resource(REXML::Document::new(response.body).root, query.model, query) else #TODO may act on different response codes differently end end |
#read_resources(query) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 234 def read_resources(query) if single_entity_query?(query) [read_resource(query)] else uri = "/#{resource_name_from_query(query).pluralize}.xml" logger.debug { "get #{uri}" } response = http_get(uri) if response.kind_of?(Net::HTTPSuccess) result = [] logger.debug { response.body.to_s } REXML::Document::new(response.body).root.each do |element| result << parse_resource(element, query.model, query) end result else #TODO may act on different response codes differently end end end |
#resource_name_from_model(model) ⇒ Object
13 14 15 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 13 def resource_name_from_model(model) ::Extlib::Inflection.underscore(model.name) end |
#resource_name_from_query(query) ⇒ Object
17 18 19 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 17 def resource_name_from_query(query) resource_name_from_model(query.model) end |
#send_request(&block) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 104 def send_request(&block) res = nil Net::HTTP.start(@uri[:host], @uri[:port].to_i) do |http| res = yield(http) end logger.debug { "response=" + res.code } res end |
#single_entity_query?(query) ⇒ Boolean
49 50 51 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 49 def single_entity_query?(query) query.conditions.count {|c| c[1].key? and c[0] == :eql} == query.model.key.size end |
#update(attributes, query) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 257 def update(attributes, query) if query.limit == 1 or single_entity_query?(query) xml = attributes_to_xml(resource_name_from_query(query), attributes) key = key_value_from_query(query) uri = "/#{resource_name_from_query(query).pluralize}/#{key}.xml" logger.debug { "put #{uri}" } response = http_put(uri, xml) response.kind_of?(Net::HTTPSuccess) else super end end |
#update_resource(resource, attributes) ⇒ Object
271 272 273 274 275 276 277 278 |
# File 'lib/datamapper4rails/adapters/restful_adapter.rb', line 271 def update_resource(resource, attributes) query = resource.to_query xml = attributes_to_xml(resource.name, attributes) key = key_value_from_query(query) logger.debug {resource.to_xml} response = http_put("/#{resource_name_from_query(query).pluralize}/#{key}.xml", xml) response.kind_of?(Net::HTTPSuccess) end |