Class: Bwkfanboy::Plugin
- Inherits:
-
Object
- Object
- Bwkfanboy::Plugin
- Includes:
- Enumerable
- Defined in:
- lib/bwkfanboy/plugin.rb
Overview
Requires defined ‘parse(streams)’ method in plugin.
Raises only PluginException on purpose.
Constant Summary collapse
- MAX_ENTRIES =
128
- NAME_RE =
/^[a-zA-Z0-9-]+$/
Instance Attribute Summary collapse
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#copyright ⇒ Object
Returns the value of attribute copyright.
-
#enc ⇒ Object
Returns the value of attribute enc.
-
#origin ⇒ Object
Returns the value of attribute origin.
-
#title ⇒ Object
Returns the value of attribute title.
-
#uri ⇒ Object
Returns the value of attribute uri.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #<<(obj) ⇒ Object
- #[](index) ⇒ Object
- #check ⇒ Object
- #each(&b) ⇒ Object
-
#entryMostRecent ⇒ Object
We can do this while adding a new entry, not here.
- #export ⇒ Object
- #full? ⇒ Boolean
-
#initialize(path, name, opt, &block) ⇒ Plugin
constructor
- path
-
an array [name] plugin’s name (without .rb extension) [opt] an array [&block] you can examine the Plugin object there.
- #load {|_self| ... } ⇒ Object
- #pack(stream = '') ⇒ Object
-
#run_parser(streams) ⇒ Object
Runs loaded plugin’s parser.
- #size ⇒ Object
- #validName?(name) ⇒ Boolean
Constructor Details
#initialize(path, name, opt, &block) ⇒ Plugin
- path
-
an array
- name
-
plugin’s name (without .rb extension)
- opt
-
an array
- &block
-
you can examine the Plugin object there
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/bwkfanboy/plugin.rb', line 77 def initialize path, name, opt, &block @path = path raise PluginInvalidName, "name doesn't match #{NAME_RE}" unless validName?(name) @name = name @origin = nil # a path where plugin was found @syslib = File.dirname __FILE__ # Variables for plugin authours @opt = (opt && opt.map(&:to_s)) || [] @uri = [] @enc = 'UTF-8' @version = 1 @copyright = '' @title = '' @content_type = '' @data = [] load &block end |
Instance Attribute Details
#content_type ⇒ Object
Returns the value of attribute content_type.
98 99 100 |
# File 'lib/bwkfanboy/plugin.rb', line 98 def content_type @content_type end |
#copyright ⇒ Object
Returns the value of attribute copyright.
98 99 100 |
# File 'lib/bwkfanboy/plugin.rb', line 98 def copyright @copyright end |
#enc ⇒ Object
Returns the value of attribute enc.
98 99 100 |
# File 'lib/bwkfanboy/plugin.rb', line 98 def enc @enc end |
#origin ⇒ Object
Returns the value of attribute origin.
97 98 99 |
# File 'lib/bwkfanboy/plugin.rb', line 97 def origin @origin end |
#title ⇒ Object
Returns the value of attribute title.
98 99 100 |
# File 'lib/bwkfanboy/plugin.rb', line 98 def title @title end |
#uri ⇒ Object
Returns the value of attribute uri.
98 99 100 |
# File 'lib/bwkfanboy/plugin.rb', line 98 def uri @uri end |
#version ⇒ Object
Returns the value of attribute version.
98 99 100 |
# File 'lib/bwkfanboy/plugin.rb', line 98 def version @version end |
Instance Method Details
#<<(obj) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/bwkfanboy/plugin.rb', line 108 def << obj return @data if full? ['title', 'link', 'updated', 'author', 'content'].each {|idx| obj[idx] &&= BH.clean obj[idx] raise PluginException, "empty '#{idx}' in the entry #{obj.inspect}" if obj[idx].size == 0 } @data << obj end |
#[](index) ⇒ Object
123 124 125 |
# File 'lib/bwkfanboy/plugin.rb', line 123 def [] index @data[index] end |
#check ⇒ Object
214 215 216 |
# File 'lib/bwkfanboy/plugin.rb', line 214 def check raise PluginException, "it ain't grab anything" if @data.size == 0 end |
#each(&b) ⇒ Object
104 105 106 |
# File 'lib/bwkfanboy/plugin.rb', line 104 def each &b @data.each &b end |
#entryMostRecent ⇒ Object
We can do this while adding a new entry, not here
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/bwkfanboy/plugin.rb', line 151 def entryMostRecent return nil if @data.size == 0 max = DateTime.parse @data.sample['updated'] @data.each {|idx| cur = DateTime.parse idx['updated'] max = cur if max < cur } return max.iso8601 end |
#export ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/bwkfanboy/plugin.rb', line 136 def export { 'channel' => { 'updated' => entryMostRecent, 'id' => @uri.to_s, 'author' => @copyright, 'title' => @title, 'link' => @uri.first, 'x_entries_content_type' => @content_type, }, 'x_entries' => @data } end |
#full? ⇒ Boolean
119 120 121 |
# File 'lib/bwkfanboy/plugin.rb', line 119 def full? @data.size >= MAX_ENTRIES end |
#load {|_self| ... } ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/bwkfanboy/plugin.rb', line 163 def load raise PluginException, 'invalid search path' unless @path && @path.respond_to?(:each) p = nil @path.each {|idx| contents = Dir.glob "#{idx}/*.rb" pos = contents.index "#{idx}/#{@name}.rb" if pos && p = contents[pos] @origin = idx break end } raise PluginNotFound, "'#{@name}' not found" unless p begin instance_eval File.read(p) rescue Exception raise PluginException, "'#{@name}' failed to parse: #{$!}" end unless BH.all_set?(uri) raise PluginException, 'uri must be an array of strings' if @opt.size != 0 raise PluginNoOptions, 'don\'t we forget about additional options?' end raise PluginException, 'enc is unset' unless BH.all_set?(enc) raise PluginException, 'version must be an integer' unless BH.all_set?(version) raise PluginException, 'copyright is unset' unless BH.all_set?(copyright) raise PluginException, 'title is unset' unless BH.all_set?(title) raise PluginException, 'content_type is unset' unless BH.all_set?(content_type) # use this, for example, to print a message to user that loading # was fine yield self if block_given? end |
#pack(stream = '') ⇒ Object
131 132 133 134 |
# File 'lib/bwkfanboy/plugin.rb', line 131 def pack stream = '' # hopefully, urf8 will survive MessagePack.pack export, stream end |
#run_parser(streams) ⇒ Object
Runs loaded plugin’s parser
200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/bwkfanboy/plugin.rb', line 200 def run_parser streams ok = streams ? true : false streams.each {|i| ok = false unless i.respond_to?(:eof) } if streams raise PluginException, 'parser expects a valid array of IO objects' unless ok begin parse streams rescue Exception raise PluginException, "'#{@name}' failed to parse: #{$!}" end check end |
#size ⇒ Object
127 128 129 |
# File 'lib/bwkfanboy/plugin.rb', line 127 def size @data.size end |
#validName?(name) ⇒ Boolean
100 101 102 |
# File 'lib/bwkfanboy/plugin.rb', line 100 def validName? name name =~ NAME_RE end |