Class: SportDb::Market::Reader

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging, FixtureHelpers, SportDb::Models
Defined in:
lib/sportdb/market/reader.rb

Overview

load quotes from plain text files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(include_path) ⇒ Reader

Returns a new instance of Reader.



18
19
20
21
# File 'lib/sportdb/market/reader.rb', line 18

def initialize( include_path )
  @include_path = include_path
  @loader       = Loader.new( include_path )
end

Instance Attribute Details

#include_pathObject (readonly)

Returns the value of attribute include_path.



15
16
17
# File 'lib/sportdb/market/reader.rb', line 15

def include_path
  @include_path
end

#loaderObject (readonly)

Returns the value of attribute loader.



16
17
18
# File 'lib/sportdb/market/reader.rb', line 16

def loader
  @loader
end

Instance Method Details

#fetch_quotes_meta(name) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sportdb/market/reader.rb', line 64

def fetch_quotes_meta( name )
  # get/fetch/find event from yml file

  ## todo/fix: use h = HashFile.load( path ) or similar instead of HashReader!!

  ## todo/fix: add option for not adding prop automatically?? w/ HashReaderV2

  reader = HashReaderV2.new( name, include_path )

  attribs = {}

  reader.each_typed do |key, value|

    ## puts "processing event attrib >>#{key}<< >>#{value}<<..."

    if key == 'league'
      attribs[ 'league_key' ] = value.to_s.strip
    elsif key == 'season'
      attribs[ 'season_key' ] = value.to_s.strip
    elsif key == 'service'
      attribs[ 'service_key' ] = value.to_s.strip
    else
      puts "*** warn: skipping unknown quote config/meta key >#{key}< w/ value >#{value}<"
    end
  end # each key,value
  
  attribs
end

#load(name) ⇒ Object

convenience helper for all-in-one reader



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sportdb/market/reader.rb', line 36

def load( name )   # convenience helper for all-in-one reader

  logger.debug "enter load( name=>>#{name}<<, include_path=>>#{include_path}<<)"
  
  # check if name exits if extension .rb  ##
  #  if yes, use loader (for ruby code)
  #  otherwise assume
  #  plain text match/game quotes
  
  ## todo: add sub for !/ prefix in name

  path = "#{@include_path}/#{name}.rb"
  
  if File.exist?( path )   # if it's ruby use (code) loader
    loader.load( name )
  else
    meta = fetch_quotes_meta( name )
 
    service_key = meta['service_key']
    event_key   = "#{meta['league_key']}.#{meta['season_key']}"
    
    load_quotes( service_key, event_key, name )
  end

end

#load_quotes(service_key, event_key, name) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sportdb/market/reader.rb', line 95

def load_quotes( service_key, event_key, name )
  path = "#{@include_path}/#{name}.txt"

  puts "*** parsing data '#{name}' (#{path})..."

  ### todo/fix: use classify  (from string)
  SportDb.lang.lang = SportDb.lang.classify_file( path )

  reader = LineReader.new( path )

  load_worker( service_key, event_key, reader )

  ## Prop.create!( key: "db.#{fixture_name_to_prop_key(name)}.version", value: "file.txt.#{File.mtime(path).strftime('%Y.%m.%d')}" )
end

#load_setup(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sportdb/market/reader.rb', line 23

def load_setup( name )
  path = "#{include_path}/#{name}.yml"

  logger.info "parsing data '#{name}' (#{path})..."

  reader = FixtureReader.new( path )

  reader.each do |fixture_name|
    load( fixture_name )
  end
end