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

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#attribute(*names) ⇒ Object



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

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



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

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

#elements(*names) ⇒ Object



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

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

#loadable!(&error_handler) ⇒ Object



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
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/atom/xml/parser.rb', line 282

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(s) URIs" unless /http[s]?/ =~ o.scheme
          response = nil

          http = http = Net::HTTP.new(o.host, o.port)

          http.use_ssl = (o.scheme == 'https')
          if File.directory? RootCA
            http.ca_path = RootCA
            http.verify_mode = OpenSSL::SSL::VERIFY_PEER
            http.verify_depth = 5
          else
            http.verify_mode = OpenSSL::SSL::VERIFY_NONE
          end

          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)

          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

#parse(xml) ⇒ Object



352
353
354
# File 'lib/atom/xml/parser.rb', line 352

def parse(xml)
  new(xml)
end

#uri_attribute(*names) ⇒ Object



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

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