feed_us_grabber 0.1.14 → 0.1.15

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,3 +1,4 @@
1
+ =begin
1
2
  = feed_us_grabber
2
3
 
3
4
  == ENVIRONEMNT
@@ -56,15 +57,20 @@ Follow these steps to download and install the feed_us_grabber gem:
56
57
  </table>
57
58
 
58
59
  == CLEARING CACHE
59
- To clear cache goto following url
60
- http://rails_app/FeedUsGrabber
61
- 1. Clear a specific cache group
62
- http://rails_app/FeedUsGrabber?cachecommand=clear&group=demo
63
- OR
60
+ To clear cache goto following url
61
+ http://rails_app/FeedUsGrabber
62
+ * Clear a specific cache group
63
+ http://rails_app/FeedUsGrabber?cachecommand=clear&group=demo
64
+ OR
64
65
 
65
- http://rails_app/FeedUsGrabber?group=demo
66
- 2. Clear entire cache
67
- http://rails_app/FeedUsGrabber?cachecommand=clearall
66
+ http://rails_app/FeedUsGrabber?group=demo
67
+ * Clear entire cache
68
+ http://rails_app/FeedUsGrabber?cachecommand=clearall
69
+
70
+ :Note that if you receive a failure message that your IP is not authorized to clear the cache, add your IP to the ClientWhiteList in your controller (replace 127.0.0.0 with your IP, visit http://whatismyip.com/)
71
+ def index
72
+ @grabber = feedUsGrabber(:FeedUsURL => 'http://render.feed.us/Feed.aspx?g=00767d50-bde8-e111-9875-4040419a7f04', :ClientWhiteList => '127.0.0.1')
73
+ end
68
74
 
69
75
  == LOCATION OF CACHE
70
76
  Cache is maintained under RAILS_ROOT/tmp
@@ -86,4 +92,4 @@ Follow these steps to download and install the feed_us_grabber gem:
86
92
 
87
93
  Copyright (c) 2012 Cliff G. See LICENSE.txt for
88
94
  further details.
89
-
95
+ =end
@@ -5,63 +5,76 @@ include FeedUsGrabberHelper
5
5
 
6
6
  class FeedUsGrabberController < ActionController::Base
7
7
  def index
8
- def initialize
9
- # Client ip check. Can remove all ip's to turn off the check.
10
- # ! IMPORTANT: TO DISABLE THIS CHECK SET BOTH ARRAYS TO nil
11
- @mClientWhiteList = [ "75.126.108.226", "75.126.107.10", "127.0.0.1", "50.56.95.92" ]
12
- @mClientHostNameWhileList = [ "app.feed.us", "request.feed.us", "feed.us", "classic.syndication.feed.us", "render.feed.us", "dev.feed.us", "stage.feed.us", "localhost"]
13
- @mClientIp
14
- end
15
-
16
- args = {}
17
-
18
- # URL that will be checked for connectivity before clearing the cache (phone home)
19
- args[:FeedUsURL] = 'http://render.feed.us/grabberdefault.htm?phonehome=true'
20
-
21
- unless params[:cachecommand].nil?
22
- args[:CacheCommand] = params[:cachecommand]
23
- end
24
- unless params[:group].nil?
25
- args[:FeedUsCacheGroup] = params[:group]
26
- end
27
-
28
- if args[:FeedUsCacheGroup] && args[:CacheCommand].nil?
29
- args[:CacheCommand] = 'clear'
30
- end
31
-
32
- if args[:CacheCommand].nil?
33
- args[:CacheCommand] = 'clearall'
34
- end
8
+ def initialize
9
+ # Client ip check. Can remove all ip's to turn off the check.
10
+ # ! IMPORTANT: TO DISABLE THIS CHECK SET BOTH ARRAYS TO nil
11
+ @mClientWhiteList = [ "75.126.108.226", "75.126.107.10", "127.0.0.1", "50.56.95.92" ]
12
+ @mClientHostNameWhileList = [ "app.feed.us", "request.feed.us", "feed.us", "classic.syndication.feed.us", "render.feed.us", "dev.feed.us", "stage.feed.us", "localhost"]
13
+ @mClientIp
14
+ end
15
+
16
+ args = {}
17
+
18
+ # URL that will be checked for connectivity before clearing the cache (phone home)
19
+ args[:FeedUsURL] = 'http://render.feed.us/grabberdefault.htm?phonehome=true'
20
+
21
+ unless params[:cachecommand].nil?
22
+ args[:CacheCommand] = params[:cachecommand]
23
+ end
24
+ unless params[:group].nil?
25
+ args[:FeedUsCacheGroup] = params[:group]
26
+ end
27
+ args[:DebugOutput] = ""
28
+ args[:Debug] = false
29
+ unless params[:debug].nil?
30
+ args[:Debug] = params[:debug] == "1"
31
+ end
32
+
33
+ if args[:FeedUsCacheGroup] && args[:CacheCommand].nil?
34
+ args[:CacheCommand] = 'clear'
35
+ end
35
36
 
36
- if args[:CacheCommand] == 'clear' && args[:FeedUsCacheGroup]
37
- fetch = true
38
- else
39
- fetch = false
40
- end
41
- if args[:CacheCommand] == 'clearall' || args[:CacheCommand] == CACHE_COMMAND_FORCE
42
- fetch = true
43
- end
44
-
45
- # Client ip check
46
- isPermittedToProceed = IsPermittedToProceed()
47
-
48
- if isPermittedToProceed == false
49
- render :text=> "<img src=\"http://feed.us/images/feedus_logo_people.png\"><br />
50
- <p style=\"font-family:arial;\"> IP " + @mClientIp + " is not authorized. Modify the feed_us_grabber_controller @mClientWhiteList array if this IP should have access."
51
- else
52
- if fetch == true
53
- feedUsGrabber(args)
54
- render :text => "<img src=\"http://feed.us/images/feedus_logo_people.png\"><br />
55
- <p style=\"font-family:arial;\">Congratulations! You have successfully refreshed your content.</p>
56
- <p style=\"font-family:arial;\"><a href=\"/\">Home</a></p>"
37
+ if args[:CacheCommand].nil?
38
+ args[:CacheCommand] = 'clearall'
39
+ end
40
+
41
+ if args[:CacheCommand] == 'clear' && args[:FeedUsCacheGroup]
42
+ fetch = true
43
+ else
44
+ fetch = false
45
+ end
46
+ if args[:CacheCommand] == 'clearall' || args[:CacheCommand] == CACHE_COMMAND_FORCE
47
+ fetch = true
48
+ end
49
+
50
+ # Client ip check
51
+ isPermittedToProceed = IsPermittedToProceed(args)
52
+
53
+ renderText = ""
54
+ if isPermittedToProceed == false
55
+ renderText = "<img src=\"http://feed.us/images/feedus_logo_people.png\"><br />
56
+ <p style=\"font-family:arial;\"> IP " + @mClientIp + " is not authorized. Modify the feed_us_grabber_controller @mClientWhiteList array if this IP should have access. "
57
+ else
58
+ if fetch == true
59
+ feedUsGrabber(args)
60
+ renderText = "<img src=\"http://feed.us/images/feedus_logo_people.png\"><br />
61
+ <p style=\"font-family:arial;\">Congratulations! You have successfully refreshed your content.</p>
62
+ <p style=\"font-family:arial;\"><a href=\"/\">Home</a></p> "
57
63
  else
58
- render :text=> "<img src=\"http://feed.us/images/feedus_logo_people.png\"><br />
59
- <p style=\"font-family:arial;\"> Please specify cachecommand=clear&group=GROUPNAME or cachecommand=clearall or cachecommand=force_clear_all in the URL"
64
+ renderText = "<img src=\"http://feed.us/images/feedus_logo_people.png\"><br />
65
+ <p style=\"font-family:arial;\"> Please specify cachecommand=clear&group=GROUPNAME or cachecommand=clearall or cachecommand=force_clear_all in the URL</p> "
60
66
  end
61
67
  end
68
+
69
+ if args[:Debug] == true
70
+ renderText << args[:DebugOutput]
71
+ renderText << @mClientWhiteList.to_s
72
+ end
73
+
74
+ render :text=> renderText
62
75
  end
63
76
 
64
- def IsPermittedToProceed()
77
+ def IsPermittedToProceed(args)
65
78
  isPermitted = false
66
79
  @mClientIp = request.remote_addr
67
80
 
@@ -69,7 +82,7 @@ class FeedUsGrabberController < ActionController::Base
69
82
  @mClientIp = request.env["HTTP_X_FORWARDED_FOR"]
70
83
  end
71
84
 
72
- isPermitted = IsClientIpInWhiteList()
85
+ isPermitted = IsClientIpInWhiteList(args)
73
86
 
74
87
  if isPermitted == false
75
88
  if @mClientHostNameWhileList.nil? == false
@@ -93,25 +106,37 @@ class FeedUsGrabberController < ActionController::Base
93
106
  if @mClientIp == "" || @mClientIp.nil?
94
107
  @mClientIp = request.env["X-Forwarded-For"]
95
108
  end
96
- isPermitted = IsClientIpInWhiteList()
109
+ isPermitted = IsClientIpInWhiteList(args)
97
110
  puts "Trace: Try to use fwd header = #{@mClientIp}"
98
111
  puts "Trace: using fwd header isPermitted = #{isPermitted}"
99
112
  end
100
113
 
114
+ # Set the Debug flag - do not want to output Debug info if not permitted
115
+ if isPermitted == false
116
+ args[:Debug] = false
117
+ end
118
+
101
119
  puts "Trace: mClientIp = #{@mClientIp}"
102
120
  puts "Trace: returning is permitted true"
103
- # TODO: temp code that always returns true
104
- return true
105
-
106
- #return isPermitted
121
+ args[:DebugOutput] << "Debug: mClientIp = #{@mClientIp}"
122
+
123
+ return isPermitted
107
124
  end
108
125
 
109
- def IsClientIpInWhiteList()
110
- included = false
126
+ def IsClientIpInWhiteList(args)
127
+ included = false
111
128
  if @mClientWhiteList.nil? && @mClientHostNameWhileList.nil?
112
129
  included = true
113
- elsif @mClientWhiteList.nil? == false && @mClientWhiteList.include?(@mClientIp) == true
114
- included = true
130
+ else
131
+ # User can specify additional IP's to add to whitelist
132
+ configuredClientWhiteList = FeedUsGrabber.new.getClientWhiteList
133
+ unless configuredClientWhiteList.nil? || configuredClientWhiteList.empty?
134
+ @mClientWhiteList.push(configuredClientWhiteList)
135
+ end
136
+
137
+ if @mClientWhiteList.nil? == false && @mClientWhiteList.include?(@mClientIp) == true
138
+ included = true
139
+ end
115
140
  end
116
141
  return included
117
142
  end
@@ -1,12 +1,12 @@
1
1
  module FeedUsGrabberHelper
2
- def feedUsGrabber(params)
2
+ def feedUsGrabber(params)
3
3
  grabber = FeedUsGrabber.new
4
-
4
+
5
5
  unless params[:CacheCommand].nil?
6
6
  grabber.setCacheCommand(params[:CacheCommand])
7
7
  end
8
- unless params[:FeedUsCacheGroup].nil?
9
- grabber.setCacheGroup(params[:FeedUsCacheGroup])
8
+ unless params[:FeedUsCacheGroup].nil?
9
+ grabber.setCacheGroup(params[:FeedUsCacheGroup])
10
10
  end
11
11
  unless params[:FeedUsCacheFolder].nil?
12
12
  grabber.setCacheFolder(params[:FeedUsCacheFolder])
@@ -21,17 +21,21 @@ module FeedUsGrabberHelper
21
21
  unless params[:FeedUsURL].nil?
22
22
  grabber.setDynURL(params[:FeedUsURL])
23
23
  if params[:includeFlag] == true
24
- grabber.setIncludeFlag(true)
25
- else
26
- grabber.setIncludeFlag(false)
27
- end
24
+ grabber.setIncludeFlag(true)
25
+ else
26
+ grabber.setIncludeFlag(false)
27
+ end
28
+ end
29
+
30
+ unless params[:ClientWhiteList].nil?
31
+ grabber.setClientWhiteList(params[:ClientWhiteList])
28
32
  end
29
-
33
+
30
34
  grabber.autoCacheToFile()
31
35
  grabber
32
- end
33
-
34
- def feedUsGrabberRender(grabber)
35
- grabber.renderCacheFromFile.to_s.html_safe
36
- end
36
+ end
37
+
38
+ def feedUsGrabberRender(grabber)
39
+ grabber.renderCacheFromFile.to_s.html_safe
40
+ end
37
41
  end
@@ -11,186 +11,224 @@ require 'fileutils'
11
11
 
12
12
 
13
13
  class FeedUsGrabber
14
- def initialize
15
- @mintCacheIntervalUnit = CI_FOREVER
16
- @mstrCacheFolder = File.join(Rails.root.to_s,'tmp','CachedWebContent')
17
- @mstrCacheFileExt = ".cache"
18
- @mstrCacheGroup = ""
19
- end
20
-
21
- def setCacheGroup(param)
22
- @mstrCacheGroup = param
23
- end
24
-
25
-
26
-
27
- def setCacheFolder(param)
14
+ def initialize
15
+ @mintCacheIntervalUnit = CI_FOREVER
16
+ @mstrCacheFolder = File.join(Rails.root.to_s,'tmp','CachedWebContent')
17
+ @mstrCacheFileExt = ".cache"
18
+ @mstrCacheGroup = ""
19
+ @mstrStateFile = File.join(Rails.root.to_s,'tmp','FeedUsGrabberState')
20
+ @mstrClientWhiteList = ""
21
+ end
22
+
23
+ def setCacheGroup(param)
24
+ @mstrCacheGroup = param
25
+ end
26
+
27
+ def setCacheFolder(param)
28
28
  @mstrCacheFolder = param
29
- end
30
-
31
-
32
-
33
- def setCacheIntervalUnit(param)
29
+ end
30
+
31
+ def setCacheIntervalUnit(param)
34
32
  @mintCacheIntervalUnit = param
35
- end
36
-
37
-
38
-
33
+ end
34
+
39
35
  def getCacheIntervalUnit
40
36
  @mintCacheIntervalUnit
41
37
  end
42
-
43
- def setCacheFileExt(strNewValue)
44
- @mstrCacheFileExt = strNewValue
45
- end
46
-
47
- def setCacheCommand(strNewValue)
48
- @mstrCacheCommand = strNewValue
49
- end
50
-
51
- def getCacheCommand
38
+
39
+ def setCacheFileExt(strNewValue)
40
+ @mstrCacheFileExt = strNewValue
41
+ end
42
+
43
+ def setCacheCommand(strNewValue)
44
+ @mstrCacheCommand = strNewValue
45
+ end
46
+
47
+ def getCacheCommand
52
48
  @mstrCacheCommand
53
49
  end
54
-
50
+
55
51
  def getCacheFolder
56
52
  @mstrCacheFolder
57
53
  end
58
-
59
- def setIncludeFlag(strNewValue)
54
+
55
+ def setIncludeFlag(strNewValue)
60
56
  @mblnInclude = $strNewValue
61
57
  end
62
-
63
- def getIncludeFlag
58
+
59
+ def getIncludeFlag
64
60
  @mblnInclude
65
61
  end
66
-
62
+
67
63
  def getCacheGroup
68
64
  @mstrCacheGroup
69
65
  end
70
66
  def getCachedFileName
71
- @getCachedFileName
67
+ @getCachedFileName
72
68
  end
73
-
74
- def setCacheIntervalLength(param)
69
+
70
+ def setCacheIntervalLength(param)
75
71
  @mintCacheIntervalLength = param
76
- end
77
-
72
+ end
73
+
78
74
  def getCacheIntervalLength
79
- @mintCacheIntervalLength
75
+ @mintCacheIntervalLength
80
76
  end
81
-
82
- def setDynURL(param)
83
- @mstrDynURL = param
84
- self.getCacheNames(param)
85
- end
86
-
87
- def getDynURL
88
- @mstrDynURL
89
- end
90
-
91
- def getCacheNames(sURL)
92
- if (sURL =~ /\?/) > 0
93
- base = sURL.split('?')[0]
94
- params = sURL.split('?')[1]
95
- else
96
- base = sURL
97
- params = ''
98
- end
99
- #strip off the file extension
77
+
78
+ def setDynURL(param)
79
+ @mstrDynURL = param
80
+ self.getCacheNames(param)
81
+ end
82
+
83
+ def setClientWhiteList(param)
84
+ # Use file to store state across requests
85
+ @mstrClientWhiteList = param
86
+ self.saveState
87
+ end
88
+
89
+ def getClientWhiteList
90
+ self.readState
91
+ @mstrClientWhiteList
92
+ end
93
+
94
+ def getDynURL
95
+ @mstrDynURL
96
+ end
97
+
98
+ def getCacheNames(sURL)
99
+ if (sURL =~ /\?/) > 0
100
+ base = sURL.split('?')[0]
101
+ params = sURL.split('?')[1]
102
+ else
103
+ base = sURL
104
+ params = ''
105
+ end
106
+ #strip off the file extension
100
107
  base = base.chomp(File.extname(base))
101
- base.sub!('http://','')
102
- base.gsub!(/[\?:\/\.]/,'_')
108
+ base.sub!('http://','')
109
+ base.gsub!(/[\?:\/\.]/,'_')
103
110
  @mstrScriptBaseName = base
104
- @mstrCacheFileQualifiers = self.getCacheFileQualifiers(params)
111
+ @mstrCacheFileQualifiers = self.getCacheFileQualifiers(params)
105
112
  @mstrCachedFileName =File.join( @mstrCacheFolder,
106
- @mstrCacheGroup,
107
- @mstrScriptBaseName + "_" + @mstrCacheFileQualifiers + @mstrCacheFileExt)
113
+ @mstrCacheGroup,
114
+ @mstrScriptBaseName + "_" + @mstrCacheFileQualifiers + @mstrCacheFileExt)
108
115
  @mstrCachedFileNameShort = @mstrScriptBaseName + "_" + @mstrCacheFileQualifiers + @mstrCacheFileExt
109
116
  end
110
-
111
- def getCacheFileQualifiers(strURLParams)
112
- strResults = nil
113
- strSeparator = "_"
114
- strResults = '_' + strURLParams.gsub(/[=&%\.\/:]+/,'_')
117
+
118
+ def getCacheFileQualifiers(strURLParams)
119
+ strResults = nil
120
+ strResults = '_' + strURLParams.gsub(/[=&%\.\/:]+/,'_')
115
121
  strResults
116
122
  end
117
-
118
- def autoCacheToFile
123
+
124
+ def autoCacheToFile
119
125
  puts "trace: start autoCacheToFile. bIsPostBack = #{@bIsPostBack}"
120
126
  if @bIsPostBack
121
- return
127
+ return
122
128
  end
123
-
129
+
124
130
  puts "trace: mstrCacheCommand = #{@mstrCacheCommand}"
125
131
  if @mstrCacheCommand.nil? || @mstrCacheCommand == ''
126
- if (!self.cachedFileExists) or self.cacheFileIsExpired
127
- self.createCacheFile
128
- end
132
+ if (!self.cachedFileExists) or self.cacheFileIsExpired
133
+ self.createCacheFile
134
+ end
129
135
  else
130
- if @mstrCacheCommand == "clear"
131
- if @mstrCacheGroup != ''
132
- self.clearCacheGroupFiles(@mstrCacheGroup)
136
+ if @mstrCacheCommand == "clear"
137
+ if @mstrCacheGroup != ''
138
+ self.clearCacheGroupFiles(@mstrCacheGroup)
139
+ end
140
+ end
141
+ if @mstrCacheCommand == 'clearall' || @mstrCacheCommand == CACHE_COMMAND_FORCE
142
+ self.clearAllCachedFiles
133
143
  end
134
- end
135
- if @mstrCacheCommand == 'clearall' || @mstrCacheCommand == CACHE_COMMAND_FORCE
136
- self.clearAllCachedFiles
137
- end
138
144
  end
139
145
  end
140
-
146
+
141
147
  def cachedFileExists()
142
- File.readable?(@mstrCachedFileName)
148
+ File.readable?(@mstrCachedFileName)
143
149
  end
144
-
150
+
145
151
  def cacheFileIsExpired()
146
152
  # Ruby 1.9.x Time.parse expects d/m/y
147
153
  strDate = File.mtime(@mstrCachedFileName).strftime('%d/%m/%Y %I:%M:%S %p')
148
154
  case @mintCacheIntervalUnit
149
- when CI_MINUTES then strUnit = 'n'
150
- when CI_HOURS then strUnit = 'h'
151
- when CI_DAYS then strUnit = 'd'
152
- when CI_FOREVER then return false
155
+ when CI_MINUTES then strUnit = 'n'
156
+ when CI_HOURS then strUnit = 'h'
157
+ when CI_DAYS then strUnit = 'd'
158
+ when CI_FOREVER then return false
153
159
  end
154
-
160
+
155
161
  if self.datediff(strUnit,strDate,Time.now().strftime('%d/%m/%Y %I:%M:%S %p')) > @mintCacheIntervalLength
156
- return true
162
+ return true
157
163
  else
158
- return false
164
+ return false
159
165
  end
160
166
  end
161
167
 
162
- def createCacheFile()
163
- strDynURL = @mstrDynURL
168
+ def saveState
169
+ if (!self.stateFileExists)
170
+ self.createStateFile
171
+ end
172
+ self.writeStateFile
173
+ end
174
+
175
+ def readState()
176
+ readStateFile
177
+ end
178
+
179
+ def createStateFile()
180
+ File.new(@mstrStateFile,'w')
181
+ end
182
+
183
+ def readStateFile()
184
+ if (self.stateFileExists)
185
+ File.open(@mstrStateFile, "r").each_line do |line|
186
+ @mstrClientWhiteList = line
187
+ end
188
+ end
189
+ end
190
+
191
+ def writeStateFile
192
+ File.open(@mstrStateFile,'w') do |file|
193
+ file.write(@mstrClientWhiteList)
194
+ end
195
+ end
196
+
197
+ def stateFileExists()
198
+ File.readable?(@mstrStateFile)
199
+ end
200
+
201
+ def createCacheFile()
164
202
  sFile = @mstrCachedFileName
165
203
  # Fetch the url
166
204
  begin
167
- r = Net::HTTP.get_response(URI.parse(@mstrDynURL))
205
+ r = Net::HTTP.get_response(URI.parse(@mstrDynURL))
168
206
  rescue
169
- logfile = File.open(File.join(Rails.root.to_s,'log','FeedUsGrabber.log'),'a');
170
- grabber_logger = FeedUsGrabberLogger.new(logfile)
207
+ logfile = File.open(File.join(Rails.root.to_s,'log','FeedUsGrabber.log'),'a');
208
+ grabber_logger = FeedUsGrabberLogger.new(logfile)
171
209
 
172
- grabber_logger.error("Unable to fetch URL #{@mstrDynURL}")
173
- logfile.close
174
- return;
210
+ grabber_logger.error("Unable to fetch URL #{@mstrDynURL}")
211
+ logfile.close
212
+ return;
175
213
  end
176
214
 
177
215
  if self.makeDirectory(File.join(@mstrCacheFolder,@mstrCacheGroup))
178
- File.open(sFile,'w') do |file|
179
- file.write(r.body)
180
- end
181
- return true
216
+ File.open(sFile,'w') do |file|
217
+ file.write(r.body)
218
+ end
219
+ return true
182
220
  end
183
221
  return false
184
- end
185
-
222
+ end
223
+
186
224
  def makeDirectory(dir, mode = 0755)
187
225
  if File.directory?(dir) || FileUtils.mkdir(dir)
188
- return true
226
+ return true
189
227
  end
190
228
  return false
191
229
  end
192
-
193
- def datediff(interval, datefrom, dateto, using_timestamps = false)
230
+
231
+ def datediff(interval, datefrom, dateto, using_timestamps = false)
194
232
  #$interval can be:
195
233
  #yyyy - Number of full years
196
234
  #q - Number of full quarters
@@ -203,106 +241,106 @@ class FeedUsGrabber
203
241
  #h - Number of full hours
204
242
  #n - Number of full minutes
205
243
  #s - Number of full seconds (default)
206
-
207
- unless using_timestamps
208
- datefrom = Time.parse(datefrom).to_i
209
- dateto = Time.parse(dateto).to_i
244
+
245
+ unless using_timestamps
246
+ datefrom = Time.parse(datefrom).to_i
247
+ dateto = Time.parse(dateto).to_i
210
248
  end
211
-
249
+
212
250
  difference = dateto - datefrom # Difference in seconds
213
- if interval == 'd'
214
- datediff = (difference / 86400).floor
215
- elsif interval == 'h'
216
- datediff = (difference / 3600).floor
217
- elsif interval == 'n'
218
- datediff = (difference / 60).floor
219
- else
220
- datediff = difference
221
- end
222
- datediff
251
+ if interval == 'd'
252
+ datediff = (difference / 86400).floor
253
+ elsif interval == 'h'
254
+ datediff = (difference / 3600).floor
255
+ elsif interval == 'n'
256
+ datediff = (difference / 60).floor
257
+ else
258
+ datediff = difference
259
+ end
260
+ datediff
223
261
  end
224
-
262
+
225
263
  def renderCacheFromFile
226
264
  sFile = @mstrCachedFileName
227
265
  data = ''
228
266
  begin
229
- File.open(sFile,'r') do |file|
230
- while temp_data = file.gets
231
- data = data + temp_data
232
- end
233
- end
234
- data
235
- rescue
236
- logfile = File.open(File.join(Rails.root.to_s,'log','FeedUsGrabber.log'),'a');
237
- grabber_logger = FeedUsGrabberLogger.new(logfile)
238
- grabber_logger.error("Unable to render/open #{@mstrCachedFileName}")
239
- logfile.close
240
- end
267
+ File.open(sFile,'r') do |file|
268
+ while temp_data = file.gets
269
+ data = data + temp_data
270
+ end
271
+ end
272
+ data
273
+ rescue
274
+ logfile = File.open(File.join(Rails.root.to_s,'log','FeedUsGrabber.log'),'a');
275
+ grabber_logger = FeedUsGrabberLogger.new(logfile)
276
+ grabber_logger.error("Unable to render/open #{@mstrCachedFileName}")
277
+ logfile.close
278
+ end
241
279
  end
242
-
243
-
280
+
281
+
244
282
  def clearCacheGroupFiles(group)
245
- if group == '.' || group == '..'
246
- logfile = File.open(File.join(Rails.root.to_s,'log','FeedUsGrabber.log'),'a');
247
- grabber_logger = FeedUsGrabberLogger.new(logfile)
248
- grabber_logger.warn("someone requested to delete . OR .. ")
249
- logfile.close
250
- return
251
- end
252
- logfile = File.open(File.join(Rails.root.to_s,'log','FeedUsGrabber.log'),'a');
283
+ if group == '.' || group == '..'
284
+ logfile = File.open(File.join(Rails.root.to_s,'log','FeedUsGrabber.log'),'a');
285
+ grabber_logger = FeedUsGrabberLogger.new(logfile)
286
+ grabber_logger.warn("someone requested to delete . OR .. ")
287
+ logfile.close
288
+ return
289
+ end
290
+ logfile = File.open(File.join(Rails.root.to_s,'log','FeedUsGrabber.log'),'a');
253
291
  grabber_logger = FeedUsGrabberLogger.new(logfile)
254
- grabber_logger.info("Clearing cache at group #{group} at path #{File.join(@mstrCacheFolder,group)}")
255
- logfile.close
256
- self.clearCacheFolder(File.join(@mstrCacheFolder,group))
257
- end
258
-
259
- def clearAllCachedFiles
260
- # For testing heroku logging
261
- puts "Trace: Clearing all caches at path = #{@mstrCacheFolder}"
262
- logfile = File.open(File.join(Rails.root.to_s,'log','FeedUsGrabber.log'),'a');
263
- grabber_logger = FeedUsGrabberLogger.new(logfile)
264
- grabber_logger.info("Clearing all caches at path #{@mstrCacheFolder}")
265
- logfile.close
266
- self.clearCacheFolder(@mstrCacheFolder);
267
- end
268
-
269
- def clearCacheFolder(folder)
270
- canConnect = canConnectToFeedUs()
271
-
272
- if canConnect == true
273
- puts "Trace: clear cache folder can connect"
274
- FileUtils.rm_r Dir.glob("#{folder}/*")
275
- else
276
- logError("Unable to connect to Feed.Us. Cache will not be cleared. URL that was checked: #{@mstrDynURL}")
277
- end
278
- end
279
-
280
- def canConnectToFeedUs()
281
- # Used for phone home check
282
- canConnect = false
283
-
284
- if @mstrCacheCommand == CACHE_COMMAND_FORCE
285
- canConnect = true
286
- else
287
- begin
288
- r = Net::HTTP.get_response(URI.parse(@mstrDynURL))
289
- if r.body.nil? == false && r.body != ERROR_RESPONSE
290
- canConnect = true
291
- end
292
- rescue
293
- logError("Unable to connect to Feed.Us. Cache will not be cleared. URL #{@mstrDynURL}")
292
+ grabber_logger.info("Clearing cache at group #{group} at path #{File.join(@mstrCacheFolder,group)}")
293
+ logfile.close
294
+ self.clearCacheFolder(File.join(@mstrCacheFolder,group))
295
+ end
296
+
297
+ def clearAllCachedFiles
298
+ # For testing heroku logging
299
+ puts "Trace: Clearing all caches at path = #{@mstrCacheFolder}"
300
+ logfile = File.open(File.join(Rails.root.to_s,'log','FeedUsGrabber.log'),'a');
301
+ grabber_logger = FeedUsGrabberLogger.new(logfile)
302
+ grabber_logger.info("Clearing all caches at path #{@mstrCacheFolder}")
303
+ logfile.close
304
+ self.clearCacheFolder(@mstrCacheFolder);
305
+ end
306
+
307
+ def clearCacheFolder(folder)
308
+ canConnect = canConnectToFeedUs()
309
+
310
+ if canConnect == true
311
+ puts "Trace: clear cache folder can connect"
312
+ FileUtils.rm_r Dir.glob("#{folder}/*")
313
+ else
314
+ logError("Unable to connect to Feed.Us. Cache will not be cleared. URL that was checked: #{@mstrDynURL}")
315
+ end
316
+ end
317
+
318
+ def canConnectToFeedUs()
319
+ # Used for phone home check
320
+ canConnect = false
321
+
322
+ if @mstrCacheCommand == CACHE_COMMAND_FORCE
323
+ canConnect = true
324
+ else
325
+ begin
326
+ r = Net::HTTP.get_response(URI.parse(@mstrDynURL))
327
+ if r.body.nil? == false && r.body != ERROR_RESPONSE
328
+ canConnect = true
329
+ end
330
+ rescue
331
+ logError("Unable to connect to Feed.Us. Cache will not be cleared. URL #{@mstrDynURL}")
332
+ end
294
333
  end
334
+
335
+ return canConnect;
295
336
  end
296
-
297
- return canConnect;
298
- end
299
-
300
- def logError(contents)
301
- logfile = File.open(File.join(Rails.root.to_s,'log','FeedUsGrabber.log'),'a');
302
- grabber_logger = FeedUsGrabberLogger.new(logfile)
303
-
304
- grabber_logger.error(contents)
305
- logfile.close
306
- end
307
-
337
+
338
+ def logError(contents)
339
+ logfile = File.open(File.join(Rails.root.to_s,'log','FeedUsGrabber.log'),'a');
340
+ grabber_logger = FeedUsGrabberLogger.new(logfile)
341
+
342
+ grabber_logger.error(contents)
343
+ logfile.close
344
+ end
345
+
308
346
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: feed_us_grabber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.1.15
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-30 00:00:00.000000000 Z
12
+ date: 2013-04-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: shoulda
@@ -124,7 +124,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
124
124
  version: '0'
125
125
  segments:
126
126
  - 0
127
- hash: 413069715
127
+ hash: -873202771
128
128
  required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  none: false
130
130
  requirements: