Module: Atom::Xml::Parseable::DeclarationMethods

Defined in:
lib/atom/xml/parser.rb

Overview

:nodoc:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) base_uri

Returns the value of attribute base_uri



273
274
275
# File 'lib/atom/xml/parser.rb', line 273

def base_uri
  @base_uri
end

Instance Method Details

- (Object) attribute(*names)



265
266
267
268
269
270
# File 'lib/atom/xml/parser.rb', line 265

def attribute(*names)
  names.each do |name|
    attr_accessor name.to_s.sub(/:/, '_').to_sym
    self.attributes << name.to_s
  end
end

- (Object) element(*names)



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/atom/xml/parser.rb', line 235

def element(*names)
  options = {:type => :single}
  options.merge!(names.pop) if names.last.is_a?(Hash) 

  names.each do |name|
    attr_accessor Atom.to_attrname(name)
    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, options)
  end
end

- (Object) elements(*names)



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/atom/xml/parser.rb', line 247

def elements(*names)
  options = {:type => :collection}
  options.merge!(names.pop) if names.last.is_a?(Hash)

  names.each do |name|
    name_sym = Atom.to_attrname(name)
    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, options)
  end
end

- (Object) loadable!(&error_handler)



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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/atom/xml/parser.rb', line 280

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 URIs" if o.scheme != 'http'
          response = nil
          Net::HTTP.start(o.host, o.port) 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, message, severity, base, line|
            if severity == XML::Reader::SEVERITY_ERROR
              raise ArgumentError, "#{message} at #{line} in #{o}"
            end
          end
        end
        
        o = self.new(xml)
        xml.close
        o
    end
  end
end

- (Object) parse(xml)



339
340
341
# File 'lib/atom/xml/parser.rb', line 339

def parse(xml)
  new(xml)
end

- (Object) uri_attribute(*names)



272
273
274
275
276
277
278
# File 'lib/atom/xml/parser.rb', line 272

def uri_attribute(*names)
  attr_accessor :base_uri
  names.each do |name|
    attr_accessor name.to_s.sub(/:/, '_').to_sym
    self.uri_attributes << name.to_s
  end
end