Class: Stockboy::Provider Abstract

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, 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

Class Method 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



72
73
74
75
# File 'lib/stockboy/provider.rb', line 72

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

Instance Attribute Details

#dataObject (readonly)

Raw input data from the source



81
82
83
84
85
# File 'lib/stockboy/provider.rb', line 81

def data
  return @data if @data
  fetch_data if validate_config?
  @data
end

#data_timeTime (readonly)

Timestamp of the received data

Returns:

  • (Time)


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

def data_time
  @data_time
end

#errorsActiveModel::Errors (readonly)

Returns:

  • (ActiveModel::Errors)


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

def errors
  @errors
end

#loggerLogger

Returns:

  • (Logger)


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

def logger
  @logger
end

Class Method Details

.loggerLogger

Default logger if none is provided to the instance

Returns:

  • (Logger)


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

def self.logger
  Logger.new(STDERR)
end

Instance Method Details

#clearBoolean Also known as: reset

Reset received data

Returns:

  • (Boolean)

    Always true



91
92
93
94
95
96
97
# File 'lib/stockboy/provider.rb', line 91

def clear
  @data = nil
  @data_time = nil
  @data_size = nil
  @errors = ActiveModel::Errors.new(self)
  true
end

#inspectString

Returns:

  • (String)


63
64
65
# File 'lib/stockboy/provider.rb', line 63

def inspect
"#<#{self.class}:#{self.object_id} data_size=#{@data_size or 'nil'} errors=#{@errors.full_messages}>"
end

#reloadString

Reload provided data

Returns:

  • (String)

    Raw data



104
105
106
107
108
# File 'lib/stockboy/provider.rb', line 104

def reload
  clear
  fetch_data if validate_config?
  @data
end

#valid?Boolean

Does the provider have what it needs for fetching data?

Returns:

  • (Boolean)


114
115
116
# File 'lib/stockboy/provider.rb', line 114

def valid?
  validate
end