Class: Stockboy::Provider Abstract

Inherits:
Object
  • Object
show all
Extended by:
DSL
Defined in:
lib/stockboy/provider.rb

Overview

This class is abstract.

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) { ... } ⇒ Provider

Must be called by subclasses via super to set up dependencies

Parameters:

  • opts (Hash) (defaults to: {})

Yields:

  • DSL context for configuration



69
70
71
72
# File 'lib/stockboy/provider.rb', line 69

def initialize(opts={}, &block)
  @logger = opts.delete(:logger) || Stockboy.configuration.logger
  clear
end

Instance Attribute Details

#data {|@data| ... } ⇒ Object (readonly)

Raw input data from the source

Yields:



78
79
80
81
82
# File 'lib/stockboy/provider.rb', line 78

def data
  fetch_data if @data.nil? && validate_config?
  yield @data if block_given?
  @data
end

#data_sizeTime (readonly)

Size of the received data

Returns:

  • (Time)


48
49
50
# File 'lib/stockboy/provider.rb', line 48

def data_size
  @data_size
end

#data_timeTime (readonly)

Timestamp of the received data

Returns:

  • (Time)


54
55
56
# File 'lib/stockboy/provider.rb', line 54

def data_time
  @data_time
end

#errorsArray (readonly)

Returns:

  • (Array)


42
43
44
# File 'lib/stockboy/provider.rb', line 42

def errors
  @errors
end

#loggerLogger

Returns:

  • (Logger)


38
39
40
# File 'lib/stockboy/provider.rb', line 38

def logger
  @logger
end

Instance Method Details

#clearBoolean Also known as: reset

Reset received data

Returns:

  • (Boolean)

    Always true



95
96
97
98
99
100
101
# File 'lib/stockboy/provider.rb', line 95

def clear
  @data = nil
  @data_time = nil
  @data_size = nil
  @errors = []
  true
end

#data?(_ = nil) ⇒ Boolean

Determine if there is returned data

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/stockboy/provider.rb', line 86

def data?(_=nil)
  return nil unless @data
  @data && !@data.empty?
end

#inspectString

Returns:

  • (String)


58
59
60
61
62
# File 'lib/stockboy/provider.rb', line 58

def inspect
  "#<#{self.class}:#{self.object_id} "\
  "data_size=#{@data_size.inspect} "\
  "errors=[#{errors.join(", ")}]>"
end

#reloadString

Reload provided data

Returns:

  • (String)

    Raw data



108
109
110
111
112
# File 'lib/stockboy/provider.rb', line 108

def reload
  clear
  fetch_data if validate_config?
  @data
end

#valid?Boolean

Does the provider have what it needs for fetching data?

Returns:

  • (Boolean)


118
119
120
# File 'lib/stockboy/provider.rb', line 118

def valid?
  validate
end