Ef9a110e6f84d3ebf3d4672ad090a1de

I have some code for a Twitter library I'm writing and I need to return a list of a specific type depending on the return XML. I know there has got to be a better way to do it than I currently am.

1
2
3
4
5
6
7
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
35
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public IList<IStatus> RepsonseHandler(string responseText)
{
	IList<IStatus> Output = null;
	bool multipleStatus = responseText.Contains("statuses type=\"array\"");
	XElement xmlElement = XElement.Load(new XmlTextReader(new StringReader(responseText)));

	if(!multipleStatus)
	{
		IStatus status = Status.ParseStatusXML(responseText);
		status.User = User.ParseUserXml(xmlElement.Element("user").ToString());
		
		if(status != null)
		{
			Output = new List<IStatus>();
			Output.Add(status);
		}
	}
	else
	{
		Output = new List<IStatus>();
		//Query for count of statuses to make sure we have some
		var query = from c in xmlElement.Descendants("status")
					select c;

		if (query.Count() > 0)
		{
			Output = new List<IStatus>();

			foreach (var status in query)
			{
				IStatus currentStatus = Status.ParseStatusXML(status.ToString());
				currentStatus.User = User.ParseUserXml(status.Element("user").ToString());
				Output.Add(currentStatus);
			}
		}
	}


	return Output;
}

	//TODO: Figure out a better way to handle this
public IList<IUser> RepsonseHandler(string responseText, string DoesNothing)
{
	IList<IUser> Output = null;
	bool multipleUsers = responseText.Contains("users type=\"array\"");
	XElement xmlElement = XElement.Load(new XmlTextReader(new StringReader(responseText)));

	if (!multipleUsers)
	{
		IUser user = User.ParseUserXml(responseText);
		user.Status = Status.ParseStatusXML(xmlElement.Element("status").ToString());

		if (user != null)
		{
			Output = new List<IUser>();
			Output.Add(user);
		}
	}
	else
	{
		Output = new List<IUser>();
		//Query for count of statuses to make sure we have some
		var query = from c in xmlElement.Descendants("user")
					select c;

		if (query.Count() > 0)
		{
			Output = new List<IUser>();

			foreach (var user in query)
			{
				IUser currentUser = User.ParseUserXml(user.ToString());
				currentUser.Status = Status.ParseStatusXML(user.Element("status").ToString());
				Output.Add(currentUser);
			}
		}
	}


	return Output;
}

Refactorings

No refactoring yet !

22e33503870d8e20493c4dd6b2f9767f

rikkus

May 13, 2009, May 13, 2009 08:49, permalink

No rating. Login to rate!

Implement Status.Load as you see fit. Obviously you'll want to load User from the "user" element contained within.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public IEnumerable<IStatus> ResponseHandler(string responseText, string unused)
{
    return ResponseHandler(responseText);
}

public IEnumerable<IStatus> ResponseHandler(string responseText)
{
    var element = XElement.Parse(responseText);

    if (element.Name == "statuses")
    {
        foreach (var statusElement in element.Descendants("status"))
        {
            yield return Status.Load(statusElement);
        }
    }
    else
    {
        yield return Status.Load(element);
    }
}
B95d4f68a96adc79c640996d2e3971ab

Sancho

May 18, 2009, May 18, 2009 17:01, permalink

No rating. Login to rate!

Good site, admin.

1
Good site, admin.

Your refactoring





Format Copy from initial code

or Cancel