Class: GamedayUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/gameday_util.rb

Overview

This class provides a variety of utility methods that are used in other classes

Constant Summary collapse

@@fetcher =
''

Class Method Summary collapse

Class Method Details

.convert_digit_to_string(digit) ⇒ Object

Converts a digit into a 2 character string, prepended with ‘0’ if necessary



43
44
45
46
47
48
49
# File 'lib/gameday_util.rb', line 43

def self.convert_digit_to_string(digit)
  if digit<10
    return '0' + digit.to_s
  else
    return digit.to_s
  end
end

.fetcherObject

Returns an instance of the configured fetcher, either remote or local



12
13
14
15
16
17
18
19
20
21
# File 'lib/gameday_util.rb', line 12

def self.fetcher
  if @@fetcher == ''
    read_config
  end
  if @@fetcher == 'local'
    return GamedayLocalFetcher
  else
    return GamedayRemoteFetcher
  end
end

.get_connection(url) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gameday_util.rb', line 76

def self.get_connection(url)
  self.read_config
  begin
    if !@@proxy_addr.empty?
      connection = open(url, :proxy => "http://#{@@proxy_addr}:#{@@proxy_port}")
    else
      connection = open(url)
    end
    connection
  rescue
    puts 'Could not open connection'
  end
end

.is_date_valid(month, date) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gameday_util.rb', line 111

def self.is_date_valid(month, date)
  if (month == 4 && date == 31) ||  
     (month == 6 && date == 31) ||
     (month == 9 && date == 31)
     return false
  end
  if month==4 and date<5 # start from 4/5 onward
    return false
  end
  return true
end

.net_httpObject



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

def self.net_http
  self.read_config
  if !@@proxy_addr.empty?
    return Net::HTTP::Proxy(@@proxy_addr, @@proxy_port)
  else
    return Net::HTTP
  end
end

.parse_date_string(date) ⇒ Object

Parses a string with the date format of YYYYMMDD into an array with the following elements:

[0] = year
[1] = month
[2] = day


34
35
36
37
38
39
# File 'lib/gameday_util.rb', line 34

def self.parse_date_string(date)
  results = []
  results << date[0..3]
  results << date[4..5]
  results << date[6..7] 
end

.parse_gameday_id(gameday_gid) ⇒ Object

Example gameday_gid = gid_2009_06_21_milmlb_detmlb_1



52
53
54
55
56
57
58
59
60
61
# File 'lib/gameday_util.rb', line 52

def self.parse_gameday_id(gameday_gid)
  gameday_info = {}
  gameday_info["year"] = gameday_gid[4..7]
  gameday_info["month"] = gameday_gid[9..10]
  gameday_info["day"] = gameday_gid[12..13]
  gameday_info["visiting_team_abbrev"] = gameday_gid[15..17]
  gameday_info["home_team_abbrev"] = gameday_gid[22..24]
  gameday_info["game_number"] = gameday_gid[29..29]
  return gameday_info
end

.read_configObject

Read configuration from gameday_config.yml file to create instance configuration variables.



66
67
68
69
70
71
72
73
# File 'lib/gameday_util.rb', line 66

def self.read_config
   settings = YAML::load_file(File.expand_path(File.dirname(__FILE__) + "/gameday_config.yml"))
   #settings = YAML::load_file(File.expand_path('gameday_config.yml'))
   set_proxy_info(settings)
   if @@fetcher == ''
     set_fetcher(settings['fetcher'])
   end
end

.read_file(filename) ⇒ Object



101
102
103
# File 'lib/gameday_util.rb', line 101

def self.read_file(filename)
  IO.readlines(filename,'').to_s
end

.save_file(filename, data) ⇒ Object



106
107
108
# File 'lib/gameday_util.rb', line 106

def self.save_file(filename, data)
  File.open(filename, 'w') {|f| f.write(data) }
end

.set_fetcher(fetcher) ⇒ Object



24
25
26
# File 'lib/gameday_util.rb', line 24

def self.set_fetcher(fetcher)
  @@fetcher = fetcher
end