Class: Bwkfanboy::Plugin

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Raises:



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_typeObject

Returns the value of attribute content_type.



98
99
100
# File 'lib/bwkfanboy/plugin.rb', line 98

def content_type
  @content_type
end

Returns the value of attribute copyright.



98
99
100
# File 'lib/bwkfanboy/plugin.rb', line 98

def copyright
  @copyright
end

#encObject

Returns the value of attribute enc.



98
99
100
# File 'lib/bwkfanboy/plugin.rb', line 98

def enc
  @enc
end

#originObject

Returns the value of attribute origin.



97
98
99
# File 'lib/bwkfanboy/plugin.rb', line 97

def origin
  @origin
end

#titleObject

Returns the value of attribute title.



98
99
100
# File 'lib/bwkfanboy/plugin.rb', line 98

def title
  @title
end

#uriObject

Returns the value of attribute uri.



98
99
100
# File 'lib/bwkfanboy/plugin.rb', line 98

def uri
  @uri
end

#versionObject

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

#checkObject

Raises:



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

#entryMostRecentObject

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

#exportObject



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

Returns:

  • (Boolean)


119
120
121
# File 'lib/bwkfanboy/plugin.rb', line 119

def full?
  @data.size >= MAX_ENTRIES
end

#load {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:



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

Raises:



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

#sizeObject



127
128
129
# File 'lib/bwkfanboy/plugin.rb', line 127

def size
  @data.size
end

#validName?(name) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/bwkfanboy/plugin.rb', line 100

def validName? name
  name =~ NAME_RE
end