Class: Bwkfanboy::Parse

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

Overview

:include: ../../doc/plugin.rdoc

Direct Known Subclasses

Page

Constant Summary collapse

ENTRIES_MAX =
128

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = []) ⇒ Parse

Returns a new instance of Parse.



15
16
17
18
# File 'lib/bwkfanboy/parser.rb', line 15

def initialize(opt = [])
  @entries = []
  @opt = opt
end

Instance Attribute Details

#optObject (readonly)

Returns the value of attribute opt.



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

def opt
  @opt
end

Instance Method Details

#checkObject

After loading a plugin, one can do basic validation of the plugin’s class with the help of this method.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bwkfanboy/parser.rb', line 75

def check
  m = get_meta()
  begin
    [:URI, :ENC, :VERSION, :COPYRIGHT, :TITLE, :CONTENT_TYPE].each {|i|
      fail "#{m}::#{i} not defined or empty" if (! m.const_defined?(i) || m.const_get(i) =~ /^\s*$/)
    }
    
    if m::URI =~ /#\{.+?\}/ && @opt.size == 0
      fail 'additional options required'
    end
  rescue
    Utils.errx(1, "incomplete plugin's instance: #{$!}")
  end
end

#dumpObject

Prints entries in ‘key: value’ formatted strings. Intended for debugging.



39
40
41
42
43
44
45
46
47
48
# File 'lib/bwkfanboy/parser.rb', line 39

def dump()
  @entries.each {|i|
    puts "title    : " + i[:title]
    puts "link     : " + i[:link]
    puts "updated  : " + i[:updated]
    puts "author   : " + i[:author]
    puts "content  : " + i[:content]
    puts ""
  }
end

#dump_infoObject

Prints plugin’s meta information.



91
92
93
94
95
96
97
# File 'lib/bwkfanboy/parser.rb', line 91

def dump_info()
  m = get_meta()
  puts "Version     : #{m::VERSION}"
  puts "Copyright   : #{m::COPYRIGHT}"
  puts "Title       : #{m::TITLE}"
  puts "URI         : #{uri}"
end

#parse(stream) ⇒ Object

Invokes #myparse & checks if it has grabbed something.



21
22
23
24
25
26
27
28
29
30
# File 'lib/bwkfanboy/parser.rb', line 21

def parse(stream)
  @entries = []
  begin
    myparse(stream)
  rescue
    @entries = []
    Utils.errx(1, "parsing failed: #{$!}\n\nBacktrace:\n\n#{$!.backtrace.join("\n")}")
  end
  Utils.errx(1, "plugin return no output") if @entries.length == 0
end

#to_jsonObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bwkfanboy/parser.rb', line 50

def to_json()
  # guess the time of the most recent entry
  u = DateTime.parse() # January 1, 4713 BCE
  @entries.each {|i|
    t = DateTime.parse(i[:updated])
    u = t if t > u
  }
  
  m = get_meta()
  j = {
    channel: {
      updated: u,
      id: m::URI,
      author: Meta::NAME,   # just a placeholder
      title: m::TITLE,
      link: m::URI,
      x_entries_content_type: m::CONTENT_TYPE
    },
    x_entries: @entries
  }
  Utils::cfg[:verbose] >= 1 ? JSON.pretty_generate(j) : JSON.generate(j)
end

#uriObject



32
33
34
35
# File 'lib/bwkfanboy/parser.rb', line 32

def uri()
  m = get_meta()
  eval("\"#{m::URI}\"")
end