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 +15 -9
- data/lib/app/controllers/feed_us_grabber_controller.rb +86 -61
- data/lib/app/helpers/feed_us_grabber_helper.rb +18 -14
- data/lib/app/models/feed_us_grabber.rb +243 -205
- metadata +3 -3
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
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
66
|
-
|
67
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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
|
-
|
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
|
-
|
104
|
-
|
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
|
-
|
114
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
def setCacheIntervalUnit(param)
|
29
|
+
end
|
30
|
+
|
31
|
+
def setCacheIntervalUnit(param)
|
34
32
|
@mintCacheIntervalUnit = param
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
33
|
+
end
|
34
|
+
|
39
35
|
def getCacheIntervalUnit
|
40
36
|
@mintCacheIntervalUnit
|
41
37
|
end
|
42
|
-
|
43
|
-
def setCacheFileExt(strNewValue)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
def setCacheCommand(strNewValue)
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
-
|
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
|
-
|
67
|
+
@getCachedFileName
|
72
68
|
end
|
73
|
-
|
74
|
-
def setCacheIntervalLength(param)
|
69
|
+
|
70
|
+
def setCacheIntervalLength(param)
|
75
71
|
@mintCacheIntervalLength = param
|
76
|
-
|
77
|
-
|
72
|
+
end
|
73
|
+
|
78
74
|
def getCacheIntervalLength
|
79
|
-
|
75
|
+
@mintCacheIntervalLength
|
80
76
|
end
|
81
|
-
|
82
|
-
def setDynURL(param)
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
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
|
-
|
102
|
-
|
108
|
+
base.sub!('http://','')
|
109
|
+
base.gsub!(/[\?:\/\.]/,'_')
|
103
110
|
@mstrScriptBaseName = base
|
104
|
-
|
111
|
+
@mstrCacheFileQualifiers = self.getCacheFileQualifiers(params)
|
105
112
|
@mstrCachedFileName =File.join( @mstrCacheFolder,
|
106
|
-
|
107
|
-
|
113
|
+
@mstrCacheGroup,
|
114
|
+
@mstrScriptBaseName + "_" + @mstrCacheFileQualifiers + @mstrCacheFileExt)
|
108
115
|
@mstrCachedFileNameShort = @mstrScriptBaseName + "_" + @mstrCacheFileQualifiers + @mstrCacheFileExt
|
109
116
|
end
|
110
|
-
|
111
|
-
def getCacheFileQualifiers(strURLParams)
|
112
|
-
strResults = nil
|
113
|
-
|
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
|
-
|
127
|
+
return
|
122
128
|
end
|
123
|
-
|
129
|
+
|
124
130
|
puts "trace: mstrCacheCommand = #{@mstrCacheCommand}"
|
125
131
|
if @mstrCacheCommand.nil? || @mstrCacheCommand == ''
|
126
|
-
|
127
|
-
|
128
|
-
|
132
|
+
if (!self.cachedFileExists) or self.cacheFileIsExpired
|
133
|
+
self.createCacheFile
|
134
|
+
end
|
129
135
|
else
|
130
|
-
|
131
|
-
|
132
|
-
|
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
|
-
|
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
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
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
|
-
|
162
|
+
return true
|
157
163
|
else
|
158
|
-
|
164
|
+
return false
|
159
165
|
end
|
160
166
|
end
|
161
167
|
|
162
|
-
def
|
163
|
-
|
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
|
-
|
205
|
+
r = Net::HTTP.get_response(URI.parse(@mstrDynURL))
|
168
206
|
rescue
|
169
|
-
|
170
|
-
|
207
|
+
logfile = File.open(File.join(Rails.root.to_s,'log','FeedUsGrabber.log'),'a');
|
208
|
+
grabber_logger = FeedUsGrabberLogger.new(logfile)
|
171
209
|
|
172
|
-
|
173
|
-
|
174
|
-
|
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
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
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
|
-
|
185
|
-
|
222
|
+
end
|
223
|
+
|
186
224
|
def makeDirectory(dir, mode = 0755)
|
187
225
|
if File.directory?(dir) || FileUtils.mkdir(dir)
|
188
|
-
|
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
|
-
|
209
|
-
|
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
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
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
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
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
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
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
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
end
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
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
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
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.
|
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-
|
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:
|
127
|
+
hash: -873202771
|
128
128
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
129
|
none: false
|
130
130
|
requirements:
|