I know, so shoot me, but I'm trying! Anyone want to take a gander on what I'm doing wrong in this code? Stuff starts in the public function PrepareNewRelease. From there the host is checked to see if its up (yes, I know its wrong, ignore that for now), then the file hosted on the site is validated for date modified, and further by the number of rows returned based on a pattern (loaded via a text file).
Problem: The rr variable cannot be assigned appropriately because I've got something screwed up somewhere, just not sure.
This is my attempt to be object oriented and modular so that I can reuse some of the functionality later. For example, getting the pattern file should be separate from using the pattern file to validate the host's file. Perhaps I've gotten too ambitious or too granular? Thanks...
Problem: The rr variable cannot be assigned appropriately because I've got something screwed up somewhere, just not sure.
This is my attempt to be object oriented and modular so that I can reuse some of the functionality later. For example, getting the pattern file should be separate from using the pattern file to validate the host's file. Perhaps I've gotten too ambitious or too granular? Thanks...
VB.NET:
Option Explicit On
Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions
Public Class NewRelease
Private Const sSite As String = "http://www.diamondcomics.com"
Private Const sNewReleaseLocationName As String = "http://www.diamondcomics.com/shipping/newreleases.txt"
Private Const sContentType As String = "text/plain"
Private Const sPatternFileLocationName As String = "\PatternFile.txt"
Private MyResponse As New Response
Private MyMatchCount As New MatchCount
Private Pattern As New PatternFile
Private rRegex As New Regex(Pattern.ReturnPattern, RegexOptions.IgnorePatternWhitespace Or RegexOptions.Multiline Or RegexOptions.Singleline)
Private sURI As New Uri(sNewReleaseLocationName)
Private sNewReleaseContents As String
Private Class Response
Public wRes As WebResponse
Public Function GetHeader(ByVal sHeader As String) As String
Return wRes.Headers.Get(sHeader)
End Function
Public Function GetResponseContents() As String
Dim str As Stream = wRes.GetResponseStream
Return New StreamReader(str).ReadToEnd
End Function
Public Sub New()
Dim wReq As WebRequest
wReq = WebRequest.Create(sNewReleaseLocationName)
wRes = wReq.GetResponse
End Sub
End Class
Private Class PatternFile
Dim sr As New StreamReader(Replace(Application.ExecutablePath, "\CSDBNewReleases.EXE", "") & sPatternFileLocationName)
Public Function ReturnPattern() As String
Return sr.ReadToEnd
End Function
End Class
Private Class NewReleaseMatches
Public Function ReturnMatches(ByVal rx As Regex, ByVal res As Response) As MatchCollection
Return rx.Matches(res.GetResponseContents)
End Function
End Class
Private Class MatchCount
Public NRM As New NewReleaseMatches
Public rr As Regex
Public Function GetCount(ByVal r As Response) As Integer
rr = rRegex
Return NRM.ReturnMatches(rr, r).Count
End Function
End Class
Private Function bIsReacheable() As Boolean
Return My.Computer.Network.IsAvailable
End Function
Private Function bCheckNewRelease(ByVal FileDate As Response) As Boolean
If bIsReacheable() AndAlso Now >= CType(FileDate.GetHeader("Last-Modified"), Date) Then
Return True
Else
Return False
End If
End Function
Private Function bValidateNewRelease(ByVal ContentType As Response, ByVal MyMatchCount As MatchCount) As Boolean
If sContentType = ContentType.GetHeader("Content-Type") AndAlso MyMatchCount.GetCount(MyResponse) > 0 Then
Return True
Else
Return False
End If
End Function
Public Function PrepareNewRelease() As Boolean
If bCheckNewRelease(MyResponse) AndAlso bValidateNewRelease(MyResponse, MyMatchCount) Then
Return True
Else
Return False
End If
End Function
End Class