Class: MaxML::XML

Inherits:
Object
  • Object
show all
Defined in:
lib/maxml/xml.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#dateObject (readonly)

Returns the value of attribute date.



8
9
10
# File 'lib/maxml/xml.rb', line 8

def date
  @date
end

#dbObject

Returns the value of attribute db.



9
10
11
# File 'lib/maxml/xml.rb', line 9

def db
  @db
end

#urlObject (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

#saveObject



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_hashObject



27
28
29
30
# File 'lib/maxml/xml.rb', line 27

def to_hash
  parser = Nori.new
  parser.parse(@content)
end

#to_jsonObject



23
24
25
# File 'lib/maxml/xml.rb', line 23

def to_json
  to_hash.to_json
end

#to_sObject



19
20
21
# File 'lib/maxml/xml.rb', line 19

def to_s
  @content
end