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

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#attribute(*names) ⇒ Object



281
282
283
284
285
286
# File 'lib/atom/xml/parser.rb', line 281

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



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/atom/xml/parser.rb', line 251

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



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/atom/xml/parser.rb', line 263

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



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 296

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, IO
          Nokogiri::XML::Reader(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
            Nokogiri::XML::Reader(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

        self.new(xml)
    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



288
289
290
291
292
293
294
# File 'lib/atom/xml/parser.rb', line 288

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