Class: Zsff

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

Constant Summary collapse

VERSION =
'1.0.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ Zsff

Returns a new instance of Zsff.



24
25
26
27
28
29
# File 'lib/zsff.rb', line 24

def initialize(attributes = nil)
  self.attributes = attributes unless attributes.nil?
  @errors = []
  @warnings = []
  @links = []
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



16
17
18
# File 'lib/zsff.rb', line 16

def author
  @author
end

#content_typeObject

Returns the value of attribute content_type.



21
22
23
# File 'lib/zsff.rb', line 21

def content_type
  @content_type
end

#contentsObject

Returns the value of attribute contents.



12
13
14
# File 'lib/zsff.rb', line 12

def contents
  @contents
end

Returns the value of attribute copyright.



20
21
22
# File 'lib/zsff.rb', line 20

def copyright
  @copyright
end

#errorsObject

Returns the value of attribute errors.



13
14
15
# File 'lib/zsff.rb', line 13

def errors
  @errors
end

Returns the value of attribute links.



22
23
24
# File 'lib/zsff.rb', line 22

def links
  @links
end

#siteObject

Returns the value of attribute site.



18
19
20
# File 'lib/zsff.rb', line 18

def site
  @site
end

#subtitleObject

Returns the value of attribute subtitle.



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

def subtitle
  @subtitle
end

#titleObject

Returns the value of attribute title.



17
18
19
# File 'lib/zsff.rb', line 17

def title
  @title
end

#urlObject

Returns the value of attribute url.



11
12
13
# File 'lib/zsff.rb', line 11

def url
  @url
end

#versionObject

Returns the value of attribute version.



15
16
17
# File 'lib/zsff.rb', line 15

def version
  @version
end

#warningsObject

Returns the value of attribute warnings.



14
15
16
# File 'lib/zsff.rb', line 14

def warnings
  @warnings
end

Class Method Details

.parse_contents(contents) ⇒ Object



37
38
39
40
41
# File 'lib/zsff.rb', line 37

def self.parse_contents(contents)
  parser = self.new(:contents => contents)
  parser.parse
  return parser
end

.parse_url(url) ⇒ Object



31
32
33
34
35
# File 'lib/zsff.rb', line 31

def self.parse_url(url)
  parser = self.new(:url => url)
  parser.parse
  return parser
end

Instance Method Details

#attributes=(attributes) ⇒ Object



43
44
45
46
47
# File 'lib/zsff.rb', line 43

def attributes=(attributes)
  attributes.each do |k, v|
    send(k.to_s + "=", v)
  end
end

#parseObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/zsff.rb', line 49

def parse
  if self.url
    begin
      uri = URI.parse(url)
      @contents = Net::HTTP.get(uri)
    rescue
      @errors.push("Invalid URL")
      return false
    end
  end
  
  parse_contents
  
  return @errors.size == 0
end

#parse_contentsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/zsff.rb', line 65

def parse_contents
  begin
    raw = @contents.split(/\n\n/)
    @errors.push("Must have two line breaks between Attributes section and Links section") and return if raw.size < 2
    
    @version = raw.first.split(/\n/).first
    raw.first.split(/\n/)[1..-1].each do |attribute|
      parts = attribute.split(":")
      next if parts.size < 2
      self.send(parts.first.downcase.gsub(/-/, "_") + "=", parts[1..-1].join(":").strip)
    end
    
    error_if :version,      @version.nil? || @version.match(/ZSFF 1\.[0-9]/).nil?
    error_if :author,       @author.nil? || @author == ""
    error_if :title,        @title.nil? || @title == ""
    error_if :site,         @site.nil? || @site == ""
    warn_if  :site,         @site.nil? || !is_valid_url?(@site) 
    warn_if  :subtitle,     @subtitle.nil? || @subtitle == ""
    warn_if  :copyright,    @copyright.nil? || @copyright == ""
    error_if :content_type, @content_type.nil? || @content_type.match(/.*\/.*/).nil?
    
    items = raw.last.split(/\n/)
    items.each do |item|
      warnings.push("Item '#{item}' does not appear to be a valid URL") and next unless is_valid_url?(item)
      unless item.match(/\.page$/)
        content = items.select{|i|item.gsub(/\.(php|php3|asp|aspx|htm|cfm|html)$/, "") + ".page" == i}.first
        @links.push({:link => item, :content => content}) 
      end
    end
  #rescue
  #  @errors.push("Cannot parse ZSFF file")
  end
  
end