lib/arstotzka/fetcher.rb in arstotzka-1.3.2 vs lib/arstotzka/fetcher.rb in arstotzka-1.4.0
- old
+ new
@@ -6,19 +6,21 @@
# Class responsible for orquestrating the fetch value from the hash
# and post-processing it
class Fetcher
include Base
+ autoload :Cache, 'arstotzka/fetcher/cache'
+
# Creates an instance of Artotzka::Fetcher
#
# @overload iniitalize(options_hash = {})
# @param options_hash [Hash] options for {Crawler}, {Wrapper},
- # {Reader} and {HashReader}
+ # {Reader}, {Cache} and {HashReader}
#
# @overload iniitalize(options)
# @param options [Arstotzka::Options] options for {Crawler}, {Wrapper},
- # {Reader} and {HashReader}
+ # {Reader}, {Cache} and {HashReader}
def initialize(options_hash = {})
self.options = options_hash
end
# Crawls the hash for the value
@@ -80,14 +82,13 @@
# fetcher.fetch # retruns [
# # Transaction.new(value: 1000.53, type: 'income'),
# # Transaction.new(value: 50.23, type: 'income')
# # ]
def fetch
- value = crawler.value(hash)
- value.flatten! if flatten && value.is_a?(Array)
- value = instance.send(after, value) if after
- value
+ Cache.new(options) do
+ fetch_value
+ end.fetch
end
# Checks if other equals self
#
# @param other [Object]
@@ -110,10 +111,24 @@
delegate :wrap, to: :wrapper
delegate :hash, to: :hash_reader
# @private
#
+ # fetch value from Hash
+ #
+ # Value is fetched trhough the usage of {Crawler},
+ # and wrapped with {Wrapper}
+ #
+ # @return [Object]
+ def fetch_value
+ value = crawler.value(hash)
+ value.flatten! if flatten && value.is_a?(Array)
+ after ? instance.send(after, value) : value
+ end
+
+ # @private
+ #
# Returns an instance of Aristotzka::Craler
#
# craler will be responsible to crawl the hash for
# the final return
#
@@ -129,10 +144,10 @@
#
# Wrapper responsible for wrapping the value found
#
# @return [Arstotzka::Wrapper] the wrapper
def wrapper
- @wrapper ||= Wrapper.new(options.merge(instance: instance))
+ @wrapper ||= Wrapper.new(options)
end
# @api private
#
# Reader responsible for fetching hash from instance