Module: Atom::Xml::Parseable::DeclarationMethods
- Defined in:
- lib/atom/xml/parser.rb
Overview
:nodoc:
Instance Method Summary collapse
- #attribute(*names) ⇒ Object
- #element(*names) ⇒ Object
- #elements(*names) ⇒ Object
- #loadable!(&error_handler) ⇒ Object
- #parse(xml) ⇒ Object
Instance Method Details
#attribute(*names) ⇒ Object
247 248 249 250 251 252 |
# File 'lib/atom/xml/parser.rb', line 247 def attribute(*names) names.each do |name| attr_accessor name.to_s.sub(/:/, '_').to_sym self.attributes << name.to_s end end |
#element(*names) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/atom/xml/parser.rb', line 217 def element(*names) = {:type => :single} .merge!(names.pop) if names.last.is_a?(Hash) names.each do |name| attr_accessor name.to_s.sub(/:/, '_').to_sym ns, local_name = name.to_s[/(.*):(.*)/,1], $2 || name self.known_namespaces << self.extensions_namespaces[ns] if ns self.ordered_element_specs << self.element_specs[local_name.to_s] = ParseSpec.new(name, ) end end |
#elements(*names) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/atom/xml/parser.rb', line 229 def elements(*names) = {:type => :collection} .merge!(names.pop) if names.last.is_a?(Hash) names.each do |name| name_sym = name.to_s.sub(/:/, '_').to_sym attr_writer name_sym define_method name_sym do ivar = :"@#{name_sym}" self.instance_variable_set ivar, [] unless self.instance_variable_defined? ivar self.instance_variable_get ivar end ns, local_name = name.to_s[/(.*):(.*)/,1], $2 || name self.known_namespaces << self.extensions_namespaces[ns] if ns self.ordered_element_specs << self.element_specs[local_name.to_s.singularize] = ParseSpec.new(name, ) end end |
#loadable!(&error_handler) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/atom/xml/parser.rb', line 254 def loadable!(&error_handler) class_name = self.name (class << self; self; end).instance_eval do define_method "load_#{class_name.demodulize.downcase}" do |*args| o = args.first opts = args.size > 1 ? args.last : {} xml = case o when String XML::Reader.string(o) when IO XML::Reader.io(o) when URI # raise ArgumentError, "#{class_name}.load only handles http & https URIs" if o.scheme != 'https' || o.scheme != 'http' response = nil http_obj = Net::HTTP.new(o.host, o.port) http_obj.use_ssl = true if o.scheme == 'https' http_obj.start do |http| request = Net::HTTP::Get.new(o.request_uri) if opts[:user] && opts[:pass] request.basic_auth(opts[:user], opts[:pass]) elsif opts[:hmac_access_id] && opts[:hmac_secret_key] if Atom::Configuration.auth_hmac_enabled? AuthHMAC.sign!(request, opts[:hmac_access_id], opts[:hmac_secret_key]) else raise ArgumentError, "AuthHMAC credentials provides by auth-hmac gem is not installed" end end response = http.request(request) end case response when Net::HTTPSuccess XML::Reader.string(response.body) when nil raise ArgumentError.new("nil response to #{o}") else raise Atom::LoadError.new(response) end else raise ArgumentError, "#{class_name}.load needs String, URI or IO, got #{o.class.name}" end if error_handler XML::Error.set_handler(&error_handler) else XML::Error.set_handler do |reader, , severity, base, line| if severity == XML::Reader::SEVERITY_ERROR raise ArgumentError, "#{} at #{line} in #{o}" end end end o = self.new(xml) xml.close o end end end |
#parse(xml) ⇒ Object
315 316 317 |
# File 'lib/atom/xml/parser.rb', line 315 def parse(xml) new(xml) end |