Class: MaxML::XML
- Inherits:
-
Object
- Object
- MaxML::XML
- Defined in:
- lib/maxml/xml.rb
Instance Attribute Summary collapse
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#db ⇒ Object
Returns the value of attribute db.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #db_config(opts) ⇒ Object
-
#initialize(url, db = MongoPersistence, **db_conf) ⇒ XML
constructor
A new instance of XML.
- #save ⇒ Object
- #to_hash ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(url, db = MongoPersistence, **db_conf) ⇒ XML
Returns a new instance of XML.
11 12 13 14 15 16 17 |
# File 'lib/maxml/xml.rb', line 11 def initialize(url, db=MongoPersistence, **db_conf) @url = url @date = Time.now @content = self.class.fetch_content(url) @db = db.new db_config(db_conf) unless db_conf.empty? end |
Instance Attribute Details
#date ⇒ Object (readonly)
Returns the value of attribute date.
8 9 10 |
# File 'lib/maxml/xml.rb', line 8 def date @date end |
#db ⇒ Object
Returns the value of attribute db.
9 10 11 |
# File 'lib/maxml/xml.rb', line 9 def db @db end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
8 9 10 |
# File 'lib/maxml/xml.rb', line 8 def url @url end |
Class Method Details
.fetch_content(url) ⇒ Object
41 42 43 44 |
# File 'lib/maxml/xml.rb', line 41 def self.fetch_content(url) uri = URI.parse(url) uri.read end |
Instance Method Details
#db_config(opts) ⇒ Object
37 38 39 |
# File 'lib/maxml/xml.rb', line 37 def db_config(opts) @db.config = opts end |
#save ⇒ Object
32 33 34 35 |
# File 'lib/maxml/xml.rb', line 32 def save content = block_given? ? yield(to_hash) : to_hash @db.save({url: @url, date: @date, content: content}) end |
#to_hash ⇒ Object
27 28 29 30 |
# File 'lib/maxml/xml.rb', line 27 def to_hash parser = Nori.new parser.parse(@content) end |
#to_json ⇒ Object
23 24 25 |
# File 'lib/maxml/xml.rb', line 23 def to_json to_hash.to_json end |
#to_s ⇒ Object
19 20 21 |
# File 'lib/maxml/xml.rb', line 19 def to_s @content end |