class Stockboy::Provider
Provider
objects handle the connection and capture of data from remote sources. This is an abstract superclass to help implement different providers.
Interface¶ ↑
A provider object must implement the following (private) methods:
- validate
-
Verify the parameters required for the data source are set to ensure a connection can be established.
fetch_data
-
Populate the object's +@data+ with raw content from source. This will usually be a raw string, and should not be parsed at this stage. Depending on the implementation, this may involve any of:
-
Establishing a connection
-
Navigating to a directory
-
Listing available files matching the configuration
-
Picking the appropriate file
-
And finally, reading/downloading data
This should also capture the timestamp of the data resource into +@data_time+. This should be the actual created or updated time of the data file from the source.
-
@abstract
Attributes
Size of the received data
@return [Time]
Timestamp of the received data
@return [Time]
@return [Array]
@return [Logger]
Public Class Methods
Must be called by subclasses via super
to set up dependencies
@param [Hash] opts @yield DSL
context for configuration
# File lib/stockboy/provider.rb, line 69 def initialize(opts={}, &block) @logger = opts.delete(:logger) || Stockboy.configuration.logger clear end
Public Instance Methods
Reset received data
@return [Boolean] Always true
# File lib/stockboy/provider.rb, line 95 def clear @data = nil @data_time = nil @data_size = nil @errors = [] true end
Raw input data from the source
@!attribute [r] data
# File lib/stockboy/provider.rb, line 78 def data fetch_data if @data.nil? && validate_config? yield @data if block_given? @data end
Determine if there is returned data
# File lib/stockboy/provider.rb, line 86 def data?(_=nil) return nil unless @data @data && !@data.empty? end
@return [String]
# File lib/stockboy/provider.rb, line 58 def inspect "#<#{self.class}:#{self.object_id} "\ "data_size=#{@data_size.inspect} "\ "errors=[#{errors.join(", ")}]>" end
Reload provided data
@return [String] Raw data
# File lib/stockboy/provider.rb, line 108 def reload clear fetch_data if validate_config? @data end
Does the provider have what it needs for fetching data?
@return [Boolean]
# File lib/stockboy/provider.rb, line 118 def valid? validate end
Private Instance Methods
Subclass should assign +@data+ with raw input, usually a string
@abstract
# File lib/stockboy/provider.rb, line 128 def fetch_data raise NoMethodError, "#{self.class}#fetch_data needs implementation" end
When picking files from a list you can supply :first
or :last
to the provider's pick
option, or else a block that can reduce to a single value, like:
proc do |best_match, current_match| current_match.better_than?(best_match) ? current_match : best_match end
# File lib/stockboy/provider.rb, line 160 def pick_from(list) case @pick when Symbol list.public_send @pick when Proc @pick.arity == 1 ? @pick.call(list) : list.reduce(&@pick) end end
Use errors << “'option' is required” for validating required provider parameters before attempting to make connections and retrieve data.
@abstract
# File lib/stockboy/provider.rb, line 138 def validate raise NoMethodError, "#{self.class}#validate needs implementation" end
# File lib/stockboy/provider.rb, line 142 def validate_config? unless validation = valid? logger.error do "Invalid #{self.class} provider configuration: #{errors.join(', ')}" end end validation end