lib/html2rss/attribute_post_processors/substring.rb in html2rss-0.3.3 vs lib/html2rss/attribute_post_processors/substring.rb in html2rss-0.4.0
- old
+ new
@@ -1,14 +1,35 @@
module Html2rss
module AttributePostProcessors
+ ## Returns a defined part of a String.
+ #
+ # The +end+ parameter can be omitted, in that case it will not cut the
+ # String at the end.
+ #
+ # Imagine this HTML:
+ # <h1>Foo bar and baz<h1>
+ #
+ # YAML usage example:
+ # selectors:
+ # title:
+ # selector: h1
+ # post_process:
+ # name: substring
+ # start: 4
+ # end: 6
+ #
+ # Would return:
+ # 'bar'
class Substring
- def initialize(value, options, _item)
+ def initialize(value, env)
@value = value
- @options = options
+ @options = env[:options]
end
+ ##
+ # @return [String]
def get
- ending = @options.fetch('end', false) ? @options['end'].to_i : @value.length
+ ending = @options.fetch('end', @value.length).to_i
@value[@options['start'].to_i..ending]
end
end
end
end